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

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

Issue 29327949: Issue 3047 - Change default search engines (Closed)
Left Patch Set: Names and stuff Created Sept. 18, 2015, 9:46 a.m.
Right Patch Set: Fixed even more review issues Created Sept. 22, 2015, 10:53 a.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 | « mozharness/abb/__init__.py ('k') | mozharness/mozilla/l10n/multi_locale_build.py » ('j') | 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}(-[A-Z]{2})?$") 19 _LOCALE_RE = re.compile("^[a-z]{2}(-[A-Z]{2})?$")
20 _SEARCH_PROPS_RE = re.compile("^browser\.search\." 20 _SEARCH_PROPS_RE = re.compile("^browser\.search\."
21 "(defaultenginename|order\.).*$") 21 "(defaultenginename|order\.).*$")
22 _SHORTNAME_RE = re.compile("^<ShortName>.*</ShortName>$") 22 _SHORTNAME_RE = re.compile("^<ShortName>(.*)</ShortName>$")
23 23
24 _SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins") 24 _SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins")
25 _LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt") 25 _LIST_TXT_PATH = os.path.join(_SEARCHPLUGINS_PATH, "list.txt")
26 _REGION_PROPS_PATH = os.path.join("browser", "region.properties") 26 _REGION_PROPS_PATH = os.path.join("browser", "region.properties")
27 27
28 _DEFAULT_LOCALE = "en-US" 28 _DEFAULT_LOCALE = "en-US"
29 29
30 _SEARCH_ENGINE_ORDER = { 30 _SEARCH_ENGINE_ORDER = {
31 "en-US": ["duckduckgo", 31 "en-US": ["duckduckgo",
32 "yahoo", 32 "yahoo",
33 "google", 33 "google",
34 "wikipedia", 34 "wikipedia",
35 "amazon"], 35 "amazon"
36 ],
36 "zh-CN": ["baidu", 37 "zh-CN": ["baidu",
37 "duckduckgo", 38 "duckduckgo",
38 "yahoo", 39 "yahoo",
39 "google", 40 "google",
40 "wikipedia", 41 "wikipedia",
41 "amazon"] 42 "amazon"
43 ]
42 } 44 }
43 45
44 46
45 def _get_shortname_from_id(needle, engine_ids, engine_names): 47 def _get_shortname_from_id(needle, engine_ids, engine_names):
46 """Fuzzy finds needle in engine_ids and returns ShortName""" 48 """Fuzzy finds needle in engine_ids and returns ShortName"""
47 regex = re.compile("^%s.*$" % needle)
48 for engine in engine_ids: 49 for engine in engine_ids:
49 if regex.match(engine.lower()): 50 if engine.startswith(needle):
50 return engine_names[engine] 51 return engine_names[engine]
51 return None 52 return None
52 53
53 54
54 def _write_lines(filename, lines): 55 def _write_lines(filename, lines):
55 """Writes lines into file appending \\n""" 56 """Writes lines into file appending \\n"""
56 with open(filename, "w") as fd: 57 with open(filename, "w") as fd:
57 for l in lines: 58 for l in lines:
58 fd.write("%s\n" % l) 59 fd.write("%s\n" % l)
59 60
60 61
61 def _transform_locale(locale, path, fns): 62 def _transform_locale(locale, path, logger):
62 fns["info"]("Processing locale '%s'..." % locale) 63 logger.info("Processing locale '%s'..." % locale)
63 64
64 # Check for list.txt existence 65 # Check for list.txt existence
65 list_file_path = os.path.join(path, _LIST_TXT_PATH) 66 list_file_path = os.path.join(path, _LIST_TXT_PATH)
66 if not os.path.exists(list_file_path): 67 if not os.path.exists(list_file_path):
67 fns["fatal"]("Missing 'list.txt' for locale '%s'" % locale) 68 logger.fatal("Missing 'list.txt' for locale '%s'" % locale)
68 69
69 # Check for region.properties existence 70 # Check for region.properties existence
70 region_file_path = os.path.join(path, _REGION_PROPS_PATH) 71 region_file_path = os.path.join(path, _REGION_PROPS_PATH)
71 if not os.path.exists(region_file_path): 72 if not os.path.exists(region_file_path):
72 fns["fatal"]("Missing 'region.properties' for locale '%s'" % locale) 73 logger.fatal("Missing 'region.properties' for locale '%s'" % locale)
73 74
74 # Get whitelist and build regex 75 # Get whitelist and build regex
75 whitelist = _SEARCH_ENGINE_ORDER.get(locale, 76 whitelist = _SEARCH_ENGINE_ORDER.get(locale,
76 _SEARCH_ENGINE_ORDER[_DEFAULT_LOCALE]) 77 _SEARCH_ENGINE_ORDER[_DEFAULT_LOCALE])
77 white_re = re.compile("^(%s).*$" % "|".join(whitelist)) 78 white_re = re.compile("^(%s).*$" % "|".join(whitelist))
78 79
79 # Read engine IDs from list.txt, discard engines not on whitelist 80 # Read engine IDs from list.txt, discard engines not on whitelist
80 engine_ids = [] 81 engine_ids = []
81 with open(list_file_path, "r") as fd: 82 with open(list_file_path, "r") as fd:
82 for line in fd: 83 for line in fd:
83 line = line.strip() 84 line = line.strip()
84 if len(line) > 0: 85 if len(line) > 0:
85 if white_re.match(line.lower()): 86 if white_re.match(line):
86 engine_ids.append(line) 87 engine_ids.append(line)
87 else: 88 else:
88 fns["info"]("Removing '%s'" % line) 89 logger.info("Removing '%s'" % line)
89 90
90 # Make sure we still have search engines left 91 # Make sure we still have search engines left
91 if len(engine_ids) == 0: 92 if len(engine_ids) == 0:
92 fns["fatal"]("No search engines left over for '%s'" % locale) 93 logger.fatal("No search engines left over for '%s'" % locale)
93 94
94 # 'Parse' XML to get matching 'ShortName' for all engine IDs 95 # 'Parse' XML to get matching 'ShortName' for all engine IDs
95 engine_names = {} 96 engine_names = {}
96 for eid in engine_ids: 97 for eid in engine_ids:
97 xml_file_path = os.path.join(path, _SEARCHPLUGINS_PATH, "%s.xml" % eid) 98 xml_file_path = os.path.join(path, _SEARCHPLUGINS_PATH, "%s.xml" % eid)
98 if os.path.exists(xml_file_path): 99 if os.path.exists(xml_file_path):
99 short_name = None 100 short_name = None
100 with open(xml_file_path, "r") as fd: 101 with open(xml_file_path, "r") as fd:
101 for line in fd: 102 for line in fd:
102 line = line.strip() 103 line = line.strip()
103 if _SHORTNAME_RE.match(line): 104 match = _SHORTNAME_RE.match(line)
104 short_name = line[11:-12].trim() 105 if match:
106 short_name = match.group(1).strip()
105 107
106 if not short_name: 108 if not short_name:
107 fns["fatal"]("No ShortName defined for '%s' in '%s" % 109 logger.fatal("No ShortName defined for '%s' in '%s" %
108 (eid, locale)) 110 (eid, locale))
109 engine_names[eid] = short_name 111 engine_names[eid] = short_name
110 else: 112 else:
111 fns["fatal"]("XML definiton for '%s' in '%s' missing" % 113 logger.fatal("XML definiton for '%s' in '%s' missing" %
112 (eid, locale)) 114 (eid, locale))
113 115
114 fns["info"]("Remaining engine IDs: %s" % ", ".join(engine_ids)) 116 logger.info("Remaining engine IDs: %s" % ", ".join(engine_ids))
115 117
116 # Create search engine order with real engine names 118 # Create search engine order with real engine names
117 engine_order = [] 119 engine_order = []
118 for eid in whitelist: 120 for eid in whitelist:
119 sn = _get_shortname_from_id(eid, engine_ids, engine_names) 121 sn = _get_shortname_from_id(eid, engine_ids, engine_names)
120 if sn: 122 if sn:
121 engine_order.append(sn) 123 engine_order.append(sn)
122 124
123 fns["info"]("Resulting ordered list: %s" % (", ".join(engine_order))) 125 logger.info("Resulting ordered list: %s" % (", ".join(engine_order)))
124 126
125 # Read region.properties and remove browser.search.* lines 127 # Read region.properties and remove browser.search.* lines
126 props = [] 128 props = []
127 with open(region_file_path, "r") as fd: 129 with open(region_file_path, "r") as fd:
128 for line in fd: 130 for line in fd:
129 line = line.rstrip("\r\n") 131 line = line.rstrip("\r\n")
130 if not _SEARCH_PROPS_RE.match(line.strip()): 132 if not _SEARCH_PROPS_RE.match(line.strip()):
131 props.append(line) 133 props.append(line)
132 134
133 # Append default search engine name 135 # Append default search engine name
134 props.append("browser.search.defaultenginename=%s" % engine_order[0]) 136 props.append("browser.search.defaultenginename=%s" % engine_order[0])
135 137
136 # Append search engine order 138 # Append search engine order
137 for i in range(0, min(3, len(engine_order))): 139 for i in range(0, min(3, len(engine_order))):
138 props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i])) 140 props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i]))
139 141
140 # Write back list.txt 142 # Write back list.txt
141 _write_lines(list_file_path, engine_ids) 143 _write_lines(list_file_path, engine_ids)
142 144
143 # Write back region.properties 145 # Write back region.properties
144 _write_lines(region_file_path, props) 146 _write_lines(region_file_path, props)
145 147
146 148
147 def _print_info(obj): 149 class MinimalLogger:
148 """ Wrapper for 'self.info' (to be self-contained) """ 150 def info(self, s):
149 def fn(s): 151 print "INFO: %s" % s
150 if obj: 152
151 obj.info(s) 153 def error(self, s):
152 else: 154 print "ERROR: %s" % s
153 print "INFO: %s" % s 155
154 return fn 156 def fatal(self, s):
157 print "FATAL: %s" % s
158 exit(1)
155 159
156 160
157 def _print_error(obj): 161 def transform_locales(obj_dir, logger=MinimalLogger()):
158 """ Wrapper for 'self.error' (to be self-contained) """
159 def fn(s):
160 if obj:
161 obj.error(s)
162 else:
163 print "ERROR: %s" % s
164 return fn
165
166
167 def _exit_fatal(obj):
168 """ Wrapper for 'self.fatal' (to be self-contained) """
169 def fn(s):
170 if obj:
171 obj.fatal(s)
172 else:
173 print "FATAL: %s" % s
174 exit(1)
175 return fn
176
177
178 def transform_locales(build_object, obj_dir):
179 fns = {"info": _print_info(build_object),
180 "error": _print_error(build_object),
181 "fatal": _exit_fatal(build_object)}
182
183 chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome") 162 chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome")
184 if not os.path.exists(chrome_path): 163 if not os.path.exists(chrome_path):
185 fns["fatal"]("'%s' does not exist" % obj_dir) 164 logger.fatal("'%s' does not exist" % obj_dir)
186 165
187 locales = [] 166 locales = []
188 for p in next(os.walk(chrome_path))[1]: 167 for p in next(os.walk(chrome_path))[1]:
189 if _LOCALE_RE.match(p): 168 if _LOCALE_RE.match(p):
190 locales.append(p) 169 locales.append(p)
191 locales.sort() 170 locales.sort()
192 171
193 fns["info"]("Found %d locales" % len(locales)) 172 logger.info("Found %d locales" % len(locales))
194 173
195 for locale in locales: 174 for locale in locales:
196 locale_path = os.path.join(chrome_path, locale, "locale", locale) 175 locale_path = os.path.join(chrome_path, locale, "locale", locale)
197 if os.path.exists(locale_path): 176 if os.path.exists(locale_path):
198 _transform_locale(locale, locale_path, fns) 177 _transform_locale(locale, locale_path, logger)
199 else: 178 else:
200 fns["error"]("Missing 'locale' folder for '%s'" % locale) 179 logger.error("Missing 'locale' folder for '%s'" % locale)
201 180
LEFTRIGHT

Powered by Google App Engine
This is Rietveld