OLD | NEW |
1 # This file is part of Adblock Plus | 1 # This file is part of Adblock Plus |
2 # Copyright (C) 2006-2015 Eyeo GmbH | 2 # Copyright (C) 2006-2015 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 os | 16 import os |
17 import re | 17 import re |
18 | 18 |
19 _LOCALE_RE = re.compile("^[a-z]{2}(-[A-Z]{2})?$") | 19 _LOCALE_RE = re.compile("^([a-z]{2,3}(?:-[A-Z]{2})?)$") |
| 20 _VALUES_LOCALE_RE = re.compile("^values-([a-z]{2,3}(?:-r[A-Z]{2})?)$") |
| 21 |
20 _SEARCH_PROPS_RE = re.compile("^browser\.search\." | 22 _SEARCH_PROPS_RE = re.compile("^browser\.search\." |
21 "(defaultenginename|order\.).*$") | 23 "(defaultenginename|order\.).*$") |
22 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$") | 24 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$") |
23 | 25 |
| 26 _PROPERTY_FORMAT_RE = re.compile("^(([^=]*)=)(.*)$") |
| 27 _ENTITY_FORMAT_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$") |
| 28 _STRING_FORMAT_RE = re.compile( |
| 29 "^(\s*<string name=\"([^\"]*)\">)(.*)(</string>)$") |
| 30 |
| 31 _CHROME_PATH = os.path.join("dist", "bin", "chrome") |
| 32 _RES_PATH = os.path.join("mobile", "android", "base", "res") |
| 33 |
24 _SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") | 34 _SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") |
25 _LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt") | 35 _LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt") |
26 _REGION_PROPS_PATH = os.path.join("browser", "region.properties") | 36 _REGION_PROPS_PATH = os.path.join("browser", "region.properties") |
27 | 37 |
| 38 _APPSTRINGS_PROPS_PATH = os.path.join("browser", "appstrings.properties") |
| 39 _STRINGS_XML_PATH = "strings.xml" |
| 40 |
28 _DEFAULT_LOCALE = "en-US" | 41 _DEFAULT_LOCALE = "en-US" |
29 | 42 |
30 _SEARCH_ENGINE_ORDER = { | 43 _SEARCH_ENGINE_ORDER = { |
31 "en-US": ["duckduckgo", | 44 "en-US": ["duckduckgo", |
32 "yahoo", | 45 "yahoo", |
33 "google", | 46 "google", |
34 "wikipedia", | 47 "wikipedia", |
35 "amazon" | 48 "amazon" |
36 ], | 49 ], |
37 "zh-CN": ["baidu", | 50 "zh-CN": ["baidu", |
38 "duckduckgo", | 51 "duckduckgo", |
39 "yahoo", | 52 "yahoo", |
40 "google", | 53 "google", |
41 "wikipedia", | 54 "wikipedia", |
42 "amazon" | 55 "amazon" |
43 ] | 56 ] |
44 } | 57 } |
45 | 58 |
| 59 _FIREFOX_REPLACE_STR = "Firefox" |
| 60 _ABB_REPLACEMENT_STR = "Adblock Browser" |
| 61 |
| 62 # Some string values that contains Firefox such as 'Firefox Sync' shouldn't be |
| 63 # replaced, so we keep a list of ids that are exceptions |
| 64 _ENTITY_EXCEPTIONS = [ |
| 65 "overlay_no_synced_devices", |
| 66 "home_remote_tabs_need_to_sign_in", |
| 67 "home_remote_tabs_need_to_finish_migrating", |
| 68 "home_remote_tabs_need_to_verify", |
| 69 "syncBrand.fullName.label", |
| 70 "sync.subtitle.connectlocation2.label", |
| 71 "sync.subtitle.failmultiple.label", |
| 72 "fxaccount_full_label", |
| 73 "fxaccount_create_account_header2", |
| 74 "fxaccount_create_account_policy_text2", |
| 75 "fxaccount_status_header2", |
| 76 "fxaccount_status_needs_finish_migrating", |
| 77 "fxaccount_remove_account_dialog_title", |
| 78 "fxaccount_remove_account_toast", |
| 79 "fxaccount_account_type_label", |
| 80 ] |
| 81 |
| 82 |
| 83 def _check_path_exists(path, logger): |
| 84 if not os.path.exists(path): |
| 85 logger.fatal("'%s' does not exist" % path) |
| 86 |
| 87 |
| 88 def _get_locales_from_path(path, locale_re): |
| 89 locales = [] |
| 90 for dir_name in next(os.walk(path))[1]: |
| 91 match = locale_re.match(dir_name) |
| 92 if match: |
| 93 locales.append(match.group(1)) |
| 94 locales.sort |
| 95 return locales |
| 96 |
46 | 97 |
47 def _get_shortname_from_id(needle, engine_ids, engine_names): | 98 def _get_shortname_from_id(needle, engine_ids, engine_names): |
48 """Fuzzy finds needle in engine_ids and returns ShortName""" | 99 """Fuzzy finds needle in engine_ids and returns ShortName""" |
49 for engine in engine_ids: | 100 for engine in engine_ids: |
50 if engine.startswith(needle): | 101 if engine.startswith(needle): |
51 return engine_names[engine] | 102 return engine_names[engine] |
52 return None | 103 return None |
53 | 104 |
54 | 105 |
| 106 def _replace_in_value(format_re, str, old, new, exceptions=[]): |
| 107 match = format_re.match(str) |
| 108 if match and match.lastindex > 2: |
| 109 # The prefix contains all characters that precedes the value, including |
| 110 # the id/key |
| 111 str_value_prefix = match.group(1) |
| 112 str_id = match.group(2) |
| 113 str_value = match.group(3) |
| 114 if str_id not in exceptions and old in str_value: |
| 115 new_str = str_value_prefix + str_value.replace(old, new) |
| 116 if match.lastindex == 4: |
| 117 # The suffix contains all characters that succeeds the value |
| 118 str_value_suffix = match.group(4) |
| 119 new_str = new_str + str_value_suffix |
| 120 return new_str |
| 121 return None |
| 122 |
| 123 |
55 def _write_lines(filename, lines): | 124 def _write_lines(filename, lines): |
56 """Writes lines into file appending \\n""" | 125 """Writes lines into file appending \\n""" |
57 with open(filename, "w") as fd: | 126 with open(filename, "w") as fd: |
58 for l in lines: | 127 for l in lines: |
59 fd.write("%s\n" % l) | 128 fd.write("%s\n" % l) |
60 | 129 |
61 | 130 |
62 def _transform_locale(locale, path, logger): | 131 def _transform_locale(locale, path, logger): |
63 logger.info("Processing locale '%s'..." % locale) | 132 logger.info("Processing locale '%s'..." % locale) |
64 | 133 |
65 # Check for list.txt existence | 134 # Check for list.txt existence |
66 list_file_path = os.path.join(path, _LIST_TXT_PATH) | 135 list_file_path = os.path.join(path, _LIST_TXT_PATH) |
67 if not os.path.exists(list_file_path): | 136 _check_path_exists(list_file_path, logger) |
68 logger.fatal("Missing 'list.txt' for locale '%s'" % locale) | |
69 | 137 |
70 # Check for region.properties existence | 138 # Check for region.properties existence |
71 region_file_path = os.path.join(path, _REGION_PROPS_PATH) | 139 region_file_path = os.path.join(path, _REGION_PROPS_PATH) |
72 if not os.path.exists(region_file_path): | 140 _check_path_exists(region_file_path, logger) |
73 logger.fatal("Missing 'region.properties' for locale '%s'" % locale) | 141 |
| 142 # Check for appstrings.properties existence |
| 143 appstrings_file_path = os.path.join(path, _APPSTRINGS_PROPS_PATH) |
| 144 _check_path_exists(appstrings_file_path, logger) |
74 | 145 |
75 # Get whitelist and build regex | 146 # Get whitelist and build regex |
76 whitelist = _SEARCH_ENGINE_ORDER.get(locale, | 147 whitelist = _SEARCH_ENGINE_ORDER.get(locale, |
77 _SEARCH_ENGINE_ORDER[_DEFAULT_LOCALE]) | 148 _SEARCH_ENGINE_ORDER[_DEFAULT_LOCALE]) |
78 white_re = re.compile("^(%s).*$" % "|".join(whitelist)) | 149 white_re = re.compile("^(%s).*$" % "|".join(whitelist)) |
79 | 150 |
80 # Read engine IDs from list.txt, discard engines not on whitelist | 151 # Read engine IDs from list.txt, discard engines not on whitelist |
81 engine_ids = [] | 152 engine_ids = [] |
| 153 removed_engine_ids = [] |
82 with open(list_file_path, "r") as fd: | 154 with open(list_file_path, "r") as fd: |
83 for line in fd: | 155 for line in fd: |
84 line = line.strip() | 156 line = line.strip() |
85 if len(line) > 0: | 157 if len(line) > 0: |
86 if white_re.match(line): | 158 if white_re.match(line): |
87 engine_ids.append(line) | 159 engine_ids.append(line) |
88 else: | 160 else: |
89 logger.info("Removing '%s'" % line) | 161 removed_engine_ids.append(line) |
90 | 162 |
91 # Make sure we still have search engines left | 163 # Make sure we still have search engines left |
92 if len(engine_ids) == 0: | 164 if len(engine_ids) == 0: |
93 logger.fatal("No search engines left over for '%s'" % locale) | 165 logger.fatal("No search engines left over for '%s'" % locale) |
94 | 166 |
95 # 'Parse' XML to get matching 'ShortName' for all engine IDs | 167 # 'Parse' XML to get matching 'ShortName' for all engine IDs |
96 engine_names = {} | 168 engine_names = {} |
97 for eid in engine_ids: | 169 for eid in engine_ids: |
98 xml_file_path = os.path.join(path, _SEARCHPLUGINS_PATH, "%s.xml" % eid) | 170 xml_file_path = os.path.join(path, _SEARCHPLUGINS_PATH, "%s.xml" % eid) |
99 if os.path.exists(xml_file_path): | 171 _check_path_exists(xml_file_path, logger) |
100 short_name = None | 172 short_name = None |
101 with open(xml_file_path, "r") as fd: | 173 with open(xml_file_path, "r") as fd: |
102 for line in fd: | 174 for line in fd: |
103 line = line.strip() | 175 line = line.strip() |
104 match = _SHORTNAME_RE.match(line) | 176 match = _SHORTNAME_RE.match(line) |
105 if match: | 177 if match: |
106 short_name = match.group(1).strip() | 178 short_name = match.group(1).strip() |
107 | 179 |
108 if not short_name: | 180 if not short_name: |
109 logger.fatal("No ShortName defined for '%s' in '%s" % | 181 logger.fatal("No ShortName defined for '%s' in '%s" % |
110 (eid, locale)) | |
111 engine_names[eid] = short_name | |
112 else: | |
113 logger.fatal("XML definiton for '%s' in '%s' missing" % | |
114 (eid, locale)) | 182 (eid, locale)) |
| 183 engine_names[eid] = short_name |
115 | 184 |
116 logger.info("Remaining engine IDs: %s" % ", ".join(engine_ids)) | 185 logger.info("Removed search engine IDs: %s" % |
| 186 ", ".join(removed_engine_ids)) |
| 187 logger.info("Remaining search engine IDs: %s" % ", ".join(engine_ids)) |
117 | 188 |
118 # Create search engine order with real engine names | 189 # Create search engine order with real engine names |
119 engine_order = [] | 190 engine_order = [] |
120 for eid in whitelist: | 191 for eid in whitelist: |
121 sn = _get_shortname_from_id(eid, engine_ids, engine_names) | 192 sn = _get_shortname_from_id(eid, engine_ids, engine_names) |
122 if sn: | 193 if sn: |
123 engine_order.append(sn) | 194 engine_order.append(sn) |
124 | 195 |
125 logger.info("Resulting ordered list: %s" % (", ".join(engine_order))) | 196 logger.info("Resulting search engine ordered list: %s" % |
| 197 (", ".join(engine_order))) |
126 | 198 |
127 # Read region.properties and remove browser.search.* lines | 199 # Read region.properties and remove browser.search.* lines |
128 props = [] | 200 props = [] |
129 with open(region_file_path, "r") as fd: | 201 with open(region_file_path, "r") as fd: |
130 for line in fd: | 202 for line in fd: |
131 line = line.rstrip("\r\n") | 203 line = line.rstrip("\r\n") |
132 if not _SEARCH_PROPS_RE.match(line.strip()): | 204 if not _SEARCH_PROPS_RE.match(line.strip()): |
133 props.append(line) | 205 props.append(line) |
134 | 206 |
135 # Append default search engine name | 207 # Append default search engine name |
136 props.append("browser.search.defaultenginename=%s" % engine_order[0]) | 208 props.append("browser.search.defaultenginename=%s" % engine_order[0]) |
137 | 209 |
138 # Append search engine order | 210 # Append search engine order |
139 for i in range(0, min(5, len(engine_order))): | 211 for i in range(0, min(5, len(engine_order))): |
140 props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i])) | 212 props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i])) |
141 | 213 |
142 # Write back region.properties | 214 # Write back region.properties |
143 _write_lines(region_file_path, props) | 215 _write_lines(region_file_path, props) |
144 | 216 |
| 217 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in |
| 218 # 'appstrings.properties' |
| 219 lines = [] |
| 220 replacement_count = 0 |
| 221 |
| 222 with open(appstrings_file_path, "r") as fd: |
| 223 for line in fd: |
| 224 line = line.rstrip("\r\n") |
| 225 replacement = _replace_in_value(_PROPERTY_FORMAT_RE, line, |
| 226 _FIREFOX_REPLACE_STR, |
| 227 _ABB_REPLACEMENT_STR) |
| 228 if replacement: |
| 229 line = replacement |
| 230 replacement_count += 1 |
| 231 lines.append(line) |
| 232 |
| 233 # Apply changes to appstrings.properties |
| 234 _write_lines(appstrings_file_path, lines) |
| 235 logger.info("Replaced %d ocurrences of %s in 'appstrings.properties'" % |
| 236 (replacement_count, _FIREFOX_REPLACE_STR)) |
| 237 |
| 238 |
| 239 def _transform_values_locale(locale, path, logger): |
| 240 logger.info("Processing values-%s..." % locale) |
| 241 |
| 242 # Check for strings.xml existence |
| 243 strings_file_path = os.path.join(path, _STRINGS_XML_PATH) |
| 244 _check_path_exists(strings_file_path, logger) |
| 245 |
| 246 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 'strings.xml' |
| 247 lines = [] |
| 248 replacement_count = 0 |
| 249 |
| 250 with open(strings_file_path, "r") as fd: |
| 251 for line in fd: |
| 252 line = line.rstrip("\r\n") |
| 253 replacement = _replace_in_value(_ENTITY_FORMAT_RE, line, |
| 254 _FIREFOX_REPLACE_STR, |
| 255 _ABB_REPLACEMENT_STR, |
| 256 _ENTITY_EXCEPTIONS) |
| 257 if replacement: |
| 258 line = replacement |
| 259 replacement_count += 1 |
| 260 else: |
| 261 replacement = _replace_in_value(_STRING_FORMAT_RE, line, |
| 262 _FIREFOX_REPLACE_STR, |
| 263 _ABB_REPLACEMENT_STR) |
| 264 if replacement: |
| 265 line = replacement |
| 266 replacement_count += 1 |
| 267 lines.append(line) |
| 268 |
| 269 # Apply changes to strings.xml |
| 270 _write_lines(strings_file_path, lines) |
| 271 logger.info("Replaced %d ocurrences of %s in 'strings.xml'" % |
| 272 (replacement_count, _FIREFOX_REPLACE_STR)) |
| 273 |
145 | 274 |
146 class MinimalLogger: | 275 class MinimalLogger: |
147 def info(self, s): | 276 def info(self, s): |
148 print "INFO: %s" % s | 277 print "INFO: %s" % s |
149 | 278 |
150 def error(self, s): | 279 def error(self, s): |
151 print "ERROR: %s" % s | 280 print "ERROR: %s" % s |
152 | 281 |
153 def fatal(self, s): | 282 def fatal(self, s): |
154 print "FATAL: %s" % s | 283 print "FATAL: %s" % s |
155 exit(1) | 284 exit(1) |
156 | 285 |
157 | 286 |
158 def transform_locales(obj_dir, logger=MinimalLogger()): | 287 def transform_locales(obj_dir, logger=MinimalLogger()): |
159 chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome") | 288 chrome_path = os.path.join(obj_dir, _CHROME_PATH) |
160 if not os.path.exists(chrome_path): | 289 _check_path_exists(chrome_path, logger) |
161 logger.fatal("'%s' does not exist" % obj_dir) | |
162 | 290 |
163 locales = [] | 291 res_path = os.path.join(obj_dir, _RES_PATH) |
164 for p in next(os.walk(chrome_path))[1]: | 292 _check_path_exists(res_path, logger) |
165 if _LOCALE_RE.match(p): | |
166 locales.append(p) | |
167 locales.sort() | |
168 | 293 |
169 logger.info("Found %d locales" % len(locales)) | 294 locales = _get_locales_from_path(chrome_path, _LOCALE_RE) |
| 295 values_locales = _get_locales_from_path(res_path, _VALUES_LOCALE_RE) |
| 296 |
| 297 locales_found_msg = "Found %d locales in %s" |
| 298 logger.info(locales_found_msg % (len(locales), chrome_path)) |
| 299 logger.info(locales_found_msg % (len(values_locales), res_path)) |
170 | 300 |
171 for locale in locales: | 301 for locale in locales: |
172 locale_path = os.path.join(chrome_path, locale, "locale", locale) | 302 locale_path = os.path.join(chrome_path, locale, "locale", locale) |
173 if os.path.exists(locale_path): | 303 if os.path.exists(locale_path): |
174 _transform_locale(locale, locale_path, logger) | 304 _transform_locale(locale, locale_path, logger) |
175 else: | 305 else: |
176 logger.error("Missing 'locale' folder for '%s'" % locale) | 306 logger.error("Missing folder for locale '%s' in path: %s" % |
| 307 (locale, locale_path)) |
177 | 308 |
| 309 for locale in values_locales: |
| 310 locale_path = os.path.join(res_path, "values-" + locale) |
| 311 _transform_values_locale(locale, locale_path, logger) |
OLD | NEW |