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

Delta Between Two Patch Sets: mozharness/abb/transform_locales.py

Issue 29633713: Issue 6077 - Create script to modify the list of search engines dynamically (Closed)
Left Patch Set: refactored _transform_locales() Created Dec. 19, 2017, 4:49 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 json
17 import os 17 import os
18 import re 18 import re
19 import shutil
19 import sys 20 import sys
20 21
21 _LOCALE_RE = re.compile("^([a-z]{2,3}(?:-[A-Z]{2})?)$") 22 _LOCALE_RE = re.compile("^([a-z]{2,3}(?:-[A-Z]{2})?)$")
22 _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})?)$")
23 24
24 _SEARCH_PROPS_RE = re.compile("^browser\.search\." 25 _SEARCH_PROPS_RE = re.compile("^browser\.search\."
25 "(defaultenginename|order\.).*$") 26 "(defaultenginename|order\.).*$")
26 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$") 27 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$")
27 28
28 _PROPERTY_FORMAT_RE = re.compile("^(([^=]*)=)(.*)$") 29 _PROPERTY_FORMAT_RE = re.compile("^(([^=]*)=)(.*)$")
29 _ENTITY_FORMAT_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$") 30 _ENTITY_FORMAT_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$")
30 _STRING_FORMAT_RE = re.compile( 31 _STRING_FORMAT_RE = re.compile(
31 "^(\s*<string name=\"([^\"]*)\">)(.*)(</string>)$") 32 "^(\s*<string name=\"([^\"]*)\">)(.*)(</string>)$")
32 33
33 _MOZBUILD_PATH = os.path.join("python", "mozbuild") 34 _MOZBUILD_PATH = os.path.join("python", "mozbuild")
34 35
35 _CHROME_PATH = os.path.join("dist", "bin", "chrome") 36 _CHROME_PATH = os.path.join("dist", "bin", "chrome")
36 _RES_PATH = os.path.join("mobile", "android", "base", "res") 37 _RES_PATH = os.path.join("mobile", "android", "base", "res")
37 _L10N_PATH = os.path.join("abb-build", "l10n") 38 _L10N_PATH = os.path.join("abb-build", "l10n")
39 _LOCALES_PATH = os.path.join("mobile", "locales")
38 _LISTJSON_PATH = os.path.join(_LOCALES_PATH, "search", "list.json") 40 _LISTJSON_PATH = os.path.join(_LOCALES_PATH, "search", "list.json")
diegocarloslima 2018/01/03 14:25:07 This seems to cause an error, since _LOCALES_PATH
39 _LOCALES_PATH = os.path.join("mobile", "locales") 41 _GENERAL_SEARCHPLUGINS_PATH = os.path.join(_LOCALES_PATH, "searchplugins")
40 _SEARCHPLUGINS_PATH = os.path.join(_LOCALES_PATH, "searchplugins")
41 42
42 _BROWSER_DIR = "browser" 43 _BROWSER_DIR = "browser"
43 _REGION_PROPS_PATH = os.path.join(_BROWSER_DIR, "region.properties") 44 _REGION_PROPS_PATH = os.path.join(_BROWSER_DIR, "region.properties")
45 _LOCALE_SEARCHPLUGINS_PATH = os.path.join(_BROWSER_DIR, "searchplugins")
44 46
45 _APPSTRINGS_PROPS_PATH = os.path.join(_BROWSER_DIR, "appstrings.properties") 47 _APPSTRINGS_PROPS_PATH = os.path.join(_BROWSER_DIR, "appstrings.properties")
46 _STRINGS_XML_PATH = "strings.xml" 48 _STRINGS_XML_PATH = "strings.xml"
47 49
48 _DEFAULT_LOCALE = "en-US" 50 _DEFAULT_LOCALE = "en-US"
51 _DEF_ENGINES = "visibleDefaultEngines"
49 52
50 # Add Ecosia as secondary search engine. 53 # Add Ecosia as secondary search engine.
51 # See https://issues.adblockplus.org/ticket/5518 54 # See https://issues.adblockplus.org/ticket/5518
52 _ECOSIA_ID = "ecosia" 55 _ECOSIA_ID = "ecosia"
56 _ECOSIA_PATH = os.path.join(_GENERAL_SEARCHPLUGINS_PATH, "ecosia.xml")
53 57
54 _SEARCH_ENGINE_ORDER_DEFAULT = [ 58 _SEARCH_ENGINE_ORDER_DEFAULT = [
55 "duckduckgo", 59 "duckduckgo",
56 "yahoo", 60 "yahoo",
57 "google", 61 "google",
58 "wikipedia", 62 "wikipedia",
59 "amazondotcom"] 63 "amazondotcom"]
60 64
61 _SEARCH_ENGINE_ORDER_ECOSIA = [ 65 _SEARCH_ENGINE_ORDER_ECOSIA = [
62 "duckduckgo", 66 "duckduckgo",
63 "yahoo", 67 "yahoo",
64 "google", 68 "google",
65 "ecosia", 69 _ECOSIA_ID,
66 "wikipedia", 70 "wikipedia",
67 "amazon"] 71 "amazon"]
68 72
69 _SEARCH_ENGINE_ORDER = { 73 _SEARCH_ENGINE_ORDER = {
70 "de": _SEARCH_ENGINE_ORDER_ECOSIA, 74 "de": _SEARCH_ENGINE_ORDER_ECOSIA,
71 "en-GB": _SEARCH_ENGINE_ORDER_ECOSIA, 75 "en-GB": _SEARCH_ENGINE_ORDER_ECOSIA,
72 "en-US": _SEARCH_ENGINE_ORDER_ECOSIA, 76 "en-US": _SEARCH_ENGINE_ORDER_ECOSIA,
73 "fr": _SEARCH_ENGINE_ORDER_ECOSIA, 77 "fr": _SEARCH_ENGINE_ORDER_ECOSIA,
74 "nl": _SEARCH_ENGINE_ORDER_ECOSIA, 78 "nl": _SEARCH_ENGINE_ORDER_ECOSIA,
75 "zh-CN": ["baidu", 79 "zh-CN": ["baidu",
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 logger.info("Processing locale '%s'..." % locale) 161 logger.info("Processing locale '%s'..." % locale)
158 162
159 # Check for region.properties existence 163 # Check for region.properties existence
160 region_file_path = os.path.join(locale_path, _REGION_PROPS_PATH) 164 region_file_path = os.path.join(locale_path, _REGION_PROPS_PATH)
161 _check_path_exists(region_file_path, logger) 165 _check_path_exists(region_file_path, logger)
162 166
163 # Check for appstrings.properties existence 167 # Check for appstrings.properties existence
164 appstrings_file_path = os.path.join(locale_path, _APPSTRINGS_PROPS_PATH) 168 appstrings_file_path = os.path.join(locale_path, _APPSTRINGS_PROPS_PATH)
165 _check_path_exists(appstrings_file_path, logger) 169 _check_path_exists(appstrings_file_path, logger)
166 170
171 ecosia_dst = os.path.join(locale_path,
172 _LOCALE_SEARCHPLUGINS_PATH, "ecosia.xml")
173
167 # Get whitelist and build regex 174 # Get whitelist and build regex
168 whitelist = _SEARCH_ENGINE_ORDER.get(locale, 175 whitelist = _SEARCH_ENGINE_ORDER.get(locale,
169 _SEARCH_ENGINE_ORDER_DEFAULT) 176 _SEARCH_ENGINE_ORDER_DEFAULT)
170 white_re = re.compile("^(%s).*$" % "|".join(whitelist)) 177 white_re = re.compile("^(%s).*$" % "|".join(whitelist))
171 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
172 all_engine_ids = [] 182 all_engine_ids = []
173 engine_ids = [] 183 engine_ids = []
174 removed_engine_ids = [] 184 removed_engine_ids = []
175 185
176 186 for item in data['locales'][locale]['default'][_DEF_ENGINES]:
diegocarloslima 2018/01/03 14:25:07 2 new lines here... seems that it should be just o
177 for item in data['locales'][locale]['default']['visibleDefaultEngines']:
178 all_engine_ids.append(item) 187 all_engine_ids.append(item)
179 if len(item) > 0: 188 if len(item) > 0:
180 if white_re.match(item): 189 if white_re.match(item):
181 engine_ids.append(item) 190 engine_ids.append(item)
182 else: 191 else:
183 removed_engine_ids.append(item) 192 removed_engine_ids.append(item)
184 193
185 if _ECOSIA_ID in whitelist and _ECOSIA_ID not in all_engine_ids: 194 if _ECOSIA_ID in whitelist and _ECOSIA_ID not in all_engine_ids:
186 all_engine_ids.append(_ECOSIA_ID) 195 all_engine_ids.append(_ECOSIA_ID)
187 engine_ids.append(_ECOSIA_ID) 196 engine_ids.append(_ECOSIA_ID)
188 197
189 # Make sure we still have search engines left 198 # Make sure we still have search engines left
190 if len(engine_ids) == 0: 199 if len(engine_ids) == 0:
191 logger.fatal("No search engines left over for '%s'" % locale) 200 logger.fatal("No search engines left over for '%s'" % locale)
192 201
193 data['locales'][locale]['default']['visibleDefaultEngines'] = all_engine_ids 202 data['locales'][locale]['default'][_DEF_ENGINES] = all_engine_ids
194
195
196 203
diegocarloslima 2018/01/03 14:25:07 3 new lines here... seems that it should be just o
197 # 'Parse' XML to get matching 'ShortName' for all engine IDs 204 # 'Parse' XML to get matching 'ShortName' for all engine IDs
198 engine_names = {} 205 engine_names = {}
199 search_plugins_path = os.path.join(project_dir, _SEARCHPLUGINS_PATH) 206 search_plugins_path = os.path.join(project_dir,
207 _GENERAL_SEARCHPLUGINS_PATH)
200 for eid in engine_ids[:]: 208 for eid in engine_ids[:]:
201 xml_file_path = os.path.join(search_plugins_path, "%s.xml" % eid) 209 xml_file_path = os.path.join(search_plugins_path, "%s.xml" % eid)
202 if not os.path.exists(xml_file_path): 210 if not os.path.exists(xml_file_path):
203 logger.info("Missing xml file for plugin %s. Searched in path %s" % 211 logger.info("Missing xml file for plugin %s. Searched in path %s" %
204 (eid, xml_file_path)) 212 (eid, xml_file_path))
205 engine_ids.remove(eid) 213 engine_ids.remove(eid)
206 continue 214 continue
207 short_name = None 215 short_name = None
208 with open(xml_file_path, "r") as fd: 216 with open(xml_file_path, "r") as fd:
209 for line in fd: 217 for line in fd:
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 browsersearch_file_path = os.path.join(res_path, raw_dir, 287 browsersearch_file_path = os.path.join(res_path, raw_dir,
280 "browsersearch.json") 288 "browsersearch.json")
281 289
282 sys.path.append(os.path.join(project_dir, _MOZBUILD_PATH)) 290 sys.path.append(os.path.join(project_dir, _MOZBUILD_PATH))
283 import mozbuild.action.generate_browsersearch as generate_browsersearch 291 import mozbuild.action.generate_browsersearch as generate_browsersearch
284 292
285 # Call generate_browsersearch.py script to regenerate 293 # Call generate_browsersearch.py script to regenerate
286 # res/raw-LOCALE/browsersearch.json with the updated search engines 294 # res/raw-LOCALE/browsersearch.json with the updated search engines
287 generate_browsersearch.main(["--verbose", "--srcdir", browser_path, 295 generate_browsersearch.main(["--verbose", "--srcdir", browser_path,
288 browsersearch_file_path]) 296 browsersearch_file_path])
297
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])
289 310
290 311
291 def _transform_values_locale(locale, path, logger): 312 def _transform_values_locale(locale, path, logger):
292 logger.info("Processing values-%s..." % locale) 313 logger.info("Processing values-%s..." % locale)
293 314
294 # Check for strings.xml existence 315 # Check for strings.xml existence
295 strings_file_path = os.path.join(path, _STRINGS_XML_PATH) 316 strings_file_path = os.path.join(path, _STRINGS_XML_PATH)
296 _check_path_exists(strings_file_path, logger) 317 _check_path_exists(strings_file_path, logger)
297 318
298 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 'strings.xml' 319 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 'strings.xml'
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 locales = _get_locales_from_path(chrome_path, _LOCALE_RE) 370 locales = _get_locales_from_path(chrome_path, _LOCALE_RE)
350 values_locales = _get_locales_from_path(res_path, _VALUES_LOCALE_RE) 371 values_locales = _get_locales_from_path(res_path, _VALUES_LOCALE_RE)
351 372
352 locales_found_msg = "Found %d locales in %s" 373 locales_found_msg = "Found %d locales in %s"
353 logger.info(locales_found_msg % (len(locales), chrome_path)) 374 logger.info(locales_found_msg % (len(locales), chrome_path))
354 logger.info(locales_found_msg % (len(values_locales), res_path)) 375 logger.info(locales_found_msg % (len(values_locales), res_path))
355 376
356 # open the Mozilla list of search engines, put it into a buffer and 377 # open the Mozilla list of search engines, put it into a buffer and
357 # close the JSON file after reading 378 # close the JSON file after reading
358 with open(list_json_path, 'r') as json_file: 379 with open(list_json_path, 'r') as json_file:
359 data = json.load(jsonFile) 380 data = json.load(json_file)
360 381
361 # set default search engine order 382 # set default search engine order
362 data['default']['visibleDefaultEngines'] = _SEARCH_ENGINE_ORDER_DEFAULT 383 data['default'][_DEF_ENGINES] = _SEARCH_ENGINE_ORDER_DEFAULT
363 384
364 for locale in locales: 385 for locale in locales:
365 locale_path = os.path.join(chrome_path, locale, "locale", locale) 386 locale_path = os.path.join(chrome_path, locale, "locale", locale)
366 if os.path.exists(locale_path): 387 if os.path.exists(locale_path):
367 388
368 # Mozilla default list does not contain locale bn-BD, so we crea te it 389 # Mozilla default list does not contain locale bn-BD,
369 # and use the values from locale bn-IN 390 # so we create it and use the values from locale bn-IN
370 if locale == 'bn-BD': 391 if locale == 'bn-BD':
371 data['locales'].update({locale: {'default': {'visibleDefault Engines': data['locales'] 392 data['locales'].update({locale: {'default':
diegocarloslima 2018/01/03 14:25:07 This line is too big to be in compliance with PEP8
372 ['bn-IN']['default']['visibleDefaultE ngines']}}}) 393 {_DEF_ENGINES:
373 # Mozilla default list does not contain locale wo, so we use the 394 data['locales']['bn-IN']['default'][_DEF_ENGINES]}}})
374 # default order. In case they will not support any other locales in 395 # Mozilla default list does not contain locale wo, so we use
375 # the future, we want the build to fail, to decide which order t o 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
376 elif locale == 'wo': 399 elif locale == 'wo':
377 data['locales'].update({locale: {'default': 400 data['locales'].update({locale: {'default':
378 {'visibleDefaultEngines': _SEARCH_ENG INE_ORDER_DEFAULT}}}) 401 {_DEF_ENGINES:
379 402 _SEARCH_ENGINE_ORDER_DEFAULT}}})
380 _transform_locale(data, locale, project_dir, locale_path, logger ) 403
381 _generate_browser_search(locale, locale_path, res_path, project_ dir) 404 _transform_locale(data, locale, project_dir, locale_path,
405 logger)
406 _generate_browser_search(locale, locale_path, res_path,
407 project_dir)
382 else: 408 else:
383 logger.error("Missing folder for locale '%s' in path: %s" % 409 logger.error("Missing folder for locale '%s' in path: %s" %
384 (locale, locale_path)) 410 (locale, locale_path))
385 411
386 # Save our changes to list.json 412 # Save changes to list.json
387 with open(list_json_path, 'w') as outfile: 413 with open(list_json_path, 'w') as outfile:
388 json.dump(data, outfile, indent=4, sort_keys=True) 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)
389 420
390 for locale in values_locales: 421 for locale in values_locales:
391 locale_path = os.path.join(res_path, "values-" + locale) 422 locale_path = os.path.join(res_path, "values-" + locale)
392 _transform_values_locale(locale, locale_path, logger) 423 _transform_values_locale(locale, locale_path, logger)
393
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld