OLD | NEW |
(Empty) | |
| 1 # This file is part of Adblock Plus |
| 2 # Copyright (C) 2006-present 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 json |
| 17 import os |
| 18 import re |
| 19 import shutil |
| 20 import sys |
| 21 |
| 22 _LOCALE_RE = re.compile("^([a-z]{2,3}(?:-[A-Z]{2})?)$") |
| 23 _VALUES_LOCALE_RE = re.compile("^values-([a-z]{2,3}(?:-r[A-Z]{2})?)$") |
| 24 |
| 25 _SEARCH_PROPS_RE = re.compile("^browser\.search\." |
| 26 "(defaultenginename|order\.).*$") |
| 27 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$") |
| 28 |
| 29 _PROPERTY_FORMAT_RE = re.compile("^(([^=]*)=)(.*)$") |
| 30 _ENTITY_FORMAT_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$") |
| 31 _STRING_FORMAT_RE = re.compile( |
| 32 "^(\s*<string name=\"([^\"]*)\">)(.*)(</string>)$") |
| 33 |
| 34 _MOZBUILD_PATH = os.path.join("python", "mozbuild") |
| 35 |
| 36 _CHROME_PATH = os.path.join("dist", "bin", "chrome") |
| 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") |
| 42 |
| 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") |
| 46 |
| 47 _APPSTRINGS_PROPS_PATH = os.path.join(_BROWSER_DIR, "appstrings.properties") |
| 48 _STRINGS_XML_PATH = "strings.xml" |
| 49 |
| 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 } |
| 87 |
| 88 _FIREFOX_REPLACE_STR = "Firefox" |
| 89 _ABB_REPLACEMENT_STR = "Adblock Browser" |
| 90 |
| 91 # Some string values that contain Firefox such as 'Firefox Sync' shouldn't be |
| 92 # replaced, so we keep a list of ids that are exceptions |
| 93 _ENTITY_EXCEPTIONS = [ |
| 94 "overlay_no_synced_devices", |
| 95 "home_remote_tabs_need_to_sign_in", |
| 96 "home_remote_tabs_need_to_finish_migrating", |
| 97 "home_remote_tabs_need_to_verify", |
| 98 "syncBrand.fullName.label", |
| 99 "sync.subtitle.connectlocation2.label", |
| 100 "sync.subtitle.failmultiple.label", |
| 101 "fxaccount_full_label", |
| 102 "fxaccount_create_account_header2", |
| 103 "fxaccount_create_account_policy_text2", |
| 104 "fxaccount_status_header2", |
| 105 "fxaccount_status_needs_finish_migrating", |
| 106 "fxaccount_remove_account_dialog_title", |
| 107 "fxaccount_remove_account_toast", |
| 108 "fxaccount_account_type_label", |
| 109 ] |
| 110 |
| 111 |
| 112 def _check_path_exists(path, logger): |
| 113 if not os.path.exists(path): |
| 114 logger.fatal("'%s' does not exist" % path) |
| 115 |
| 116 |
| 117 def _get_locales_from_path(path, locale_re): |
| 118 locales = [] |
| 119 for dir_name in next(os.walk(path))[1]: |
| 120 match = locale_re.match(dir_name) |
| 121 if match: |
| 122 locales.append(match.group(1)) |
| 123 locales.sort |
| 124 return locales |
| 125 |
| 126 |
| 127 def _get_shortname_from_id(needle, engine_ids, engine_names): |
| 128 """Fuzzy finds needle in engine_ids and returns ShortName""" |
| 129 for engine in engine_ids: |
| 130 if engine.startswith(needle): |
| 131 return engine_names[engine] |
| 132 return None |
| 133 |
| 134 |
| 135 def _replace_in_value(format_re, str, old, new, exceptions=[]): |
| 136 match = format_re.match(str) |
| 137 if match and match.lastindex > 2: |
| 138 # The prefix contains all characters that precedes the value, including |
| 139 # the id/key |
| 140 str_value_prefix = match.group(1) |
| 141 str_id = match.group(2) |
| 142 str_value = match.group(3) |
| 143 if str_id not in exceptions and old in str_value: |
| 144 new_str = str_value_prefix + str_value.replace(old, new) |
| 145 if match.lastindex == 4: |
| 146 # The suffix contains all characters that succeeds the value |
| 147 str_value_suffix = match.group(4) |
| 148 new_str = new_str + str_value_suffix |
| 149 return new_str |
| 150 return None |
| 151 |
| 152 |
| 153 def _write_lines(filename, lines): |
| 154 """Writes lines into file appending \\n""" |
| 155 with open(filename, "w") as fd: |
| 156 for l in lines: |
| 157 fd.write("%s\n" % l) |
| 158 |
| 159 |
| 160 def _transform_locale(data, locale, project_dir, locale_path, logger): |
| 161 logger.info("Processing locale '%s'..." % locale) |
| 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 |
| 167 # Check for appstrings.properties existence |
| 168 appstrings_file_path = os.path.join(locale_path, _APPSTRINGS_PROPS_PATH) |
| 169 _check_path_exists(appstrings_file_path, logger) |
| 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 |
| 260 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in |
| 261 # 'appstrings.properties' |
| 262 lines = [] |
| 263 replacement_count = 0 |
| 264 |
| 265 with open(appstrings_file_path, "r") as fd: |
| 266 for line in fd: |
| 267 line = line.rstrip("\r\n") |
| 268 replacement = _replace_in_value(_PROPERTY_FORMAT_RE, line, |
| 269 _FIREFOX_REPLACE_STR, |
| 270 _ABB_REPLACEMENT_STR) |
| 271 if replacement: |
| 272 line = replacement |
| 273 replacement_count += 1 |
| 274 lines.append(line) |
| 275 |
| 276 # Apply changes to appstrings.properties |
| 277 _write_lines(appstrings_file_path, lines) |
| 278 logger.info("Replaced %d ocurrences of %s in 'appstrings.properties'" % |
| 279 (replacement_count, _FIREFOX_REPLACE_STR)) |
| 280 |
| 281 |
| 282 def _generate_browser_search(locale, locale_path, res_path, project_dir): |
| 283 raw_dir = "raw" if locale == _DEFAULT_LOCALE else ( |
| 284 "raw-%s" % locale.replace("-", "-r")) |
| 285 |
| 286 browser_path = os.path.join(locale_path, _BROWSER_DIR) |
| 287 browsersearch_file_path = os.path.join(res_path, raw_dir, |
| 288 "browsersearch.json") |
| 289 |
| 290 sys.path.append(os.path.join(project_dir, _MOZBUILD_PATH)) |
| 291 import mozbuild.action.generate_browsersearch as generate_browsersearch |
| 292 |
| 293 # Call generate_browsersearch.py script to regenerate |
| 294 # res/raw-LOCALE/browsersearch.json with the updated search engines |
| 295 generate_browsersearch.main(["--verbose", "--srcdir", browser_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]) |
| 310 |
| 311 |
| 312 def _transform_values_locale(locale, path, logger): |
| 313 logger.info("Processing values-%s..." % locale) |
| 314 |
| 315 # Check for strings.xml existence |
| 316 strings_file_path = os.path.join(path, _STRINGS_XML_PATH) |
| 317 _check_path_exists(strings_file_path, logger) |
| 318 |
| 319 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 'strings.xml' |
| 320 lines = [] |
| 321 replacement_count = 0 |
| 322 |
| 323 with open(strings_file_path, "r") as fd: |
| 324 for line in fd: |
| 325 line = line.rstrip("\r\n") |
| 326 replacement = _replace_in_value(_ENTITY_FORMAT_RE, line, |
| 327 _FIREFOX_REPLACE_STR, |
| 328 _ABB_REPLACEMENT_STR, |
| 329 _ENTITY_EXCEPTIONS) |
| 330 if replacement: |
| 331 line = replacement |
| 332 replacement_count += 1 |
| 333 else: |
| 334 replacement = _replace_in_value(_STRING_FORMAT_RE, line, |
| 335 _FIREFOX_REPLACE_STR, |
| 336 _ABB_REPLACEMENT_STR) |
| 337 if replacement: |
| 338 line = replacement |
| 339 replacement_count += 1 |
| 340 lines.append(line) |
| 341 |
| 342 # Apply changes to strings.xml |
| 343 _write_lines(strings_file_path, lines) |
| 344 logger.info("Replaced %d ocurrences of %s in 'strings.xml'" % |
| 345 (replacement_count, _FIREFOX_REPLACE_STR)) |
| 346 |
| 347 |
| 348 class MinimalLogger: |
| 349 def info(self, s): |
| 350 print "INFO: %s" % s |
| 351 |
| 352 def error(self, s): |
| 353 print "ERROR: %s" % s |
| 354 |
| 355 def fatal(self, s): |
| 356 print "FATAL: %s" % s |
| 357 exit(1) |
| 358 |
| 359 |
| 360 def transform_locales(project_dir, obj_dir, logger=MinimalLogger()): |
| 361 chrome_path = os.path.join(obj_dir, _CHROME_PATH) |
| 362 _check_path_exists(chrome_path, logger) |
| 363 |
| 364 res_path = os.path.join(obj_dir, _RES_PATH) |
| 365 _check_path_exists(res_path, logger) |
| 366 |
| 367 list_json_path = os.path.join(project_dir, _LISTJSON_PATH) |
| 368 _check_path_exists(list_json_path, logger) |
| 369 |
| 370 locales = _get_locales_from_path(chrome_path, _LOCALE_RE) |
| 371 values_locales = _get_locales_from_path(res_path, _VALUES_LOCALE_RE) |
| 372 |
| 373 locales_found_msg = "Found %d locales in %s" |
| 374 logger.info(locales_found_msg % (len(locales), chrome_path)) |
| 375 logger.info(locales_found_msg % (len(values_locales), res_path)) |
| 376 |
| 377 # open the Mozilla list of search engines, put it into a buffer and |
| 378 # close the JSON file after reading |
| 379 with open(list_json_path, 'r') as json_file: |
| 380 data = json.load(json_file) |
| 381 |
| 382 # set default search engine order |
| 383 data['default'][_DEF_ENGINES] = _SEARCH_ENGINE_ORDER_DEFAULT |
| 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) |
OLD | NEW |