smart_change.pl 3.95 KB
Newer Older
1 2 3 4 5
#!/usr/bin/perl -w

# This script perform changes in WML source files and bump version
# number when translated files are up to date.

Rhonda D'Vine's avatar
Rhonda D'Vine committed
6 7 8 9
# Known Issues:
# when there is no change to the origfile the translation="" revision is
# updated for current translations nevertheless

10 11 12 13 14 15 16 17 18
use strict;
use Getopt::Long;

#    These modules reside under webwml/Perl
use lib ($0 =~ m|(.*)/|, $1 or ".") ."/Perl";
use Local::Cvsinfo;
use Webwml::TransCheck;
use Webwml::Langs;

Denis Barbier's avatar
Denis Barbier committed
19
our ($opt_h, $opt_v, $opt_n, $opt_p, @opt_l, @opt_s);
20 21 22

sub usage {
        print <<'EOT';
23
Usage: smart_change.pl [options] origfile [origfile ...]
24 25 26
Options:
  -h, --help         display this message
  -v, --verbose      run verbosely
Denis Barbier's avatar
Denis Barbier committed
27 28
  -n, --no-bump      do not bump translation-check headers
  -p, --previous     get previous CVS revision
29 30 31 32 33 34 35 36 37 38 39
  -l, --lang=STRING  process this language (may be used more than once)
  -s, --substitute=REGEXP
                     Perl regexp applied to source files
                     (may be used more than once)
EOT
        exit(0);
}

if (not Getopt::Long::GetOptions(qw(
                h|help
                v|verbose
Denis Barbier's avatar
Denis Barbier committed
40 41
                n|no-bump
                p|previous
42 43 44 45 46 47 48 49
                l|lang=s@
                s|substitute=s@
))) {
        warn "Try `$0 --help' for more information.\n";
        exit(1);
}

$opt_h && usage;
50
die "Invalid number of arguments\n" unless @ARGV;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

sub verbose {
        print STDERR $_[0] . "\n" if $opt_v;
}

#   We call constructor without argument.  It means there must be a
#   CVS/Repository file or program will abort.
if (not @opt_l) {
        my $l = Webwml::Langs->new();
        @opt_l = $l->names();
}

my $eval_opt_s = '1';
foreach (@opt_s) {
        $eval_opt_s .= "; $_";
}
verbose("-s flags: $eval_opt_s");
68

69 70 71
my $substitute = eval "sub { \$_ = shift; $eval_opt_s; die \$@ if \$@; return \$_}";
die "Invalid -s option" if $@;

72
foreach my $argfile (@ARGV) {
Peter Karlsson's avatar
Peter Karlsson committed
73
        $argfile =~ m+^(english.*)/(.*\.wml)+ or die "unknown path '$argfile'";
74 75 76 77 78 79 80 81 82
        my ($path, $file) = ($1, $2);

        verbose("File: $argfile");
        my $cvs = Local::Cvsinfo->new();
        $cvs->options(matchfile => [ $file ]);
        $cvs->readinfo($path);
        my $origrev = $cvs->revision($argfile) || "1.0";
        if ($opt_p) {
                $origrev =~ s/(\d+)$/($1 - 1)/e;
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
        verbose("Original revision: $origrev");

        my $nextrev = $origrev;
        $nextrev =~ s/(\d+)$/(1+$1)/e;
        verbose("Next revision: $nextrev");

        foreach my $lang (@opt_l) {
                my $transfile = $argfile;
                $transfile =~ s/^english/$lang/ || next;
                next unless -f $transfile;
                verbose("Now checking $transfile");

                # Parse the translated file
                my $transcheck = Webwml::TransCheck->new($transfile);
                next unless $transcheck->revision() || $lang eq 'english';
                my $langrev = $transcheck->revision();

                my $origtext = '';
                my $transtext = '';
                open (TRANS, "< $transfile");
                while (<TRANS>) {
                        $origtext .= $_;
                        if (m/^#use wml::debian::translation-check/ && !$opt_n &&
                            ($langrev eq $origrev || $langrev eq $nextrev)) {
                                #   Also check for $nextrev in case this script
                                #   is run several times
                                s/(translation="?)($origrev|$nextrev)("?)/$1$nextrev$3/;
                                verbose("Bump version number to $nextrev");
                        }
                        $transtext .= $_;
                }
115
                close (TRANS);
116 117 118 119 120 121 122
                $transtext = &$substitute($transtext);
                if ($origtext ne $transtext) {
                        verbose("Writing $transfile");
                        open (TRANS, "> $transfile");
                        print TRANS $transtext;
                        close (TRANS);
                }
123 124
        }
}