blib/lib/App/iosdiff.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 183 CC 12 SCAR 28.9
CC
12
Cov
0%
diff
30.0
generate_lookups
30.0
slurp_config
20.0
CRAP 18.0
1 package App::iosdiff;
2 BEGIN {
3 $App::iosdiff::VERSION = '1.200001';
4 }
5  
6 use strict;
7 use warnings FATAL => 'all';
8  
9 use File::Slurp;
10 use Algorithm::Diff;
11 use File::Temp; # core
12 use List::Util 'max'; # core
13  
14 use base 'Exporter';
15 our @EXPORT_OK = qw/ diff /;
16  
17 sub diff {
18 my $args = shift;
19 die "args should be a hashref"
20 unless ref $args eq ref {};
21  
22 my ($left_file, $right_file) = ($args->{left}, $args->{right});
23 die "two args: left and right files\n"
24 unless defined $left_file and -r $left_file
25 and defined $right_file and -r $right_file;
26  
27 # load up two files to be diffed
28 my $left_lines = slurp_config( $left_file );
29 my $right_lines = slurp_config( $right_file );
30  
31 # get indexes into left and right configs
32 my ($left_stanzas, $left_backref) = generate_lookups( @$left_lines );
33 my ($right_stanzas, $right_backref) = generate_lookups( @$right_lines );
34  
35 # ============================================================================
36  
37 # must have indexes for lines, as there are dupes, so use this module
38 # rather than other list comparing tools (which refer to line content not idx)
39 my $diff = Algorithm::Diff->new( $left_lines, $right_lines );
40  
41 my %seen_stanza; # might have multiple hunks in one stanza
42 my @total_output; # build up result
43  
44 # work through hunks in config, diff each stanza once only
45 while ($diff->Next) {
46 next if $diff->Same;
47 my %affected_stanza;
48  
49 map { ++$affected_stanza{ $left_stanzas->[$_] } } ($diff->Range(1));
50 map { ++$affected_stanza{ $right_stanzas->[$_] } } ($diff->Range(2));
51  
52 foreach my $stanza (keys %affected_stanza) {
53 next if exists $seen_stanza{$stanza};
54 $left_backref->{$stanza} ||= [];
55 $right_backref->{$stanza} ||= [];
56  
57 my $lfh = File::Temp->new;
58 my $rfh = File::Temp->new;
59  
60 write_file( $lfh, $left_backref->{$stanza} );
61 write_file( $rfh, $right_backref->{$stanza} );
62  
63 my $size = (max (scalar @{$left_backref->{$stanza}},
64 scalar @{$right_backref->{$stanza}})) + 1;
65  
66 # run the real diff command here, gather output
67 my $command = "diff -U $size ". $lfh->filename .' '. $rfh->filename;
68 my @output = `$command`;
69  
70 # strip diff command header, and store
71 push @total_output, @output[3 .. $#output], "\n";
72 }
73  
74 map { ++$seen_stanza{$_} } (keys %affected_stanza);
75 }
76  
77 return @total_output;
78 }
79  
80 # load a config from a file, removing uninteresting lines
81 # also where rancid might have commented a secret line, uncomment it
82 sub slurp_config {
83 my $file = shift;
84 my @lines;
85  
86 # skip comments and blank lines
87 foreach ( read_file( $file ) ) {
88 s/^!// if m/<removed>/;
89 next if m/^!/;
90 chomp;
91 next if m/^$/;
92 push @lines, $_;
93 }
94  
95 return \@lines;
96 }
97  
98 # provides indexes into the config
99 # find the stanza which a line is in, and all the lines in a stanza.
100 # that way when we're told a line changes, we can diff its parent stanza
101 sub generate_lookups {
102 my @lines = @_;
103 my $stanza = '';
104 my (@stanzas, %backref);
105  
106 for (my $i = 0; $i <= $#lines; $i++) {
107 my $line = $lines[$i];
108  
109 if ($i < $#lines and $line !~ m/^\s+/ and $lines[$i+1] =~ m/^\s+/) {
110 # start of new indented stanza
111 $stanza = $line;
112 $backref{$line} ||= [];
113 push @{$backref{$line}}, "$line\n";
114 $stanzas[$i] = $line;
115 next;
116 }
117  
118 if ($line =~ m/^\s+/) {
119 # continuation of indented stanza
120 push @{$backref{$stanza}}, "$line\n";
121 $stanzas[$i] = $stanza;
122 next;
123 }
124  
125 # new stanza, or continuation of unindented stanza
126 # stanza are based on first two words in line, so they are not large
127 $line =~ s/^\s*(no\s+)?//;
128 my ($no) = $1 || '';
129  
130 my ($key1, $key2, undef) = split (/\s+/, $line, 3);
131 $key2 ||= '';
132  
133 if ("$key1 $key2" ne $stanza and $i < $#lines) {
134 $stanza = $key1;
135 $stanza = "$key1 $key2" if $lines[$i+1] =~ m/^\Q$key1\E \Q$key2\E/;
136 }
137 else {
138 # last line special case
139 $stanza = "$key1 $key2";
140 }
141  
142 $backref{$stanza} ||= [];
143 push @{$backref{$stanza}}, "$no$line\n";
144 $stanzas[$i] = $stanza;
145 }
146  
147 return (\@stanzas, \%backref);
148 }
149  
150 1;
151  
152 # ABSTRACT: Cisco IOS Config Diff
153  
154  
155 __END__