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 23 24 25 26 27 28 29

foreach my $c qw(DATA DOCROOT) {
    exists $ENV{$c} or die ("\$ENV{$c} undefined");
}

my $DATA      = $ENV{DATA};
my $DOCROOT   = $ENV{DOCROOT};

30
my $CLEANCMD  = "/usr/bin/php $FindBin::Bin/clean-php.php";
31 32 33 34 35
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";
36
my $BZRDIR    = "${DATA}/bzr";
37 38 39
my $DIRTY     = "${DATA}/old";
my $CLEAN     = "${DATA}/new";
my $TARBALLS  = "${DATA}/tarballs";
40 41 42
my $MAHARA    = "${DATA}/mahara";

my $MAHARAREMOTE = 'git://gitorious.org/mahara/mahara.git';
43
my $REPOLIST     = 'http://gitorious.org/mahara/mahara-scripts/blobs/raw/master/mahara-langpacks/language-repos.txt';
44

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

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

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

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

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

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

my @langkeys = sort keys %langs;
if ( scalar @langkeys < 1 ) {
88
    @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);
89 90 91
}

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

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

102

103 104 105 106 107 108 109
# 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";


110 111
# For launchpad, all languages are in a single branch, so update the lot
! -d $BZRDIR && system "bzr init-repo $BZRDIR";
112
my @branches = qw(1.3_STABLE 1.4_STABLE 1.5_STABLE 1.6_STABLE 1.7_STABLE master);
113 114 115

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

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

    if ( ! defined $last->{$lang} ) {
        $last->{$lang} = { repo => "git://gitorious.org/mahara-lang/$lang.git" };
    }

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

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

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


144 145
    if ( $remote =~ m/^lp:mahara-lang/ ) {
        $repotype = 'launchpad';
146
        @branches = qw(1.3_STABLE 1.4_STABLE 1.5_STABLE 1.6_STABLE 1.7_STABLE master);
147 148 149 150 151 152
    }
    elsif ( $remote =~ m{^git://gitorious\.org} ) {
        $repotype = 'gitorious';
        ! -d "$gitlangdir" && system "git clone --quiet $remote $gitlangdir";
        chdir $gitlangdir;
        system "git fetch --quiet";
153
        my $remotebranchcmd = 'git branch -r | grep -v "HEAD" | grep "origin\/\(master\|1.3_STABLE\|1.4_STABLE\|1.5_STABLE\|1.6_STABLE\|1.7_STABLE\)$"';
154 155 156 157 158 159 160 161 162 163 164 165
        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;
    }
166

167
    foreach my $branch (@branches) {
168

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

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

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

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

        my $lastruncommit = '';

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

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

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

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

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

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

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

            if ( -f $pofile ) {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

328
            chdir $currentdir;
329

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

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