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

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

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

Powered by Google App Engine
This is Rietveld