-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_trans.pl
executable file
·1221 lines (1037 loc) · 31.3 KB
/
check_trans.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# This is a little utility designed to keep track of translations
# in the Debian web site Subversion repository.
#
## For information about translation revisions please see
## https://www.debian.org/devel/website/uptodate
#
# Copyright (C) 2008 Bas Zoetekouw <[email protected]>
# Based on on code from:
# Copyright (C) 1998 Paolo Molaro <[email protected]>
# Copyright (C) 1999-2003 Peter Karlsson <[email protected]>
# Copyright (C) 2000,2001 Martin Quinson <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
## This program 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.
#
#
# Invocation:
# check_trans.pl [-cdlvqQ] [-C dir] [-p pattern] [-s subtree]
# [-m { -n <num> | -M <email> } [-g] ]
# [-t outputtype]
# [language]
#
# It needs to be run from the top level webwml directory.
# If you don't specify a language on the command line, the language name
# will be loaded from a file called language.conf, if such a file exists.
#
# For example:
# $ check_trans.pl -v italian
# You may also check only some subtrees as in:
# $ check_trans.pl -s devel italian
#
# Options:
# -Q be really quiet (only show errors/warnings on stderr)
# -q just don't whine about missing files
# -v show the status of all files (verbose)
# -V output what we're doing (very verbose)
# -d output diffs
# -l output log messages
# -T output translated diffs
# -p <pattern> include only files matching <pattern>,
# default is *.src|*.wml
# -s <subtree> check only that subtree
# -a output age of translation (if older than 2 months)
# -c disable use of color in the output
#
# Options useful when sending mails:
# -m sends mails to translation maintainers as specified in
# in database in $lang/international/$lang/translator.db.pl
# PLEASE CAREFULLY READ THE BELOW TEXT ABOUT MAKING MAILS!
# -n <1|2|3> send mails of priority upper or equal to 1 (monthly),
# 2 (weekly) or 3 (daily), as specified in the translator
# database
# -M <email> instead of using the translator database, send all email
# the specified address. The translator database is not
# used.
# -g instead of sending mails, dump them to the console
# (no mails will be sent)
#
# GENERATING EMAILS
# If you want to, this script send mails to the maintainer of the mails.
# BEWARE, SOME PEOPLE DO NOT LIKE TO RECEIVE AUTOMATIC MAILS!
#
# PREREQUISITES:
# You will need one database:
# - one in which to get info about translators and the frequency at
# which they want to get mails. It must be named
# webwml/$lang/international/$lang/translator.db.pl
# Please refer to the French one for more info.
#
# USAGE:
# If you give the "-g" option, all mails are written to the console. No
# mails are sent out at all. This is useful for debugging.
# If you specify an email addres with the "-M" options, all mails are sent
# to the specified addressee. No mails are sent to any other addresses. It
# is useful if you want to run it for yourself.
# Without either of these options, real mails will be sent to real
# addresses.
# MAKE SURE THE ADDRESSEES REALLY WANT TO GET THESE MAILS!
use Getopt::Std;
use File::Basename;
use File::Spec::Functions;
use Term::ANSIColor;
use Encode;
#use Data::Dumper;
use FindBin;
FindBin::again();
# These modules reside under webwml/Perl
use lib "$FindBin::Bin/Perl";
use Local::VCS ':all';
use Local::Util qw/ uniq read_file /;
use Local::WmlDiffTrans;
use Webwml::TransCheck;
use Webwml::TransIgnore;
use strict;
use warnings;
# global variable to record verbosity
my $VERBOSE = 0;
# default files to process
my $DEFAULT_PATTERN = '(?:\.wml|\.src)$';
# status codes
use constant {
ST_MISSING => 1,
ST_NEEDSUPDATE => 3,
ST_UPTODATE => 4,
ST_NOTATRANSL => 5,
ST_BROKEN => 6,
ST_OBSOLETE => 7,
ST_UNDEFINED => 8,
};
# how to colour each different status
my %COLOURS = (
main::ST_MISSING => 'magenta',
main::ST_NEEDSUPDATE => 'blue',
main::ST_UPTODATE => 'green',
main::ST_NOTATRANSL => 'yellow',
main::ST_BROKEN => 'red',
main::ST_OBSOLETE => 'red',
main::ST_UNDEFINED => 'red',
'warn' => 'bold red',
);
# default values for sending mails
my $MY_EMAIL = q{Debian WWW translation watch <[email protected]>};
my $DEFAULT_SUBJECT = q{Debian web page translations needing updates};
(my $DEFAULT_BODY = <<"EOF") =~ s/^\t//gm;
Hi!
This is an automatic message providing an overview of Debian webpages
of which the translation is outdated.
Kind regards,
Your automatic daemon.
EOF
# these is called in "main" so need to be declared here
sub switch_var(\$\$);
sub verbose;
#=================================================
#== "main"
#==
{
# install a signal handler to catch Ctrl-C
$SIG{'INT'} = \&handle_INT;
# parse the command line
my ($language,$file_pattern,%OPT) = parse_cmdargs();
# read the tranlator db if we need it (-n was specified)
my %translators = $OPT{n} ? read_translators( $language ) : ();
# this hash will be used to store the emails we want to send out
my %emails_to_send;
# the subdirs where the english and translated files are located
my $english_path = 'english';
my $language_path = $language;
# -s allows the user to restrict processing to a subtree
my $subdir = $OPT{'s'} || undef;
# Global .transignore
my $transignore = Webwml::TransIgnore->new( vcs_get_topdir );
# first get a list with revision information from all files in english...
my %english_revs = vcs_path_info( $english_path,
'recursive' => 1,
'match_pat' => $file_pattern,
'skip_pat' => '^template/'
);
# ... and in the translation
my %translation_revs = vcs_path_info( $language_path,
'recursive' => 1,
'match_pat' => $file_pattern,
'skip_pat' => '^template/'
);
# construct a list with all files that either occur in english or
# in the translation
my @files = uniq ( keys %english_revs, keys %translation_revs );
# now check each of the files
foreach my $file (sort @files)
{
# ignore this file?
next if $transignore->is_global( $file );
next if $subdir and not $file =~ m{^$subdir};
# note: $language is the name of the current language we're
# processing, whereas $transl is the name of the language which the
# current file is translated into (which might be english!)
my $orig = 'english';
my $transl = $language;
my $file_orig = catfile( $orig, $file );
my $file_transl = catfile( $transl, $file );
my $revinfo_orig = $english_revs{$file};
my $revinfo_transl = $translation_revs{$file};
# TODO: put this in a separate function
# first we check if the translated file has an origin other than
# english
if ( -e $file_transl )
{
my $transcheck = Webwml::TransCheck->new( $file_transl );
my $original_lang = $transcheck->original();
if ( $original_lang and $original_lang ne 'english' )
{
die( "Unknown original language `$original_lang' "
."for `$file_transl'\n" ) unless -d $original_lang;
verbose "`$file_transl' is translated from $original_lang";
# now, we use the correct (non-english) original file
$file_orig = catfile( $original_lang, $file );
# and find the correct revision info for this file
$revinfo_orig = { vcs_file_info( $file_orig ) };
}
}
# TODO: put this in a separate function
# secondly, we check if perhaps the original file is a translation
# (such as in the case of english/international/Swedish/index.wml)
if ( -e $file_transl and -e $file_orig )
{
my $transcheck = Webwml::TransCheck->new( $file_orig );
my $original_lang = $transcheck->original();
my $rev = $transcheck->revision();
if ( $rev )
{
## This check is too strict: some translators like to translate
##from other translations rather than from the original english
##(see e.g., danish/international/Norwegian.wml)
#if ( not $original_lang )
#{
# # TODO: ideally, this would also be mailed out to the
# # translation team
# warn "`$file_orig' has a revision header but no origin language\n";
# next;
#}
if ( $original_lang and $original_lang eq $language )
{
verbose "`$file_orig' is a translation from $language";
# switch $orig and $transl
switch_var( $orig, $transl );
switch_var( $file_orig, $file_transl );
switch_var( $revinfo_orig, $revinfo_transl );
}
}
}
# skip original files (i.e., most of english)
next if ( $file_orig eq $file_transl );
# determine the status of the file
my ($status,$str,$rev_transl,$maintainer,$maxdelta) = check_file(
$file,
$orig, $transl,
$revinfo_orig, $revinfo_transl,
);
######################################################################
## Everything below is just output logic
######################################################################
# print info
if ( ( $OPT{v} )
or ( $status == ST_MISSING and not $OPT{q} )
or ( $status != ST_MISSING and $status != ST_UPTODATE
and $status != ST_NOTATRANSL )
)
{
print colored( "$str\n", $COLOURS{$status} );
}
# check age of the translation
if ( $OPT{a} and $status == ST_NEEDSUPDATE )
{
my $age = int get_revision_age( $revinfo_transl );
# only warn if the translation is older than 2 weeks
if ( $age > 14 )
{
print colored( "$file is outdated by $age days\n",
$COLOURS{warn} );
}
}
# print log if requested and an update is needed
if ( $OPT{'l'} and $status == ST_NEEDSUPDATE )
{
my $log = get_log(
$file_orig,
$rev_transl,
$revinfo_orig->{'cmt_rev'},
);
print $log;
}
# print diff if requested and an update is needed
if ( $OPT{'d'} and $status == ST_NEEDSUPDATE )
{
my $diff = get_diff(
$file_orig,
$rev_transl,
$revinfo_orig->{'cmt_rev'},
);
print $diff;
}
# print text diff, if requested and an update is needed
if ( $OPT{'T'} and $status == ST_NEEDSUPDATE )
{
my $diff = get_diff_txt(
$file_orig,
$rev_transl,
$revinfo_orig->{'cmt_rev'},
$file_transl
);
print $diff;
}
# prepare a mail to be sent
if ( $OPT{'m'} and $status != ST_UPTODATE )
{
# -M was specified, so all mails to there
if ( $OPT{'M'} )
{
$maintainer = 'default';
# don't send mail about untranslated files if -q was specified
$maintainer = 'none'
if $status == ST_MISSING and $OPT{'q'}
}
else # addresses from the database is used
{
# handle special case maintainer fields
$maintainer = 'unmaintained'
unless $maintainer and exists $translators{$maintainer};
$maintainer = 'untranslated'
if $status == ST_MISSING;
}
verbose "Found maintainer $maintainer for this file";
# mail to send to the maintainer
push @{ $emails_to_send{$maintainer} }, {
'file' => $file,
'status' => $status,
'info' => $str,
'last_trans_rev' => $rev_transl,
};
# additionally, if -n was specified, also see if we need to
# send a mail to maxdelta
if ( $OPT{'n'} and $status != ST_MISSING and -e $file_orig )
{
$maxdelta ||= $translators{maxdelta}{maxdelta} || 5;
my $delta;
$delta = vcs_count_changes( $file_orig, $rev_transl, 'HEAD' );
if ( $delta >= $maxdelta )
{
push @{ $emails_to_send{'maxdelta'} }, {
'file' => $file,
'status' => $status,
'info' => $str,
'delta' => $delta,
'last_trans_rev' => $rev_transl,
}
}
}
}
}
send_email( \%emails_to_send, \%translators, $language,
$OPT{'n'}, $OPT{'M'}, $OPT{'g'} );
exit 0;
}
die("Never reached");
#=================================================
#== swich two variables around
#==
sub switch_var(\$\$)
{
my $a = shift;
my $b = shift;
my $c = $$a;
$$a = $$b;
$$b = $c;
}
#=================================================
#== output verbose messages
#==
sub verbose
{
return unless $VERBOSE;
print @_, "\n";
}
#=================================================
#== handles INT signal
#==
sub handle_INT
{
# reset terminal color
print color('reset');
die( "Interrupted by user" );
}
#=================================================
#== send out the emails
#==
sub send_email
{
my $emails = shift or die("No emails specified");
my $translators = shift or die("No translators specified");
my $lang = shift or die("No language specified");
my $priority = shift;
my $default_rec = shift;
my $debug = shift;
foreach my $name (sort keys %$emails)
{
# special case
next if $name eq 'none';
verbose("Preparing email for $name");
my $recipient;
my $subject;
my $mailbody;
# First handle the case in whcih all mail goes to the -M address
if ( $default_rec )
{
# address was already validated while parsing the command line
$recipient = $default_rec;
$subject = $DEFAULT_SUBJECT;
$mailbody = $DEFAULT_BODY;
}
else # handle the case in whcih addresses are fetch from the db
{
# skip unconfigured users
if ( not exists $translators->{$name}
or not $translators->{$name}{'email'} )
{
verbose( "Woops! Can't find info for user `$name'\n" );
next;
}
# check the user's email addres
if ( not Email::Address->parse( $translators->{$name}{'email'} ) )
{
printf STDERR "Can't parse email address `%s' for %s!\n",
$translators->{$name}{'email'}, $name;
next;
}
# skip if the user doesn't want a summary at all
if ( $translators->{$name}{'summary'} < $priority )
{
verbose( "Not sending message to $name (prio "
. $translators->{$name}{'summary'} . " < $priority)" );
next;
}
$recipient = $translators->{$name}{'email'};
$subject = $translators->{'default'}{'mailsubject'};
# read body and interpret perl that's embedded there
$mailbody = read_file_enc( $translators->{'default'}{'mailbody'} )
or die("Can't read $translators->{'default'}{'mailbody'}");
{
# a bit hackish, but I want to keep the curent format of
# the mail body files intact, for now
# so we need to use the same old variable names as the original
# script used
my %translators = %{$translators};
$mailbody =~ s{#(.*?)#}{eval $1}mge;
}
}
my $msg = MIME::Lite->new(
'From' => $MY_EMAIL,
'To' => $recipient,
'Subject' => $subject,
'Type' => 'multipart/mixed',
);
# and attach the body to the mail
my $part = MIME::Lite->new(
'Type' => 'text/plain',
'Data' => encode('utf-8',$mailbody),
);
$part->attr( 'content-type.charset' => 'utf-8' );
$msg->attach( $part );
# attach part about NeedToUpdate files
my $text = '';
foreach my $file ( @{ $emails->{$name} } )
{
next unless $file->{'status'} == ST_NEEDSUPDATE;
$text .= $file->{'info'};
if ( exists $file->{'delta'} )
{
$text .= sprintf( " [out of date by %d revisions]",
$file->{'delta'} );
}
$text .= "\n";
}
$msg->attach(
'Type' => 'TEXT',
'Filename' => 'NeedToUpdate summary',
'Data' => $text,
)
if $text;
# attach part about Missing files
if ( not $default_rec )
{
$text = '';
foreach my $file ( @{ $emails->{$name} } )
{
next unless $file->{'status'} == ST_MISSING;
$text .= sprintf( "%s\n", $file->{'info'} );
}
$msg->attach(
'Type' => 'TEXT',
'Filename' => 'Missing summary',
'Data' => $text,
'Encoding' => 'quoted-printable',
)
if $text;
}
# add diffs, if requested
if ( $default_rec or $priority <= $translators->{$name}{'diff'} )
{
foreach my $file ( @{ $emails->{$name} } )
{
# diffs really only make sense if there is an existing
# translation
next unless $file->{'status'} == ST_NEEDSUPDATE;
my $filename = catfile( 'english', $file->{'file'} );
my $rev = $file->{'last_trans_rev'};
my $diff = get_diff( $filename, $rev, 'HEAD' );
$msg->attach(
'Type' => 'TEXT',
'Filename' => "$filename.diff",
'Data' => $diff,
'Encoding' => 'quoted-printable',
);
}
}
else
{
verbose( "Not attaching diffs (prio "
. $translators->{$name}{'diff'} . " < $priority)" );
}
# add tdiffs, if requested
if ( not $default_rec and $priority <= $translators->{$name}{'tdiff'} )
{
foreach my $file ( @{ $emails->{$name} } )
{
# diffs really only make sense if there is an existing
# translation
next unless $file->{'status'} == ST_NEEDSUPDATE;
my $filename = catfile( 'english', $file->{'file'} );
my $filename2 = catfile( $lang, $file->{'file'} );
my $rev = $file->{'last_trans_rev'};
my $tdiff = get_diff_txt( $filename, $rev, 'HEAD',
$filename2 );
$msg->attach(
'Type' => 'TEXT',
'Filename' => "$filename.tdiff",
'Data' => $tdiff,
'Encoding' => 'quoted-printable',
);
}
}
else
{
verbose( "Not attaching tdiffs (prio "
. $translators->{$name}{'tdiff'} . " < $priority)" )
unless $default_rec;
}
# add logs, if requested
if ( $default_rec or $priority <= $translators->{$name}{'logs'} )
{
foreach my $file ( @{ $emails->{$name} } )
{
# logs really only make sense if there is an existing
# translation
next unless $file->{'status'} == ST_NEEDSUPDATE;
my $filename = catfile( 'english', $file->{'file'} );
my $rev = $file->{'last_trans_rev'};
my $log = get_log( $filename, $rev, 'HEAD' );
my $part = MIME::Lite->new(
'Type' => 'TEXT',
'Filename' => "$filename.log",
'Data' => $log,
'Encoding' => 'quoted-printable',
);
$part->attr( 'content-type.charset' => 'utf-8' );
$msg->attach( $part );
}
}
else
{
verbose( "Not attaching logs (prio "
. $translators->{$name}{'logs'} . " < $priority)" );
}
# add file, if requested
if ( not $default_rec and $priority <= $translators->{$name}{'file'} )
{
foreach my $file ( @{ $emails->{$name} } )
{
my $filename = catfile( $lang, $file->{'file'} );
my $part = MIME::Lite->new(
'Type' => 'text/wml',
'Filename' => $filename,
'Path' => $filename,
'Encoding' => 'quoted-printable',
);
$part->attr( 'content-type.charset' => get_file_charset($filename) );
$msg->attach( $part );
}
}
else
{
verbose( "Not attaching files (prio "
. $translators->{$name}{'file'} . " < $priority)" )
unless $default_rec;
}
# check if we really want to send the mail
if ( $debug )
{
print color('bold yellow');
print '*'x72, "\n";
printf "Would send email to %s (but -g was specified):\n",
$recipient;
print '-'x72, "\n";
print color('reset');
# make sure perl doesn't do any annoying charset conversions
binmode( \*STDOUT, ':bytes' );
print $msg->as_string;
print color('bold yellow');
print '*'x72, "\n";
print color('reset');
}
else
{
verbose "Sending email to $recipient";
$msg->send or warn("Couldn't send message to $name");
}
}
}
#=================================================
#== return the age of the revision (in days)
#==
sub get_revision_age
{
my $rev_info = shift;
die("No revision info specified") unless ref $rev_info eq 'HASH';
my $rev_timestamp = $rev_info->{'cmt_date'};
my $age = time - $rev_timestamp;
warn( "Timestamp is in the future!" ) if $age < 0;
# return age in days
return $age / ( 60*60*24 );
}
#=================================================
#== get a log
#==
sub get_log
{
my $file = shift or die("No file specified for diff");
my $rev1 = shift;
my $rev2 = shift;
die("NO such file `$file'") unless -e $file;
my @log = vcs_get_log( $file, $rev1, $rev2 );
# remove the first item of the log, because we only want
# to see when changed in the (left-open) range (rev1,rev2]
shift @log;
# format it nicely
my $str = '-' x 78 . "\n";
foreach my $l (@log)
{
chomp $l->{'message'};
$str .= sprintf( "%s | %s | %s\n",
$l->{'rev'}, $l->{'author'}, scalar localtime $l->{'date'} );
$str .= "\n";
$str .= $l->{'message'} . "\n";
$str .= "\n";
$str .= '-' x 78 . "\n";
}
return $str;
}
#=================================================
#== get a diff
#==
sub get_diff
{
my $file = shift or die("No file specified for diff");
my $rev1 = shift;
my $rev2 = shift;
die("NO such file `$file'") unless -e $file;
my %diffs = vcs_get_diff( $file, $rev1, $rev2 );
# just glue all diffs together and return it as a big string
my $difftxt = join( '', values %diffs );
return $difftxt;
}
#=================================================
#== get a diff while trying to match html tags
#==
sub get_diff_txt
{
my $english_file = shift or die("No file specified");
my $rev1 = shift or die("No revision specified");
my $rev2 = shift or die("No revision specified");
my $transl_file = shift or die("No transl file specified");
die("No such file $english_file") unless -e $english_file;
die("No such file $transl_file") unless -e $transl_file;
# Get old revision file
my @english_txt = split( "\n", vcs_get_file( $english_file, $rev1 ) );
# Get translation file
my $transl_txt = read_file( $transl_file )
or die("Couln't read `$transl_file': $!");
my @transl_txt = split( "\n", $transl_txt );
# Get diff lines
my @diff_txt = split( "\n", get_diff( $english_file, $rev1, $rev2 ) );
# do the matching
my $txt = Local::WmlDiffTrans::find_trans_parts(
\@english_txt,
\@transl_txt,
\@diff_txt
);
return $txt;
}
#=================================================
#== show help from the top of this file
#==
sub show_help
{
# read the help from the comments above and display it
open( my $me, '<', $0 ) or die "Unable to display help: $!\n";
while ( my $line = <$me> )
{
last if $line =~ m{^use};
print "\n" if $line =~ m{^#$};
next unless $line =~ m{^# };
$line =~ s{^# ?}{};
print $line;
}
close( $me );
}
#=================================================
#== parse command line options and read defaults
#==
sub parse_cmdargs
{
my %OPT;
$OPT{s} = '';
# parse options
if ( not getopts( 'acdghlmM:n:p:Qqs:TvV', \%OPT ) )
{
show_help();
exit -1;
}
# show help
if ( $OPT{h} )
{
show_help();
exit 0;
}
# handle verbosity setting
if ( ( $OPT{'v'} or $OPT{'V'} ) and ( $OPT{'q'} or $OPT{'Q'} ) )
{
die "you can't have both verbose and quiet, doh!\n";
}
$VERBOSE = 1 if $OPT{'V'};
$OPT{'v'} = 1 if $OPT{'V'};
# handle really quiet setting
if ( $OPT{'Q'} )
{
# redirect stdout to /dev/null
close( STDOUT );
open( STDOUT, '>', '/dev/null' )
or die( "Can't redirect STDOUT to /dev/null: $!" );
}
# handle -c (disable color) setting
if ( $OPT{'c'} )
{
# nice feature of Term::ANSIColor
$ENV{'ANSI_COLORS_DISABLED'} = '1';
}
else
{
# we need flushed STDOUT putput, because otherwise the colours wills
# blend into STDERR
$| = 1;
}
# handle -s (subtree check) setting
if ( $OPT{s})
{
verbose "Checking subtree $OPT{s} only\n";
}
# check validity of mail options
# if -m is specified, either -n or -M must also be given
# furthermore, the argument to -n must be 1, 2, or 3, and
# the argument to -M must be a valid email address
if ( $OPT{'m'} )
{
# load additional module we need for mail
eval {
require MIME::Lite;
import MIME::Lite;
};
die "The module MIME::Lite could not be loaded.\n"
."Please install libmime-lite-perl\n" if $@;
eval {
require Email::Address;
import Email::Address;
};
die "The module Email::Address could not be loaded.\n"
."Please install libemail-address-perl\n" if $@;
# now check the options
if ( $OPT{'n'} and $OPT{'M'} )
{
die "You can't specify both -n and -M\n";
}
elsif ( $OPT{'n'} )
{
die "Invalid priority `$OPT{n}'. "
."Please set -n value to 1, 2 or 3.\n"
unless $OPT{'n'} =~ m{^[123]$}
}
elsif ( $OPT{'M'} )
{
die "Invalid email address `$OPT{M}'\n"
unless Email::Address->parse( $OPT{M} );
}
else
{
die "You specified -m (send mails), but you didn't specify "
."either -n or -M, so I don't knwo where to send my mails\n";
}
}
if ( $OPT{'g'} and not $OPT{'m'} )
{
die "Option -g (debuG mail) without -m (use mail) "
."really doesn't make much sense\n";
}
# include only files matching $filename
my $file_pattern = $OPT{'p'} || $DEFAULT_PATTERN;
my $translation = shift @ARGV || '';
# language configuration
if ( not $translation )
{
if ( exists $ENV{DWWW_LANG} )
{
$translation = $ENV{DWWW_LANG};
}
elsif ( -e "language.conf" )
{
open( my $conf, '<', 'language.conf' )
or die("Can't read language.conf: $!\n");
while (<$conf>)
{
next if /^#/;
chomp;
$translation = $_;
last;
}
close $conf;
}
}
# Remove slash from the end
$translation =~ s{/$}{};
if ( $translation eq '' )
{
die "Language not defined in DWWW_LANG, language.conf, "
."or on command line\n";
}
return ($translation,$file_pattern,%OPT);
}
#=================================================
#== read the translators from translator.db
#==
sub read_translators
{
my $lang = shift or die("Internal error: no language specified");
my %translators;