langpacks.pl 11.6 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 30 31 32 33 34 35

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

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

my $CLEANCMD  = "/usr/bin/php $FindBin::Bin/langpack.php";
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 40
my $DIRTY     = "${DATA}/old";
my $CLEAN     = "${DATA}/new";
my $TARBALLS  = "${DATA}/tarballs";

41 42 43 44
mkpath $GITDIR;
mkpath $DIRTY;
mkpath $CLEAN;
mkpath $TARBALLS;
45 46 47

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

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
# 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 {
    my $repolisturl = 'https://gitorious.org/mahara/mahara-scripts/blobs/raw/master/mahara-langpacks/language-repos.txt';
    print STDERR "Retrieving repository list from $repolisturl\n";

    my $ua = LWP::UserAgent->new;
    $ua->timeout(10);
    $ua->env_proxy;
    my $response = $ua->get($repolisturl);
    $repolist = $response->is_success ? $response->content : undef;
}

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

my @langkeys = sort keys %langs;
if ( scalar @langkeys < 1 ) {
    @langkeys = qw(ar ca cs da de en_us es eu fi fr he it ja ko mi nl no_nb sl zh_tw);
}

print STDERR "Languages: " . join(' ', @langkeys) . "\n";
84 85 86 87 88 89 90 91 92 93

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

94 95 96 97 98 99 100 101 102 103 104 105 106 107

# For launchpad, all languages are in a single branch, so update the lot
! -d $BZRDIR && system "bzr init-repo $BZRDIR";
my @branches = qw(1.2_STABLE 1.3_STABLE 1.4_STABLE master);

foreach my $branch (@branches) {
    if ( ! -d "$BZRDIR/$branch" ) {
        system "bzr branch lp:~mahara-lang/mahara-lang/$branch $BZRDIR/$branch";
    }
    else {
        system "bzr update $BZRDIR/$branch";
    }
}

108
foreach my $lang (@langkeys) {
109 110 111 112 113

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

114 115 116 117
    if ( defined $langs{$lang}->{repo} ) {
        $last->{$lang}->{repo} = $langs{$lang}->{repo};
    }

118
    my $repotype;
119 120 121 122 123
    my $remote       = $last->{$lang}->{repo};
    my $gitlangdir   = "$GITDIR/$lang";
    my $dirtylangdir = "$DIRTY/$lang";
    my $cleanlangdir = "$CLEAN/$lang";

124 125
    mkpath $dirtylangdir;
    mkpath $cleanlangdir;
126 127


128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    if ( $remote =~ m/^lp:mahara-lang/ ) {
        $repotype = 'launchpad';
    }
    elsif ( $remote =~ m{^git://gitorious\.org} ) {
        $repotype = 'gitorious';
        ! -d "$gitlangdir" && system "git clone --quiet $remote $gitlangdir";
        chdir $gitlangdir;
        system "git fetch --quiet";
        my $remotebranchcmd = 'git branch -r | grep -v "HEAD" | grep "origin\/\(master\|1.2_STABLE\|1.3_STABLE\|1.4_STABLE\)$"';
        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;
    }
149

150
    foreach my $branch (@branches) {
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        my $remotecommit;
        my $currentdir;
        if ( $repotype eq 'launchpad' ) {
            $currentdir = "$BZRDIR/$branch";
            chdir $currentdir;
            next if ! -d "$currentdir/mahara/$lang.po";
            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;
        }
167 168
        chomp $remotecommit;

169 170
        if ( ! defined $last->{$lang}->{branches}->{$branch} ) {
            $last->{$lang}->{branches}->{$branch} = {};
171 172
        }

173
        my $filenamebase = "$lang-$branch";
174 175 176 177 178 179 180 181
        my $tarball = "$TARBALLS/$filenamebase.tar.gz";
        my $diff    = "$TARBALLS/$filenamebase.diff";

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

        my $lastruncommit = '';

182 183
        if ( defined $last->{$lang}->{branches}->{$branch}->{commit} ) {
            $lastruncommit = $last->{$lang}->{branches}->{$branch}->{commit};
184 185 186
        }

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

189 190 191
            if ( $repotype eq 'gitorious' ) {
                my $branchcmd = 'git branch | grep "' . $branch . '$"';
                my $branchexists = `$branchcmd`;
192

193 194 195 196 197 198 199
                if ( length $branchexists ) {
                    system "git checkout --quiet $branch";
                    system "git reset --hard -q origin/$branch";
                }
                else {
                    system "git checkout --quiet -b $branch origin/$branch";
                }
200 201
            }

202 203
            $last->{$lang}->{branches}->{$branch}->{status} = 0;
            $last->{$lang}->{branches}->{$branch}->{errors} = '';
204

205
            my $cleanbranchdir = "$cleanlangdir/$branch";
206 207
            -d "$cleanbranchdir/lang" && rmtree $cleanbranchdir;
            ! -d $cleanbranchdir && mkpath $cleanbranchdir;
208

209
            my $pofile = "$currentdir/mahara/$lang.po";
210 211 212

            if ( -f $pofile ) {

213
                $last->{$lang}->{branches}->{$branch}->{type} = 'po';
214

215
                print STDERR "$lang $branch: using .po file\n";
216 217 218 219

                # Check utf8ness of .po file?
                my $output = `$UTF8CMD $pofile`;
                if ( length $output ) {
220 221
                    $last->{$lang}->{branches}->{$branch}->{errors} = "$pofile\n$output";
                    $last->{$lang}->{branches}->{$branch}->{status} = -1;
222 223 224 225 226 227 228
                }

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

                if ( length $output ) {
229 230 231
                    $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;
232 233
                }

234
            }
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
            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;
                }
253

254
                $last->{$lang}->{branches}->{$branch}->{type} = 'mahara';
255

256
                print STDERR "$lang $branch: sanitising\n";
257 258

                # sanitise langpack
259
                my $dirtybranchdir = "$dirtylangdir/$branch";
260
                ! -d $dirtybranchdir && mkpath $dirtybranchdir;
261

262
                system("cp -r $currentdir/" . '[a-z]* ' . $dirtybranchdir);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

                # Clean out stray php from the langpacks
                system "$CLEANCMD $dirtybranchdir $cleanbranchdir";

                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 ) {
278 279
                            $last->{$lang}->{branches}->{$branch}->{errors} = "$phpfile\n$output";
                            $last->{$lang}->{branches}->{$branch}->{status} = -1;
280 281 282 283 284 285 286 287 288 289 290
                        }
                    }
                }

                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 ) {
291 292
                        $last->{$lang}->{branches}->{$branch}->{errors} .= "$file\n$output";
                        $last->{$lang}->{branches}->{$branch}->{status} = -1;
293 294 295
                    }
                }
            }
296
            else {
297
                print STDERR "$lang $branch: Couldn't find mahara/$lang.po or lang/$lang.utf8/langconfig.php in $currentdir; skipping\n";
298 299
                next;
            }
300

301
            if ( $last->{$lang}->{branches}->{$branch}->{status} == 0 ) {
302
                my $strip = $cleanbranchdir;
303
                $strip =~ s{^/}{^};
304 305 306
                system "tar --transform \"s,$strip,$lang.utf8,\" -zcf $tarball $cleanbranchdir";
            }

307
            chdir $currentdir;
308

309 310 311 312 313 314 315
            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`;
            }
316
            chomp $localcommit;
317
            $last->{$lang}->{branches}->{$branch}->{commit} = $localcommit;
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
        }
    }
}

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