-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpo_stats.pl
executable file
·377 lines (310 loc) · 9.05 KB
/
po_stats.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd qw{ cwd };
use File::Spec::Functions qw{ catfile catdir };
use File::Find::Rule;
use File::Basename qw{ basename };
use File::Temp qw{ tempdir };
use Data::Dumper qw{ Dumper };
use Env qw{ LANG };
use Getopt::Long qw{ GetOptions };
use List::MoreUtils qw{ uniq };
#use Locale::PO;
# TODO: on the HTML show percentages instead of errors (or have both reports).
# maybe show some progress bar
my %reports;
my $text;
my $html;
my $dir;
my $project_dir;
my $details;
my $all;
my $trunk;
GetOptions(
'text' => \$text,
'html' => \$html,
'dir=s' => \$dir,
'project=s' => \$project_dir,
'details' => \$details,
'all' => \$all,
'trunk=s' => \$trunk,
) or usage();
usage() if not $text and not $html;
$LANG = 'C';
my $tempdir = tempdir( CLEANUP => 1 );
my $cwd = cwd;
my $errors = '';
usage("--all and --project are mutually exclusive")
if $all and $project_dir;
if ( not $all and not $project_dir ) {
$project_dir = $cwd;
}
if ($project_dir) {
$reports{ basename $project_dir} = collect_report($project_dir);
} elsif ($all) {
$trunk ||= $cwd;
foreach my $project_dir ( "$trunk/Padre", glob("$trunk/Padre-Plugin-*") ) {
#print "P: $project_dir\n";
$reports{ basename $project_dir} = collect_report($project_dir);
}
}
my $text_report_file = catfile( $cwd, 'po_report.txt' );
if ($text) {
save_text_report($text_report_file);
}
if ($html) {
usage('--dir was missing') if not $dir;
usage("--dir $dir does not exist") if not -d $dir;
save_html_report($dir);
}
exit 0;
sub collect_report {
my ($project_dir) = @_;
my %data;
my $plugin_name = basename $project_dir;
$plugin_name =~ s/-/__/g;
my $share = catdir( $project_dir, 'share' );
if ( not -e $share ) {
($share) = File::Find::Rule->directory()->name('share')->in( catdir( $project_dir, 'lib' ) );
}
if ( not $share or not -e $share ) {
if ( $project_dir =~ m/Padre-Plugin-(.*)$/ ) {
my $plugin_name = $1;
_warn("Plugin '$plugin_name' is not internationalized.\n");
} else {
_warn("Could not find a 'share' directory in '$project_dir'");
}
return;
}
my $localedir = catdir( $share, 'locale' );
if ( not -d $localedir ) {
if ( $project_dir =~ m/Padre-Plugin-(.*)$/ ) {
my $plugin_name = $1;
_warn("Plugin '$plugin_name' is not internationalized.\n");
} else {
_warn("Can't find locale directory '$localedir'.\n");
}
return;
}
my @po_files = glob catfile( $localedir, '*.po');
my $pot_file = catfile( $localedir, 'messages.pot' );
if ( open my $fh, '<', $pot_file ) {
while ( my $line = <$fh> ) {
if ( $line =~ /^msgid/ ) {
$data{total}++;
}
}
}
foreach my $po_file ( sort @po_files ) {
#print "$po_file\n";
my $err = "$tempdir/err";
system "msgcmp $po_file $pot_file 2> $err";
my $language = substr( basename($po_file), 0, -3 );
if ( $plugin_name ne 'Padre' ) {
$language =~ s/^$plugin_name-//;
}
if ( open my $fh, '<', $err ) {
local $/ = undef;
$data{$language}{details} = <$fh>;
if ( $data{$language}{details} =~ /msgcmp: found (\d+) fatal errors?/ ) {
$data{$language}{errors} = $1;
} else {
$data{$language}{errors} = 0;
}
} else {
# TODO: report that could not open file
}
}
return \%data;
}
sub save_html_report {
my ($dir) = @_;
my $html = <<'END_HEAD';
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<title>Padre translation status report</title>
<style type="text/css">
body, th, tr {
font: 13px Verdana,Arial,'Bitstream Vera Sans',Helvetica,sans-serif;
}
th {
font-weight: bold;
text-align: left;
}
th.lang {
font-weight: normal;
text-align: center;
}
.num, .num-red, .num-orange, .num-yellow, .num-green, .num-lgreen {
text-align: right;
}
.red, .num-red {
background-color: red;
}
.orange, .num-orange {
background-color: orange;
}
.yellow, .num-yellow {
background-color: yellow;
}
.green, .num-green {
background-color: green;
}
.lgreen, .num-lgreen {
background-color: lightgreen;
}
</style>
</head><body>
END_HEAD
my $time = localtime();
$html .= <<"END_HTML";
<h1>Padre translation status report</h1>
<p><a href="http://padre.perlide.org/translators.html">Get to know our translators on the Padre website.</a></p>
<p>If you want to help translating Padre, you can check out our
<a href="http://padre.perlide.org/trac/wiki/TranslationIntro">wiki page on translations</a>.</p>
<p>The numbers showing the number of errors. An empty cell means that translation does not exist at all</p>
<p>Generated on: $time (it is generated using a cron-job)</p>
<table id="legend">
<caption>Legend</caption>
<tbody>
<tr><td class="red">more than 40% missing</td></tr>
<tr><td class="yellow">10%-40% missing</td></tr>
<tr><td class="green">less than 10% missing</td></tr>
<tr><td class="lgreen">perfect</td></tr>
</tbody>
</table>
<table border="1">
END_HTML
#die Dumper $reports{"Padre-Plugin-SpellCheck"};
my @languages = uniq sort grep { !/total/ } map { keys %{ $reports{$_} } } keys %reports;
$html .= '<thead>';
$html .= _header(@languages);
$html .= '</thead><tbody>';
my %totals;
foreach my $project ( sort keys %reports ) {
$html .= "<tr><th>$project</th><td class='num'>";
my $total = $reports{$project}{total};
$html .= defined $total ? $total : ' ';
$total ||= 0;
$html .= "</td>";
if ( $reports{$project}{total} ) {
$totals{total} += $reports{$project}{total};
}
foreach my $language (@languages) {
if ( $reports{$project}{total} ) {
if ( defined $reports{$project}{$language}{errors} ) {
$html .= _td_open( $reports{$project}{$language}{errors}, $total );
$html .= $reports{$project}{$language}{errors};
$totals{$language} += $reports{$project}{$language}{errors};
} else {
#$html .= '<td class=red>-';
$html .= "<td class='num-red'>$reports{$project}{total}";
$totals{$language} += $reports{$project}{total};
}
} else {
$html .= '<td> ';
}
$html .= '</td>';
}
$html .= "</tr>\n";
}
$html .= "<tr><td>TOTAL</td><td>$totals{total}</td>";
foreach my $language (@languages) {
$html .= _td_open( $totals{$language}, $totals{total} );
$html .= $totals{$language};
$html .= "</td>";
}
$html .= "</tr>";
$html .= _header(@languages);
$html .= "</tbody></table>\n";
$html .= "<h2>Padre GUI level of completeness</h2>\n";
$html .= "<table>";
foreach my $language (@languages) {
my $p = 100 - int( 100 * $totals{$language} / $totals{total} );
$html .= "<tr><th>$language</th><td><img src='../img/$p.png' alt='$p %'/></td><td class='num'>$p %</td></tr>\n";
}
$html .= "</table>";
$html .= "<h2>Errors</h2>\n";
if ($errors) {
$html .= "<pre>\n$errors\n</pre>\n";
} else {
$html .= "<p>No errors were reported</p>\n";
}
$html .= "</body></html>";
open my $fh, '>:utf8', "$dir/index.html" or die;
print $fh $html;
}
sub _header {
return "<tr><td></td><th>Total</th>" . ( join "", map {"<th class='lang'>$_</th>"} @_ ) . "</tr>\n";
}
sub _td_open {
my ( $errors, $total ) = @_;
if ( $errors > $total * 0.40 ) {
return q(<td class="num-red">);
# } elsif ( $errors > $total * 0.20 ) {
# return q(<td class=orange>);
} elsif ( $errors > $total * 0.10 ) {
return q(<td class="num-yellow">);
} elsif ( $errors > 0 ) {
return q(<td class="num-green">);
} else {
return q(<td class="num-lgreen">);
}
}
sub save_text_report {
my ($text_report_file) = @_;
open my $fh, '>', $text_report_file or die;
print $fh "Generated by $0 on " . localtime() . "\n\n";
foreach my $project ( sort keys %reports ) {
print $fh "--------------------\n";
print $fh "Project $project\n\n";
print $fh generate_text_report( $reports{$project} );
}
print "file $text_report_file generated.\n";
}
sub generate_text_report {
my ($data) = @_;
my $report .= "Language Errors\n";
foreach my $language ( sort keys %$data ) {
next if $language eq 'total';
$report .= sprintf( "%-10s %s\n", $language, $data->{$language}{errors} );
}
if ($details) {
foreach my $language ( sort keys %$data ) {
next if $language eq 'total';
$report .= "\n------------------\n";
$report .= "Language: $language \n\n";
if ( $data->{$language}{errors} ) {
$report .= "Fatal errors: $data->{$language}{errors}\n\n";
}
$report .= $data->{$language}{details};
}
}
return $report;
}
sub _warn {
my $w = shift;
$errors .= $w;
warn($w);
}
sub usage {
my $msg = shift;
print "$msg\n\n" if defined $msg;
print <<"END_USAGE";
Usage: $0
--text generate text report
--html generate html report
--details provides details (in text report only)
--dir DIR where to save the html report
--project PATH path to the project directory (e.g. ~/padre/Padre-Plugin-Vi)
--all all the projects
--trunk PATH to root of all the projects
--all and --project are mutually exclusive
At least one of --html and --text need to be given
END_USAGE
exit 1;
}