OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import filecmp | 3 import filecmp |
4 import os | 4 import os |
5 import re | 5 import re |
6 import shutil | 6 import shutil |
7 import sys | 7 import sys |
8 import urllib2 | 8 import urllib2 |
| 9 from lxml import etree |
9 from StringIO import StringIO | 10 from StringIO import StringIO |
10 from zipfile import ZipFile | 11 from zipfile import ZipFile |
11 | 12 |
12 import buildtools.localeTools | 13 import buildtools.localeTools |
13 | 14 |
14 BASE_PATH = os.path.dirname(os.path.abspath(__file__)) | 15 BASE_PATH = os.path.dirname(os.path.abspath(__file__)) |
15 LOCALES_PATH = os.path.join(BASE_PATH, "mobile", "android", "base", "locales", | 16 LOCALES_PATH = os.path.join(BASE_PATH, "mobile", "android", "base", "locales", |
16 "adblockbrowser") | 17 "adblockbrowser") |
17 PROJECT_URL = "http://api.crowdin.net/api/project/adblockbrowserandroid" | 18 PROJECT_URL = "http://api.crowdin.net/api/project/adblockbrowserandroid" |
18 DEFAULT_LOCALE = "en-US" | 19 DEFAULT_LOCALE = "en-US" |
19 | 20 |
20 | 21 |
21 def print_usage(): | 22 def print_usage(): |
22 print >>sys.stderr, "Usage: %s get API_KEY" % os.path.basename(sys.argv[0]) | 23 print >>sys.stderr, "Usage: %s get API_KEY" % os.path.basename(sys.argv[0]) |
23 | 24 |
24 | 25 |
25 def get_translations(api_key): | 26 def get_translations(api_key): |
26 # We could use buildtools.localeTools.getTranslations here if we were | 27 # We could use buildtools.localeTools.getTranslations here if we were |
27 # submitting JSON wrappers for DTDs, the same way ABP for Firefox does it. | 28 # submitting JSON wrappers for DTDs, the same way ABP for Firefox does it. |
28 # See https://issues.adblockplus.org/2911. | 29 # See https://issues.adblockplus.org/2911. |
29 | 30 |
30 result = urllib2.urlopen("%s/export?key=%s" % (PROJECT_URL, api_key)) | 31 result = urllib2.urlopen("%s/export?key=%s" % (PROJECT_URL, api_key)) |
31 if result.read().find("<success") < 0: | 32 if result.read().find("<success") < 0: |
32 raise Exception("Export failed: " + result) | 33 raise Exception("Export failed: " + result) |
33 | 34 |
34 result = urllib2.urlopen("%s/download/all.zip?key=%s" % | 35 result = urllib2.urlopen("%s/download/all.zip?key=%s" % |
35 (PROJECT_URL, api_key)) | 36 (PROJECT_URL, api_key)) |
36 zip = ZipFile(StringIO(result.read())) | 37 zip = ZipFile(StringIO(result.read())) |
| 38 progress_for_translation = get_progress_for_translation_dictionary(api_key) |
37 for info in zip.infolist(): | 39 for info in zip.infolist(): |
| 40 # skip languages without translations |
| 41 index = info.filename.index('/') |
| 42 if progress_for_translation[info.filename[:index]] == '0': |
| 43 continue |
| 44 |
38 if not info.filename.endswith(".dtd"): | 45 if not info.filename.endswith(".dtd"): |
39 continue | 46 continue |
40 | 47 |
41 dir, file = os.path.split(info.filename) | 48 dir, file = os.path.split(info.filename) |
42 if not re.match(r"^[\w\-]+$", dir) or dir == DEFAULT_LOCALE: | 49 if not re.match(r"^[\w\-]+$", dir) or dir == DEFAULT_LOCALE: |
43 continue | 50 continue |
44 | 51 |
45 mapping = buildtools.localeTools.langMappingGecko | 52 mapping = buildtools.localeTools.langMappingGecko |
46 for key, value in mapping.iteritems(): | 53 for key, value in mapping.iteritems(): |
47 if value == dir: | 54 if value == dir: |
48 dir = key | 55 dir = key |
49 | 56 |
50 data = zip.open(info.filename).read() | 57 data = zip.open(info.filename).read() |
51 if not data: | 58 if not data: |
52 continue | 59 continue |
53 | 60 |
54 path = os.path.join(LOCALES_PATH, dir, file) | 61 path = os.path.join(LOCALES_PATH, dir, file) |
55 if not os.path.exists(os.path.dirname(path)): | 62 if not os.path.exists(os.path.dirname(path)): |
56 os.makedirs(os.path.dirname(path)) | 63 os.makedirs(os.path.dirname(path)) |
57 with open(path, "w") as file: | 64 with open(path, "w") as file: |
58 file.write(data) | 65 file.write(data) |
59 | 66 |
| 67 def get_progress_for_translation_dictionary(api_key): |
| 68 parser = etree.HTMLParser() |
| 69 f = urllib2.urlopen('{}/status?key={}'.format(PROJECT_URL, api_key)) |
| 70 tree = etree.parse(f, parser) |
| 71 languages = {} |
| 72 for language in tree.iter(): |
| 73 languages.update({ |
| 74 language.findtext('code'): language.findtext('translated_progress') |
| 75 }) |
| 76 return languages |
60 | 77 |
61 def remove_redundant_translations(): | 78 def remove_redundant_translations(): |
62 default_locale_path = os.path.join(LOCALES_PATH, DEFAULT_LOCALE) | 79 default_locale_path = os.path.join(LOCALES_PATH, DEFAULT_LOCALE) |
63 for locale in os.listdir(LOCALES_PATH): | 80 for locale in os.listdir(LOCALES_PATH): |
64 if locale == DEFAULT_LOCALE: | 81 if locale == DEFAULT_LOCALE: |
65 continue | 82 continue |
66 locale_path = os.path.join(LOCALES_PATH, locale) | 83 locale_path = os.path.join(LOCALES_PATH, locale) |
67 if not filecmp.dircmp(locale_path, default_locale_path).diff_files: | 84 if not filecmp.dircmp(locale_path, default_locale_path).diff_files: |
68 shutil.rmtree(locale_path) | 85 shutil.rmtree(locale_path) |
69 | 86 |
70 if __name__ == "__main__": | 87 if __name__ == "__main__": |
71 if len(sys.argv) < 3: | 88 if len(sys.argv) < 3: |
72 print_usage() | 89 print_usage() |
73 sys.exit(1) | 90 sys.exit(1) |
74 | 91 |
75 command, api_key = sys.argv[1:] | 92 command, api_key = sys.argv[1:] |
76 if command != "get": | 93 if command != "get": |
77 print_usage() | 94 print_usage() |
78 sys.exit(2) | 95 sys.exit(2) |
79 | 96 |
80 get_translations(api_key) | 97 get_translations(api_key) |
81 remove_redundant_translations() | 98 remove_redundant_translations() |
OLD | NEW |