[Reproducible-commits] [strip-nondeterminism] 01/01: Normalize POT-Creation-Date in Gettext GMO files
Reiner Herrmann
reiner at reiner-h.de
Sat Feb 20 19:10:30 UTC 2016
This is an automated email from the git hooks/post-receive script.
deki-guest pushed a commit to branch master
in repository strip-nondeterminism.
commit 1d4f16d8f3331f4b7d26222a6627ab8169aa5339
Author: Reiner Herrmann <reiner at reiner-h.de>
Date: Sat Feb 20 19:49:58 2016 +0100
Normalize POT-Creation-Date in Gettext GMO files
---
lib/File/StripNondeterminism.pm | 6 ++
lib/File/StripNondeterminism/handlers/gettext.pm | 101 +++++++++++++++++++++++
2 files changed, 107 insertions(+)
diff --git a/lib/File/StripNondeterminism.pm b/lib/File/StripNondeterminism.pm
index 733bc1c..26a227c 100644
--- a/lib/File/StripNondeterminism.pm
+++ b/lib/File/StripNondeterminism.pm
@@ -22,6 +22,7 @@ use strict;
use warnings;
use File::StripNondeterminism::handlers::ar;
+use File::StripNondeterminism::handlers::gettext;
use File::StripNondeterminism::handlers::gzip;
use File::StripNondeterminism::handlers::jar;
use File::StripNondeterminism::handlers::javadoc;
@@ -53,6 +54,10 @@ sub get_normalizer_for_file {
if (m/\.a$/ && _get_file_type($_) =~ m/ar archive/) {
return \&File::StripNondeterminism::handlers::ar::normalize;
}
+ # gettext
+ if (m/\.g?mo$/ && _get_file_type($_) =~ m/GNU message catalog/) {
+ return \&File::StripNondeterminism::handlers::gettext::normalize;
+ }
# gzip
if (m/\.(gz|dz)$/ && _get_file_type($_) =~ m/gzip compressed data/) {
return \&File::StripNondeterminism::handlers::gzip::normalize;
@@ -87,6 +92,7 @@ sub get_normalizer_for_file {
sub get_normalizer_by_name {
$_ = shift;
return \&File::StripNondeterminism::handlers::ar::normalize if $_ eq 'ar';
+ return \&File::StripNondeterminism::handlers::gettext::normalize if $_ eq 'gettext';
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';
diff --git a/lib/File/StripNondeterminism/handlers/gettext.pm b/lib/File/StripNondeterminism/handlers/gettext.pm
new file mode 100644
index 0000000..1f489e8
--- /dev/null
+++ b/lib/File/StripNondeterminism/handlers/gettext.pm
@@ -0,0 +1,101 @@
+#
+# Copyright 2016 Reiner Herrmann <reiner at reiner-h.de>
+#
+# 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::gettext;
+
+use Time::Piece;
+use POSIX qw(strftime);
+
+use strict;
+use warnings;
+
+sub read_file {
+ my $filename = shift;
+
+ local $/ = undef;
+ open(my $fh, '<', $filename) or die "Can't open file $filename for reading: $!";
+ binmode($fh);
+ my $buf = <$fh>;
+ close($fh);
+
+ return $buf;
+}
+
+sub normalize {
+ my ($mo_filename, %options) = @_;
+
+ my $norm_time = $File::StripNondeterminism::canonical_time // 0;
+ my $fmt;
+
+ my $buf = read_file($mo_filename);
+
+ my $magic = unpack("V", substr($buf, 0*4, 4));
+ if ($magic == 0x950412DE) {
+ # little endian
+ $fmt = "V";
+ } elsif ($magic == 0xDE120495) {
+ # big endian
+ $fmt = "N";
+ } else {
+ # unknown format
+ return 0;
+ }
+
+ my ($revision, $nstrings, $orig_to, $trans_to) = unpack($fmt x 4, substr($buf, 1*4, 4*4));
+ my $major = int($revision / 255);
+ my $minor = int($revision % 255);
+ return 0 if $major > 1;
+
+ my $changed = 0;
+ for (my $i=0; $i < $nstrings; $i++) {
+ my $len = unpack($fmt, substr($buf, $orig_to + $i*8, 4));
+ next if $len > 0;
+
+ my $offset = unpack($fmt, substr($buf, $orig_to + $i*8 + 4, 4));
+ my $trans_len = unpack($fmt, substr($buf, $trans_to + $i*8));
+ my $trans_offset = unpack($fmt, substr($buf, $trans_to + $i*8 + 4));
+ my $trans_msg = substr($buf, $trans_offset, $trans_len);
+ next unless $trans_msg =~ m/^POT-Creation-Date: (.*)/m;
+
+ my $pot_date = $1;
+ my $time;
+ eval {
+ $time = Time::Piece->strptime($pot_date, "%Y-%m-%d %H:%M%z");
+ };
+ next if $@;
+ next if $time <= $norm_time;
+
+ my $new_time = strftime("%Y-%m-%d %H:%M%z", gmtime($norm_time));
+ $trans_msg =~ s/\QPOT-Creation-Date: $pot_date\E/POT-Creation-Date: $new_time/;
+ next if length($trans_msg) != $trans_len;
+
+ $buf = substr($buf, 0, $trans_offset) . $trans_msg . substr($buf, $trans_offset + $trans_len);
+ $changed = 1;
+ }
+
+ if ($changed) {
+ open(my $fh, '>', $mo_filename) or die "Can't open file $mo_filename for writing: $!";
+ binmode($fh);
+ print $fh $buf;
+ close($fh);
+ }
+
+ return $changed;
+}
+
+1;
--
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