[Reproducible-commits] [strip-nondeterminism] 07/10: strip generation time from pom.properties
Andrew Ayer
agwa at andrewayer.name
Sun Nov 16 23:16:06 UTC 2014
This is an automated email from the git hooks/post-receive script.
agwa-guest pushed a commit to branch master
in repository strip-nondeterminism.
commit 0acea0d0aadb80268e0a6f0398a2924929977f13
Author: Chris West (Faux) <git at goeswhere.com>
Date: Mon Oct 27 15:19:34 2014 -0700
strip generation time from pom.properties
Modified-by: Andrew Ayer <agwa at andrewayer.name>
* Add copyright notice
---
lib/File/StripNondeterminism.pm | 6 ++
.../StripNondeterminism/handlers/pomproperties.pm | 70 ++++++++++++++++++++++
t/pomproperties.t | 50 ++++++++++++++++
3 files changed, 126 insertions(+)
diff --git a/lib/File/StripNondeterminism.pm b/lib/File/StripNondeterminism.pm
index af04b6c..55eebbe 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, $canonical_time);
@@ -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..785f5e6
--- /dev/null
+++ b/lib/File/StripNondeterminism/handlers/pomproperties.pm
@@ -0,0 +1,70 @@
+#
+# Copyright 2014 Chris West (Faux)
+#
+# 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 generation date 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..6982deb
--- /dev/null
+++ b/t/pomproperties.t
@@ -0,0 +1,50 @@
+#!perl
+
+#
+# Copyright 2014 Chris West (Faux)
+#
+# 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/>.
+#
+
+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
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/strip-nondeterminism.git
More information about the Reproducible-commits
mailing list