| 1 | #! /usr/bin/perl -w
|
|---|
| 2 |
|
|---|
| 3 | #####################################################################
|
|---|
| 4 | ### http://www.perl.com/doc/manual/html/utils/pod2man.html
|
|---|
| 5 | ### http://search.cpan.org/dist/perl/pod/perlpod.pod
|
|---|
| 6 |
|
|---|
| 7 | =head1 NAME
|
|---|
| 8 |
|
|---|
| 9 | poimport.pl - Import the translation from the tarball downloaded from
|
|---|
| 10 | Launchpad.
|
|---|
| 11 |
|
|---|
| 12 | =head1 SYNOPSIS
|
|---|
| 13 |
|
|---|
| 14 | B<poimport.pl> [B<--help>] [B<--man>] [B<--podir> I<po>]
|
|---|
| 15 | [B<--workdir> I<poimport>] [B<--(no)rmworkdir>] I<tarball>
|
|---|
| 16 |
|
|---|
| 17 | =head1 DESCRIPTION
|
|---|
| 18 |
|
|---|
| 19 | Import the plugin translations from Launchpad. The argument
|
|---|
| 20 | I<tarball> can be a tarball file that was downloaded from Launchpad, a
|
|---|
| 21 | tarball download URL, a JOSM translation branch revision number
|
|---|
| 22 | (e.g. I<789>), or the keyword I<latest> for the latest revision.
|
|---|
| 23 | Default is to download the latest translation branch revision.
|
|---|
| 24 |
|
|---|
| 25 | =head1 OPTIONS
|
|---|
| 26 |
|
|---|
| 27 | =over 4
|
|---|
| 28 |
|
|---|
| 29 | =item B<--help>
|
|---|
| 30 |
|
|---|
| 31 | Prints a brief help message and exits.
|
|---|
| 32 |
|
|---|
| 33 | =item B<--man>
|
|---|
| 34 |
|
|---|
| 35 | Prints the manual page and exits.
|
|---|
| 36 |
|
|---|
| 37 | =item B<--podir>
|
|---|
| 38 |
|
|---|
| 39 | Destination directory relative to directory where this script was
|
|---|
| 40 | started in. Default is F<po>.
|
|---|
| 41 |
|
|---|
| 42 | =item B<--workdir>
|
|---|
| 43 |
|
|---|
| 44 | Temporary directory. A unique directory name that is not used for
|
|---|
| 45 | anything else. Default is F<poimport>.
|
|---|
| 46 |
|
|---|
| 47 | =item B<--rmworkdir>
|
|---|
| 48 |
|
|---|
| 49 | Remove the temporary directory after the work is done. Disable
|
|---|
| 50 | removal with B<--normworkdir>. Default is to remove the temporary
|
|---|
| 51 | directory.
|
|---|
| 52 |
|
|---|
| 53 | =back
|
|---|
| 54 |
|
|---|
| 55 | =cut
|
|---|
| 56 | #####################################################################
|
|---|
| 57 |
|
|---|
| 58 | use strict;
|
|---|
| 59 | use File::Copy;
|
|---|
| 60 | use Cwd;
|
|---|
| 61 | use File::Spec::Functions;
|
|---|
| 62 | use File::Basename;
|
|---|
| 63 | use Getopt::Long;
|
|---|
| 64 | use Pod::Usage;
|
|---|
| 65 |
|
|---|
| 66 | ### Name of the tarball downloaded from Launchpad. Or download URL.
|
|---|
| 67 | ### Or JOSM translation branch revision number. Or keyword "latest".
|
|---|
| 68 | my $tarball;
|
|---|
| 69 | #$tarball = "launchpad-export.tar.gz";
|
|---|
| 70 | #$tarball = "http://launchpadlibrarian.net/159932691/launchpad-export.tar.gz";
|
|---|
| 71 | #$tarball = "http://bazaar.launchpad.net/~openstreetmap/josm/josm_trans/tarball/747";
|
|---|
| 72 | #$tarball = "747";
|
|---|
| 73 | #$tarball = "http://bazaar.launchpad.net/~openstreetmap/josm/josm_trans/tarball";
|
|---|
| 74 | $tarball = "latest";
|
|---|
| 75 | my $workdir = "poimport"; ### Temp. directory.
|
|---|
| 76 | my $rmworkdir = 1; ### Remove the temp. directory (0/1)?
|
|---|
| 77 | my $podir = "po"; ### Destination directory.
|
|---|
| 78 | my $showhelp = 0; ### Show help screen.
|
|---|
| 79 | my $showman = 0; ### Show manual page of this script.
|
|---|
| 80 |
|
|---|
| 81 | GetOptions('help|?|h' => \$showhelp,
|
|---|
| 82 | 'man' => \$showman,
|
|---|
| 83 | 'podir=s' => \$podir,
|
|---|
| 84 | 'workdir=s' => \$workdir,
|
|---|
| 85 | 'rmworkdir!' => \$rmworkdir,
|
|---|
| 86 | ) or pod2usage(2);
|
|---|
| 87 |
|
|---|
| 88 | pod2usage(1) if $showhelp;
|
|---|
| 89 | pod2usage(-exitstatus => 0, -verbose => 2) if $showman;
|
|---|
| 90 |
|
|---|
| 91 | ### Check for arguments. The only supported argument is the tarball.
|
|---|
| 92 | if ($#ARGV == 0) {
|
|---|
| 93 | $tarball = $ARGV[0];
|
|---|
| 94 | }
|
|---|
| 95 | elsif ($#ARGV > 0) {
|
|---|
| 96 | die "This script accepts only one argument.\n";
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | my $josmtburl = "http://bazaar.launchpad.net/~openstreetmap/josm/josm_trans/"
|
|---|
| 100 | . "tarball";
|
|---|
| 101 |
|
|---|
| 102 | ### Check for JOSM translation branch revision number.
|
|---|
| 103 | if ($tarball =~ m/^\d+$/) {
|
|---|
| 104 | $tarball = $josmtburl . "/" . $tarball;
|
|---|
| 105 | }
|
|---|
| 106 | ### Or for keyword "latest", i.e. the latest JOSM translation revision.
|
|---|
| 107 | elsif ($tarball eq "latest") {
|
|---|
| 108 | $tarball = $josmtburl;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | ### Check if tarball is a URL and download it. The downloaded file
|
|---|
| 112 | ### will not be removed and is available for a second import.
|
|---|
| 113 | my $downurl;
|
|---|
| 114 | if ($tarball =~ m,^http://.+/([^/]+)$,) {
|
|---|
| 115 | ### URL: Download file.
|
|---|
| 116 | $downurl = $tarball;
|
|---|
| 117 | my $downfile = $1;
|
|---|
| 118 | if ($downfile =~ m/^\d+$/) {
|
|---|
| 119 | ### Download of revision number.
|
|---|
| 120 | if ($tarball =~ m:/([^/]+)/tarball/(\d+)$:) {
|
|---|
| 121 | $downfile = $1 . "_" . $2 . ".tar.gz";
|
|---|
| 122 | }
|
|---|
| 123 | else {
|
|---|
| 124 | $downfile .= ".tar.gz";
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | elsif ($downfile eq "tarball") {
|
|---|
| 128 | ### Download of latest revision.
|
|---|
| 129 | if ($tarball =~ m:/([^/]+)/tarball$:) {
|
|---|
| 130 | $downfile = $1 . "_latest.tar.gz";
|
|---|
| 131 | }
|
|---|
| 132 | else {
|
|---|
| 133 | $downfile .= ".tar.gz";
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | print "Will download file $downfile from $downurl.\n";
|
|---|
| 137 | system("wget -O $downfile $downurl") == 0 or die "wget failed: $?";
|
|---|
| 138 | $tarball = $downfile;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | die "Tarball $tarball not found.\n" if (! -r $tarball);
|
|---|
| 142 | if (! -d $workdir) {
|
|---|
| 143 | mkdir $workdir or die "Failed to create work directory $workdir: $!";
|
|---|
| 144 | }
|
|---|
| 145 | copy($tarball, $workdir);
|
|---|
| 146 | my $startdir = getcwd();
|
|---|
| 147 | chdir $workdir;
|
|---|
| 148 | my $tarballfile = basename($tarball);
|
|---|
| 149 | system "tar -xf $tarballfile";
|
|---|
| 150 | print "Copy language files:";
|
|---|
| 151 | foreach my $lpponame (split("\n", `find . -name "*.po"`)) {
|
|---|
| 152 | if ($lpponame =~ /([a-zA-Z_@]+)\.po/) {
|
|---|
| 153 | my $lang = $1;
|
|---|
| 154 | my $poname = $1 . ".po";
|
|---|
| 155 | print " $lang";
|
|---|
| 156 | copy($lpponame, catfile($startdir, $podir, $poname));
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | print "\n";
|
|---|
| 160 |
|
|---|
| 161 | if ($rmworkdir) {
|
|---|
| 162 | chdir $startdir;
|
|---|
| 163 | system "rm -rf $workdir";
|
|---|
| 164 | }
|
|---|