#!/usr/local/bin/perl -w # compares two directories on lewis and skye # and offers to copy files to bring up to date # # a perl program that compares the files # in two directories (eg laptop /tex/ and master machine /tex/) # and copies over the files that are modified in # the appropriate direction. By default, it # copies from the master to the laptop without asking, # but it asks (y/n) whenever overwriting a master file # with a laptop file. # # Uses md5sum to compare # # usage # lewis.p # (updates pwd and the corresponding directory) # lewis.p l=code r=code lhead=/home/mackay/ rhead=/home/mackay/rhome/ file1 file2 file3 # (updates only the named files) # lewis.p l=code r=code lhead=/home/mackay/ rhead=/home/mackay/rhome/ # (updates all files in l and r directories) # # local and remote # implicit /home/mackay and /home/mackay/rhome # by default, it works in the current directory on lewis # and compares with rhome/same_directory # # this should be run on lewis # uses cp -pf to preserve # cp --preserve --force # $cpcommand = "cp -pf" ; $threshold = 5 ; # each thing that needs doing, a rating is assigned to # it ; above this threshold, commands really are done. Below it, we check (y/n) # with the human first. # $usewc = 0 ; # whether to use wc or md5sum $verbose = 1 ; $r = 0 ; $l = 0 ; $lhead = "/home/mackay/" ; # this is added to any 'l=blah' local directory given on command line $rhead = "/home/mackay/rhome/" ; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $copied = 0 ; # l and r should be directory names # the command line remainder optionally contains file names in those dirs. if ( !$r || !$l ) { # use current directory to define directory pair open(IN,"pwd |") ; $pwd = ; close(IN); $pwd =~ s/\n// ; if ( !$l ) { $l = $pwd ; } if ( !$r ) { $r = $pwd ; $r =~ s/mackay/mackay\/rhome/ ; } } # add leading /home/... if it is not there if ( !( $l =~ m:^/home: ) ) { $l = $lhead.$l ; } # if ( !( $r =~ m:^/home: ) ) { # $r = $rhead.$r ; } # add trailing / if ( !( $l =~ m:/$: ) ) { $l = $l."/" ; } # if ( !( $r =~ m:/$: ) ) { $r = $r."/" ; } # remove double slashes $r =~ s://:/:g ; $l =~ s://:/:g ; if ( $verbose >= 1 ) { print "======================================\n" ; print "local $l\n" ; print "remote $r\n" ; print "======================================\n" ; } # if there are arguments, these specify the files, # otherwise list these directories to get the files. if ( @ARGV ) { $listedfiles = 1 ; print "Using command line to make file list\n" ; $i = 0 ; foreach $f ( @ARGV ) { $lfilearray[$i] = $l.$f ; $rfilearray[$i] = $r.$f ; $i ++ ; } } else { $listedfiles = 0 ; print "Checking all files in these directories\n" ; $lfilelist = $l; $rfilelist = $r; $i = 0 ; # this ls command returns the file names only, without the leading path open ( IN , "ls $rfilelist $lfilelist | " ) ; while ( ) { s /\s*\n// ; if ( m:^/: || m :^\s*$: || m :\~$: || m :^\#: ) { # throw away /home/balh (directory titles) # and blank lines # and backup files or tmp files if ( $verbose >= 2 ) { print "skipping: $_\n" ; } } else { $f = $_ ; if (!$done{$f}) { $done{$f} = 1 ; $lfilearray[$i] = $l.$f ; $rfilearray[$i] = $r.$f ; $i ++ ; } } } } $I = $i ; # exit(0); print "Here are the files we are going to check out\n" if ($verbose>=2) ; foreach $i ( 0..$I-1 ) { print "$lfilearray[$i]\t$rfilearray[$i]: " if ($verbose>=2) ; # existence? $el = -e $lfilearray[$i] ; $er = -e $rfilearray[$i] ; # symbolic link? $sll = $el && ( -l $lfilearray[$i] ) ; $slr = $er && ( -l $rfilearray[$i] ); # directory? $dl = -d $lfilearray[$i] ; $dr = -d $rfilearray[$i] ; # modification time # to get file age use -M $file $Ml = -M $lfilearray[$i] ; $Mr = -M $rfilearray[$i] ; $command = 0 ; $docommand = 0 ; if ( $sll || $slr ) { if ( !( $sll && $slr ) ) { print "symbolic link $sll $slr, human needed!\n" ; } } elsif ( !$el && !$er ) { print "NEITHER EXISTS!\n" ; } else { $cprl = "$cpcommand $rfilearray[$i] $lfilearray[$i]" ; $cplr = "$cpcommand $lfilearray[$i] $rfilearray[$i]" ; if ( !$el ) { if ( !$dr ) { $command = "$cprl" ; $docommand = 10 ; } else { $command = "mkdir $lfilearray[$i]" ;$docommand = 10 ; } } elsif ( !$er ) { if ( !$dl ) { $command = "$cplr" ;$docommand = 10 ; } else { $command = "mkdir $rfilearray[$i]" ;$docommand = 10 ; } } else { # both files exist; check if they differ # if they are both files if ( !$dl && !$dr ) { $lolder = 0 ; $rolder = 0 ; $eps = 0.000000001 ; if ( $Ml > $Mr + $eps ) { $lolder = 1 ; $thestring = ">" ; $command = "$cprl" ; $docommand = 6 ; # copying onto local # machine is a good idea. } elsif ( $Mr > $Ml + $eps ) { $rolder = 1 ; $thestring = "<" ; $command = "$cplr" ; $docommand = 1 ; # not so sure about copying onto the remote master machine! } else { $thestring = "~=~" ; # if the files differ (implausible), our default is to copy from master machine $command = "$cprl" ; $docommand = 0 ; # copying onto local # machine is a good idea. # but we will not have an urge to do it, as there is no reason yet. } if ( $verbose >=2 ) { print "\n $Ml $thestring $Mr" ; } # diffs. if ($usewc) { open ( WC , "wc $lfilearray[$i] $rfilearray[$i] |" ) ; $wcl = ; $wcr = ; $wcl =~ s'$lfilearray[$i]'' ; $wcr =~ s'$rfilearray[$i]'' ; close ( WC ) ; } else { # use md5sum open ( WC , "md5sum $lfilearray[$i] $rfilearray[$i] |" ) ; $_ = ; ($wcl,$filenamel) = split ; $_ = ; ($wcr,$filenamer) = split ; close ( WC ) ; } if ( $wcl eq $wcr ) { print " - files seem identical " if ($verbose>=2) ; $docommand = 0 ; $command = 0 ; } else { if ( !$docommand ) { $docommand = 1 ; } } } } # print "\n" if($verbose>=2) ; print "\t$command" if ( $command ) ; if ( $command && $docommand ) { if ( ($docommand > $threshold) || &y_or_n(" COPY? (y/n)") ) { system $command ; # $copied ++ ; } } print "\n" if ( $command ) ; } } print "$copied files updated\n" ; sub y_or_n { local( $prompt ) = @_ ; print STDOUT $prompt ; local ($answer) = scalar () ; $answer =~ /^y/i ; } exit(0) ;