Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: mozharness/abb/transform_locales.py

Issue 29348650: Issue 3199 - "Firefox" string is displayed on several error messages (Closed)
Left Patch Set: Removed unrelated changes and added 2 more entity exceptions Created July 28, 2016, 11:08 p.m.
Right Patch Set: Fixing typo Created Jan. 2, 2017, 4:32 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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})?)$")
Felix Dahlke 2016/12/13 13:06:05 What's the "r" for here?
Felix Dahlke 2016/12/13 13:11:15 Whoops, I was planning to remove this comment once
21 21
22 _SEARCH_PROPS_RE = re.compile("^browser\.search\." 22 _SEARCH_PROPS_RE = re.compile("^browser\.search\."
23 "(defaultenginename|order\.).*$") 23 "(defaultenginename|order\.).*$")
24 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$") 24 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$")
25 25
26 _PROPERTY_RE = re.compile("^(([^=]*)=)(.*)$") 26 _PROPERTY_FORMAT_RE = re.compile("^(([^=]*)=)(.*)$")
27 _ENTITY_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$") 27 _ENTITY_FORMAT_RE = re.compile("^(\s*<!ENTITY\s*([^\"\s]*)\s*\")(.*)(\">)$")
28 _STRING_RE = re.compile("^(\s*<string name=\"([^\"]*)\">)(.*)(</string>)$") 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
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 = [
Felix Dahlke 2016/12/13 13:06:05 Why do we exclude these? Would be helpful to have
diegocarloslima 2016/12/21 13:21:48 These are excluded because they relate to some fea
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",
67 "sync.subtitle.connectlocation2.label", 70 "sync.subtitle.connectlocation2.label",
68 "sync.subtitle.failmultiple.label", 71 "sync.subtitle.failmultiple.label",
69 "fxaccount_full_label", 72 "fxaccount_full_label",
70 "fxaccount_create_account_header2", 73 "fxaccount_create_account_header2",
71 "fxaccount_create_account_policy_text2", 74 "fxaccount_create_account_policy_text2",
(...skipping 21 matching lines...) Expand all
93 96
94 97
95 def _get_shortname_from_id(needle, engine_ids, engine_names): 98 def _get_shortname_from_id(needle, engine_ids, engine_names):
96 """Fuzzy finds needle in engine_ids and returns ShortName""" 99 """Fuzzy finds needle in engine_ids and returns ShortName"""
97 for engine in engine_ids: 100 for engine in engine_ids:
98 if engine.startswith(needle): 101 if engine.startswith(needle):
99 return engine_names[engine] 102 return engine_names[engine]
100 return None 103 return None
101 104
102 105
103 def _replace_str_re(str, old, new, replacement_re, exceptions=[]): 106 def _replace_in_value(format_re, str, old, new, exceptions=[]):
Felix Dahlke 2016/12/13 13:06:06 Took me a little while to wrap my head around what
diegocarloslima 2016/12/21 13:21:48 Acknowledged.
104 match = replacement_re.match(str) 107 match = format_re.match(str)
105 if match and match.lastindex > 2: 108 if match and match.lastindex > 2:
106 if match.group(2) not in exceptions and old in match.group(3): 109 # The prefix contains all characters that precedes the value, including
Felix Dahlke 2016/12/13 13:06:05 Shouldn't `old` be `in match.group(2)` rather than
diegocarloslima 2016/12/21 13:21:48 Actually match.group(2) matches the id/key of the
107 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)
108 if match.lastindex == 4: 116 if match.lastindex == 4:
109 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
110 return new_str 120 return new_str
111 return None 121 return None
112 122
113 123
114 def _write_lines(filename, lines): 124 def _write_lines(filename, lines):
115 """Writes lines into file appending \\n""" 125 """Writes lines into file appending \\n"""
116 with open(filename, "w") as fd: 126 with open(filename, "w") as fd:
117 for l in lines: 127 for l in lines:
118 fd.write("%s\n" % l) 128 fd.write("%s\n" % l)
119 129
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 # Append default search engine name 207 # Append default search engine name
198 props.append("browser.search.defaultenginename=%s" % engine_order[0]) 208 props.append("browser.search.defaultenginename=%s" % engine_order[0])
199 209
200 # Append search engine order 210 # Append search engine order
201 for i in range(0, min(5, len(engine_order))): 211 for i in range(0, min(5, len(engine_order))):
202 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]))
203 213
204 # Write back region.properties 214 # Write back region.properties
205 _write_lines(region_file_path, props) 215 _write_lines(region_file_path, props)
206 216
207 """Replaces ocurrences of 'Firefox' by 'Adblock Browser' 217 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in
Felix Dahlke 2016/12/13 13:06:06 Shouldn't this be a normal comment rather than a d
diegocarloslima 2016/12/21 13:21:48 Actually, I had to make it a multiline comment to
208 in 'appstrings.properties'""" 218 # 'appstrings.properties'
209 lines = [] 219 lines = []
210 replacement_count = 0 220 replacement_count = 0
211 221
212 with open(appstrings_file_path, "r") as fd: 222 with open(appstrings_file_path, "r") as fd:
213 for line in fd: 223 for line in fd:
214 line = line.rstrip("\r\n") 224 line = line.rstrip("\r\n")
215 replacement = _replace_str_re(line, _FIREFOX_REPLACE_STR, 225 replacement = _replace_in_value(_PROPERTY_FORMAT_RE, line,
216 _ABB_REPLACEMENT_STR, _PROPERTY_RE) 226 _FIREFOX_REPLACE_STR,
227 _ABB_REPLACEMENT_STR)
217 if replacement: 228 if replacement:
218 line = replacement 229 line = replacement
219 replacement_count += 1 230 replacement_count += 1
220 lines.append(line) 231 lines.append(line)
221 232
222 # Apply changes to appstrings.properties 233 # Apply changes to appstrings.properties
223 _write_lines(appstrings_file_path, lines) 234 _write_lines(appstrings_file_path, lines)
224 logger.info("Replaced %d ocurrences of %s in 'appstrings.properties'" % 235 logger.info("Replaced %d ocurrences of %s in 'appstrings.properties'" %
225 (replacement_count, _FIREFOX_REPLACE_STR)) 236 (replacement_count, _FIREFOX_REPLACE_STR))
226 237
227 238
228 def _transform_values_locale(locale, path, logger): 239 def _transform_values_locale(locale, path, logger):
229 logger.info("Processing values-%s..." % locale) 240 logger.info("Processing values-%s..." % locale)
230 241
231 # Check for strings.xml existence 242 # Check for strings.xml existence
232 strings_file_path = os.path.join(path, _STRINGS_XML_PATH) 243 strings_file_path = os.path.join(path, _STRINGS_XML_PATH)
233 _check_path_exists(strings_file_path, logger) 244 _check_path_exists(strings_file_path, logger)
234 245
235 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 'strings.xml' 246 # Replaces ocurrences of 'Firefox' by 'Adblock Browser' in 'strings.xml'
236 lines = [] 247 lines = []
237 replacement_count = 0 248 replacement_count = 0
238 249
239 with open(strings_file_path, "r") as fd: 250 with open(strings_file_path, "r") as fd:
240 for line in fd: 251 for line in fd:
241 line = line.rstrip("\r\n") 252 line = line.rstrip("\r\n")
242 replacement = _replace_str_re(line, _FIREFOX_REPLACE_STR, 253 replacement = _replace_in_value(_ENTITY_FORMAT_RE, line,
243 _ABB_REPLACEMENT_STR, _ENTITY_RE, 254 _FIREFOX_REPLACE_STR,
244 _ENTITY_EXCEPTIONS) 255 _ABB_REPLACEMENT_STR,
256 _ENTITY_EXCEPTIONS)
245 if replacement: 257 if replacement:
246 line = replacement 258 line = replacement
247 replacement_count += 1 259 replacement_count += 1
248 else: 260 else:
249 replacement = _replace_str_re(line, _FIREFOX_REPLACE_STR, 261 replacement = _replace_in_value(_STRING_FORMAT_RE, line,
250 _ABB_REPLACEMENT_STR, _STRING_RE) 262 _FIREFOX_REPLACE_STR,
263 _ABB_REPLACEMENT_STR)
251 if replacement: 264 if replacement:
252 line = replacement 265 line = replacement
253 replacement_count += 1 266 replacement_count += 1
254 lines.append(line) 267 lines.append(line)
255 268
256 # Apply changes to strings.xml 269 # Apply changes to strings.xml
257 _write_lines(strings_file_path, lines) 270 _write_lines(strings_file_path, lines)
258 logger.info("Replaced %d ocurrences of %s in 'strings.xml'" % 271 logger.info("Replaced %d ocurrences of %s in 'strings.xml'" %
259 (replacement_count, _FIREFOX_REPLACE_STR)) 272 (replacement_count, _FIREFOX_REPLACE_STR))
260 273
(...skipping 28 matching lines...) Expand all
289 locale_path = os.path.join(chrome_path, locale, "locale", locale) 302 locale_path = os.path.join(chrome_path, locale, "locale", locale)
290 if os.path.exists(locale_path): 303 if os.path.exists(locale_path):
291 _transform_locale(locale, locale_path, logger) 304 _transform_locale(locale, locale_path, logger)
292 else: 305 else:
293 logger.error("Missing folder for locale '%s' in path: %s" % 306 logger.error("Missing folder for locale '%s' in path: %s" %
294 (locale, locale_path)) 307 (locale, locale_path))
295 308
296 for locale in values_locales: 309 for locale in values_locales:
297 locale_path = os.path.join(res_path, "values-" + locale) 310 locale_path = os.path.join(res_path, "values-" + locale)
298 _transform_values_locale(locale, locale_path, logger) 311 _transform_values_locale(locale, locale_path, logger)
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld