[363] | 1 | #!/usr/athena/bin/perl |
---|
| 2 | |
---|
| 3 | use File::Spec::Functions; |
---|
| 4 | use Data::Dumper; |
---|
| 5 | use Getopt::Long; |
---|
| 6 | use Cwd; |
---|
| 7 | |
---|
| 8 | my ($redodelete, $redoadd, $redoreplace, $redodiff) = (0,0,0,0); |
---|
| 9 | |
---|
| 10 | GetOptions("redo-delete" => \$redodelete, |
---|
| 11 | "redo-add" => \$redoadd, |
---|
| 12 | "redo-replace" => \$redoreplace, |
---|
| 13 | "redo-diff" => \$redodiff, |
---|
| 14 | "redo-all" => sub {$redodelete = $redoadd = $redoreplace = $redodiff = 1;} |
---|
| 15 | ); |
---|
| 16 | |
---|
| 17 | if (@ARGV < 3) { |
---|
| 18 | print STDERR "Usage: $0 [--redo-{delete,add,replace,diff,all}] package oldversion newversion\n"; |
---|
| 19 | exit(1); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | my ($package, $oldversion, $newversion) = @ARGV; |
---|
| 23 | my ($old, $new, $updatename) = ($package.'-'.$oldversion, $package.'-'.$newversion, $package.'-'.$oldversion.'-to-'.$newversion); |
---|
| 24 | |
---|
| 25 | my $outdir = $updatename.".proposal"; |
---|
| 26 | |
---|
| 27 | (-d $outdir || mkdir($outdir)) or die "mkdir($outdir) failed: $!"; |
---|
| 28 | |
---|
| 29 | my $olddir = catdir($outdir,$old); |
---|
| 30 | my $newdir = catdir($outdir,$new); |
---|
| 31 | |
---|
| 32 | unpackPackage($old, $olddir); |
---|
| 33 | unpackPackage($old, $newdir); |
---|
| 34 | |
---|
| 35 | sub unpackPackage($$) { |
---|
| 36 | my ($package, $dir) = @_; |
---|
| 37 | print "Extracting $package to $dir..."; |
---|
| 38 | if (-d $dir) { |
---|
| 39 | warn "$dir already exists; assuming unpacking was successful"; |
---|
| 40 | return; |
---|
| 41 | } |
---|
| 42 | mkdir($dir) or die "mkdir($dir) failed: $!"; |
---|
| 43 | my $cwd = cwd(); |
---|
| 44 | chdir($dir) or die $!; |
---|
| 45 | `athrun scripts gtar zxf "/mit/scripts/deploy$scriptsdev/$package.tar.gz"`; $? && die "Failed to unpack $package.tar.gz: $?"; |
---|
| 46 | my @files=`athrun scripts gfind . -mindepth 1 -maxdepth 1 | grep -v .admin`; |
---|
| 47 | if (@files <= 1) { |
---|
| 48 | `athrun scripts gfind . -mindepth 2 -maxdepth 2 | xargs -i mv \{} .`; |
---|
| 49 | rmdir($files[0]); |
---|
| 50 | } |
---|
| 51 | chdir($cwd) or die "Couldn't return to $cwd"; |
---|
| 52 | print "done.\n"; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | my @oldfiles = sort { $a->[1] cmp $b->[1] } map { chomp; s|$olddir\/?||g; [split(' ', $_, 2)] } `athrun scripts gfind $olddir -type f | xargs -i md5sum \{}`; |
---|
| 56 | #print Dumper(\@oldfiles); |
---|
| 57 | my @newfiles = sort { $a->[1] cmp $b->[1] } map { chomp; s|$newdir\/?||g; [split(' ', $_, 2)] } `athrun scripts gfind $newdir -type f | xargs -i md5sum \{}`; |
---|
| 58 | #print Dumper(\@newfiles); |
---|
| 59 | |
---|
| 60 | sub compareDirectories($$) { |
---|
| 61 | my ($alist, $blist) = @_; |
---|
| 62 | my @a = @$alist; |
---|
| 63 | my @b = @$blist; |
---|
| 64 | my @aonly, @bonly, @both; |
---|
| 65 | $a = $b = 0; |
---|
| 66 | my $debug = 1; |
---|
| 67 | local $Data::Dumper::Indent = 0; |
---|
| 68 | while ($a <= $#a || $b <= $#a) { |
---|
| 69 | my $fa = $a[$a]; |
---|
| 70 | my $fb = $b[$b]; |
---|
| 71 | print STDERR "Comparing ".Dumper($fa, $fb)."\n" if $debug; |
---|
| 72 | if ($fa->[1] eq $fb->[1]) { # Same file exists on both |
---|
| 73 | print STDERR "Same file\n" if $debug; |
---|
| 74 | if ($fa->[0] ne $fb->[0]) { # File has changed in some way |
---|
| 75 | print STDERR "Different md5, pushing on \@both\n" if $debug; |
---|
| 76 | push(@both, [$fa->[1], $fa, $fb]); |
---|
| 77 | } |
---|
| 78 | $a++; $b++; # increment both counters |
---|
| 79 | } else { |
---|
| 80 | my $a2 = $a; |
---|
| 81 | while ($a2 <= $#a && $a[$a2]->[1] lt $fb->[1]) { |
---|
| 82 | $a2++; |
---|
| 83 | } |
---|
| 84 | if ($a2 <= $#a && $a[$a2]->[1] eq $fb->[1]) { |
---|
| 85 | for my $i ($a..$a2-1) { |
---|
| 86 | push @aonly, $a[$i]; |
---|
| 87 | } |
---|
| 88 | $a = $a2; |
---|
| 89 | } else { |
---|
| 90 | my $b2 = $b; |
---|
| 91 | while ($b2 <= $#b && $b[$b2]->[1] lt $fa->[1]) { |
---|
| 92 | $b2++; |
---|
| 93 | } |
---|
| 94 | if ($b2 <= $#b && $b[$b2]->[1] eq $fa->[1]) { |
---|
| 95 | for my $i ($b..$b2-1) { |
---|
| 96 | push @bonly, $b[$i]; |
---|
| 97 | } |
---|
| 98 | $b = $b2; |
---|
| 99 | } else { |
---|
| 100 | push @aonly, $a[$a]; |
---|
| 101 | push @bonly, $b[$b]; |
---|
| 102 | $a++; $b++; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | return (\@aonly, \@bonly, \@both); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | my (@todelete, @toadd, @changed); |
---|
| 111 | my @comp = compareDirectories(\@oldfiles, \@newfiles); |
---|
| 112 | print Dumper(@comp); |
---|
| 113 | @todelete = @{$comp[0]}; |
---|
| 114 | @toadd = @{$comp[1]}; |
---|
| 115 | @changed = @{$comp[2]}; |
---|
| 116 | |
---|
| 117 | if ($redodelete or ! -e catfile($outdir, "files.delete")) { |
---|
| 118 | open(TODELETE, ">", catfile($outdir, "files.delete")) or die "Can't open files.delete: $!"; |
---|
| 119 | foreach my $file (@todelete) { |
---|
| 120 | printf TODELETE "%s %s\n", $file->[0], $file->[1]; |
---|
| 121 | } |
---|
| 122 | close(TODELETE); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if ($redoadd or ! -e catfile($outdir, "files.add")) { |
---|
| 126 | open(TOADD, ">", catfile($outdir, "files.add")) or die "Can't open files.add: $!"; |
---|
| 127 | foreach my $file (@toadd) { |
---|
| 128 | printf TOADD "%s # MD5 = %s\n", $file->[1], $file->[0]; |
---|
| 129 | } |
---|
| 130 | close(TOADD); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | my @toreplace; |
---|
| 134 | my @topatch; |
---|
| 135 | |
---|
| 136 | foreach my $file (@changed) { |
---|
| 137 | if (-B catdir($newdir, $file->[0])) { |
---|
| 138 | push (@toreplace, $file); |
---|
| 139 | } else { |
---|
| 140 | push (@topatch, $file); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | if ($redoreplace or ! -e catfile($outdir, "files.replace")) { |
---|
| 145 | open(TOREPLACE, ">", catfile($outdir, "files.replace")) or die "Can't open files.replace: $!"; |
---|
| 146 | foreach my $file (@toreplace) { |
---|
| 147 | printf TOREPLACE "%s %s\n", $file->[2][0], $file->[0]; |
---|
| 148 | } |
---|
| 149 | close(TOREPLACE); |
---|
| 150 | } |
---|