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

Last change on this file since 30744 was 30399, checked in by stoecker, 11 years ago

remove outdated translations

  • Property svn:executable set to *
  • Property svn:keywords set to Revision
File size: 12.2 KB
RevLine 
[16400]1#!/usr/bin/perl -w
[27208]2# -CDSL would be better than explicit encoding settings
[16400]3
4use strict;
[27210]5use utf8;
[16400]6
[27020]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!)
[28253]11 # create a file "launchpad.pl_credits" containing the two lines with proper values
[27020]12 # leave credits empty and enter them on runtime
13$user = '';
14$pwd = '';
15
16# list of supported languages
[26928]17my %lang = map {$_ => 1} (
[28409]18"bg", "ca", "cs", "da", "de", "el", "en_AU", "en_GB",
[30399]19"es", "et", "fi", "fr", "gl", "hu", "id",
20"it", "ja", "nl", "pl", "pt", "pt_BR", "ru", "sk",
21"sv", "uk", "zh_CN", "zh_TW"
[26928]22);
23
[27210]24my $revision = '$Revision: 30399 $';
25$revision =~ s/^.*?(\d+).*$/$1/;
[27208]26my $agent = "JOSM_Launchpad/1.$revision";
27
[30179]28my $debugfile = 0;#1;
[25528]29my $cleanall = 0;#1;
[16400]30
[30179]31open TXTFILE,">","launchpad_debug.log" or die if $debugfile;
32
[16973]33if($#ARGV != 0)
[16400]34{
[27020]35 warn "No argument given (try Launchpad download URL, \"bzr\", \"bzronly\", \"upload\" or \"download\").";
36 system "ant";
[16400]37}
[26928]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";
[27020]46 if($ARGV[0] ne "bzronly")
47 {
48 system "ant";
49 }
[26928]50}
[27020]51elsif($ARGV[0] eq "upload")
52{
53 potupload();
54}
[28769]55elsif($ARGV[0] eq "uploadall")
56{
57 makeupload();
58}
59elsif($ARGV[0] eq "approveall")
60{
[28806]61 approveall(dologin());
[28769]62}
[27020]63elsif($ARGV[0] eq "download")
64{
65 podownload();
66}
[27208]67elsif($ARGV[0] eq "stats")
68{
69 getstats();
70}
[16973]71else
72{
[26849]73 mkdir "build";
[26928]74 mkdir "build/josm_trans";
75 die "Could not change into new data dir." if !chdir "build/josm_trans";
[16973]76 system "wget $ARGV[0]";
77 system "tar -xf laun*";
[26849]78 chdir "../..";
[26928]79 copypo("build/josm_trans");
80 system "rm -rv build/josm_trans";
[27020]81 system "ant";
[26928]82}
83
[28769]84sub approveall
[26928]85{
[28769]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()))
[16973]91 {
[28769]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)
[27020]101 {
[28769]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 $@;
[27020]111 }
[26928]112 }
113}
114
[28769]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
[26928]154sub copypo
155{
156 my ($path) = @_;
157 foreach my $name (split("\n", `find $path -name "*.po"`))
158 {
[27020]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 }
[16973]175 }
176}
[27020]177
178sub dologin
179{
180 require WWW::Mechanize;
181
[27208]182 my $mech = WWW::Mechanize->new("agent" => $agent);
[27020]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(with_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");
[27026]199 die "Could not login.\n" if !$form;
[27020]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();
[28491]216 print "Starting upload.\n";
[28490]217 sleep(2);
[27020]218 $mech->get("https://translations.launchpad.net/josm/trunk/+translations-upload");
219 chdir("po");
220 $mech->submit_form(with_fields => {"file" => "josm.pot"});
[30179]221 print "Start approving of upload.\n";
222 for(1..10)
[28424]223 {
[30179]224 eval
[28424]225 {
[30179]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 !$@;
[28424]267 }
268
[27020]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}
[27208]325
326sub doget
327{
[27211]328 my ($mech, $page, $arg) = @_;
329 for(my $i = 1; $i <= 5; $i++)
[27208]330 {
[27210]331 $mech->timeout(30);
[27208]332 eval
333 {
334 $mech->get($page);
335 };
[27210]336 my $code = $mech->status();
337 print "Try $i: ($code) $@" if $@;
[27208]338 return $mech if !$@;
339 sleep(30+5*$i);
[27211]340 last if $arg && $arg eq "no503" and $code == 503;
[27208]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 {
[27210]369 doget($mech, "https://translations.launchpad.net/josm/trunk/+pots/josm/$lang/");
[27208]370 sleep(1);
[27210]371 my $cont = $mech->content();
[27208]372 while($cont =~ /<a href="https?:\/\/launchpad.net\/~(.*?)" class="sprite person(-inactive)?">(.*?)<\/a>/g)
373 {
374 my ($code, $inactive, $name) = ($1, $2, $3);
[30229]375 if(exists($results{$code}{$lang}{count}) && exists($results{$code}{$lang}{time}) && time()-$results{$code}{$lang}{time} < 5*24*60*60)
[27208]376 {
377 printf "%-5s - %-30s - Found - %s\n", $lang,$code,$name;
378 next;
379 }
380 my $urlcode = URI::Escape::uri_escape($code);
[27211]381 $mech = doget($mech, "https://translations.launchpad.net/josm/trunk/+pots/josm/$lang/+filter?person=$urlcode", "no503");
[27208]382 sleep(1);
[27211]383 my $cont = $mech->content() || "";
384 my ($count) = $cont =~ /of[\r\n\t ]+?(\d+)[\r\n\t ]+?result/;
[27208]385 if($count && $mech->status == 200)
386 {
[30229]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 }
[27208]404 $results{$code}{NAME} = $name;
[30229]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;
[27208]409 if(open DFILE,">:utf8","launchpadtrans.data")
410 {
411 print DFILE Data::Dumper->Dump([\%results],['*results']);
412 close DFILE;
413 }
[30229]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 }
[27208]422 }
423 else
424 {
425 printf "%-5s - %-30s - Skip - %s\n", $lang,$code,$name;
426 }
427 }
428 }
[30233]429 for my $code (sort {($results{$b}{TOTAL}||0) <=> ($results{$a}{TOTAL}||0)} keys %results)
[27208]430 {
[30233]431 next if !$results{$code}{TOTAL};
[27208]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";
[30229]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};
[27208]440 }
441 print FILE "\n";
442 print "\n";
443 }
444}
Note: See TracBrowser for help on using the repository browser.