--- loncom/build/filecompare.pl 2001/11/16 20:06:08 1.4 +++ loncom/build/filecompare.pl 2001/11/17 01:16:12 1.6 @@ -1,6 +1,6 @@ #!/usr/bin/perl -# The LearningOnline Network witih CAPA +# The LearningOnline Network with CAPA # # filecompare.pl - script used to help probe and compare file statistics # @@ -9,9 +9,26 @@ # 11/14 Guy Albertelli # 11/16 Scott Harrison # -# $Id: filecompare.pl,v 1.4 2001/11/16 20:06:08 harris41 Exp $ +# $Id: filecompare.pl,v 1.6 2001/11/17 01:16:12 harris41 Exp $ ### +############################################################################### +## ## +## ORGANIZATION OF THIS PERL SCRIPT ## +## ## +## 1. Invocation ## +## 2. Notes ## +## 3. Dependencies ## +## 4. Process command line arguments ## +## 5. Process file/dir location arguments ## +## 6. Process comparison restrictions ## +## 7. Define output and measure subroutines ## +## 8. Loop through files and calculate differences ## +## 9. Subroutines ## +## 10. POD (plain old documentation, CPAN style) ## +## ## +############################################################################### + # ------------------------------------------------------------------ Invocation my $invocation=< 1; cvstime<0 --> 2 + N=2: same as N=1 except without md5sum + N=3: md5sum=same --> 1; age<0 --> 2 + N=4: cvstime>0 --> 2 END unless (@ARGV) { print $invocation; exit 1; } + # ----------------------------------------------------------------------- Notes # # What are all the different ways to compare two files and how to look @@ -41,7 +64,7 @@ unless (@ARGV) { # # Ways of comparison: # existence similarity -# cvs time similarity (first argument treated as CVS source) +# cvs time similarity (1st arg treated as CVS source; only for buildmode) # age similarity (modification time) # md5sum similarity # size similarity (bytes) @@ -56,25 +79,8 @@ unless (@ARGV) { # size similarity (byte difference) # line count difference (integer) # number of different lines (integer) -# -# Text output of comparison: -# existence VALUE -# cvstime VALUE -# age VALUE -# md5sum VALUE -# size VALUE -# lines VALUE -# diffs VALUE -# -# Output of comparison: -# exist -# if md5sum not same, then different -# if cvstime not 0, then older/newer -# if age not 0, then older/newer -# if size not 0, then bigger/smaller -# if lines not 0, then more lines of code/less lines of code -# if diffs not 0, then subtracted lines/added lines/changed lines +# ---------------------------------------------------------------- Dependencies # implementing from unix command line (assuming bash) # md5sum, diff, wc -l @@ -85,14 +91,20 @@ unless (@ARGV) { # -a show all files (with comparisons) # -q only show file names (based on first file/dir) # -v verbose mode (default) -# -b build/install mode (returns exitcode) +# -bN build/install mode (returns exitcode) my $verbose='1'; my $show='all'; my $buildmode=0; -while (@ARGV) { +ALOOP: while (@ARGV) { my $flag; if ($ARGV[0]=~/^\-(\w)/) { $flag=$1; + if ($flag eq 'b') { + $ARGV[0]=~/^\-\w(\d)/; + $buildmode=$1; + shift @ARGV; + next ALOOP; + } shift @ARGV; SWITCH: { $verbose=0, last SWITCH if $flag eq 'q'; @@ -100,10 +112,6 @@ while (@ARGV) { $show='same', last SWITCH if $flag eq 'p'; $show='different', last SWITCH if $flag eq 'n'; $show='all', last SWITCH if $flag eq 'a'; - $buildmode=1, last SWITCH if $flag eq 'b'; - $buildmode=2, last SWITCH if $flag eq 'B'; - $buildmode=3, last SWITCH if $flag eq 'g'; - $buildmode=4, last SWITCH if $flag eq 'G'; print($invocation), exit(1); } } @@ -114,6 +122,7 @@ while (@ARGV) { dowarn('Verbose: '.$verbose."\n"); dowarn('Show: '.$show."\n"); +# ----------------------------------------- Process file/dir location arguments # FILE1 FILE2 or DIR1 DIR2 my $loc1=shift @ARGV; my $loc2=shift @ARGV; @@ -139,29 +148,23 @@ dowarn('Processing for mode: '.$dirmode. dowarn('Location #1: '.$loc1."\n"); dowarn('Location #2: '.$loc2."\n"); +# --------------------------------------------- Process comparison restrictions # A list of space separated values (after the file/dir names) # can restrict the comparison. +my %rhash=('existence'=>0,'cvstime'=>0,'md5sum'=>0,'age'=>0,'size'=>0, + 'lines'=>0,'diffs'=>0); my %restrict; while (@ARGV) { my $r=shift @ARGV; - if ($r eq 'existence' or - $r eq 'cvstime' or - $r eq 'md5sum' or - $r eq 'age' or - $r eq 'size' or - $r eq 'lines' or - $r eq 'diffs') { - $restrict{$r}=1; - } - else { - print($invocation), exit(1); - } + if ($rhash{$r}==0) {$restrict{$r}=1;} + else {print($invocation), exit(1);} } if (%restrict) { - warn('Restricting comparison to: '. + dowarn('Restricting comparison to: '. join(' ',keys %restrict)."\n"); } +# --------------------------------------- Define output and measure subroutines my %OUTPUT=( 'existence'=>( sub {print 'existence: '.@_[0]; return;}), 'md5sum'=>(sub {print 'md5sum: '.@_[0];return;}), @@ -206,7 +209,7 @@ my %MEASURE=( return ($rv1,$rv2); } ), ); -FLOP: foreach my $file (@files) { +FLOOP: foreach my $file (@files) { my $file1; my $file2; if ($dirmode eq 'directories') { @@ -229,8 +232,13 @@ FLOP: foreach my $file (@files) { $diffs='n/a'; } else { - my ($cvstime1,$cvstime2)=&{$MEASURE{'cvstime'}}($file1,$file2); - $cvstime=$cvstime1-$cvstime2; + if ($buildmode) { + my ($cvstime1,$cvstime2)=&{$MEASURE{'cvstime'}}($file1,$file2); + $cvstime=$cvstime1-$cvstime2; + } + else { + $cvstime='n/a'; + } my ($age1,$age2)=&{$MEASURE{'age'}}($file1,$file2); $age=$age1-$age2; my ($md5sum1,$md5sum2)=&{$MEASURE{'md5sum'}}($file1,$file2); @@ -238,7 +246,7 @@ FLOP: foreach my $file (@files) { $md5sum='same'; $size=0; $lines=0; - $diffs=0; + $diffs='0:0'; } elsif ($md5sum1 ne $md5sum2) { $md5sum='different'; @@ -259,7 +267,7 @@ FLOP: foreach my $file (@files) { unless (@ks) { @ks=('existence','cvstime','md5sum','age','size','lines','diffs'); } - FLOP2: for my $key (@ks) { + FLOOP2: for my $key (@ks) { if ($key eq 'existence') { if ($existence ne 'yes:yes') { $showflag=1; @@ -270,7 +278,7 @@ FLOP: foreach my $file (@files) { $showflag=1; } } - elsif ($key eq 'cvstime') { + elsif ($key eq 'cvstime' and $buildmode) { if ($cvstime!=0) { $showflag=1; } @@ -296,7 +304,7 @@ FLOP: foreach my $file (@files) { } } if ($showflag) { - last FLOP2; + last FLOOP2; } } } @@ -306,7 +314,8 @@ FLOP: foreach my $file (@files) { @ks=('existence','md5sum','cvstime','age','size','lines','diffs'); } my $showcount=length(@ks); - FLOP3: for my $key (@ks) { + $showcount-- unless $buildmode; + FLOOP3: for my $key (@ks) { if ($key eq 'existence') { if ($existence ne 'yes:yes') { $showcount--; @@ -317,7 +326,7 @@ FLOP: foreach my $file (@files) { $showcount--; } } - elsif ($key eq 'cvstime') { + elsif ($key eq 'cvstime' and $buildmode) { if ($cvstime!=0) { $showcount--; } @@ -385,26 +394,30 @@ FLOP: foreach my $file (@files) { exit(0); } } - print "$file"; - if ($verbose==1) { - print "\t"; - print &{$OUTPUT{'existence'}}($existence); - print "\t"; - print &{$OUTPUT{'cvstime'}}($cvstime); - print "\t"; - print &{$OUTPUT{'age'}}($age); - print "\t"; - print &{$OUTPUT{'md5sum'}}($md5sum); - print "\t"; - print &{$OUTPUT{'size'}}($size); - print "\t"; - print &{$OUTPUT{'lines'}}($lines); - print "\t"; - print &{$OUTPUT{'diffs'}}($diffs); + if ($showflag) { + print "$file"; + if ($verbose==1) { + print "\t"; + print &{$OUTPUT{'existence'}}($existence); + print "\t"; + print &{$OUTPUT{'cvstime'}}($cvstime); + print "\t"; + print &{$OUTPUT{'age'}}($age); + print "\t"; + print &{$OUTPUT{'md5sum'}}($md5sum); + print "\t"; + print &{$OUTPUT{'size'}}($size); + print "\t"; + print &{$OUTPUT{'lines'}}($lines); + print "\t"; + print &{$OUTPUT{'diffs'}}($diffs); + } + print "\n"; } - print "\n"; } +# ----------------------------------------------------------------- Subroutines + sub cvstime { my ($f)=@_; my $path; my $file; @@ -442,6 +455,8 @@ sub dowarn { warn($msg) unless $buildmode; } +# ----------------------------------- POD (plain old documentation, CPAN style) + =head1 NAME filecompare.pl - script used to help probe and compare file statistics