OLD | NEW |
(Empty) | |
| 1 # This file is part of Adblock Plus |
| 2 # Copyright (C) 2006-2015 Eyeo GmbH |
| 3 # |
| 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 |
| 6 # published by the Free Software Foundation. |
| 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 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/>. |
| 15 |
| 16 import os |
| 17 import re |
| 18 |
| 19 _LOCALE_RE = re.compile("^[a-z]{2}(-[A-Z]{2})?$") |
| 20 _SEARCH_PROPS_RE = re.compile("^browser\.search\." |
| 21 "(defaultenginename|order\.).*$") |
| 22 _SHORTNAME_RE = re.compile("^<ShortName>.*</ShortName>$") |
| 23 |
| 24 _SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") |
| 25 _LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt") |
| 26 _REGION_PROPS_PATH = os.path.join("browser", "region.properties") |
| 27 |
| 28 _DEFAULT_LOCALE = "en-US" |
| 29 |
| 30 _SEARCH_ENGINE_ORDER = { |
| 31 "en-US": ["duckduckgo", |
| 32 "yahoo", |
| 33 "google", |
| 34 "wikipedia", |
| 35 "amazon"], |
| 36 "zh-CN": ["baidu", |
| 37 "duckduckgo", |
| 38 "yahoo", |
| 39 "google", |
| 40 "wikipedia", |
| 41 "amazon"] |
| 42 } |
| 43 |
| 44 |
| 45 def _get_shortname_from_id(needle, engine_ids, engine_map): |
| 46 """Fuzzy finds needle in engine_ids and returns ShortName""" |
| 47 regex = re.compile("^%s.*$" % needle) |
| 48 for engine in engine_ids: |
| 49 if regex.match(engine.lower()): |
| 50 return engine_map[engine] |
| 51 return None |
| 52 |
| 53 |
| 54 def _write_lines(filename, lines): |
| 55 """Writes lines into file appending \\n""" |
| 56 with open(filename, "w") as fd: |
| 57 for l in lines: |
| 58 fd.write("%s\n" % l) |
| 59 |
| 60 |
| 61 def _transform_locale(locale, path, fns): |
| 62 fns["info"]("Processing locale '%s'..." % locale) |
| 63 |
| 64 # Check for list.txt existence |
| 65 list_file = os.path.join(path, _LIST_TXT_PATH) |
| 66 if not os.path.exists(list_file): |
| 67 fns["fatal"]("Missing 'list.txt' for locale '%s'" % locale) |
| 68 |
| 69 # Check for region.properties existence |
| 70 region_file = os.path.join(path, _REGION_PROPS_PATH) |
| 71 if not os.path.exists(region_file): |
| 72 fns["fatal"]("Missing 'region.properties' for locale '%s'" % locale) |
| 73 |
| 74 # Get whitelist and build regex |
| 75 whitelist = _SEARCH_ENGINE_ORDER.get(locale, |
| 76 _SEARCH_ENGINE_ORDER[_DEFAULT_LOCALE]) |
| 77 white_re = re.compile("^(%s).*$" % ("|".join(whitelist))) |
| 78 |
| 79 # Read engine IDs from list.txt, discard engines not on whitelist |
| 80 engine_ids = [] |
| 81 for line in open(list_file, "r"): |
| 82 line = line.strip() |
| 83 if len(line) > 0: |
| 84 if white_re.match(line.lower()): |
| 85 engine_ids.append(line) |
| 86 else: |
| 87 fns["info"]("Removing '%s'" % line) |
| 88 |
| 89 # Make sure we still have search engines left |
| 90 if len(engine_ids) == 0: |
| 91 fns["fatal"]("No search engines left over for '%s'" % locale) |
| 92 |
| 93 # 'Parse' XML to get matching 'ShortName' for all engine IDs |
| 94 engine_names = {} |
| 95 for eid in engine_ids: |
| 96 xml_file = os.path.join(path, _SEARCHPLUGINS_PATH, "%s.xml" % eid) |
| 97 if os.path.exists(xml_file): |
| 98 short_name = None |
| 99 for line in open(xml_file, "r"): |
| 100 line = line.strip() |
| 101 if _SHORTNAME_RE.match(line): |
| 102 short_name = line[11:-12].trim() |
| 103 |
| 104 if not short_name: |
| 105 fns["fatal"]("No ShortName defined for '%s' in '%s" % |
| 106 (eid, locale)) |
| 107 engine_names[eid] = short_name |
| 108 else: |
| 109 fns["fatal"]("XML definiton for '%s' in '%s' missing" % |
| 110 (eid, locale)) |
| 111 |
| 112 fns["info"]("Remaining engine IDs: %s" % ", ".join(engine_ids)) |
| 113 |
| 114 # Create search engine order with real engine names |
| 115 engine_order = [] |
| 116 for eid in whitelist: |
| 117 sn = _get_shortname_from_id(eid, engine_ids, engine_names) |
| 118 if sn: |
| 119 engine_order.append(sn) |
| 120 |
| 121 fns["info"]("Resulting ordered list: %s" % (", ".join(engine_order))) |
| 122 |
| 123 # Read region.properties and remove browser.search.* lines |
| 124 props = [] |
| 125 for line in open(region_file, "r"): |
| 126 line = line.rstrip("\r\n") |
| 127 if not _SEARCH_PROPS_RE.match(line.strip()): |
| 128 props.append(line) |
| 129 |
| 130 # Append default search engine name |
| 131 props.append("browser.search.defaultenginename=%s" % engine_order[0]) |
| 132 |
| 133 # Append search engine order |
| 134 for i in range(0, min(3, len(engine_order))): |
| 135 props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i])) |
| 136 |
| 137 # Write back list.txt |
| 138 _write_lines(list_file, engine_ids) |
| 139 |
| 140 # Write back region.properties |
| 141 _write_lines(region_file, props) |
| 142 |
| 143 |
| 144 def _print_info(obj): |
| 145 """ Wrapper for 'self.info' (to be self-contained) """ |
| 146 def fn(s): |
| 147 if obj: |
| 148 obj.info(s) |
| 149 else: |
| 150 print "INFO: %s" % s |
| 151 return fn |
| 152 |
| 153 |
| 154 def _print_error(obj): |
| 155 """ Wrapper for 'self.error' (to be self-contained) """ |
| 156 def fn(s): |
| 157 if obj: |
| 158 obj.error(s) |
| 159 else: |
| 160 print "ERROR: %s" % s |
| 161 return fn |
| 162 |
| 163 |
| 164 def _exit_fatal(obj): |
| 165 """ Wrapper for 'self.fatal' (to be self-contained) """ |
| 166 def fn(s): |
| 167 if obj: |
| 168 obj.fatal(s) |
| 169 else: |
| 170 print "FATAL: %s" % s |
| 171 exit(1) |
| 172 return fn |
| 173 |
| 174 |
| 175 def transform_locales(obj_dir, obj): |
| 176 fns = {"info": _print_info(obj), |
| 177 "error": _print_error(obj), |
| 178 "fatal": _exit_fatal(obj)} |
| 179 |
| 180 chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome") |
| 181 if not os.path.exists(chrome_path): |
| 182 fns["fatal"]("'dist/bin/chrome' not existent in '%s'" % obj_dir) |
| 183 |
| 184 locales = [] |
| 185 for p in next(os.walk(chrome_path))[1]: |
| 186 if _LOCALE_RE.match(p): |
| 187 locales.append(p) |
| 188 locales.sort() |
| 189 |
| 190 fns["info"]("Found %d locales" % len(locales)) |
| 191 |
| 192 for locale in locales: |
| 193 locale_path = os.path.join(chrome_path, locale, "locale", locale) |
| 194 if os.path.exists(locale_path): |
| 195 _transform_locale(locale, locale_path, fns) |
| 196 else: |
| 197 fns["error"]("Missing 'locale' folder for '%s'" % locale) |
| 198 |
OLD | NEW |