forked from mquinson/po4a
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Po4aBuilder.pm
executable file
·304 lines (264 loc) · 10.2 KB
/
Po4aBuilder.pm
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
package Po4aBuilder;
use Module::Build;
use File::Basename;
use File::Path qw(mkpath rmtree);
use File::Spec;
use File::Copy qw(copy);
use File::stat;
@ISA = qw(Module::Build);
sub ACTION_build {
my $self = shift;
$self->depends_on('code');
$self->depends_on('docs');
$self->depends_on('distmeta'); # regenerate META.yml
$self->depends_on('man');
$self->depends_on('postats');
}
sub make_files_writable {
my $self = shift;
my $dir = shift;
my $files = $self->rscan_dir($dir, sub {-f});
foreach my $file (@$files) {
my $current_mode = stat($file)->mode;
chmod $current_mode | oct(200), $file;
}
}
sub perl_scripts {
return ('po4a-gettextize', 'po4a-updatepo', 'po4a-translate',
'po4a-normalize', 'po4a', 'scripts/msguntypot');
}
sub shell_scripts {
return ('share/po4a-build');
}
# Update po/bin/*.po files
sub ACTION_binpo {
my $self = shift;
my ($cmd, $sources);
$self->depends_on('code');
$self->make_files_writable("po/bin");
my @perl_files = sort((perl_scripts(), @{$self->rscan_dir('lib',qr{\.pm$})}));
my @shell_files = sort(shell_scripts());
my @all_files = (@perl_files, @shell_files);
unless ($self->up_to_date(\@all_files, "po/bin/po4a.pot")) {
print "XX Update po/bin/po4a-perl.pot\n";
chdir "po/bin";
$sources = join ("", map {" ../../".$_ } @perl_files);
$cmd = "xgettext ";
$cmd .= "--from-code=utf-8 ";
$cmd .= "-L Perl ";
$cmd .= "--add-comments ";
$cmd .= "--msgid-bugs-address po4a\@packages.debian.org ";
$cmd .= "--package-name po4a ";
$cmd .= "--package-version ".$self->dist_version()." ";
$cmd .= "$sources ";
$cmd .= "-o po4a-perl.pot";
system($cmd) && die;
print "XX Update po/bin/po4a-shell.pot\n";
$sources = join ("", map {" ../../".$_ } @shell_files);
$cmd = "xgettext ";
$cmd .= "--from-code=utf-8 ";
$cmd .= "-L shell ";
$cmd .= "--add-comments ";
$cmd .= "--msgid-bugs-address po4a\@packages.debian.org ";
$cmd .= "--package-name po4a ";
$cmd .= "--package-version ".$self->dist_version()." ";
$cmd .= "$sources ";
$cmd .= "-o po4a-shell.pot";
system($cmd) && die;
$cmd = "msgcat po4a-perl.pot po4a-shell.pot -o po4a.pot.new";
system($cmd) && die;
chdir "../..";
unlink "po/bin/po4a-perl.pot" || die;
unlink "po/bin/po4a-shell.pot" || die;
if ( -e "po/bin/po4a.pot") {
$diff = qx(diff -q -I'#:' -I'POT-Creation-Date:' -I'PO-Revision-Date:' po/bin/po4a.pot po/bin/po4a.pot.new);
if ( $diff eq "" ) {
unlink "po/bin/po4a.pot.new" || die;
# touch it
my ($atime, $mtime) = (time,time);
utime $atime, $mtime, "po/bin/po4a.pot";
} else {
rename "po/bin/po4a.pot.new", "po/bin/po4a.pot" || die;
}
} else {
rename "po/bin/po4a.pot.new", "po/bin/po4a.pot" || die;
}
} else {
print "XX po/bin/po4a.pot uptodate.\n";
}
# update languages
foreach (@{$self->rscan_dir('po/bin',qr{\.po$})}) {
my $lang = fileparse($_, qw{.po});
unless ($self->up_to_date("po/bin/po4a.pot", $_)) {
print "XX Sync $_: ";
system("msgmerge --previous $_ po/bin/po4a.pot -o $_.new") && die;
# Typically all that changes was a date. I'd
# prefer not to commit such changes, so detect
# and ignore them.
$diff = qx(diff -q -I'#:' -I'POT-Creation-Date:' -I'PO-Revision-Date:' $_ $_.new);
if ($diff eq "") {
unlink "$_.new" || die;
# touch it
my ($atime, $mtime) = (time,time);
utime $atime, $mtime, $_;
} else {
rename "$_.new", $_ || die;
}
} else {
print "XX $_ uptodate.\n";
}
unless ($self->up_to_date($_,"blib/po/$lang/LC_MESSAGES/po4a.mo")) {
mkpath( File::Spec->catdir( 'blib', 'po', $lang, "LC_MESSAGES" ), 0, oct(755) );
system("msgfmt -o blib/po/$lang/LC_MESSAGES/po4a.mo $_") && die;
}
}
}
sub ACTION_install {
my $self = shift;
require ExtUtils::Install;
# print ("KEYS\n");
# foreach my $k ($self->install_types()) {
# print ("$k -> ".$self->install_destination($k)."\n");
# }
my $mandir = $self->install_destination('libdoc');
$mandir =~ s,/man3$,,;
$self->install_path(man => $mandir);
my $localedir = $self->install_destination('libdoc');
$localedir =~ s,/man/man3$,/locale,;
$self->install_path(po => $localedir);
ExtUtils::Install::install($self->install_map, !$self->quiet, 0, $self->{args}{uninst}||0);
}
sub ACTION_dist {
my ($self) = @_;
$ENV{PO4AFLAGS} ||= '--force';
$self->depends_on('distcheck');
$self->depends_on('test');
$self->depends_on('binpo');
$self->depends_on('manpo');
$self->depends_on('distdir');
my $dist_dir = $self->dist_dir;
$self->make_files_writable($dist_dir);
if ( -e "$dist_dir.tar.gz") {
# Delete the distfile if it already exists
unlink "$dist_dir.tar.gz" || die;
}
$self->make_tarball($dist_dir);
$self->delete_filetree($dist_dir);
}
sub ACTION_manpo {
my $self = shift;
$self->depends_on('code');
$self->make_files_writable("po/pod");
my $cmd = "PERL5LIB=lib perl po4a "; # Use this version of po4a
$cmd .= "--previous ";
$cmd .= "--no-translations ";
$cmd .= "--msgid-bugs-address po4a-devel\@lists.alioth.debian.org ";
$cmd .= "--package-name po4a ";
$cmd .= "--package-version ".$self->dist_version()." ";
$cmd .= $ENV{PO4AFLAGS}." " if defined($ENV{PO4AFLAGS});
$cmd .= "po/pod.cfg";
system($cmd)
and die;
}
sub ACTION_man {
my $self = shift;
$self->depends_on('manpo');
use Pod::Man;
use Encode;
# Translate binaries manpages
my %options;
$options{utf8} = 1;
my $parser = Pod::Man->new (%options);
my $manpath = File::Spec->catdir( 'blib', 'man' );
File::Path::rmtree( $manpath, 0, 1);
my $cmd = "PERL5LIB=lib perl po4a "; # Use this version of po4a
$cmd .= $ENV{PO4AFLAGS}." " if defined($ENV{PO4AFLAGS});
$cmd .= "--previous po/pod.cfg";
system($cmd) and die;
my $man1path = File::Spec->catdir( $manpath, 'man1' );
my $man3path = File::Spec->catdir( $manpath, 'man3' );
my $man5path = File::Spec->catdir( $manpath, 'man5' );
my $man7path = File::Spec->catdir( $manpath, 'man7' );
File::Path::mkpath( $man1path, 0, oct(755) ) or die;
File::Path::mkpath( $man3path, 0, oct(755) ) or die;
File::Path::mkpath( $man5path, 0, oct(755) ) or die;
File::Path::mkpath( $man7path, 0, oct(755) ) or die;
copy ( File::Spec->catdir("doc", "po4a-build.conf.5.pod"), $man5path) or die;
copy ( File::Spec->catdir("doc", "po4a.7.pod"), $man7path) or die;
copy ( File::Spec->catdir("doc", "po4a-runtime.7.pod"), $man7path) or die;
foreach $file (perl_scripts()) {
$file =~ m,([^/]*)$,;
copy($file, File::Spec->catdir($man1path, "$1.1p.pod")) or die;
}
foreach $file (@{$self->rscan_dir('lib',qr{\.pm$})}) {
$file =~ m,([^/]*).pm$,;
copy($file, File::Spec->catdir($man3path, "Locale::Po4a::$1.3pm.pod")) or die;
}
$self->delete_filetree( File::Spec->catdir("blib", "bindoc") );
$self->delete_filetree( File::Spec->catdir("blib", "libdoc") );
foreach $file (@{$self->rscan_dir($manpath, qr{\.pod$})}) {
next if $file =~ m/^man7/;
my $out = $file;
$out =~ s/\.pod$//;
$parser->{name} = $out;
$parser->{name} =~ s/^.*\///;
$parser->{name} =~ s/^(.*).(1p|3pm|5|7)/$1/;
$parser->{section} = $2;
if ($parser->{section} ne "3pm") {
$parser->{name} = uc $parser->{name};
}
my $lang = $out;
$lang =~ s/^blib\/man\/([^\/]*)\/.*$/$1/;
if ($lang =~ m/man\d/) {
$parser->{release} = $parser->{center} = "Po4a Tools";
} else {
my $command;
$command = "msggrep -K -E -e \"Po4a Tools\" po/pod/$lang.po |";
$command .= "msgconv -t UTF-8 | ";
$command .= "msgexec /bin/sh -c '[ -n \"\$MSGEXEC_MSGID\" ] ";
$command .= "&& cat || cat > /dev/null'";
my $title = `$command 2> /dev/null`;
$title = "Po4a Tools" unless length $title;
$title = Encode::decode_utf8($title);
$parser->{release} = $parser->{center} = $title;
}
$parser->parse_from_file ($file, $out);
system("gzip -9 -f $out") and die;
unlink "$file" || die;
}
# Install the manpages written in XML DocBook
foreach $file (qw(po4a-build.xml po4aman-display-po.xml po4apod-display-po.xml)) {
copy ( File::Spec->catdir("share", "doc", $file), $man1path) or die;
}
foreach $file (@{$self->rscan_dir($manpath, qr{\.xml$})}) {
if ($file =~ m,(.*/man(.))/([^/]*)\.xml$,) {
my ($outdir, $section, $outfile) = ($1, $2, $3);
system("xsltproc -o $outdir/$outfile.$section --nonet http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl $file") and die;
system ("gzip -9 -f $outdir/$outfile.$section") and die;
}
unlink "$file" || die;
}
}
sub ACTION_postats {
my $self = shift;
$self->depends_on('binpo');
$self->postats( File::Spec->catdir("po", "bin"));
$self->postats( File::Spec->catdir("po", "pod"));
$self->postats( File::Spec->catdir("po", "www")) if -d File::Spec->catdir("po", "www");
}
sub postats {
my ($self,$dir) = (shift,shift);
my $potfiles = $self->rscan_dir($dir,qr{\.pot$});
die "No POT file found in $dir" unless scalar $potfiles;
my $potfile = pop @$potfiles;
my $potsize = stat($potfile)->size;
print "$dir (pot: $potsize)\n";
my @files = @{$self->rscan_dir($dir,qr{\.po$})};
foreach (sort @files) {
$file = $_;
my $lang = fileparse($file, qw{.po});
my $stat = `msgfmt -o /dev/null -c -v --statistics $file 2>&1`;
print " $lang: $stat";
}
}
1;