Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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,3}(?:-[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})?)$") | 20 _VALUES_LOCALE_RE = re.compile("^values-([a-z]{2,3}(?:-r[A-Z]{2})?)$") |
21 | 21 |
22 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$") | |
René Jeschke
2016/07/26 12:49:31
Moving this line around is a unrelated change.
| |
23 _SEARCH_PROPS_RE = re.compile("^browser\.search\." | 22 _SEARCH_PROPS_RE = re.compile("^browser\.search\." |
24 "(defaultenginename|order\.).*$") | 23 "(defaultenginename|order\.).*$") |
25 | 24 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$") |
26 _PROPERTY_RE = re.compile("^(([^=]*)=)(.*)$") | 25 |
27 _ENTITY_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$") | 26 _PROPERTY_FORMAT_RE = re.compile("^(([^=]*)=)(.*)$") |
28 _STRING_RE = re.compile("^(\s*<string name=\"([^\"]*)\">)(.*)(</string>)$") | 27 _ENTITY_FORMAT_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$") |
28 _STRING_FORMAT_RE = re.compile( | |
29 "^(\s*<string name=\"([^\"]*)\">)(.*)(</string>)$") | |
29 | 30 |
30 _CHROME_PATH = os.path.join("dist", "bin", "chrome") | 31 _CHROME_PATH = os.path.join("dist", "bin", "chrome") |
31 _RES_PATH = os.path.join("mobile", "android", "base", "res") | 32 _RES_PATH = os.path.join("mobile", "android", "base", "res") |
32 | 33 |
33 _SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") | 34 _SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") |
34 _LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt") | 35 _LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt") |
35 _REGION_PROPS_PATH = os.path.join("browser", "region.properties") | 36 _REGION_PROPS_PATH = os.path.join("browser", "region.properties") |
36 | 37 |
37 _APPSTRINGS_PROPS_PATH = os.path.join("browser", "appstrings.properties") | 38 _APPSTRINGS_PROPS_PATH = os.path.join("browser", "appstrings.properties") |
38 _STRINGS_XML_PATH = "strings.xml" | 39 _STRINGS_XML_PATH = "strings.xml" |
(...skipping 12 matching lines...) Expand all Loading... | |
51 "yahoo", | 52 "yahoo", |
52 "google", | 53 "google", |
53 "wikipedia", | 54 "wikipedia", |
54 "amazon" | 55 "amazon" |
55 ] | 56 ] |
56 } | 57 } |
57 | 58 |
58 _FIREFOX_REPLACE_STR = "Firefox" | 59 _FIREFOX_REPLACE_STR = "Firefox" |
59 _ABB_REPLACEMENT_STR = "Adblock Browser" | 60 _ABB_REPLACEMENT_STR = "Adblock Browser" |
60 | 61 |
62 # Some string values that contain Firefox such as 'Firefox Sync' shouldn't be | |
63 # replaced, so we keep a list of ids that are exceptions | |
61 _ENTITY_EXCEPTIONS = [ | 64 _ENTITY_EXCEPTIONS = [ |
62 "overlay_no_synced_devices", | 65 "overlay_no_synced_devices", |
63 "home_remote_tabs_need_to_sign_in", | 66 "home_remote_tabs_need_to_sign_in", |
64 "home_remote_tabs_need_to_finish_migrating", | 67 "home_remote_tabs_need_to_finish_migrating", |
65 "home_remote_tabs_need_to_verify", | 68 "home_remote_tabs_need_to_verify", |
66 "syncBrand.fullName.label", | 69 "syncBrand.fullName.label", |
70 "sync.subtitle.connectlocation2.label", | |
71 "sync.subtitle.failmultiple.label", | |
67 "fxaccount_full_label", | 72 "fxaccount_full_label", |
68 "fxaccount_create_account_header2", | 73 "fxaccount_create_account_header2", |
69 "fxaccount_create_account_policy_text2", | 74 "fxaccount_create_account_policy_text2", |
70 "fxaccount_status_header2", | 75 "fxaccount_status_header2", |
71 "fxaccount_status_needs_finish_migrating", | 76 "fxaccount_status_needs_finish_migrating", |
72 "fxaccount_remove_account_dialog_title", | 77 "fxaccount_remove_account_dialog_title", |
73 "fxaccount_remove_account_toast", | 78 "fxaccount_remove_account_toast", |
74 "fxaccount_account_type_label", | 79 "fxaccount_account_type_label", |
75 ] | 80 ] |
76 | 81 |
77 | 82 |
78 def _check_path_exists(path, logger): | 83 def _check_path_exists(path, logger): |
79 if not os.path.exists(path): | 84 if not os.path.exists(path): |
80 logger.fatal("'%s' does not exist" % path) | 85 logger.fatal("'%s' does not exist" % path) |
81 | 86 |
82 | 87 |
83 def _get_locales_from_path(path, locale_re): | 88 def _get_locales_from_path(path, locale_re): |
84 locales = [] | 89 locales = [] |
85 for dir_name in next(os.walk(path))[1]: | 90 for dir_name in next(os.walk(path))[1]: |
86 match = locale_re.match(dir_name) | 91 match = locale_re.match(dir_name) |
87 if match: | 92 if match: |
88 locales.append(match.group(1)) | 93 locales.append(match.group(1)) |
89 locales.sort | 94 locales.sort |
90 return locales | 95 return locales |
91 | 96 |
92 | 97 |
93 def _get_shortname_from_id(needle, engine_ids, engine_names): | 98 def _get_shortname_from_id(needle, engine_ids, engine_names): |
94 # Fuzzy finds needle in engine_ids and returns ShortName | 99 """Fuzzy finds needle in engine_ids and returns ShortName""" |
René Jeschke
2016/07/26 12:49:31
Why do you change a docstring into comment here? S
| |
95 for engine in engine_ids: | 100 for engine in engine_ids: |
96 if engine.startswith(needle): | 101 if engine.startswith(needle): |
97 return engine_names[engine] | 102 return engine_names[engine] |
98 return None | 103 return None |
99 | 104 |
100 | 105 |
101 def _replace_str_re(str, old, new, replacement_re, exceptions=[]): | 106 def _replace_in_value(format_re, str, old, new, exceptions=[]): |
102 match = replacement_re.match(str) | 107 match = format_re.match(str) |
103 if match and match.lastindex > 2: | 108 if match and match.lastindex > 2: |
104 if match.group(2) not in exceptions and old in match.group(3): | 109 # The prefix contains all characters that precedes the value, including |
105 new_str = match.group(1) + match.group(3).replace(old, new) | 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) | |
106 if match.lastindex == 4: | 116 if match.lastindex == 4: |
107 new_str = new_str + match.group(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 | |
108 return new_str | 120 return new_str |
109 return None | 121 return None |
110 | 122 |
111 | 123 |
112 def _write_lines(filename, lines): | 124 def _write_lines(filename, lines): |
113 # Writes lines into file appending \n | 125 """Writes lines into file appending \\n""" |
114 with open(filename, "w") as fd: | 126 with open(filename, "w") as fd: |
115 for l in lines: | 127 for l in lines: |
116 fd.write("%s\n" % l) | 128 fd.write("%s\n" % l) |
117 | 129 |
118 | 130 |
119 def _transform_locale(locale, path, logger): | 131 def _transform_locale(locale, path, logger): |
120 logger.info("Processing locale '%s'..." % locale) | 132 logger.info("Processing locale '%s'..." % locale) |
121 | 133 |
122 # Check for list.txt existence | 134 # Check for list.txt existence |
123 list_file_path = os.path.join(path, _LIST_TXT_PATH) | 135 list_file_path = os.path.join(path, _LIST_TXT_PATH) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 # Append default search engine name | 207 # Append default search engine name |
196 props.append("browser.search.defaultenginename=%s" % engine_order[0]) | 208 props.append("browser.search.defaultenginename=%s" % engine_order[0]) |
197 | 209 |
198 # Append search engine order | 210 # Append search engine order |
199 for i in range(0, min(5, len(engine_order))): | 211 for i in range(0, min(5, len(engine_order))): |
200 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])) |
201 | 213 |
202 # Write back region.properties | 214 # Write back region.properties |
203 _write_lines(region_file_path, props) | 215 _write_lines(region_file_path, props) |
204 | 216 |
205 """Replaces ocurrences of 'Firefox' by 'Adblock Browser' | 217 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in |
206 in appstrings.properties""" | 218 # 'appstrings.properties' |
207 lines = [] | 219 lines = [] |
208 replacement_count = 0 | 220 replacement_count = 0 |
209 | 221 |
210 with open(appstrings_file_path, "r") as fd: | 222 with open(appstrings_file_path, "r") as fd: |
211 for line in fd: | 223 for line in fd: |
212 line = line.rstrip("\r\n") | 224 line = line.rstrip("\r\n") |
213 replacement = _replace_str_re(line, _FIREFOX_REPLACE_STR, | 225 replacement = _replace_in_value(_PROPERTY_FORMAT_RE, line, |
214 _ABB_REPLACEMENT_STR, _PROPERTY_RE) | 226 _FIREFOX_REPLACE_STR, |
227 _ABB_REPLACEMENT_STR) | |
215 if replacement: | 228 if replacement: |
216 line = replacement | 229 line = replacement |
217 replacement_count += 1 | 230 replacement_count += 1 |
218 lines.append(line) | 231 lines.append(line) |
219 | 232 |
220 # Apply changes to appstrings.properties | 233 # Apply changes to appstrings.properties |
221 _write_lines(appstrings_file_path, lines) | 234 _write_lines(appstrings_file_path, lines) |
222 logger.info("Replaced %d ocurrences of %s in 'appstrings.properties'" % | 235 logger.info("Replaced %d ocurrences of %s in 'appstrings.properties'" % |
223 (replacement_count, _FIREFOX_REPLACE_STR)) | 236 (replacement_count, _FIREFOX_REPLACE_STR)) |
224 | 237 |
225 | 238 |
226 def _transform_values_locale(locale, path, logger): | 239 def _transform_values_locale(locale, path, logger): |
227 logger.info("Processing values-%s..." % locale) | 240 logger.info("Processing values-%s..." % locale) |
228 | 241 |
229 # Check for strings.xml existence | 242 # Check for strings.xml existence |
230 strings_file_path = os.path.join(path, _STRINGS_XML_PATH) | 243 strings_file_path = os.path.join(path, _STRINGS_XML_PATH) |
231 _check_path_exists(strings_file_path, logger) | 244 _check_path_exists(strings_file_path, logger) |
232 | 245 |
233 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in strings.xml | 246 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 'strings.xml' |
234 lines = [] | 247 lines = [] |
235 replacement_count = 0 | 248 replacement_count = 0 |
236 | 249 |
237 with open(strings_file_path, "r") as fd: | 250 with open(strings_file_path, "r") as fd: |
238 for line in fd: | 251 for line in fd: |
239 line = line.rstrip("\r\n") | 252 line = line.rstrip("\r\n") |
240 replacement = _replace_str_re(line, _FIREFOX_REPLACE_STR, | 253 replacement = _replace_in_value(_ENTITY_FORMAT_RE, line, |
241 _ABB_REPLACEMENT_STR, _ENTITY_RE, | 254 _FIREFOX_REPLACE_STR, |
242 _ENTITY_EXCEPTIONS) | 255 _ABB_REPLACEMENT_STR, |
256 _ENTITY_EXCEPTIONS) | |
243 if replacement: | 257 if replacement: |
244 line = replacement | 258 line = replacement |
245 replacement_count += 1 | 259 replacement_count += 1 |
246 else: | 260 else: |
247 replacement = _replace_str_re(line, _FIREFOX_REPLACE_STR, | 261 replacement = _replace_in_value(_STRING_FORMAT_RE, line, |
248 _ABB_REPLACEMENT_STR, _STRING_RE) | 262 _FIREFOX_REPLACE_STR, |
263 _ABB_REPLACEMENT_STR) | |
249 if replacement: | 264 if replacement: |
250 line = replacement | 265 line = replacement |
251 replacement_count += 1 | 266 replacement_count += 1 |
252 lines.append(line) | 267 lines.append(line) |
253 | 268 |
254 # Apply changes to strings.xml | 269 # Apply changes to strings.xml |
255 _write_lines(strings_file_path, lines) | 270 _write_lines(strings_file_path, lines) |
256 logger.info("Replaced %d ocurrences of %s in 'strings.xml'" % | 271 logger.info("Replaced %d ocurrences of %s in 'strings.xml'" % |
257 (replacement_count, _FIREFOX_REPLACE_STR)) | 272 (replacement_count, _FIREFOX_REPLACE_STR)) |
258 | 273 |
(...skipping 28 matching lines...) Expand all Loading... | |
287 locale_path = os.path.join(chrome_path, locale, "locale", locale) | 302 locale_path = os.path.join(chrome_path, locale, "locale", locale) |
288 if os.path.exists(locale_path): | 303 if os.path.exists(locale_path): |
289 _transform_locale(locale, locale_path, logger) | 304 _transform_locale(locale, locale_path, logger) |
290 else: | 305 else: |
291 logger.error("Missing folder for locale '%s' in path: %s" % | 306 logger.error("Missing folder for locale '%s' in path: %s" % |
292 (locale, locale_path)) | 307 (locale, locale_path)) |
293 | 308 |
294 for locale in values_locales: | 309 for locale in values_locales: |
295 locale_path = os.path.join(res_path, "values-" + locale) | 310 locale_path = os.path.join(res_path, "values-" + locale) |
296 _transform_values_locale(locale, locale_path, logger) | 311 _transform_values_locale(locale, locale_path, logger) |
LEFT | RIGHT |