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