Perhaps a little too much copy-paste from javadoc.pm, but not obviously so. Test.
https://github.com/FauxFaux/strip-nondeterminism --- lib/File/StripNondeterminism.pm | 6 ++ .../StripNondeterminism/handlers/pomproperties.pm | 70 ++++++++++++++++++++++ t/pomproperties.t | 31 ++++++++++ 3 files changed, 107 insertions(+) create mode 100644 lib/File/StripNondeterminism/handlers/pomproperties.pm create mode 100644 t/pomproperties.t diff --git a/lib/File/StripNondeterminism.pm b/lib/File/StripNondeterminism.pm index d20c2dc..9f86acd 100644 --- a/lib/File/StripNondeterminism.pm +++ b/lib/File/StripNondeterminism.pm @@ -25,6 +25,7 @@ use File::StripNondeterminism::handlers::ar; use File::StripNondeterminism::handlers::gzip; use File::StripNondeterminism::handlers::jar; use File::StripNondeterminism::handlers::javadoc; +use File::StripNondeterminism::handlers::pomproperties; use File::StripNondeterminism::handlers::zip; our($VERSION); @@ -62,6 +63,10 @@ sub get_normalizer_for_file { if (m/\.html$/ && File::StripNondeterminism::handlers::javadoc::is_javadoc_file($_)) { return \&File::StripNondeterminism::handlers::javadoc::normalize; } + # pomproperties + if (m/pom\.properties$/ && File::StripNondeterminism::handlers::pomproperties::is_pom_properties_file($_)) { + return \&File::StripNondeterminism::handlers::pomproperties::normalize; + } # zip if (m/\.zip$/ && _get_file_type($_) =~ m/Zip archive data/) { return \&File::StripNondeterminism::handlers::zip::normalize; @@ -75,6 +80,7 @@ sub get_normalizer_by_name { return \&File::StripNondeterminism::handlers::gzip::normalize if $_ eq 'gzip'; return \&File::StripNondeterminism::handlers::jar::normalize if $_ eq 'jar'; return \&File::StripNondeterminism::handlers::javadoc::normalize if $_ eq 'javadoc'; + return \&File::StripNondeterminism::handlers::pomproperties::normalize if $_ eq 'pomproperties'; return \&File::StripNondeterminism::handlers::zip::normalize if $_ eq 'zip'; return undef; } diff --git a/lib/File/StripNondeterminism/handlers/pomproperties.pm b/lib/File/StripNondeterminism/handlers/pomproperties.pm new file mode 100644 index 0000000..990add3 --- /dev/null +++ b/lib/File/StripNondeterminism/handlers/pomproperties.pm @@ -0,0 +1,70 @@ +# +# Copyright 2014 Andrew Ayer +# +# This file is part of strip-nondeterminism. +# +# strip-nondeterminism is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# strip-nondeterminism is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with strip-nondeterminism. If not, see <http://www.gnu.org/licenses/>. +# +package File::StripNondeterminism::handlers::pomproperties; + +use strict; +use warnings; + +use File::Temp; +use File::Basename; + +sub is_pom_properties_file { + my ($filename) = @_; + + # If this is a pom.properties file, '#Generated by Maven' should appear in first 1kb + my $fh; + my $str; + return open($fh, '<', $filename) && read($fh, $str, 1024) && $str =~ /#Generated by Maven/; +} + +sub normalize { + my ($filename) = @_; + + open(my $fh, '<', $filename) or die "Unable to open $filename for reading: $!"; + my $tempfile = File::Temp->new(DIR => dirname($filename)); + + # Strip the javadoc comment, which contains a timestamp. + # It should appear within first 10 lines. + while (defined(my $line = <$fh>) && $. <= 10) { + # Yes, there really is no comma here + if ($line =~ /^#\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \w{3} \d{4}\s*$/) { + $line = ''; + print $tempfile $line; + + # Copy through rest of file + my $bytes_read; + my $buf; + while ($bytes_read = read($fh, $buf, 4096)) { + print $tempfile $buf; + } + defined($bytes_read) or die "$filename: read failed: $!"; + + # Rename temporary file over the file + chmod((stat($fh))[2] & 07777, $tempfile->filename); + rename($tempfile->filename, $filename) or die "$filename: unable to overwrite: rename: $!"; + $tempfile->unlink_on_destroy(0); + return 1; + } + print $tempfile $line; + } + + return 0; +} + +1; diff --git a/t/pomproperties.t b/t/pomproperties.t new file mode 100644 index 0000000..5f31930 --- /dev/null +++ b/t/pomproperties.t @@ -0,0 +1,31 @@ +#!perl + +use File::Temp 'tempdir'; +use Test::More tests => 2; +use File::StripNondeterminism; + +$dir = tempdir( CLEANUP => 1 ); +$path = "$dir/pom.properties"; +open(my $fh, '>', $path) or die("error opening $path"); + +# note that the line has a load of random trailing whitespace (?!) +print $fh <<'ORIGINAL'; +#Generated by Maven +#Mon Oct 27 09:12:51 UTC 2014 +version=2.4 +ORIGINAL + +close $fh; + +$normalizer = File::StripNondeterminism::get_normalizer_for_file($path); +isnt(undef, $normalizer); +$normalizer->($path); + +open FILE,$path or die("error opening $path"); +binmode FILE; +local $/ = undef; +is(<FILE>, <<'EXPECTED'); +#Generated by Maven +version=2.4 +EXPECTED + -- 1.9.1 _______________________________________________ Reproducible-builds mailing list Reproducible-builds@lists.alioth.debian.org http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/reproducible-builds