X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=regen_lib.pl;h=6735bb929499a9e2684df1434b866bbd968ac5f3;hb=cbfd0a879b2bf2ade4a309e6d56c08ba19f320e1;hp=896a9ad0fe4ad6a980ed3e4faaf157f50304f865;hpb=b6b9a09997c80269af874aff41936e014ed728f7;p=p5sagit%2Fp5-mst-13.2.git diff --git a/regen_lib.pl b/regen_lib.pl index 896a9ad..6735bb9 100644 --- a/regen_lib.pl +++ b/regen_lib.pl @@ -1,7 +1,10 @@ #!/usr/bin/perl -w use strict; -use vars qw($Is_W32 $Is_OS2 $Is_Cygwin $Is_NetWare $Needs_Write); +use vars qw($Is_W32 $Is_OS2 $Is_Cygwin $Is_NetWare $Needs_Write $Verbose + @Changed); use Config; # Remember, this is running using an existing perl +use File::Compare; +use Symbol; # Common functions needed by the regen scripts @@ -15,22 +18,12 @@ if ($Is_NetWare) { $Needs_Write = $Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_NetWare; -eval "use Digest::MD5 'md5'; 1;" - or warn "Digest::MD5 unavailable, doing unconditional regen\n"; +$Verbose = 0; +@ARGV = grep { not($_ eq '-q' and $Verbose = -1) } + grep { not($_ eq '-v' and $Verbose = 1) } @ARGV; -sub cksum { - my $pl = shift; - my ($buf, $cksum); - local *FH; - if (open(FH, $pl)) { - local $/; - $buf = ; - $cksum = defined &md5 ? md5($buf) : 0; - close FH; - } else { - warn "$0: $pl: $!\n"; - } - return $cksum; +END { + print STDOUT "Changed: @Changed\n" if @Changed; } sub safer_unlink { @@ -56,23 +49,32 @@ sub safer_rename_silent { rename $from, $to; } -sub safer_rename_always { +sub rename_if_different { my ($from, $to) = @_; - safer_rename_silent($from, $to) or die "renaming $from to $to: $!"; -} -sub safer_rename { - my ($from, $to) = @_; - - my $fc = cksum($from); - my $tc = cksum($to); - - if ($fc and $fc eq $tc) { - warn "no changes between '$from' & '$to'\n"; + if (compare($from, $to) == 0) { + warn "no changes between '$from' & '$to'\n" if $Verbose > 0; safer_unlink($from); return; } - warn "changed '$from' to '$to'\n"; + warn "changed '$from' to '$to'\n" if $Verbose > 0; + push @Changed, $to unless $Verbose < 0; safer_rename_silent($from, $to) or die "renaming $from to $to: $!"; } + +# Saf*er*, but not totally safe. And assumes always open for output. +sub safer_open { + my $name = shift; + my $fh = gensym; + open $fh, ">$name" or die "Can't create $name: $!"; + *{$fh}->{SCALAR} = $name; + binmode $fh; + $fh; +} + +sub safer_close { + my $fh = shift; + close $fh or die 'Error closing ' . *{$fh}->{SCALAR} . ": $!"; +} + 1;