[Po4a-commits] "po4a/lib/Locale/Po4a Po.pm,1.49,1.50"

Nicolas FRANCOIS nekral-guest at alioth.debian.org
Thu Nov 17 23:33:16 UTC 2005


Update of /cvsroot/po4a/po4a/lib/Locale/Po4a
In directory haydn:/tmp/cvs-serv1307/lib/Locale/Po4a

Modified Files:
	Po.pm 
Log Message:
Add a write_if_needed subroutine in Po.pm to check if an update is needed.
Use it in po4a to write the POT when the --force option is not specified.
see #339459.


Index: Po.pm
===================================================================
RCS file: /cvsroot/po4a/po4a/lib/Locale/Po4a/Po.pm,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -d -r1.49 -r1.50
--- Po.pm	17 Nov 2005 21:00:20 -0000	1.49
+++ Po.pm	17 Nov 2005 23:33:14 -0000	1.50
@@ -76,7 +76,7 @@
 use subs qw(makespace);
 use vars qw(@ISA @EXPORT);
 @ISA = ();
- at EXPORT = qw(read write gettext);
+ at EXPORT = qw(read write write_if_needed gettext);
 
 use Carp qw(croak);
 use File::Path; # mkdir before write
@@ -303,6 +303,45 @@
 #    }
 }
 
+=item write_if_needed($$)
+
+Like write, but if the PO or POT file already exists, the object will be
+written in a temporary file which will be compared with the existing file
+to check that the update is needed (this avoids to change a POT just to
+update a line reference or the POT-Creation-Date field).
+
+=cut
+
+sub write_if_needed {
+    my $self=shift;
+    my $filename=shift 
+	or croak (dgettext("po4a","Can't write to a file without filename")."\n");
+
+    if (-e $filename) {
+        my ($tmp_filename, $diff);
+        (undef,$tmp_filename)=File::Temp->tempfile($filename."XXXX",
+                                                   DIR    => "/tmp",
+                                                   OPEN   => 0,
+                                                   UNLINK => 0);
+        $self->write($tmp_filename);
+        $diff = qx(diff -q -I'#:' -I'POT-Creation-Date:' -I'PO-Revision-Date:' $filename $tmp_filename);
+        if ( $diff eq "" ) {
+            unlink $tmp_filename ||
+                die wrap_msg(dgettext("po4a","Can't unlink %s."),
+                             $tmp_filename);
+            # touch it
+            my ($atime, $mtime) = (time,time);
+            utime $atime, $mtime, $filename;
+        } else {
+            rename $tmp_filename, $filename ||
+                die wrap_msg(dgettext("po4a","Can't rename %s to %s."),
+                             $tmp_filename, $filename);
+        }
+    } else {
+        $self->write($filename);
+    }
+}
+
 =item gettextize($$)
 
 This function produces one translated message catalog from two catalogs, an




More information about the Po4a-commits mailing list