LEFT | RIGHT |
1 # This file is part of Adblock Plus | 1 # This file is part of Adblock Plus |
2 # Copyright (C) 2006-2015 Eyeo GmbH | 2 # Copyright (C) 2006-2015 Eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
16 import os | 16 import os |
17 import re | 17 import re |
18 | 18 |
19 _LOCALE_RE = re.compile("^[a-z]{2}(-[A-Z]{2})?$") | 19 _LOCALE_RE = re.compile("^[a-z]{2}(-[A-Z]{2})?$") |
20 _SEARCH_PROPS_RE = re.compile("^browser\.search\." | 20 _SEARCH_PROPS_RE = re.compile("^browser\.search\." |
21 "(defaultenginename|order\.).*$") | 21 "(defaultenginename|order\.).*$") |
22 _SHORTNAME_RE = re.compile("^<ShortName>.*</ShortName>$") | 22 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$") |
23 | 23 |
24 _SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") | 24 _SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") |
25 _LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt") | 25 _LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt") |
26 _REGION_PROPS_PATH = os.path.join("browser", "region.properties") | 26 _REGION_PROPS_PATH = os.path.join("browser", "region.properties") |
27 | 27 |
28 _DEFAULT_LOCALE = "en-US" | 28 _DEFAULT_LOCALE = "en-US" |
29 | 29 |
30 _SEARCH_ENGINE_ORDER = { | 30 _SEARCH_ENGINE_ORDER = { |
31 "en-US": ["duckduckgo", | 31 "en-US": ["duckduckgo", |
32 "yahoo", | 32 "yahoo", |
33 "google", | 33 "google", |
34 "wikipedia", | 34 "wikipedia", |
35 "amazon" | 35 "amazon" |
36 ], | 36 ], |
37 "zh-CN": ["baidu", | 37 "zh-CN": ["baidu", |
38 "duckduckgo", | 38 "duckduckgo", |
39 "yahoo", | 39 "yahoo", |
40 "google", | 40 "google", |
41 "wikipedia", | 41 "wikipedia", |
42 "amazon" | 42 "amazon" |
43 ] | 43 ] |
44 } | 44 } |
45 | 45 |
46 | 46 |
47 def _get_shortname_from_id(needle, engine_ids, engine_names): | 47 def _get_shortname_from_id(needle, engine_ids, engine_names): |
48 """Fuzzy finds needle in engine_ids and returns ShortName""" | 48 """Fuzzy finds needle in engine_ids and returns ShortName""" |
49 regex = re.compile("^%s.*$" % needle) | |
50 for engine in engine_ids: | 49 for engine in engine_ids: |
51 if regex.match(engine.lower()): | 50 if engine.startswith(needle): |
52 return engine_names[engine] | 51 return engine_names[engine] |
53 return None | 52 return None |
54 | 53 |
55 | 54 |
56 def _write_lines(filename, lines): | 55 def _write_lines(filename, lines): |
57 """Writes lines into file appending \\n""" | 56 """Writes lines into file appending \\n""" |
58 with open(filename, "w") as fd: | 57 with open(filename, "w") as fd: |
59 for l in lines: | 58 for l in lines: |
60 fd.write("%s\n" % l) | 59 fd.write("%s\n" % l) |
61 | 60 |
(...skipping 15 matching lines...) Expand all Loading... |
77 whitelist = _SEARCH_ENGINE_ORDER.get(locale, | 76 whitelist = _SEARCH_ENGINE_ORDER.get(locale, |
78 _SEARCH_ENGINE_ORDER[_DEFAULT_LOCALE]) | 77 _SEARCH_ENGINE_ORDER[_DEFAULT_LOCALE]) |
79 white_re = re.compile("^(%s).*$" % "|".join(whitelist)) | 78 white_re = re.compile("^(%s).*$" % "|".join(whitelist)) |
80 | 79 |
81 # Read engine IDs from list.txt, discard engines not on whitelist | 80 # Read engine IDs from list.txt, discard engines not on whitelist |
82 engine_ids = [] | 81 engine_ids = [] |
83 with open(list_file_path, "r") as fd: | 82 with open(list_file_path, "r") as fd: |
84 for line in fd: | 83 for line in fd: |
85 line = line.strip() | 84 line = line.strip() |
86 if len(line) > 0: | 85 if len(line) > 0: |
87 if white_re.match(line.lower()): | 86 if white_re.match(line): |
88 engine_ids.append(line) | 87 engine_ids.append(line) |
89 else: | 88 else: |
90 logger.info("Removing '%s'" % line) | 89 logger.info("Removing '%s'" % line) |
91 | 90 |
92 # Make sure we still have search engines left | 91 # Make sure we still have search engines left |
93 if len(engine_ids) == 0: | 92 if len(engine_ids) == 0: |
94 logger.fatal("No search engines left over for '%s'" % locale) | 93 logger.fatal("No search engines left over for '%s'" % locale) |
95 | 94 |
96 # 'Parse' XML to get matching 'ShortName' for all engine IDs | 95 # 'Parse' XML to get matching 'ShortName' for all engine IDs |
97 engine_names = {} | 96 engine_names = {} |
98 for eid in engine_ids: | 97 for eid in engine_ids: |
99 xml_file_path = os.path.join(path, _SEARCHPLUGINS_PATH, "%s.xml" % eid) | 98 xml_file_path = os.path.join(path, _SEARCHPLUGINS_PATH, "%s.xml" % eid) |
100 if os.path.exists(xml_file_path): | 99 if os.path.exists(xml_file_path): |
101 short_name = None | 100 short_name = None |
102 with open(xml_file_path, "r") as fd: | 101 with open(xml_file_path, "r") as fd: |
103 for line in fd: | 102 for line in fd: |
104 line = line.strip() | 103 line = line.strip() |
105 if _SHORTNAME_RE.match(line): | 104 match = _SHORTNAME_RE.match(line) |
106 short_name = line[11:-12].trim() | 105 if match: |
| 106 short_name = match.group(1).strip() |
107 | 107 |
108 if not short_name: | 108 if not short_name: |
109 logger.fatal("No ShortName defined for '%s' in '%s" % | 109 logger.fatal("No ShortName defined for '%s' in '%s" % |
110 (eid, locale)) | 110 (eid, locale)) |
111 engine_names[eid] = short_name | 111 engine_names[eid] = short_name |
112 else: | 112 else: |
113 logger.fatal("XML definiton for '%s' in '%s' missing" % | 113 logger.fatal("XML definiton for '%s' in '%s' missing" % |
114 (eid, locale)) | 114 (eid, locale)) |
115 | 115 |
116 logger.info("Remaining engine IDs: %s" % ", ".join(engine_ids)) | 116 logger.info("Remaining engine IDs: %s" % ", ".join(engine_ids)) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 print "INFO: %s" % s | 151 print "INFO: %s" % s |
152 | 152 |
153 def error(self, s): | 153 def error(self, s): |
154 print "ERROR: %s" % s | 154 print "ERROR: %s" % s |
155 | 155 |
156 def fatal(self, s): | 156 def fatal(self, s): |
157 print "FATAL: %s" % s | 157 print "FATAL: %s" % s |
158 exit(1) | 158 exit(1) |
159 | 159 |
160 | 160 |
161 def transform_locales(build_object, obj_dir): | 161 def transform_locales(obj_dir, logger=MinimalLogger()): |
162 logger = build_object or MinimalLogger() | |
163 | |
164 chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome") | 162 chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome") |
165 if not os.path.exists(chrome_path): | 163 if not os.path.exists(chrome_path): |
166 logger.fatal("'%s' does not exist" % obj_dir) | 164 logger.fatal("'%s' does not exist" % obj_dir) |
167 | 165 |
168 locales = [] | 166 locales = [] |
169 for p in next(os.walk(chrome_path))[1]: | 167 for p in next(os.walk(chrome_path))[1]: |
170 if _LOCALE_RE.match(p): | 168 if _LOCALE_RE.match(p): |
171 locales.append(p) | 169 locales.append(p) |
172 locales.sort() | 170 locales.sort() |
173 | 171 |
174 logger.info("Found %d locales" % len(locales)) | 172 logger.info("Found %d locales" % len(locales)) |
175 | 173 |
176 for locale in locales: | 174 for locale in locales: |
177 locale_path = os.path.join(chrome_path, locale, "locale", locale) | 175 locale_path = os.path.join(chrome_path, locale, "locale", locale) |
178 if os.path.exists(locale_path): | 176 if os.path.exists(locale_path): |
179 _transform_locale(locale, locale_path, logger) | 177 _transform_locale(locale, locale_path, logger) |
180 else: | 178 else: |
181 logger.error("Missing 'locale' folder for '%s'" % locale) | 179 logger.error("Missing 'locale' folder for '%s'" % locale) |
182 | 180 |
LEFT | RIGHT |