langpacks.pl 12.3 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 112
# For launchpad, all languages are in a single branch, so update the lot
! -d $BZRDIR && system "bzr init-repo $BZRDIR";
113
my @branches = qw(1.7_STABLE 1.8_STABLE 1.9_STABLE 1.10_STABLE 15.04_STABLE master);
114 115 116

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

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

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

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

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

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


145 146
    if ( $remote =~ m/^lp:mahara-lang/ ) {
        $repotype = 'launchpad';
147
        @branches = qw(1.7_STABLE 1.8_STABLE 1.9_STABLE 1.10_STABLE 15.04_STABLE master);
148
    }
149
    elsif ( $remote =~ m{^https://git\.mahara\.org} ) {
150
        $repotype = 'git';
151 152 153
        ! -d "$gitlangdir" && system "git clone --quiet $remote $gitlangdir";
        chdir $gitlangdir;
        system "git fetch --quiet";
154
        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\)$"';
155 156 157 158 159 160 161 162 163 164 165 166
        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;
    }
167

168
    foreach my $branch (@branches) {
169

170 171 172 173 174
        my $remotecommit;
        my $currentdir;
        if ( $repotype eq 'launchpad' ) {
            $currentdir = "$BZRDIR/$branch";
            chdir $currentdir;
175
            next if ! -f "$currentdir/mahara/$lang.po";
176 177 178 179 180 181 182 183 184
            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;
        }
185 186
        chomp $remotecommit;

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

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

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

        my $lastruncommit = '';

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

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

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

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

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

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

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

            if ( -f $pofile ) {

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

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

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

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

                if ( length $output ) {
247 248 249
                    $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;
250 251
                }

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

                # .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;
                }
271

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

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

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

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

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

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

                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 ) {
300 301
                            $last->{$lang}->{branches}->{$branch}->{errors} = "$phpfile\n$output";
                            $last->{$lang}->{branches}->{$branch}->{status} = -1;
302 303 304 305 306 307 308 309 310 311 312
                        }
                    }
                }

                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 ) {
313 314
                        $last->{$lang}->{branches}->{$branch}->{errors} .= "$file\n$output";
                        $last->{$lang}->{branches}->{$branch}->{status} = -1;
315 316 317
                    }
                }
            }
318
            else {
319
                print STDERR "$lang $branch: Couldn't find mahara/$lang.po or lang/$lang.utf8/langconfig.php in $currentdir; skipping\n";
320 321
                next;
            }
322

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

329
            chdir $currentdir;
330

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

# 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";