Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: translations.py

Issue 29325540: Issue 2977 - Don't add redundant translations (Closed)
Patch Set: Created Sept. 2, 2015, 12:25 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import filecmp
3 import os 4 import os
4 import re 5 import re
6 import shutil
5 import sys 7 import sys
6 import urllib2 8 import urllib2
7 from StringIO import StringIO 9 from StringIO import StringIO
8 from zipfile import ZipFile 10 from zipfile import ZipFile
9 11
10 import buildtools.localeTools 12 import buildtools.localeTools
11 13
12 BASE_PATH = os.path.dirname(os.path.abspath(__file__)) 14 BASE_PATH = os.path.dirname(os.path.abspath(__file__))
13 LOCALES_PATH = os.path.join(BASE_PATH, "mobile", "android", "base", "locales", 15 LOCALES_PATH = os.path.join(BASE_PATH, "mobile", "android", "base", "locales",
14 "adblockbrowser") 16 "adblockbrowser")
15 PROJECT_URL = "http://api.crowdin.net/api/project/adblockbrowserandroid" 17 PROJECT_URL = "http://api.crowdin.net/api/project/adblockbrowserandroid"
18 DEFAULT_LOCALE = "en-US"
16 19
17 def print_usage(): 20 def print_usage():
18 print >>sys.stderr, "Usage: %s get API_KEY" % os.path.basename(sys.argv[0]) 21 print >>sys.stderr, "Usage: %s get API_KEY" % os.path.basename(sys.argv[0])
19 22
20 def get_translations(api_key): 23 def get_translations(api_key):
21 # We could use buildtools.localeTools.getTranslations here if we were 24 # We could use buildtools.localeTools.getTranslations here if we were
22 # submitting JSON wrappers for DTDs, the same way ABP for Firefox does it. 25 # submitting JSON wrappers for DTDs, the same way ABP for Firefox does it.
23 # See https://issues.adblockplus.org/2911. 26 # See https://issues.adblockplus.org/2911.
24 27
25 result = urllib2.urlopen("%s/export?key=%s" % (PROJECT_URL, api_key)) 28 result = urllib2.urlopen("%s/export?key=%s" % (PROJECT_URL, api_key))
26 if result.read().find("<success") < 0: 29 if result.read().find("<success") < 0:
27 raise Exception("Export failed: " + result) 30 raise Exception("Export failed: " + result)
28 31
29 result = urllib2.urlopen("%s/download/all.zip?key=%s" % \ 32 result = urllib2.urlopen("%s/download/all.zip?key=%s" % \
30 (PROJECT_URL, api_key)) 33 (PROJECT_URL, api_key))
31 zip = ZipFile(StringIO(result.read())) 34 zip = ZipFile(StringIO(result.read()))
32 for info in zip.infolist(): 35 for info in zip.infolist():
33 if not info.filename.endswith(".dtd"): 36 if not info.filename.endswith(".dtd"):
34 continue 37 continue
35 38
36 dir, file = os.path.split(info.filename) 39 dir, file = os.path.split(info.filename)
37 if not re.match(r"^[\w\-]+$", dir) or dir == "en-US": 40 if not re.match(r"^[\w\-]+$", dir) or dir == DEFAULT_LOCALE:
38 continue 41 continue
39 42
40 mapping = buildtools.localeTools.langMappingGecko 43 mapping = buildtools.localeTools.langMappingGecko
41 for key, value in mapping.iteritems(): 44 for key, value in mapping.iteritems():
42 if value == dir: 45 if value == dir:
43 dir = key 46 dir = key
44 47
45 data = zip.open(info.filename).read() 48 data = zip.open(info.filename).read()
46 if not data: 49 if not data:
47 continue 50 continue
48 51
49 path = os.path.join(LOCALES_PATH, dir, file) 52 path = os.path.join(LOCALES_PATH, dir, file)
50 if not os.path.exists(os.path.dirname(path)): 53 if not os.path.exists(os.path.dirname(path)):
51 os.makedirs(os.path.dirname(path)) 54 os.makedirs(os.path.dirname(path))
52 with open(path, "w") as file: 55 with open(path, "w") as file:
53 file.write(data) 56 file.write(data)
54 57
58 def remove_redundant_translations():
Felix Dahlke 2015/09/02 12:29:16 It feels a bit unelegant to do this as a separate
59 default_locale_path = os.path.join(LOCALES_PATH, DEFAULT_LOCALE)
60 for locale in os.listdir(LOCALES_PATH):
61 if locale == DEFAULT_LOCALE:
62 continue
63 locale_path = os.path.join(LOCALES_PATH, locale)
64 if not filecmp.dircmp(locale_path, default_locale_path).diff_files:
65 shutil.rmtree(locale_path)
66
55 if __name__ == "__main__": 67 if __name__ == "__main__":
56 if len(sys.argv) < 3: 68 if len(sys.argv) < 3:
57 print_usage() 69 print_usage()
58 sys.exit(1) 70 sys.exit(1)
59 71
60 command, api_key = sys.argv[1:] 72 command, api_key = sys.argv[1:]
61 if command != "get": 73 if command != "get":
62 print_usage() 74 print_usage()
63 sys.exit(2) 75 sys.exit(2)
64 76
65 get_translations(api_key) 77 get_translations(api_key)
78 remove_redundant_translations()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld