LRpt/CSVDiff.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 260 CC 20 SCAR 45.6
CC
20
Cov
0%
compare
210.0
diff
12.0
parse_skip_cols
12.0
CRAP 95.8
1 ###########################################################################
2 #
3 # $Id: CSVDiff.pm,v 1.5 2006/09/17 13:40:33 pkaluski Exp $
4 # $Name: Stable_0_16 $
5 #
6 # This is a tool for comparing 2 sets of csv files.
7 #
8 # $Log: CSVDiff.pm,v $
9 # Revision 1.5 2006/09/17 13:40:33 pkaluski
10 # Keys can be given from command line. Skip columns can be given. Only files with proper extension are selected (feature 1544070)
11 #
12 # Revision 1.4 2006/09/10 18:04:12 pkaluski
13 # Implemented chunking.
14 #
15 # Revision 1.3 2006/04/09 15:42:19 pkaluski
16 # Small code clean-up. Each module has comprehensive POD
17 #
18 # Revision 1.2 2006/02/10 22:32:16 pkaluski
19 # Major redesign in progress. Updated POD. Works.
20 #
21 # Revision 1.1 2006/01/14 12:52:33 pkaluski
22 # New tool design in progress
23 #
24 ###########################################################################
25 package LRpt::CSVDiff;
26 use strict;
27 use File::Find;
28 use Getopt::Long;
29 use File::Basename;
30 use LRpt::Collection;
31 use LRpt::CollDiff;
32 use LRpt::RKeysRdr;
33  
34 =head1 NAME
35  
36 LRpt::CSVDiff - A module for comparing 2 sets of csv files
37  
38 =head1 SYNOPSIS
39  
40 lcsvdiff.pl --all --keys_file=rkeys.txt --chunk_size=num before_dir after_dir
41  
42 =head1 DESCRIPTION
43  
44 This module is a part of L<C<LRpt>|LRpt> (B<LReport>) library.
45 It is used to compare 2 sets of I<csv> files and report found differences.
46 You should not use C<LRpt::CSVDiff> module directly in your code.
47 Instead you should use B<lcsvdiff.pl> tool, which is a simple wrapper
48 around the module. B<lcsvdiff.pl> looks like this:
49  
50 use LRpt::CSVDiff;
51 use strict;
52
53 diff( @ARGV );
54
55  
56 =head1 COMMAND LINE OPTIONS
57  
58 =over 4
59  
60 =item --all
61  
62 If set, not only differences are reported, but all rows from both rowsets.
63  
64 =item --key=string
65  
66 Row key defined in command line as one string. Don't use it. I will probably
67 get read of this switch in future
68  
69 =item --keys_file=file
70  
71 Name of the file containing row keys definitions
72  
73 =item --chunk_size=num
74  
75 Number of rows retrieved in one chunk. If not defined - default chunking size
76 is used. If equal to zero, than no chunking is used (all rows are loaded
77 to memory
78  
79 =item --help
80  
81 Prints help screen.
82  
83 =item before_dir
84  
85 A file name or a directory containing files with I<before> state.
86  
87 =item after_dir
88  
89 A file name or a directory containing files with I<after> state.
90  
91 =back
92  
93 =cut
94  
95 use vars qw( @EXPORT @ISA );
96 @ISA = qw( Exporter );
97 @EXPORT = qw( diff );
98  
99 #
100 # Reader of row keys
101 #
102 our $rkeys_rdr = "";
103  
104 our %skip_cols = ();
105 #
106 # Command line options
107 #
108  
109 #
110 # Switch deciding what to print on the difference report.
111 #
112 our $print_all = "";
113  
114 =head1 METHODS
115  
116 In this sections you will find a more or less complete listing of all
117 methods provided by the package.
118  
119 =cut
120  
121 ############################################################################
122  
123 =head2 C<diff>
124  
125 diff( @ARGV );
126  
127 Main function. @ARGV is processed by standard Getopt::Long module.
128 Switches have the following meaning (see L<SYNOPSIS|"SYNOPSIS">.
129  
130 =over 4
131  
132 =item --all
133  
134 If set, not only differences are reported, but all rows from both rowsets.
135  
136 =item --key=string
137  
138 Row key defined in command line as one string. Don't use it. I will probably
139 get read of this switch in future
140  
141 =item --keys_file=file
142  
143 Name of the file containing row keys definitions
144  
145 =item --chunk_size=num
146  
147 Number of rows retrieved in one chunk. If not defined - default chunking size
148 is used. If equal to zero, than no chunking is used (all rows are loaded
149 to memory
150  
151 =item before_dir
152  
153 A file name or a directory containing files with I<before> state.
154  
155 =item after_dir
156  
157 A file name of a directory containing files with I<after> state.
158  
159 =back
160  
161 =cut
162  
163 ##########################################################################
164 sub diff
165 {
166 my ( $ext, $path, $sep, $chunk_size, $keys_file, $global_keys );
167 my $skip_cols_file;
168 local ( @ARGV ) = @_;
169 my $help = "";
170 my @key = ();
171 my @key_cols = ();
172 my $skip_cols_string;
173  
174 GetOptions( "ext=s" => \$ext,
175 "path=s" => \$path,
176 "sep=s" => \$sep,
177 "key=s" => \@key,
178 "key_cols=s" => \@key_cols,
179 "keys_file=s" => \$keys_file,
180 "skip_cols=s" => \$skip_cols_string,
181 "skip_cols_file=s" => \$skip_cols_file,
182 "all" => \$print_all,
183 "global_keys" => \$global_keys,
184 "chunk_size=s" => \$chunk_size,
185 "help" => \$help );
186  
187 if( $print_all ){
188 $print_all = 'all';
189 }
190  
191 if( $help ){
192 print_usage();
193 }
194  
195 my $config = LRpt::Config->new( 'ext' => $ext,
196 'path' => $path,
197 'sep' => $sep,
198 'chunk_size' => $chunk_size );
199  
200 $rkeys_rdr = LRpt::RKeysRdr->new( 'fname' => $keys_file,
201 'key' => \@key,
202 'key_cols' => \@key_cols,
203 'global_keys' => $global_keys );
204  
205 parse_skip_cols( $skip_cols_string, $skip_cols_file );
206 my $file1 = shift( @ARGV );
207 my $file2 = shift( @ARGV );
208  
209 compare( $file1, $file2 );
210 }
211  
212 sub parse_skip_cols
213 {
214 my $skip_cols_string = shift;
215 my $skip_cols_file = shift;
216  
217 $skip_cols{ 'default' } = {};
218 my @skip_strings = ();
219 if( $skip_cols_string ){
220 @skip_strings = split( /,/, $skip_cols_string );
221 my $def_ref = $skip_cols{ 'default' };
222 @$def_ref{ @skip_strings } = 1;
223 }
224  
225 if( $skip_cols_file ){
226 my @skip_cols_rules = ( { 'name' => 'select_name' },
227 { 'name' => 'columns' } );
228 open( SKIPS, "<$skip_cols_file" ) or
229 die "Cannot open '$skip_cols_file' : $!";
230 my $jr = LRpt::JarReader->new(
231 'rules' => \@skip_cols_rules, 'filehandle' => *SKIPS );
232 $jr->read_all();
233 my @selects = $jr->get_all_values_of( 'select_name' );
234  
235 foreach my $select ( @selects ){
236 my $sect = $jr->get_section_with( 'select_name' => $select );
237 my @selects_in_entry = split( /\s*,\s*/, $select );
238 foreach my $s ( @selects_in_entry ){
239 my %skips = ();
240 @skips{ @skip_strings } = 1;
241 @skips{ split( /\s*,\s*/, $sect->{ 'columns' } ) } = 1;
242 $skip_cols{ $s } = \%skips;
243 }
244 }
245 close( SKIPS ) or die "Cannot close $skip_cols_file";
246 }
247  
248 }
249
250
251  
252 ######################################################################
253  
254 =head2 C<compare>
255  
256 compare( $st1_file, $st2_file );
257  
258 Compares 2 files. If one or two of given filenames are actually directory
259 names then diff behavior is mimiced.
260  
261 =cut
262  
263 ########################################################################
264 sub compare
265 {
266 my $st1_file = shift;
267 my $st2_file = shift;
268 my %st1_files = ();
269 my %st2_files = ();
270  
271 $st1_file =~ s/\\/\//g; # If using windows backslashes, convert them to
272 # slashes
273 $st2_file =~ s/\\/\//g;
274 #
275 # If both arguments are regular files...
276 #
277 if( -f $st1_file and -f $st2_file ){
278 compare_two_files( $st1_file, $st2_file );
279 return;
280 }
281  
282 #
283 # If one file is actually a directory...
284 #
285 if( -f $st1_file and -d $st2_file ){
286 compare_two_files( $st1_file, "$st2_file/$st1_file", 1 );
287 return;
288 }
289 if( -d $st1_file and -f $st2_file ){
290 compare_two_files( $st2_file, "$st1_file/$st2_file", 1 );
291 return;
292 }
293  
294 #
295 # OK. Both arguments are directories. We have to scan those directories
296 #
297 my $config = LRpt::Config->new();
298 my $ext = $config->get_value( 'ext' );
299 find( sub {
300 if( $File::Find::name eq $st1_file ){
301 return;
302 }
303 if( not ( $File::Find::name =~ /\.$ext$/ ) ){
304 return;
305 }
306 my $cmp_path = $File::Find::name;
307 $cmp_path =~ s/^$st1_file\///;
308 $st1_files{ $cmp_path } = 1;
309 },
310 $st1_file );
311  
312 find( sub {
313 if( $File::Find::name eq $st2_file ){
314 return;
315 }
316 if( not ( $File::Find::name =~ /\.$ext$/ ) ){
317 return;
318 }
319 my $cmp_path = $File::Find::name;
320 $cmp_path =~ s/^$st2_file\///;
321 $st2_files{ $cmp_path } = 1;
322 },
323 $st2_file );
324  
325  
326 my %all_files = ();
327 @all_files{ keys %st1_files } = 1;
328 @all_files{ keys %st2_files } = 1;
329  
330 #
331 # At that stage %all_files contains a sum of all files from st1 and st2
332 # directories. Names of those files are keys in the hash.
333 #
334  
335 foreach my $file ( keys %all_files ){
336 if( not -e "$st1_file/$file" ){
337 print "$file only in $st2_file\n";
338 next;
339 }
340 if( not -e "$st2_file/$file" ){
341 print "$file only in $st1_file\n";
342 next;
343 }
344 if( -d "$st1_file/$file" and !( -d "$st2_file/$file" ) ){
345 print "$st1_file/$file is a directory, " .
346 "$st2_file/$file is a regular file\n";
347 next;
348 }
349 if( -d "$st2_file/$file" and !( -d "$st1_file/$file" ) ){
350 print "$st1_file/$file is a regular file, " .
351 "$st2_file/$file is a directory\n";
352 next;
353 }
354 if( -d "$st1_file/$file" and -d "$st2_file/$file" ){
355 next;
356 }
357 if( -f "$st1_file/$file" and -f "$st2_file/$file" ){
358 compare_two_files( "$st1_file/$file" , "$st2_file/$file", 1 );
359 next;
360 }
361 }
362 }
363  
364 ######################################################################
365  
366 =head2 C<compare_two_files>
367  
368 compare_two_files( $file1, $file2 );
369  
370 Performs row/column oriented comparison between 2 files
371  
372 =cut
373  
374 ######################################################################
375 sub compare_two_files
376 {
377 my $file1 = shift;
378 my $file2 = shift;
379 my $print_header = shift;
380  
381 my $config = LRpt::Config->new();
382 my $ext = $config->get_value( 'ext' );
383  
384 my $name = basename( $file1 );
385 $name =~ s/\.$ext$//;
386 my $rkey = $rkeys_rdr->find_key( $name );
387
388 my $header = undef;
389 if( $print_header ){
390 $header = "lcsvdiff";
391 }
392
393 my $coll_skip_cols;
394 if( exists $skip_cols{ $name } ){
395 $coll_skip_cols = $skip_cols{ $name };
396 }else{
397 $coll_skip_cols = $skip_cols{ 'default' }
398 }
399 my $coll1 = LRpt::Collection->new_from_csv( 'name' => 'coll',
400 'data_file' => $file1,
401 'key' => $rkey );
402 my $coll2 = LRpt::Collection->new_from_csv( 'name' => 'coll',
403 'data_file' => $file2,
404 'key' => $rkey );
405 my $cd = LRpt::CollDiff->new( 'before' => $coll1,
406 'after' => $coll2,
407 'skip_cols' => $coll_skip_cols );
408 $cd->compare_collections( $print_all, $header );
409 }
410  
411 ######################################################################
412  
413 =head2 C<print_usage>
414  
415 print_usage();
416  
417 Prints usage text.
418  
419 =cut
420  
421 ######################################################################
422 sub print_usage
423 {
424 print "Usage: $0 --help \n";
425 print " --key=<string> --keys_file=<name>" .
426 " --all file_before file_after\n";
427 print "\n";
428 print " --all - Prints not only differences but also\n";
429 print " all rows\n";
430 print " --help - prints this help screen\n";
431 print " --key - Command line row key definition. Don't use\n";
432 print " --keys_file - Name of the file containing row keys " .
433 "definitions\n";
434 print " --chunk_size - Number of rows retrieved in one chunk. \n";
435 print " If not defined - default chunking size is \n";
436 print " used. If equal to zero, than no chunking is\n";
437 print " used (all rows are loaded to memory\n";
438 print " file_before - A file name or a directory containing files\n" .
439 " with before state.\n";
440 print " file_after - A file name or a directory containing files\n" .
441 " with after state.\n";
442 exit( 0 );
443 }
444  
445 =head1 SEE ALSO
446  
447 The project is maintained on Source Forge L<http://lreport.sourceforge.net>.
448 You can find there links to some helpful documentation like tutorial.
449  
450 =head1 AUTHORS
451  
452 Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt>
453  
454 =head1 COPYRIGHT
455  
456 Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved.
457  
458 You may distribute under the terms of either the GNU General Public License
459 or the Artistic License, as specified in the Perl README file.
460  
461 =cut
462  
463 1;
464