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

Side by Side Diff: mozharness/abb/transform_locales.py

Issue 29633713: Issue 6077 - Create script to modify the list of search engines dynamically (Closed)
Patch Set: Created Jan. 10, 2018, 4:14 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 # This file is part of Adblock Plus 1 # This file is part of Adblock Plus
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2006-present 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 json
16 import os 17 import os
17 import re 18 import re
19 import shutil
18 import sys 20 import sys
19 21
20 _LOCALE_RE = re.compile("^([a-z]{2,3}(?:-[A-Z]{2})?)$") 22 _LOCALE_RE = re.compile("^([a-z]{2,3}(?:-[A-Z]{2})?)$")
21 _VALUES_LOCALE_RE = re.compile("^values-([a-z]{2,3}(?:-r[A-Z]{2})?)$") 23 _VALUES_LOCALE_RE = re.compile("^values-([a-z]{2,3}(?:-r[A-Z]{2})?)$")
22 24
25 _SEARCH_PROPS_RE = re.compile("^browser\.search\."
26 "(defaultenginename|order\.).*$")
27 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$")
28
23 _PROPERTY_FORMAT_RE = re.compile("^(([^=]*)=)(.*)$") 29 _PROPERTY_FORMAT_RE = re.compile("^(([^=]*)=)(.*)$")
24 _ENTITY_FORMAT_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$") 30 _ENTITY_FORMAT_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$")
25 _STRING_FORMAT_RE = re.compile( 31 _STRING_FORMAT_RE = re.compile(
26 "^(\s*<string name=\"([^\"]*)\">)(.*)(</string>)$") 32 "^(\s*<string name=\"([^\"]*)\">)(.*)(</string>)$")
27 33
28 _MOZBUILD_PATH = os.path.join("python", "mozbuild") 34 _MOZBUILD_PATH = os.path.join("python", "mozbuild")
29 35
30 _CHROME_PATH = os.path.join("dist", "bin", "chrome") 36 _CHROME_PATH = os.path.join("dist", "bin", "chrome")
31 _RES_PATH = os.path.join("mobile", "android", "base", "res") 37 _RES_PATH = os.path.join("mobile", "android", "base", "res")
38 _L10N_PATH = os.path.join("abb-build", "l10n")
39 _LOCALES_PATH = os.path.join("mobile", "locales")
40 _LISTJSON_PATH = os.path.join(_LOCALES_PATH, "search", "list.json")
41 _GENERAL_SEARCHPLUGINS_PATH = os.path.join(_LOCALES_PATH, "searchplugins")
32 42
33 _BROWSER_DIR = "browser" 43 _BROWSER_DIR = "browser"
44 _REGION_PROPS_PATH = os.path.join(_BROWSER_DIR, "region.properties")
45 _LOCALE_SEARCHPLUGINS_PATH = os.path.join(_BROWSER_DIR, "searchplugins")
34 46
35 _APPSTRINGS_PROPS_PATH = os.path.join(_BROWSER_DIR, "appstrings.properties") 47 _APPSTRINGS_PROPS_PATH = os.path.join(_BROWSER_DIR, "appstrings.properties")
36 _STRINGS_XML_PATH = "strings.xml" 48 _STRINGS_XML_PATH = "strings.xml"
37 49
38 _DEFAULT_LOCALE = "en-US" 50 _DEFAULT_LOCALE = "en-US"
51 _DEF_ENGINES = "visibleDefaultEngines"
52
53 # Add Ecosia as secondary search engine.
54 # See https://issues.adblockplus.org/ticket/5518
55 _ECOSIA_ID = "ecosia"
56 _ECOSIA_PATH = os.path.join(_GENERAL_SEARCHPLUGINS_PATH, "ecosia.xml")
57
58 _SEARCH_ENGINE_ORDER_DEFAULT = [
59 "duckduckgo",
60 "yahoo",
61 "google",
62 "wikipedia",
63 "amazondotcom"]
64
65 _SEARCH_ENGINE_ORDER_ECOSIA = [
66 "duckduckgo",
67 "yahoo",
68 "google",
69 _ECOSIA_ID,
70 "wikipedia",
71 "amazon"]
72
73 _SEARCH_ENGINE_ORDER = {
74 "de": _SEARCH_ENGINE_ORDER_ECOSIA,
75 "en-GB": _SEARCH_ENGINE_ORDER_ECOSIA,
76 "en-US": _SEARCH_ENGINE_ORDER_ECOSIA,
77 "fr": _SEARCH_ENGINE_ORDER_ECOSIA,
78 "nl": _SEARCH_ENGINE_ORDER_ECOSIA,
79 "zh-CN": ["baidu",
80 "duckduckgo",
81 "yahoo",
82 "google",
83 "wikipedia",
84 "amazon"
85 ]
86 }
39 87
40 _FIREFOX_REPLACE_STR = "Firefox" 88 _FIREFOX_REPLACE_STR = "Firefox"
41 _ABB_REPLACEMENT_STR = "Adblock Browser" 89 _ABB_REPLACEMENT_STR = "Adblock Browser"
42 90
43 # Some string values that contain Firefox such as 'Firefox Sync' shouldn't be 91 # Some string values that contain Firefox such as 'Firefox Sync' shouldn't be
44 # replaced, so we keep a list of ids that are exceptions 92 # replaced, so we keep a list of ids that are exceptions
45 _ENTITY_EXCEPTIONS = [ 93 _ENTITY_EXCEPTIONS = [
46 "overlay_no_synced_devices", 94 "overlay_no_synced_devices",
47 "home_remote_tabs_need_to_sign_in", 95 "home_remote_tabs_need_to_sign_in",
48 "home_remote_tabs_need_to_finish_migrating", 96 "home_remote_tabs_need_to_finish_migrating",
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 return None 150 return None
103 151
104 152
105 def _write_lines(filename, lines): 153 def _write_lines(filename, lines):
106 """Writes lines into file appending \\n""" 154 """Writes lines into file appending \\n"""
107 with open(filename, "w") as fd: 155 with open(filename, "w") as fd:
108 for l in lines: 156 for l in lines:
109 fd.write("%s\n" % l) 157 fd.write("%s\n" % l)
110 158
111 159
112 def _transform_locale(locale, path, logger): 160 def _transform_locale(data, locale, project_dir, locale_path, logger):
113 logger.info("Processing locale '%s'..." % locale) 161 logger.info("Processing locale '%s'..." % locale)
114 162
163 # Check for region.properties existence
164 region_file_path = os.path.join(locale_path, _REGION_PROPS_PATH)
165 _check_path_exists(region_file_path, logger)
166
115 # Check for appstrings.properties existence 167 # Check for appstrings.properties existence
116 appstrings_file_path = os.path.join(path, _APPSTRINGS_PROPS_PATH) 168 appstrings_file_path = os.path.join(locale_path, _APPSTRINGS_PROPS_PATH)
117 _check_path_exists(appstrings_file_path, logger) 169 _check_path_exists(appstrings_file_path, logger)
118 170
171 ecosia_dst = os.path.join(locale_path,
172 _LOCALE_SEARCHPLUGINS_PATH, "ecosia.xml")
173
174 # Get whitelist and build regex
175 whitelist = _SEARCH_ENGINE_ORDER.get(locale,
176 _SEARCH_ENGINE_ORDER_DEFAULT)
177 white_re = re.compile("^(%s).*$" % "|".join(whitelist))
178
179 if _ECOSIA_ID in whitelist and not os.path.exists(ecosia_dst):
180 shutil.copyfile(os.path.join(project_dir, _ECOSIA_PATH), ecosia_dst)
181
182 all_engine_ids = []
183 engine_ids = []
184 removed_engine_ids = []
185
186 for item in data['locales'][locale]['default'][_DEF_ENGINES]:
187 all_engine_ids.append(item)
188 if len(item) > 0:
189 if white_re.match(item):
190 engine_ids.append(item)
191 else:
192 removed_engine_ids.append(item)
193
194 if _ECOSIA_ID in whitelist and _ECOSIA_ID not in all_engine_ids:
195 all_engine_ids.append(_ECOSIA_ID)
196 engine_ids.append(_ECOSIA_ID)
197
198 # Make sure we still have search engines left
199 if len(engine_ids) == 0:
200 logger.fatal("No search engines left over for '%s'" % locale)
201
202 data['locales'][locale]['default'][_DEF_ENGINES] = all_engine_ids
203
204 # 'Parse' XML to get matching 'ShortName' for all engine IDs
205 engine_names = {}
206 search_plugins_path = os.path.join(project_dir,
207 _GENERAL_SEARCHPLUGINS_PATH)
208 for eid in engine_ids[:]:
209 xml_file_path = os.path.join(search_plugins_path, "%s.xml" % eid)
210 if not os.path.exists(xml_file_path):
211 logger.info("Missing xml file for plugin %s. Searched in path %s" %
212 (eid, xml_file_path))
213 engine_ids.remove(eid)
214 continue
215 short_name = None
216 with open(xml_file_path, "r") as fd:
217 for line in fd:
218 line = line.strip()
219 match = _SHORTNAME_RE.match(line)
220 if match:
221 short_name = match.group(1).strip()
222
223 if not short_name:
224 logger.fatal("No ShortName defined for '%s' in '%s" %
225 (eid, locale))
226 engine_names[eid] = short_name
227
228 logger.info("Removed search engine IDs: %s" %
229 ", ".join(removed_engine_ids))
230 logger.info("Remaining search engine IDs: %s" % ", ".join(engine_ids))
231
232 # Create search engine order with real engine names
233 engine_order = []
234 for eid in whitelist:
235 sn = _get_shortname_from_id(eid, engine_ids, engine_names)
236 if sn:
237 engine_order.append(sn)
238
239 logger.info("Resulting search engine ordered list: %s" %
240 (", ".join(engine_order)))
241
242 # Read region.properties and remove browser.search.* lines
243 props = []
244 with open(region_file_path, "r") as fd:
245 for line in fd:
246 line = line.rstrip("\r\n")
247 if not _SEARCH_PROPS_RE.match(line.strip()):
248 props.append(line)
249
250 # Append default search engine name
251 props.append("browser.search.defaultenginename=%s" % engine_order[0])
252
253 # Append search engine order
254 for i in range(0, len(engine_order)):
255 props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i]))
256
257 # Write back region.properties
258 _write_lines(region_file_path, props)
259
119 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 260 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in
120 # 'appstrings.properties' 261 # 'appstrings.properties'
121 lines = [] 262 lines = []
122 replacement_count = 0 263 replacement_count = 0
123 264
124 with open(appstrings_file_path, "r") as fd: 265 with open(appstrings_file_path, "r") as fd:
125 for line in fd: 266 for line in fd:
126 line = line.rstrip("\r\n") 267 line = line.rstrip("\r\n")
127 replacement = _replace_in_value(_PROPERTY_FORMAT_RE, line, 268 replacement = _replace_in_value(_PROPERTY_FORMAT_RE, line,
128 _FIREFOX_REPLACE_STR, 269 _FIREFOX_REPLACE_STR,
129 _ABB_REPLACEMENT_STR) 270 _ABB_REPLACEMENT_STR)
130 if replacement: 271 if replacement:
131 line = replacement 272 line = replacement
132 replacement_count += 1 273 replacement_count += 1
133 lines.append(line) 274 lines.append(line)
134 275
135 # Apply changes to appstrings.properties 276 # Apply changes to appstrings.properties
136 _write_lines(appstrings_file_path, lines) 277 _write_lines(appstrings_file_path, lines)
137 logger.info("Replaced %d ocurrences of %s in 'appstrings.properties'" % 278 logger.info("Replaced %d ocurrences of %s in 'appstrings.properties'" %
138 (replacement_count, _FIREFOX_REPLACE_STR)) 279 (replacement_count, _FIREFOX_REPLACE_STR))
139 280
140 281
141 def _generate_browser_search(locale, locale_path, res_path, build_dir): 282 def _generate_browser_search(locale, locale_path, res_path, project_dir):
142 raw_dir = "raw" if locale == _DEFAULT_LOCALE else ( 283 raw_dir = "raw" if locale == _DEFAULT_LOCALE else (
143 "raw-%s" % locale.replace("-", "-r")) 284 "raw-%s" % locale.replace("-", "-r"))
144 285
145 browser_path = os.path.join(locale_path, _BROWSER_DIR) 286 browser_path = os.path.join(locale_path, _BROWSER_DIR)
146 browsersearch_file_path = os.path.join(res_path, raw_dir, 287 browsersearch_file_path = os.path.join(res_path, raw_dir,
147 "browsersearch.json") 288 "browsersearch.json")
148 289
149 sys.path.append(os.path.join(build_dir, _MOZBUILD_PATH)) 290 sys.path.append(os.path.join(project_dir, _MOZBUILD_PATH))
150 import mozbuild.action.generate_browsersearch as generate_browsersearch 291 import mozbuild.action.generate_browsersearch as generate_browsersearch
151 292
152 # Call generate_browsersearch.py script to regenerate 293 # Call generate_browsersearch.py script to regenerate
153 # res/raw-LOCALE/browsersearch.json with the updated search engines 294 # res/raw-LOCALE/browsersearch.json with the updated search engines
154 generate_browsersearch.main(["--verbose", "--srcdir", browser_path, 295 generate_browsersearch.main(["--verbose", "--srcdir", browser_path,
155 browsersearch_file_path]) 296 browsersearch_file_path])
156 297
157 298
299 def _generate_search_json(locale, locale_path, project_dir):
300 script_path = os.path.join(project_dir, "python", "mozbuild",
301 "mozbuild", "action", "generate_searchjson.py")
302 list_json_path = os.path.join(project_dir, _LISTJSON_PATH)
303 searchplugins_path = os.path.join(locale_path, _BROWSER_DIR,
304 "searchplugins", "list.json")
305
306 import subprocess as s
307 # Call generate_searchjson.py script
308 s.check_call(['python', script_path, list_json_path,
309 locale, searchplugins_path])
310
311
158 def _transform_values_locale(locale, path, logger): 312 def _transform_values_locale(locale, path, logger):
159 logger.info("Processing values-%s..." % locale) 313 logger.info("Processing values-%s..." % locale)
160 314
161 # Check for strings.xml existence 315 # Check for strings.xml existence
162 strings_file_path = os.path.join(path, _STRINGS_XML_PATH) 316 strings_file_path = os.path.join(path, _STRINGS_XML_PATH)
163 _check_path_exists(strings_file_path, logger) 317 _check_path_exists(strings_file_path, logger)
164 318
165 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 'strings.xml' 319 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 'strings.xml'
166 lines = [] 320 lines = []
167 replacement_count = 0 321 replacement_count = 0
(...skipping 28 matching lines...) Expand all
196 print "INFO: %s" % s 350 print "INFO: %s" % s
197 351
198 def error(self, s): 352 def error(self, s):
199 print "ERROR: %s" % s 353 print "ERROR: %s" % s
200 354
201 def fatal(self, s): 355 def fatal(self, s):
202 print "FATAL: %s" % s 356 print "FATAL: %s" % s
203 exit(1) 357 exit(1)
204 358
205 359
206 def transform_locales(build_dir, obj_dir, logger=MinimalLogger()): 360 def transform_locales(project_dir, obj_dir, logger=MinimalLogger()):
207 chrome_path = os.path.join(obj_dir, _CHROME_PATH) 361 chrome_path = os.path.join(obj_dir, _CHROME_PATH)
208 _check_path_exists(chrome_path, logger) 362 _check_path_exists(chrome_path, logger)
209 363
210 res_path = os.path.join(obj_dir, _RES_PATH) 364 res_path = os.path.join(obj_dir, _RES_PATH)
211 _check_path_exists(res_path, logger) 365 _check_path_exists(res_path, logger)
212 366
367 list_json_path = os.path.join(project_dir, _LISTJSON_PATH)
368 _check_path_exists(list_json_path, logger)
369
213 locales = _get_locales_from_path(chrome_path, _LOCALE_RE) 370 locales = _get_locales_from_path(chrome_path, _LOCALE_RE)
214 values_locales = _get_locales_from_path(res_path, _VALUES_LOCALE_RE) 371 values_locales = _get_locales_from_path(res_path, _VALUES_LOCALE_RE)
215 372
216 locales_found_msg = "Found %d locales in %s" 373 locales_found_msg = "Found %d locales in %s"
217 logger.info(locales_found_msg % (len(locales), chrome_path)) 374 logger.info(locales_found_msg % (len(locales), chrome_path))
218 logger.info(locales_found_msg % (len(values_locales), res_path)) 375 logger.info(locales_found_msg % (len(values_locales), res_path))
219 376
220 for locale in locales: 377 # open the Mozilla list of search engines, put it into a buffer and
221 locale_path = os.path.join(chrome_path, locale, "locale", locale) 378 # close the JSON file after reading
222 if os.path.exists(locale_path): 379 with open(list_json_path, 'r') as json_file:
223 _transform_locale(locale, locale_path, logger) 380 data = json.load(json_file)
224 _generate_browser_search(locale, locale_path, res_path, build_dir)
225 else:
226 logger.error("Missing folder for locale '%s' in path: %s" %
227 (locale, locale_path))
228 381
229 for locale in values_locales: 382 # set default search engine order
230 locale_path = os.path.join(res_path, "values-" + locale) 383 data['default'][_DEF_ENGINES] = _SEARCH_ENGINE_ORDER_DEFAULT
231 _transform_values_locale(locale, locale_path, logger) 384
385 for locale in locales:
386 locale_path = os.path.join(chrome_path, locale, "locale", locale)
387 if os.path.exists(locale_path):
388
389 # Mozilla default list does not contain locale bn-BD,
390 # so we create it and use the values from locale bn-IN
391 if locale == 'bn-BD':
392 data['locales'].update({locale: {'default':
393 {_DEF_ENGINES:
394 data['locales']['bn-IN']['default'][_DEF_ENGINES]}}})
395 # Mozilla default list does not contain locale wo, so we use
396 # the default order. In case they will not support any other
397 # locales in the future, we want the build to fail, to decide
398 # which order to use
399 elif locale == 'wo':
400 data['locales'].update({locale: {'default':
401 {_DEF_ENGINES:
402 _SEARCH_ENGINE_ORDER_DEFAULT}}})
403
404 _transform_locale(data, locale, project_dir, locale_path,
405 logger)
406 _generate_browser_search(locale, locale_path, res_path,
407 project_dir)
408 else:
409 logger.error("Missing folder for locale '%s' in path: %s" %
410 (locale, locale_path))
411
412 # Save changes to list.json
413 with open(list_json_path, 'w') as outfile:
414 json.dump(data, outfile, indent=4, sort_keys=True)
415
416 # Generate search.json for each locale
417 for locale in locales:
418 locale_path = os.path.join(chrome_path, locale, "locale", locale)
419 _generate_search_json(locale, locale_path, project_dir)
420
421 for locale in values_locales:
422 locale_path = os.path.join(res_path, "values-" + locale)
423 _transform_values_locale(locale, locale_path, logger)
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