1 | #!/usr/bin/perl -w
|
---|
2 | # -CDSL would be better than explicit encoding settings
|
---|
3 |
|
---|
4 | use strict;
|
---|
5 |
|
---|
6 | my ($user, $pwd);
|
---|
7 |
|
---|
8 | # Three ways to handle login data:
|
---|
9 | # Enter data directly in these two lines (Be careful witn svn checkin later!)
|
---|
10 | # create a file "uploadpot.pl_credits" containing the two lines with proper values
|
---|
11 | # leave credits empty and enter them on runtime
|
---|
12 | $user = '';
|
---|
13 | $pwd = '';
|
---|
14 |
|
---|
15 | # list of supported languages
|
---|
16 | my %lang = map {$_ => 1} (
|
---|
17 | "ar", "bg", "cs", "da", "de", "el", "en_AU", "en_GB",
|
---|
18 | "es", "et", "eu", "fi", "fr", "gl", "he", "id", "is",
|
---|
19 | "it", "ja", "nb", "nl", "pl", "pt_BR", "ru", "sk",
|
---|
20 | "sv", "tr", "uk", "zh_CN", "zh_TW"
|
---|
21 | );
|
---|
22 |
|
---|
23 | my $revision = '$Revision: 27208 $revision =~ s/^.*?(\d+).*$/$1/;
|
---|
24 | my $agent = "JOSM_Launchpad/1.$revision";
|
---|
25 |
|
---|
26 | my $count = 0;#11;
|
---|
27 | my $cleanall = 0;#1;
|
---|
28 | my $upload = 0;#1;
|
---|
29 |
|
---|
30 | if($#ARGV != 0)
|
---|
31 | {
|
---|
32 | warn "No argument given (try Launchpad download URL, \"bzr\", \"bzronly\", \"upload\" or \"download\").";
|
---|
33 | system "ant";
|
---|
34 | makeupload();
|
---|
35 | }
|
---|
36 | elsif($ARGV[0] eq "bzr" || $ARGV[0] eq "bzronly")
|
---|
37 | {
|
---|
38 | mkdir "build";
|
---|
39 | die "Could not change into new data dir." if !chdir "build";
|
---|
40 | system "bzr export -v josm_trans lp:~openstreetmap/josm/josm_trans";
|
---|
41 | chdir "..";
|
---|
42 | copypo("build/josm_trans/josm");
|
---|
43 | system "rm -rv build/josm_trans";
|
---|
44 | if($ARGV[0] ne "bzronly")
|
---|
45 | {
|
---|
46 | system "ant";
|
---|
47 | makeupload();
|
---|
48 | }
|
---|
49 | }
|
---|
50 | elsif($ARGV[0] eq "upload")
|
---|
51 | {
|
---|
52 | potupload();
|
---|
53 | }
|
---|
54 | elsif($ARGV[0] eq "download")
|
---|
55 | {
|
---|
56 | podownload();
|
---|
57 | }
|
---|
58 | elsif($ARGV[0] eq "stats")
|
---|
59 | {
|
---|
60 | getstats();
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | mkdir "build";
|
---|
65 | mkdir "build/josm_trans";
|
---|
66 | die "Could not change into new data dir." if !chdir "build/josm_trans";
|
---|
67 | system "wget $ARGV[0]";
|
---|
68 | system "tar -xf laun*";
|
---|
69 | chdir "../..";
|
---|
70 | copypo("build/josm_trans");
|
---|
71 | system "rm -rv build/josm_trans";
|
---|
72 | system "ant";
|
---|
73 | makeupload();
|
---|
74 | }
|
---|
75 |
|
---|
76 | sub makeupload
|
---|
77 | {
|
---|
78 | if($upload)
|
---|
79 | {
|
---|
80 | my $outdate = `date -u +"%Y-%m-%dT%H_%M_%S"`;
|
---|
81 | chomp $outdate;
|
---|
82 | mkdir "build/josm";
|
---|
83 | system "cp po/*.po po/josm.pot build/josm";
|
---|
84 | chdir "build";
|
---|
85 | if(!$count)
|
---|
86 | {
|
---|
87 | system "tar -cjf ../launchpad_upload_josm_$outdate.tar.bz2 josm";
|
---|
88 | }
|
---|
89 | else
|
---|
90 | {
|
---|
91 | my @files = sort glob("josm/*.po");
|
---|
92 | my $num = 1;
|
---|
93 | while($#files >= 0)
|
---|
94 | {
|
---|
95 | my @f = splice(@files, 0, $count);
|
---|
96 | system "tar -cjf ../launchpad_upload_josm_${outdate}_$num.tar.bz2 josm/josm.pot ".join(" ",@f);
|
---|
97 | ++$num;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | system "rm -rv josm";
|
---|
101 | chdir "..";
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | sub copypo
|
---|
106 | {
|
---|
107 | my ($path) = @_;
|
---|
108 | foreach my $name (split("\n", `find $path -name "*.po"`))
|
---|
109 | {
|
---|
110 | $name =~ /([a-zA-Z_]+)\.po/;
|
---|
111 | if($lang{$1})
|
---|
112 | {
|
---|
113 | system "cp -v $name po/$1.po";
|
---|
114 | }
|
---|
115 | elsif($cleanall)
|
---|
116 | {
|
---|
117 | local $/; undef $/;
|
---|
118 | open FILE,"<",$name or die;
|
---|
119 | my $x = <FILE>;
|
---|
120 | close FILE;
|
---|
121 | $x =~ s/\n\n.*$/\n/s;
|
---|
122 | open FILE,">","po/$1.po" or die;
|
---|
123 | print FILE $x;
|
---|
124 | close FILE;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | sub dologin
|
---|
130 | {
|
---|
131 | require WWW::Mechanize;
|
---|
132 |
|
---|
133 | my $mech = WWW::Mechanize->new("agent" => $agent);
|
---|
134 |
|
---|
135 | #$mech->add_handler("request_send" => sub {
|
---|
136 | # my($request, $ua, $h) = @_;
|
---|
137 | # printf "FORM: %s\n", $request->content();
|
---|
138 | # return undef;
|
---|
139 | #});
|
---|
140 | $mech->get("https://translations.launchpad.net/josm/trunk/+login");
|
---|
141 | #print $mech->status() ." - ". $mech->uri()."\n";
|
---|
142 | $mech->submit_form(form_number => 1);
|
---|
143 | getcredits();
|
---|
144 | #print $mech->status() ." - ". $mech->uri()."\n";
|
---|
145 | $mech->submit_form(with_fields => {"email" => $user, "password" => $pwd});
|
---|
146 | #$mech->dump_headers();
|
---|
147 | #print $mech->status() ." - ". $mech->uri()."\n";
|
---|
148 | #print $mech->content();
|
---|
149 | my $form = $mech->form_name("decideform");
|
---|
150 | die "Could not login.\n" if !$form;
|
---|
151 | my %par = ("yes" => ""); # We need to add "yes" or it does not work
|
---|
152 | foreach my $p ($form->param)
|
---|
153 | {
|
---|
154 | $par{$p} = $form->value($p);
|
---|
155 | }
|
---|
156 | $mech->post($form->action, \%par);
|
---|
157 | #$mech->dump_headers();
|
---|
158 | #print $mech->content();
|
---|
159 | #print $mech->status() ." - ". $mech->uri()."\n";
|
---|
160 | #$mech->dump_forms();
|
---|
161 | return $mech;
|
---|
162 | }
|
---|
163 |
|
---|
164 | sub potupload
|
---|
165 | {
|
---|
166 | my $mech = dologin();
|
---|
167 | $mech->get("https://translations.launchpad.net/josm/trunk/+translations-upload");
|
---|
168 | chdir("po");
|
---|
169 | $mech->submit_form(with_fields => {"file" => "josm.pot"});
|
---|
170 | chdir("..");
|
---|
171 | }
|
---|
172 |
|
---|
173 | sub podownload
|
---|
174 | {
|
---|
175 | my $mech = dologin();
|
---|
176 | $mech->get("https://translations.launchpad.net/josm/trunk/+export");
|
---|
177 | $mech->submit_form(with_fields => {"format" => "PO"});
|
---|
178 | if(!($mech->content() =~ /receive an email shortly/))
|
---|
179 | {
|
---|
180 | warn "Error requesting file\n";
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | sub getcredits
|
---|
185 | {
|
---|
186 | if(!$user || !$pwd)
|
---|
187 | {
|
---|
188 | require Term::ReadKey;
|
---|
189 | local undef $/;
|
---|
190 | if(open FILE, "launchpad.pl_credits")
|
---|
191 | {
|
---|
192 | eval <FILE>;
|
---|
193 | close FILE;
|
---|
194 | }
|
---|
195 |
|
---|
196 | if(!$user)
|
---|
197 | {
|
---|
198 | Term::ReadKey::ReadMode(4); # Turn off controls keys
|
---|
199 | printf("Enter username: ");
|
---|
200 | for(;;)
|
---|
201 | {
|
---|
202 | my $c = getc();
|
---|
203 | print $c;
|
---|
204 | last if $c eq "\n";
|
---|
205 | $user .= $c;
|
---|
206 | }
|
---|
207 | Term::ReadKey::ReadMode(0);
|
---|
208 | }
|
---|
209 |
|
---|
210 | if(!$pwd)
|
---|
211 | {
|
---|
212 | Term::ReadKey::ReadMode(4); # Turn off controls keys
|
---|
213 | printf("Enter password: ");
|
---|
214 | for(;;)
|
---|
215 | {
|
---|
216 | my $c = getc();
|
---|
217 | last if $c eq "\n";
|
---|
218 | print "*";
|
---|
219 | $pwd .= $c;
|
---|
220 | }
|
---|
221 | print "\n";
|
---|
222 | Term::ReadKey::ReadMode(0);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | sub doget
|
---|
228 | {
|
---|
229 | my ($mech, $page) = @_;
|
---|
230 | print "$page\n";
|
---|
231 | for(my $i = 1; $i <= 50; $i++)
|
---|
232 | {
|
---|
233 | eval
|
---|
234 | {
|
---|
235 | $mech->get($page);
|
---|
236 | };
|
---|
237 | print "Try $i: $@" if $@;
|
---|
238 | return $mech if !$@;
|
---|
239 | sleep(30+5*$i);
|
---|
240 | $mech = WWW::Mechanize->new("agent" => $agent);
|
---|
241 | }
|
---|
242 | return $mech;
|
---|
243 | }
|
---|
244 |
|
---|
245 | sub getstats
|
---|
246 | {
|
---|
247 | my %results;
|
---|
248 | require WWW::Mechanize;
|
---|
249 | require Data::Dumper;
|
---|
250 | require URI::Escape;
|
---|
251 | my $mech = WWW::Mechanize->new("agent" => $agent);
|
---|
252 |
|
---|
253 | if(open DFILE,"<:utf8","launchpadtrans.data")
|
---|
254 | {
|
---|
255 | local $/;
|
---|
256 | $/ = undef;
|
---|
257 | my $val = <DFILE>;
|
---|
258 | eval $val;
|
---|
259 | close DFILE;
|
---|
260 | }
|
---|
261 |
|
---|
262 | binmode STDOUT, ":utf8";
|
---|
263 |
|
---|
264 | open FILE,">:utf8","launchpadtrans.txt" or die "Could not open output file.";
|
---|
265 |
|
---|
266 | for my $lang (sort keys %lang)
|
---|
267 | {
|
---|
268 | $mech->get("https://translations.launchpad.net/josm/trunk/+pots/josm/$lang/");
|
---|
269 | sleep(1);
|
---|
270 | my $cont = utf8::upgrade($mech->content());
|
---|
271 | while($cont =~ /<a href="https?:\/\/launchpad.net\/~(.*?)" class="sprite person(-inactive)?">(.*?)<\/a>/g)
|
---|
272 | {
|
---|
273 | my ($code, $inactive, $name) = ($1, $2, $3);
|
---|
274 | if(exists($results{$code}{$lang}))
|
---|
275 | {
|
---|
276 | printf "%-5s - %-30s - Found - %s\n", $lang,$code,$name;
|
---|
277 | next;
|
---|
278 | }
|
---|
279 | my $urlcode = URI::Escape::uri_escape($code);
|
---|
280 | $mech = doget($mech, "https://translations.launchpad.net/josm/trunk/+pots/josm/$lang/+filter?person=$urlcode");
|
---|
281 | sleep(1);
|
---|
282 | my ($count) = $mech->content() =~ /of[\r\n\t ]+?(\d+)[\r\n\t ]+?result/;
|
---|
283 | if($count && $mech->status == 200)
|
---|
284 | {
|
---|
285 | $results{$code}{NAME} = $name;
|
---|
286 | $results{$code}{$lang} = $count;
|
---|
287 | $results{$code}{TOTAL} += $count;
|
---|
288 | if(open DFILE,">:utf8","launchpadtrans.data")
|
---|
289 | {
|
---|
290 | print DFILE Data::Dumper->Dump([\%results],['*results']);
|
---|
291 | close DFILE;
|
---|
292 | }
|
---|
293 | printf "%-5s - %-30s - %5d - %s\n", $lang,$code,$count,$name;
|
---|
294 |
|
---|
295 | }
|
---|
296 | else
|
---|
297 | {
|
---|
298 | printf "%-5s - %-30s - Skip - %s\n", $lang,$code,$name;
|
---|
299 | }
|
---|
300 | }
|
---|
301 | }
|
---|
302 | for my $code (sort {$results{$b}{TOTAL} <=> $results{$a}{TOTAL}} keys %results)
|
---|
303 | {
|
---|
304 | print FILE "$results{$code}{NAME}:$results{$code}{TOTAL}";
|
---|
305 | printf "%5d - %-50s",$results{$code}{TOTAL}, $results{$code}{NAME};
|
---|
306 | for my $lang (sort keys %{$results{$code}})
|
---|
307 | {
|
---|
308 | next if $lang eq "NAME" or $lang eq "TOTAL";
|
---|
309 | print FILE ";$lang=$results{$code}{$lang}";
|
---|
310 | printf " - %-5s=%5d",$lang, $results{$code}{$lang};
|
---|
311 | }
|
---|
312 | print FILE "\n";
|
---|
313 | print "\n";
|
---|
314 | }
|
---|
315 | }
|
---|