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

Side by Side Diff: mozharness/abb/transform_locales.py

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

Powered by Google App Engine
This is Rietveld