langpacks.pl 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/perl -w

# Copyright (C) 2010 Catalyst IT Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use Data::Dumper;
use FindBin;
20
use File::Path qw(mkpath rmtree);
21
use LWP::UserAgent;
22

Son Nguyen's avatar
Son Nguyen committed
23
foreach my $c (qw(DATA DOCROOT SCRIPTS)) {
24 25 26 27 28
    exists $ENV{$c} or die ("\$ENV{$c} undefined");
}

my $DATA      = $ENV{DATA};
my $DOCROOT   = $ENV{DOCROOT};
29
my $SCRIPTS   = $ENV{SCRIPTS};
30

31
my $CLEANCMD  = "/usr/bin/php $FindBin::Bin/clean-php.php";
32 33 34 35 36
my $SYNTAXCMD = "/usr/bin/php -l";
my $UTF8CMD   = "/usr/bin/perl $FindBin::Bin/check-utf8.pl";
my $POCMD     = "/usr/bin/perl $FindBin::Bin/po-php.pl";

my $GITDIR    = "${DATA}/git";
37
my $BZRDIR    = "${DATA}/bzr";
38 39 40
my $DIRTY     = "${DATA}/old";
my $CLEAN     = "${DATA}/new";
my $TARBALLS  = "${DATA}/tarballs";
41 42
my $MAHARA    = "${DATA}/mahara";

43
my $MAHARAREMOTE = 'https://git.mahara.org/mahara/mahara.git';
44
my $REPOLIST     = 'https://git.mahara.org/scripts/mahara-scripts/blob/master/mahara-langpacks/language-repos.txt';
45

46 47 48 49
mkpath $GITDIR;
mkpath $DIRTY;
mkpath $CLEAN;
mkpath $TARBALLS;
50 51 52

print STDERR "Checking langpacks for updates: " . `date \"+%Y-%m-%d %H:%M:%S\"`;

53
# A language repo list can be put in the $DATA dir for testing.  If there's not one
54
# there, try to get an up-to-date one out of the mahara-scripts repository
55 56
# (allows updates to the repo list without having to redeploy the package).
my $repolist;
57 58 59
if ( -f "$SCRIPTS/language-repos.txt" ) {
    print STDERR "Using repository list in $SCRIPTS/language-repos.txt\n";
    open $repofh, '<', "$SCRIPTS/language-repos.txt" or die $!;
60 61 62 63
    local $/ = undef;
    $repolist = <$repofh>;
}
else {
64
    print STDERR "Retrieving repository list from $REPOLIST\n";
65 66 67 68

    my $ua = LWP::UserAgent->new;
    $ua->timeout(10);
    $ua->env_proxy;
69
    my $response = $ua->get($REPOLIST);
70
    $repolist = $response->is_success ? $response->content : undef;
71 72 73 74 75

    if ( ! defined $repolist ) {
        print STDERR "Failed to get language list, exiting.\n";
        exit;
    }
76 77 78 79 80
}

my %langs = ();
if ( defined $repolist ) {
    foreach ( split "\n", $repolist ) {
81
        if ( m/^([a-zA-Z_]{2,5})\s+(\S+)\s*$/ ) {
82 83 84 85 86 87 88
            $langs{$1} = { repo => $2 };
        }
    }
}

my @langkeys = sort keys %langs;
if ( scalar @langkeys < 1 ) {
89
    @langkeys = qw(ar ca cs da de el en_GB en_us es eu fi fr he it ja ko mi nl nb ru sl zh_TW);
90 91 92
}

print STDERR "Languages: " . join(' ', @langkeys) . "\n";
93 94 95 96 97 98 99 100 101 102

my $last;
my $savefile = "$TARBALLS/mahara-langpacks.last";
if ( -f $savefile ) {
    eval(`cat $savefile`);
}
else {
    $last = {};
}

103

104 105 106 107 108 109 110
# Mahara checkout, used for reading the en.utf8 langpack
# during php langpack sanitisation.
! -d "$MAHARA" && system "git clone --quiet $MAHARAREMOTE $MAHARA";
chdir $MAHARA;
system "git fetch --quiet origin";


111
# For launchpad, all languages are in a single branch, so update the lot
112
system "bzr launchpad-login dev-mahara";
113
! -d $BZRDIR && system "bzr init-repo $BZRDIR";
114
my @branches = qw(1.7_STABLE 1.8_STABLE 1.9_STABLE 1.10_STABLE 15.04_STABLE 15.10_STABLE master);
115 116 117

foreach my $branch (@branches) {
    if ( ! -d "$BZRDIR/$branch" ) {
118
        system "bzr branch lp:~mahara-lang/mahara-lang/$branch-export $BZRDIR/$branch";
119 120
    }
    else {
121 122
        chdir "$BZRDIR/$branch";
        system "bzr pull";
123 124 125
    }
}

126
foreach my $lang (@langkeys) {
127 128

    if ( ! defined $last->{$lang} ) {
129
        $last->{$lang} = { repo => "https://git.mahara.org/lang-old/$lang.git" };
130 131
    }

132 133 134 135
    if ( defined $langs{$lang}->{repo} ) {
        $last->{$lang}->{repo} = $langs{$lang}->{repo};
    }

136
    my $repotype;
137 138 139 140 141
    my $remote       = $last->{$lang}->{repo};
    my $gitlangdir   = "$GITDIR/$lang";
    my $dirtylangdir = "$DIRTY/$lang";
    my $cleanlangdir = "$CLEAN/$lang";

142 143
    mkpath $dirtylangdir;
    mkpath $cleanlangdir;
144 145


146 147
    if ( $remote =~ m/^lp:mahara-lang/ ) {
        $repotype = 'launchpad';
148
        @branches = qw(1.7_STABLE 1.8_STABLE 1.9_STABLE 1.10_STABLE 15.04_STABLE 15.10_STABLE master);
149
    }
150
    elsif ( $remote =~ m{^https://git\.mahara\.org|^https://gitlab\.com} ) {
151
        $repotype = 'git';
152 153 154
        ! -d "$gitlangdir" && system "git clone --quiet $remote $gitlangdir";
        chdir $gitlangdir;
        system "git fetch --quiet";
155
        my $remotebranchcmd = 'git branch -r | grep -v "HEAD" | grep "origin\/\(master\|1.7_STABLE\|1.8_STABLE\|1.9_STABLE\|1.10_STABLE|15.04_STABLE|15.10_STABLE\)$"';
156 157 158 159 160 161 162 163 164 165 166 167
        my $remotebranches = `$remotebranchcmd`;
        $remotebranches =~ s/\s+/ /;
        @branches = ();
        foreach my $b (split(" ", $remotebranches)) {
            $b =~ s{^origin/}{};
            push @branches, $b;
        }
    }
    else {
        print STDERR "Don't know what to do with $remote; skipping $lang\n";
        next;
    }
168

169
    foreach my $branch (@branches) {
170

171 172 173 174 175
        my $remotecommit;
        my $currentdir;
        if ( $repotype eq 'launchpad' ) {
            $currentdir = "$BZRDIR/$branch";
            chdir $currentdir;
176
            next if ! -f "$currentdir/mahara/$lang.po";
177 178 179 180 181 182 183 184 185
            my $remotecommitcmd = "bzr log --line mahara/$lang.po | head -1";
            $remotecommit = `$remotecommitcmd`;
        }
        else {
            my $remotecommitcmd = 'git log --pretty=format:"%H %ai %an" origin/' . $branch . ' | head -1';
            $remotecommit = `$remotecommitcmd`;
            $currentdir = $gitlangdir;
            chdir $currentdir;
        }
186 187
        chomp $remotecommit;

188 189
        if ( ! defined $last->{$lang}->{branches}->{$branch} ) {
            $last->{$lang}->{branches}->{$branch} = {};
190 191
        }

192
        my $filenamebase = "$lang-$branch";
193 194 195 196 197 198 199 200
        my $tarball = "$TARBALLS/$filenamebase.tar.gz";
        my $diff    = "$TARBALLS/$filenamebase.diff";

        -f $tarball && unlink $tarball;
        -f $diff && unlink $diff;

        my $lastruncommit = '';

201 202
        if ( defined $last->{$lang}->{branches}->{$branch}->{commit} ) {
            $lastruncommit = $last->{$lang}->{branches}->{$branch}->{commit};
203 204 205
        }

        if ( "$remotecommit" ne "$lastruncommit" ) {
206
            print STDERR "Updating $lang $branch\n";
207

208
            if ( $repotype eq 'git' ) {
209 210
                my $branchcmd = 'git branch | grep "' . $branch . '$"';
                my $branchexists = `$branchcmd`;
211

212 213 214 215 216 217 218
                if ( length $branchexists ) {
                    system "git checkout --quiet $branch";
                    system "git reset --hard -q origin/$branch";
                }
                else {
                    system "git checkout --quiet -b $branch origin/$branch";
                }
219 220
            }

221 222
            $last->{$lang}->{branches}->{$branch}->{status} = 0;
            $last->{$lang}->{branches}->{$branch}->{errors} = '';
223

224
            my $cleanbranchdir = "$cleanlangdir/$branch";
225 226
            -d "$cleanbranchdir/lang" && rmtree $cleanbranchdir;
            ! -d $cleanbranchdir && mkpath $cleanbranchdir;
227

228
            my $pofile = "$currentdir/mahara/$lang.po";
229 230 231

            if ( -f $pofile ) {

232
                $last->{$lang}->{branches}->{$branch}->{type} = 'po';
233

234
                print STDERR "$lang $branch: using .po file\n";
235 236 237 238

                # Check utf8ness of .po file?
                my $output = `$UTF8CMD $pofile`;
                if ( length $output ) {
239 240
                    $last->{$lang}->{branches}->{$branch}->{errors} = "$pofile\n$output";
                    $last->{$lang}->{branches}->{$branch}->{status} = -1;
241 242 243 244 245 246 247
                }

                # Create langpack from .po file
                my $pocmd = "$POCMD $pofile $cleanbranchdir \"$lang.utf8\"";
                $output = `$pocmd`;

                if ( length $output ) {
248 249 250
                    $last->{$lang}->{branches}->{$branch}->{errors} .= "Failed to create langpack from .po file $pofile\n";
                    $last->{$lang}->{branches}->{$branch}->{errors} .= "$output";
                    $last->{$lang}->{branches}->{$branch}->{status} = -1;
251 252
                }

253
            }
254
            elsif ( $repotype eq 'git' ) {
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

                # .po is not available, so this is a php langpack

                my $langconfig = 0;

                if ( $lang =~ m/^([a-z]{2})_([a-z]{2})$/ ) {
                    $langconfig = -f "$currentdir/lang/$1_" . lc($2) . '.utf8/langconfig.php'
                      || -f "$currentdir/lang/$1_" . uc($2) . '.utf8/langconfig.php';
                }
                else {
                    $langconfig = -f "$currentdir/lang/$lang.utf8/langconfig.php";
                }

                if ( ! $langconfig ) {
                    print STDERR "$lang $branch: Couldn't find lang/$lang.utf8/langconfig.php in $currentdir; skipping\n";
                    next;
                }
272

273
                $last->{$lang}->{branches}->{$branch}->{type} = 'mahara';
274

275
                print STDERR "$lang $branch: sanitising\n";
276 277

                # sanitise langpack
278
                my $dirtybranchdir = "$dirtylangdir/$branch";
279
                ! -d $dirtybranchdir && mkpath $dirtybranchdir;
280

281
                system("cp -r $currentdir/" . '[a-z]* ' . $dirtybranchdir);
282

283 284 285 286
                # Make en.utf8 available
                chdir $MAHARA;
                system "git reset --hard -q origin/$branch";

287
                # Clean out stray php from the langpacks
288
                system "$CLEANCMD $MAHARA/htdocs $dirtybranchdir $cleanbranchdir";
289 290 291 292 293 294 295 296 297 298 299 300

                chdir $DATA;
                system "diff -Bwr $dirtybranchdir $cleanbranchdir > $diff";

                # Check syntax of php files
                chdir $cleanbranchdir;
                my $phpfiles = `find . -name \"\*.php\"`;
                foreach my $phpfile (split("\n", $phpfiles)) {
                    $phpfile =~ s/^\s*(\S.*\S)\s*$/$1/;
                    if ( $phpfile =~ m/php$/ ) {
                        my $output = `$SYNTAXCMD $phpfile >/dev/null`;
                        if ( length $output ) {
301 302
                            $last->{$lang}->{branches}->{$branch}->{errors} = "$phpfile\n$output";
                            $last->{$lang}->{branches}->{$branch}->{status} = -1;
303 304 305 306 307 308 309 310 311 312 313
                        }
                    }
                }

                my $allfiles = `find .`;

                # Check utf8ness of all files
                foreach my $file (split("\n", $allfiles)) {
                    $file =~ s/^\s*(\S.*\S)\s*$/$1/;
                    $output = `$UTF8CMD $file`;
                    if ( length $output ) {
314 315
                        $last->{$lang}->{branches}->{$branch}->{errors} .= "$file\n$output";
                        $last->{$lang}->{branches}->{$branch}->{status} = -1;
316 317 318
                    }
                }
            }
319
            else {
320
                print STDERR "$lang $branch: Couldn't find mahara/$lang.po or lang/$lang.utf8/langconfig.php in $currentdir; skipping\n";
321 322
                next;
            }
323

324
            if ( $last->{$lang}->{branches}->{$branch}->{status} == 0 ) {
325
                my $strip = $cleanbranchdir;
326
                $strip =~ s{^/}{^};
327 328 329
                system "tar --transform \"s,$strip,$lang.utf8,\" -zcf $tarball $cleanbranchdir";
            }

330
            chdir $currentdir;
331

332
            my $localcommit;
333
            if ( $repotype eq 'git' ) {
334 335 336 337 338
                $localcommit = `git log --pretty=format:\"%H %ai %an\" $branch | head -1`;
            }
            else {
                $localcommit = `bzr log --line mahara/$lang.po | head -1`;
            }
339
            chomp $localcommit;
340
            $last->{$lang}->{branches}->{$branch}->{commit} = $localcommit;
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
        }
    }
}

# Move new tarballs & log files to web directory
foreach my $file (split /\n/, `find $TARBALLS -name \"\*.tar.gz\"`) {
    system "mv $file $DOCROOT";
}

foreach my $file (split /\n/, `find $TARBALLS -name \"\*.diff\"`) {
    my $base = $file;
    $base =~ s{^.*/([^/\s]+)\.diff\s*$}{$1};
    system "mv $file $DOCROOT/$base-diff.txt";
}

# Generate index.html
system "/usr/bin/perl $FindBin::Bin/generate-index.pl $DOCROOT";

# Save latest commits
open $savefh, '>', $savefile;
print $savefh Data::Dumper->Dump([$last], ['last']);

# Generate status.html
system "/usr/bin/perl $FindBin::Bin/generate-status.pl $TARBALLS $DOCROOT";

print STDERR "Done.\n";