| Index: mozharness/abb/abb_transform_locales.py |
| diff --git a/mozharness/abb/abb_transform_locales.py b/mozharness/abb/abb_transform_locales.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e8a18ec18cfd51e05d6b0500c160616ad8df17cd |
| --- /dev/null |
| +++ b/mozharness/abb/abb_transform_locales.py |
| @@ -0,0 +1,198 @@ |
| +# This file is part of Adblock Plus |
|
Felix Dahlke
2015/09/17 14:57:57
As I suggested before, this file should be renamed
|
| +# Copyright (C) 2006-2015 Eyeo GmbH |
| +# |
| +# Adblock Plus is free software: you can redistribute it and/or modify |
| +# it under the terms of the GNU General Public License version 3 as |
| +# published by the Free Software Foundation. |
| +# |
| +# Adblock Plus is distributed in the hope that it will be useful, |
| +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| +# GNU General Public License for more details. |
| +# |
| +# You should have received a copy of the GNU General Public License |
| +# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + |
| +import os |
| +import re |
| + |
| +_LOCALE_RE = re.compile("^[a-z]{2}(-[A-Z]{2})?$") |
| +_SEARCH_PROPS_RE = re.compile("^browser\.search\." |
| + "(defaultenginename|order\.).*$") |
| +_SHORTNAME_RE = re.compile("^<ShortName>.*</ShortName>$") |
| + |
| +_SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") |
| +_LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt") |
| +_REGION_PROPS_PATH = os.path.join("browser", "region.properties") |
| + |
| +_DEFAULT_LOCALE = "en-US" |
| + |
| +_SEARCH_ENGINE_ORDER = { |
| + "en-US": ["duckduckgo", |
|
Felix Dahlke
2015/09/17 14:57:56
Being our file, I guess we should stick to 2 space
René Jeschke
2015/09/18 09:46:38
Sure. Wasn't aware that we're not fully pep8 :-D
|
| + "yahoo", |
| + "google", |
| + "wikipedia", |
| + "amazon"], |
| + "zh-CN": ["baidu", |
| + "duckduckgo", |
| + "yahoo", |
| + "google", |
| + "wikipedia", |
| + "amazon"] |
| +} |
| + |
| + |
| +def _get_shortname_from_id(needle, engine_ids, engine_map): |
|
Felix Dahlke
2015/09/17 14:57:57
Nit: I would find "engine_names" more fitting than
René Jeschke
2015/09/18 09:46:38
Done.
|
| + """Fuzzy finds needle in engine_ids and returns ShortName""" |
| + regex = re.compile("^%s.*$" % needle) |
| + for engine in engine_ids: |
| + if regex.match(engine.lower()): |
| + return engine_map[engine] |
| + return None |
| + |
| + |
| +def _write_lines(filename, lines): |
| + """Writes lines into file appending \\n""" |
| + with open(filename, "w") as fd: |
| + for l in lines: |
| + fd.write("%s\n" % l) |
| + |
| + |
| +def _transform_locale(locale, path, fns): |
|
Felix Dahlke
2015/09/17 14:57:57
Wouldn't hurt readability to split this big functi
René Jeschke
2015/09/18 09:46:38
Then I prefer leaving it like it is.
Felix Dahlke
2015/09/18 16:34:11
Acknowledged.
|
| + fns["info"]("Processing locale '%s'..." % locale) |
| + |
| + # Check for list.txt existence |
| + list_file = os.path.join(path, _LIST_TXT_PATH) |
|
Felix Dahlke
2015/09/17 14:57:57
Nit: Being a path, not a file object, `list_path`?
René Jeschke
2015/09/18 09:46:38
Done.
|
| + if not os.path.exists(list_file): |
| + fns["fatal"]("Missing 'list.txt' for locale '%s'" % locale) |
| + |
| + # Check for region.properties existence |
| + region_file = os.path.join(path, _REGION_PROPS_PATH) |
| + if not os.path.exists(region_file): |
| + fns["fatal"]("Missing 'region.properties' for locale '%s'" % locale) |
| + |
| + # Get whitelist and build regex |
| + whitelist = _SEARCH_ENGINE_ORDER.get(locale, |
| + _SEARCH_ENGINE_ORDER[_DEFAULT_LOCALE]) |
| + white_re = re.compile("^(%s).*$" % ("|".join(whitelist))) |
|
Felix Dahlke
2015/09/17 14:57:57
Nit: No need for the extra parentheses around "|".
René Jeschke
2015/09/18 09:46:37
Done.
|
| + |
| + # Read engine IDs from list.txt, discard engines not on whitelist |
| + engine_ids = [] |
| + for line in open(list_file, "r"): |
|
Felix Dahlke
2015/09/17 14:57:57
This way, the file will stay open, you should use
René Jeschke
2015/09/18 09:46:38
Done.
|
| + line = line.strip() |
| + if len(line) > 0: |
| + if white_re.match(line.lower()): |
| + engine_ids.append(line) |
| + else: |
| + fns["info"]("Removing '%s'" % line) |
| + |
| + # Make sure we still have search engines left |
| + if len(engine_ids) == 0: |
| + fns["fatal"]("No search engines left over for '%s'" % locale) |
| + |
| + # 'Parse' XML to get matching 'ShortName' for all engine IDs |
| + engine_names = {} |
| + for eid in engine_ids: |
| + xml_file = os.path.join(path, _SEARCHPLUGINS_PATH, "%s.xml" % eid) |
| + if os.path.exists(xml_file): |
| + short_name = None |
| + for line in open(xml_file, "r"): |
| + line = line.strip() |
| + if _SHORTNAME_RE.match(line): |
| + short_name = line[11:-12].strip() |
| + |
| + if not short_name: |
| + fns["fatal"]("No ShortName defined for '%s' in '%s" % |
| + (eid, locale)) |
| + engine_names[eid] = short_name |
| + else: |
| + fns["fatal"]("XML definiton for '%s' in '%s' missing" % |
| + (eid, locale)) |
| + |
| + fns["info"]("Remaining engine IDs: %s" % ", ".join(engine_ids)) |
| + |
| + # Create search engine order with real engine names |
| + engine_order = [] |
| + for eid in whitelist: |
| + sn = _get_shortname_from_id(eid, engine_ids, engine_names) |
| + if sn: |
| + engine_order.append(sn) |
| + |
| + fns["info"]("Resulting ordered list: %s" % (", ".join(engine_order))) |
| + |
| + # Read region.properties and remove browser.search.* lines |
| + props = [] |
| + for line in open(region_file, "r"): |
| + line = line.rstrip("\r\n") |
| + if not _SEARCH_PROPS_RE.match(line.strip()): |
| + props.append(line) |
| + |
| + # Append default search engine name |
| + props.append("browser.search.defaultenginename=%s" % engine_order[0]) |
| + |
| + # Append search engine order |
| + for i in range(0, min(3, len(engine_order))): |
| + props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i])) |
| + |
| + # Write back list.txt |
| + _write_lines(list_file, engine_ids) |
| + |
| + # Write back region.properties |
| + _write_lines(region_file, props) |
| + |
| + |
| +def _print_info(obj): |
| + """ Wrapper for 'self.info' (to be self-contained) """ |
| + def fn(s): |
| + if obj: |
| + obj.info(s) |
| + else: |
| + print "INFO: %s" % s |
| + return fn |
| + |
| + |
| +def _print_error(obj): |
| + """ Wrapper for 'self.error' (to be self-contained) """ |
| + def fn(s): |
| + if obj: |
| + obj.error(s) |
| + else: |
| + print "ERROR: %s" % s |
| + return fn |
| + |
| + |
| +def _exit_fatal(obj): |
| + """ Wrapper for 'self.fatal' (to be self-contained) """ |
| + def fn(s): |
| + if obj: |
| + obj.fatal(s) |
| + else: |
| + print "FATAL: %s" % s |
| + exit(1) |
| + return fn |
| + |
| + |
| +def transform_locales(obj_dir, obj): |
|
Felix Dahlke
2015/09/17 14:57:57
I found the name "obj" pretty non-obvious here, it
René Jeschke
2015/09/18 09:46:38
Done.
|
| + fns = {"info": _print_info(obj), |
|
Felix Dahlke
2015/09/17 14:57:56
I think this is more complex than it would have to
René Jeschke
2015/09/18 09:46:37
I thought you wanted to be able to use this script
Felix Dahlke
2015/09/18 16:34:11
Well, I think we should either use the build objec
|
| + "error": _print_error(obj), |
| + "fatal": _exit_fatal(obj)} |
| + |
| + chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome") |
| + if not os.path.exists(chrome_path): |
| + fns["fatal"]("'dist/bin/chrome' not existent in '%s'" % obj_dir) |
|
Felix Dahlke
2015/09/17 14:57:56
I guess we shouldn't duplicate "dist/bin/chrome" h
René Jeschke
2015/09/18 09:46:37
Done.
|
| + |
| + locales = [] |
| + for p in next(os.walk(chrome_path))[1]: |
| + if _LOCALE_RE.match(p): |
| + locales.append(p) |
| + locales.sort() |
|
Felix Dahlke
2015/09/17 14:57:56
Why sort them?
René Jeschke
2015/09/18 09:46:38
Why not? :-)
It makes the log output clearer, as
Felix Dahlke
2015/09/18 16:34:11
No, no concerns, was just wondering if there's a t
|
| + |
| + fns["info"]("Found %d locales" % len(locales)) |
| + |
| + for locale in locales: |
| + locale_path = os.path.join(chrome_path, locale, "locale", locale) |
| + if os.path.exists(locale_path): |
| + _transform_locale(locale, locale_path, fns) |
| + else: |
| + fns["error"]("Missing 'locale' folder for '%s'" % locale) |
| + |