]> git.lyx.org Git - lyx.git/blob - po/diff_po.pl
Use true temporary files
[lyx.git] / po / diff_po.pl
1 #! /usr/bin/env perl
2
3 # file diff_po.pl
4 # script to compare changes between translation files before merging them
5 #
6 # Examples of usage:
7 # ./diff_po.pl cs.po.old cs.po
8 # svn diff -r38367 --diff-cmd ./diff_po.pl cs.po
9 # git difftool --extcmd=./diff_po.pl sk.po
10 # ./diff_po.pl -r HEAD~100 cs.po        #fetch git revision and compare
11 # ./diff_po.pl -r39229 cs.po            #fetch svn revision and compare
12 #
13 # This file is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public
15 # License as published by the Free Software Foundation; either
16 # version 2 of the License, or (at your option) any later version.
17 #
18 # This software is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public
24 # License along with this software; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 #
27 # Copyright (c) 1010-2011 Kornel Benko, kornel@lyx.org
28 #
29 # TODO:
30 # 1.) Check for ".git" or ".svn" to decide about revisioning
31 # 2.) Search for good correlations of deleted <==> inserted string
32 #     using Text::Levenshtein or Algorithm::Diff
33
34 use strict;
35 use Term::ANSIColor qw(:constants);
36 use File::Temp;
37
38 my ($status, $foundline, $msgid, $msgstr, $fuzzy);
39
40 my %Messages = ();              # Used for original po-file
41 my %newMessages = ();           # new po-file
42 my %Untranslated = ();          # inside new po-file
43 my %Fuzzy = ();                 # inside new po-file
44 my $result = 0;                 # exit value
45 my $printlines = 0;
46 my @names = ();
47
48 # Check first, if called as standalone program for git
49 if ($ARGV[0] =~ /^-r(.*)/) {
50   my $rev = $1;
51   shift(@ARGV);
52   if ($rev eq "") {
53     $rev = shift(@ARGV);
54   }
55   for my $argf (@ARGV) {
56     my $baseargf;
57     my $filedir;
58     if ($argf =~ /^(.*)\/([^\/]+)$/) {
59       $baseargf = $2;
60       $filedir = $1;
61     }
62     else {
63       $baseargf = $argf;
64       $filedir = ".";
65     }
66     if (-d "$filedir/../.git") {
67       my @args = ();
68       my $tmpfile = File::Temp->new();
69       push(@args, "-L", $argf . "    (" . $rev . ")");
70       push(@args, "-L", $argf . "    (local copy)");
71       open(FI, "git show $rev:po/$baseargf|");
72       $tmpfile->unlink_on_destroy( 1 );
73       while(my $l = <FI>) {
74         print $tmpfile $l;
75       }
76       close(FI);
77       push(@args, $tmpfile->filename, $argf);
78       print "===================================================================\n";
79       &diff_po(@args);
80     }
81     elsif (-d "$filedir/.svn") {
82       # call it again indirectly
83       my @cmd = ("svn", "diff", "-r$rev", "--diff-cmd", $0, $argf);
84       print "cmd = " . join(' ', @cmd) . "\n";
85       system(@cmd);
86     }
87   }
88 }
89 else {
90   &diff_po(@ARGV);
91 }
92
93 exit($result);
94 #########################################################
95
96 sub diff_po($$)
97 {
98   my @args = @_;
99   %Messages = ();
100   %newMessages = ();
101   %Untranslated = ();
102   %Fuzzy = ();
103   @names = ();
104   while(defined($args[0])) {
105     last if ($args[0] !~ /^\-/);
106     my $param = shift(@args);
107     if ($param eq "-L") {
108       my $name = shift(@args);
109       push(@names, $name);
110     }
111   }
112   if (! defined($names[0])) {
113     push(@names, "original");
114   }
115   if (! defined($names[1])) {
116     push(@names, "new");
117   }
118
119   if (@args != 2) {
120     die("names = \"", join('" "', @names) . "\"... args = \"" . join('" "', @args) . "\" Expected exactly 2 parameters");
121   }
122
123   &check($names[0], $args[0]);
124   &check($names[1], $args[1]);
125
126   &parse_po_file($args[0], \%Messages);
127   &parse_po_file($args[1], \%newMessages);
128
129   my @MsgKeys = &getLineSortedKeys(\%newMessages);
130
131   print RED "<<< \"$names[0]\"\n", RESET;
132   print GREEN ">>> \"$names[1]\"\n", RESET;
133   for my $k (@MsgKeys) {
134     if ($newMessages{$k}->{msgstr} eq "") {
135       # this is still untranslated string
136       $Untranslated{$newMessages{$k}->{line}} = $k;
137     }
138     elsif ($newMessages{$k}->{fuzzy}) {
139       #fuzzy string
140       $Fuzzy{$newMessages{$k}->{line}} = $k;
141     }
142     if (exists($Messages{$k})) {
143       &printIfDiff($k, $Messages{$k}, $newMessages{$k});
144       delete($Messages{$k});
145       delete($newMessages{$k});
146     }
147   }
148
149   @MsgKeys = &getLineSortedKeys(\%Messages);
150   for my $k (@MsgKeys) {
151     $result |= 8;
152     print "deleted message\n";
153     print "< line = " . $Messages{$k}->{line} . "\n" if ($printlines);
154     print RED "< fuzzy = " . $Messages{$k}->{fuzzy} . "\n", RESET;
155     print RED "< msgid = \"$k\"\n", RESET;
156     print RED "< msgstr = \"" . $Messages{$k}->{msgstr} . "\"\n", RESET;
157   }
158
159   @MsgKeys = &getLineSortedKeys(\%newMessages);
160   for my $k (@MsgKeys) {
161     $result |= 16;
162     print "new message\n";
163     print "> line = " . $newMessages{$k}->{line} . "\n" if ($printlines);
164     print GREEN "> fuzzy = " . $newMessages{$k}->{fuzzy} . "\n", RESET;
165     print GREEN "> msgid = \"$k\"\n", RESET;
166     print GREEN "> msgstr = \"" . $newMessages{$k}->{msgstr} . "\"\n", RESET;
167   }
168
169   &printExtraMessages("fuzzy", \%Fuzzy);
170   &printExtraMessages("untranslated", \%Untranslated);
171 }
172
173 sub check($$)
174 {
175   my ($spec, $filename) = @_;
176
177   if (! -e $filename ) {
178     die("$spec po file does not exist");
179   }
180   if ( ! -f $filename ) {
181     die("$spec po file is not regular");
182   }
183   if ( ! -r $filename ) {
184     die("$spec po file is not readable");
185   }
186 }
187
188 sub printDiff($$$$)
189 {
190   my ($k, $nk, $rM, $rnM) = @_;
191   print "diffline = " . $rM->{line} . "," . $rnM->{line} . "\n" if ($printlines);
192   print "  msgid = \"$k\"\n";
193   if ($rM->{fuzzy} eq $rnM->{fuzzy}) {
194     print "  fuzzy = " . $rM->{fuzzy} . "\n" if ($printlines);
195   }
196   else {
197     print RED "< fuzzy = " . $rM->{fuzzy} . "\n", RESET;
198   }
199   print RED "< msgstr = " . $rM->{msgstr} . "\n", RESET;
200   if ($k ne $nk) {
201     print GREEN "> msgid = \"$nk\"\n", RESET;
202   }
203   if ($rM->{fuzzy} ne $rnM->{fuzzy}) {
204     print GREEN "> fuzzy = " . $rnM->{fuzzy} . "\n", RESET;
205   }
206   print GREEN "> msgstr = " . $rnM->{msgstr} . "\n", RESET;
207   print "\n";
208 }
209
210 sub printIfDiff($$$)
211 {
212   my ($k, $rM, $rnM) = @_;
213   my $doprint = 0;
214   $doprint = 1 if ($rM->{fuzzy} != $rnM->{fuzzy});
215   $doprint = 1 if ($rM->{msgstr} ne $rnM->{msgstr});
216   if ($doprint) {
217     $result |= 4;
218     &printDiff($k, $k, $rM, $rnM);
219   }
220 }
221
222 sub parse_po_file($$)
223 {
224   my ($file, $rMessages) = @_;
225   if (open(FI, '<', $file)) {
226     $status = "normal";
227     $fuzzy = 0;
228     my $lineno = 0;
229     while (my $line = <FI>) {
230       $lineno++;
231       &parse_po_line($line, $lineno, $rMessages);
232     }
233     &parse_po_line("", $lineno + 1, $rMessages);
234     close(FI);
235   }
236 }
237
238 sub parse_po_line($$$)
239 {
240   my ($line, $lineno, $rMessages) = @_;
241   chomp($line);
242
243   if ($status eq "normal") {
244     if ($line =~ /^#, fuzzy/) {
245       $fuzzy = 1;
246     }
247     elsif ($line =~ s/^msgid\s+//) {
248       $foundline = $lineno;
249       $status = "msgid";
250       $msgid = "";
251       &parse_po_line($line);
252     }
253   }
254   elsif ($status eq "msgid") {
255     if ($line =~ /^\s*"(.*)"\s*/) {
256       $msgid .= $1;
257     }
258     elsif ($line =~ s/^msgstr\s+//) {
259       $status = "msgstr";
260       $msgstr = "";
261       &parse_po_line($line);
262     }
263   }
264   elsif ($status eq "msgstr") {
265     if ($line =~ /^\s*"(.*)"\s*/) {
266       $msgstr .= $1;
267     }
268     else {
269       if ($msgid ne "") {
270         $rMessages->{$msgid}->{line} = $foundline;
271         $rMessages->{$msgid}->{fuzzy} = $fuzzy;
272         $rMessages->{$msgid}->{msgstr} = $msgstr;
273       }
274       $fuzzy = 0;
275       $status = "normal";
276     }
277   }
278   else {
279     die("invalid status");
280   }
281 }
282
283 sub getLineSortedKeys($)
284 {
285   my ($rMessages) = @_;
286
287   return sort {$rMessages->{$a}->{line} <=> $rMessages->{$b}->{line};} keys %{$rMessages};
288 }
289
290 sub printExtraMessages($$)
291 {
292   my ($type, $rExtra) = @_;
293   my @UntranslatedKeys = sort { $a <=> $b;} keys %{$rExtra};
294
295   if (@UntranslatedKeys > 0) {
296     print "Still " . 0 + @UntranslatedKeys . " $type messages found in $ARGV[1]\n";
297     for my $l (@UntranslatedKeys) {
298       print "> line $l: \"" . $rExtra->{$l} . "\"\n"; 
299     }
300   }
301 }