source: osm/applications/editors/josm/i18n/launchpad.pl@ 32990

Last change on this file since 32990 was 31724, checked in by stoecker, 9 years ago

readd nb language

  • Property svn:executable set to *
  • Property svn:keywords set to Revision
File size: 12.2 KB
Line 
1#!/usr/bin/perl -w
2# -CDSL would be better than explicit encoding settings
3
4use strict;
5use utf8;
6
7my ($user, $pwd);
8
9# Three ways to handle login data:
10 # Enter data directly in these two lines (Be careful witn svn checkin later!)
11 # create a file "launchpad.pl_credits" containing the two lines with proper values
12 # leave credits empty and enter them on runtime
13$user = '';
14$pwd = '';
15
16# list of supported languages
17my %lang = map {$_ => 1} (
18"ast", "bg", "be", "ca", "ca\@valencia", "cs", "da", "de", "el", "en_AU", "en_GB",
19"es", "et", "fi", "fr", "gl", "hu", "id",
20"it", "ja", "km", "lt", "nb", "nl", "pl", "pt", "pt_BR", "ru", "sk",
21"sv", "uk", "vi", "zh_CN", "zh_TW"
22);
23
24my $revision = '$Revision: 31724 $';
25$revision =~ s/^.*?(\d+).*$/$1/;
26my $agent = "JOSM_Launchpad/1.$revision";
27
28my $debugfile = 0;#1;
29my $cleanall = 0;#1;
30
31open TXTFILE,">","launchpad_debug.log" or die if $debugfile;
32
33if($#ARGV != 0)
34{
35 warn "No argument given (try Launchpad download URL, \"bzr\", \"bzronly\", \"upload\" or \"download\").";
36 system "ant";
37}
38elsif($ARGV[0] eq "bzr" || $ARGV[0] eq "bzronly")
39{
40 mkdir "build";
41 die "Could not change into new data dir." if !chdir "build";
42 system "bzr export -v josm_trans lp:~openstreetmap/josm/josm_trans";
43 chdir "..";
44 copypo("build/josm_trans/josm");
45 system "rm -rv build/josm_trans";
46 if($ARGV[0] ne "bzronly")
47 {
48 system "ant";
49 }
50}
51elsif($ARGV[0] eq "upload")
52{
53 potupload();
54}
55elsif($ARGV[0] eq "uploadall")
56{
57 makeupload();
58}
59elsif($ARGV[0] eq "approveall")
60{
61 approveall(dologin());
62}
63elsif($ARGV[0] eq "download")
64{
65 podownload();
66}
67elsif($ARGV[0] eq "stats")
68{
69 getstats();
70}
71else
72{
73 mkdir "build";
74 mkdir "build/josm_trans";
75 die "Could not change into new data dir." if !chdir "build/josm_trans";
76 system "wget $ARGV[0]";
77 system "tar -xf laun*";
78 chdir "../..";
79 copypo("build/josm_trans");
80 system "rm -rv build/josm_trans";
81 system "ant";
82}
83
84sub approveall
85{
86 my ($mech) = @_;
87 print "Trying to approve upload.\n";
88 $mech->get("https://translations.launchpad.net/josm/trunk/+imports?field.filter_status=NEEDS_REVIEW&field.filter_extension=all");
89 my @links;
90 foreach my $line (split("\n", $mech->content()))
91 {
92 push (@links, $1) if $line =~ /href="(\/\+imports\/\d+)"/;
93 }
94 if(!@links)
95 {
96 warn "No upload found in import list, upload possibly failed.";
97 }
98 else
99 {
100 foreach my $link (@links)
101 {
102 eval
103 {
104 print "Approve $link upload.\n";
105 $mech->get("https://translations.launchpad.net$link");
106 $mech->form_with_fields("field.potemplate");
107 $mech->select("field.potemplate", "josm");
108 $mech->click_button(name => "field.actions.approve");
109 };
110 print $@ if $@;
111 }
112 }
113}
114
115sub makeupload
116{
117 my $count = 11;
118 my $outdate = `date -u +"%Y-%m-%dT%H_%M_%S"`;
119 my $mech = dologin();
120 $outdate =~ s/[\r\n]+//;
121 mkdir "build/josm";
122 system "cp po/*.po po/josm.pot build/josm";
123 chdir "build";
124 print "Starting upload ($outdate).\n";
125 if(!$count)
126 {
127 system "tar -cjf launchpad_upload_josm_$outdate.tar.bz2 josm";
128 $mech->get("https://translations.launchpad.net/josm/trunk/+translations-upload");
129 $mech->submit_form(with_fields => {"file" => "launchpad_upload_josm_$outdate.tar.bz2"});
130 sleep(10);
131 }
132 else
133 {
134 my @files = sort glob("josm/*.po");
135 my $num = 1;
136 while($#files >= 0)
137 {
138 my @f = splice(@files, 0, $count);
139 my $file = "launchpad_upload_josm_${outdate}_$num.tar.bz2";
140 print "Create file $file.\n";
141 system "tar -cjf $file ".join(" ",@f);
142 print "Upload file $file.\n";
143 $mech->get("https://translations.launchpad.net/josm/trunk/+translations-upload");
144 $mech->submit_form(with_fields => {"file" => $file});
145 sleep(10);
146 ++$num;
147 }
148 }
149 system "rm -rv josm";
150 chdir "..";
151 approveall($mech);
152}
153
154sub copypo
155{
156 my ($path) = @_;
157 foreach my $name (split("\n", `find $path -name "*.po"`))
158 {
159 $name =~ /([a-zA-Z_@]+)\.po/;
160 if($lang{$1})
161 {
162 system("cp", "-v", $name, "po/$1.po");
163 }
164 elsif($cleanall)
165 {
166 local $/; undef $/;
167 open FILE,"<",$name or die;
168 my $x = <FILE>;
169 close FILE;
170 $x =~ s/\n\n.*$/\n/s;
171 open FILE,">","po/$1.po" or die;
172 print FILE $x;
173 close FILE;
174 }
175 }
176}
177
178sub dologin
179{
180 require WWW::Mechanize;
181
182 my $mech = WWW::Mechanize->new("agent" => $agent);
183
184 #$mech->add_handler("request_send" => sub {
185 # my($request, $ua, $h) = @_;
186 # printf "FORM: %s\n", $request->content();
187 # return undef;
188 #});
189 $mech->get("https://translations.launchpad.net/josm/trunk/+login");
190 #print $mech->status() ." - ". $mech->uri()."\n";
191 $mech->submit_form(form_number => 1);
192 getcredits();
193 #print $mech->status() ." - ". $mech->uri()."\n";
194 $mech->submit_form(form_id => "login-form", fields => {"email" => $user, "password" => $pwd});
195 #$mech->dump_headers();
196 #print $mech->status() ." - ". $mech->uri()."\n";
197 #print $mech->content();
198 my $form = $mech->form_name("decideform");
199 die "Could not login.\n" if !$form;
200 my %par = ("yes" => ""); # We need to add "yes" or it does not work
201 foreach my $p ($form->param)
202 {
203 $par{$p} = $form->value($p);
204 }
205 $mech->post($form->action, \%par);
206 #$mech->dump_headers();
207 #print $mech->content();
208 #print $mech->status() ." - ". $mech->uri()."\n";
209 #$mech->dump_forms();
210 return $mech;
211}
212
213sub potupload
214{
215 my $mech = dologin();
216 print "Starting upload.\n";
217 sleep(2);
218 $mech->get("https://translations.launchpad.net/josm/trunk/+translations-upload");
219 chdir("po");
220 $mech->submit_form(with_fields => {"file" => "josm.pot"});
221 print "Start approving of upload.\n";
222 for(1..10)
223 {
224 eval
225 {
226 sleep(10);
227 print "Trying to approve upload.\n";
228 $mech->get("https://translations.launchpad.net/josm/trunk/+imports?field.filter_status=NEEDS_REVIEW&field.filter_extension=pot");
229 my @links;
230 foreach my $line (split("\n", $mech->content()))
231 {
232 if($line =~ /href="(\/\+imports\/\d+)"/)
233 {
234 push (@links, $1);
235 print TXTFILE $line if $debugfile;
236 }
237 }
238 if(!@links)
239 {
240 print TXTFILE $mech->content() if $debugfile;
241 die "Upload not found in import list, upload possibly failed.";
242 }
243 elsif(@links > 1)
244 {
245 die "More than one upload found in import list, cannot approve.";
246 }
247 else
248 {
249 $mech->get("https://translations.launchpad.net$links[0]");
250 if($debugfile)
251 {
252 print TXTFILE "LINK: $links[0]\n";
253 $mech->dump_headers(\*TXTFILE);
254 print TXTFILE $mech->content();
255 print TXTFILE $mech->status() ." - ". $mech->uri()."\n";
256 $mech->dump_forms(\*TXTFILE);
257 }
258 $mech->submit_form(form_name => "launchpadform", button => "field.actions.approve");
259 if(!($mech->content() =~ /There are no entries that match this filtering/))
260 {
261 die "Approving possibly failed.";
262 }
263 }
264 };
265 print $@ if $@;
266 last if !$@;
267 }
268
269 chdir("..");
270}
271
272sub podownload
273{
274 my $mech = dologin();
275 $mech->get("https://translations.launchpad.net/josm/trunk/+export");
276 $mech->submit_form(with_fields => {"format" => "PO"});
277 if(!($mech->content() =~ /receive an email shortly/))
278 {
279 warn "Error requesting file\n";
280 }
281}
282
283sub getcredits
284{
285 if(!$user || !$pwd)
286 {
287 require Term::ReadKey;
288 local undef $/;
289 if(open FILE, "launchpad.pl_credits")
290 {
291 eval <FILE>;
292 close FILE;
293 }
294
295 if(!$user)
296 {
297 Term::ReadKey::ReadMode(4); # Turn off controls keys
298 printf("Enter username: ");
299 for(;;)
300 {
301 my $c = getc();
302 print $c;
303 last if $c eq "\n";
304 $user .= $c;
305 }
306 Term::ReadKey::ReadMode(0);
307 }
308
309 if(!$pwd)
310 {
311 Term::ReadKey::ReadMode(4); # Turn off controls keys
312 printf("Enter password: ");
313 for(;;)
314 {
315 my $c = getc();
316 last if $c eq "\n";
317 print "*";
318 $pwd .= $c;
319 }
320 print "\n";
321 Term::ReadKey::ReadMode(0);
322 }
323 }
324}
325
326sub doget
327{
328 my ($mech, $page, $arg) = @_;
329 for(my $i = 1; $i <= 5; $i++)
330 {
331 $mech->timeout(30);
332 eval
333 {
334 $mech->get($page);
335 };
336 my $code = $mech->status();
337 print "Try $i: ($code) $@" if $@;
338 return $mech if !$@;
339 sleep(30+5*$i);
340 last if $arg && $arg eq "no503" and $code == 503;
341 $mech = WWW::Mechanize->new("agent" => $agent);
342 }
343 return $mech;
344}
345
346sub getstats
347{
348 my %results;
349 require WWW::Mechanize;
350 require Data::Dumper;
351 require URI::Escape;
352 my $mech = WWW::Mechanize->new("agent" => $agent);
353
354 if(open DFILE,"<:utf8","launchpadtrans.data")
355 {
356 local $/;
357 $/ = undef;
358 my $val = <DFILE>;
359 eval $val;
360 close DFILE;
361 }
362
363 binmode STDOUT, ":utf8";
364
365 open FILE,">:utf8","launchpadtrans.txt" or die "Could not open output file.";
366
367 for my $lang (sort keys %lang)
368 {
369 doget($mech, "https://translations.launchpad.net/josm/trunk/+pots/josm/$lang/");
370 sleep(1);
371 my $cont = $mech->content();
372 while($cont =~ /<a href="https?:\/\/launchpad.net\/~(.*?)" class="sprite person(-inactive)?">(.*?)<\/a>/g)
373 {
374 my ($code, $inactive, $name) = ($1, $2, $3);
375 if(exists($results{$code}{$lang}{count}) && exists($results{$code}{$lang}{time}) && time()-$results{$code}{$lang}{time} < 5*24*60*60)
376 {
377 printf "%-5s - %-30s - Found - %s\n", $lang,$code,$name;
378 next;
379 }
380 my $urlcode = URI::Escape::uri_escape($code);
381 $mech = doget($mech, "https://translations.launchpad.net/josm/trunk/+pots/josm/$lang/+filter?person=$urlcode", "no503");
382 sleep(1);
383 my $cont = $mech->content() || "";
384 my ($count) = $cont =~ /of[\r\n\t ]+?(\d+)[\r\n\t ]+?result/;
385 if($count && $mech->status == 200)
386 {
387 my $t = time();
388 my $old = "";
389 if(exists($results{$code}{$lang}{count}))
390 {
391 if($results{$code}{$lang}{count} != $count)
392 {
393 $old .= sprintf " %d %s",abs($count-$results{$code}{$lang}{count}),$count-$results{$code}{$lang}{count} > 0 ? "more" : "less";
394 }
395 else
396 {
397 $old .= " unchanged";
398 }
399 }
400 if(exists($results{$code}{$lang}{time}))
401 {
402 $old .= sprintf " %.2f days later",($t-$results{$code}{$lang}{time})/86400.0;
403 }
404 $results{$code}{NAME} = $name;
405 $results{$code}{TOTAL} += $count-($results{$code}{$lang}{count}||0);
406 $results{$code}{$lang}{count} = $count;
407 $results{$code}{$lang}{time} = $t;
408 $results{$code}{$lang}{$t} = $count;
409 if(open DFILE,">:utf8","launchpadtrans.data")
410 {
411 print DFILE Data::Dumper->Dump([\%results],['*results']);
412 close DFILE;
413 }
414 if($old)
415 {
416 printf "%-5s - %-30s - %5d - %-70s%s\n", $lang,$code,$count,$name,$old;
417 }
418 else
419 {
420 printf "%-5s - %-30s - %5d - %s\n", $lang,$code,$count,$name;
421 }
422 }
423 else
424 {
425 printf "%-5s - %-30s - Skip - %s\n", $lang,$code,$name;
426 }
427 }
428 }
429 for my $code (sort {($results{$b}{TOTAL}||0) <=> ($results{$a}{TOTAL}||0)} keys %results)
430 {
431 next if !$results{$code}{TOTAL};
432 print FILE "$results{$code}{NAME}:$results{$code}{TOTAL}";
433 printf "%5d - %-50s",$results{$code}{TOTAL}, $results{$code}{NAME};
434 for my $lang (sort keys %{$results{$code}})
435 {
436 next if $lang eq "NAME" or $lang eq "TOTAL";
437 next if !exists($results{$code}{$lang}{time}) || time()-$results{$code}{$lang}{time} > 6*24*60*60;
438 print FILE ";$lang=$results{$code}{$lang}{count}";
439 printf " - %-5s=%5d",$lang, $results{$code}{$lang}{count};
440 }
441 print FILE "\n";
442 print "\n";
443 }
444}
Note: See TracBrowser for help on using the repository browser.