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

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

Issue 29327949: Issue 3047 - Change default search engines (Closed)
Patch Set: Removed a comma. Created Sept. 15, 2015, 5:45 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 | « 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 # vim:fileencoding=utf-8:et:ts=4:sts=4
2 #
3 # This file is part of Adblock Plus
4 # Copyright (C) 2006-2015 Eyeo GmbH
5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17
18 import os
19 import re
20
21 ABB_LOCALE_RE = re.compile("^[a-z]{2}(-[A-Z]{2})?$")
22 ABB_SEARCH_PROPS_RE = re.compile("^browser\.search\."
23 "(defaultenginename|order\.).*$")
24 ABB_SHORTNAME_RE = re.compile("^<ShortName>.*</ShortName>$")
25
26 ABB_SEARCHPLUGINS_PATH = os.path.join("browser", "searchplugins")
27 ABB_LIST_TXT_PATH = os.path.join(ABB_SEARCHPLUGINS_PATH, "list.txt")
28 ABB_REGION_PROPS_PATH = os.path.join("browser", "region.properties")
29
30 ABB_DEFAULT_LOCALE = "en-US"
31
32 ABB_CFG = {
33 "en-US": {"ordered-whitelist": ["duckduckgo",
34 "yahoo",
35 "google",
36 "wikipedia",
37 "amazon"]},
38 "zh-CN": {"ordered-whitelist": ["baidu",
39 "duckduckgo",
40 "yahoo",
41 "google",
42 "wikipedia",
43 "amazon"]}
44 }
45
46
47 def abb_get_shortname_from_id(needle, engine_ids, engine_map):
48 regex = re.compile("^%s.*$" % needle)
49 for engine in engine_ids:
50 if regex.match(engine.lower()):
51 return engine_map[engine]
52 return None
53
54
55 def abb_transform_locale(locale, path, fns):
56 fns["info"]("Processing locale '%s'..." % locale)
57
58 # Get configuration for current locale
59 cfg = ABB_CFG.get(locale, ABB_CFG[ABB_DEFAULT_LOCALE])
60
61 # Check for list.txt existence
62 list_file = os.path.join(path, ABB_LIST_TXT_PATH)
63 if not os.path.exists(list_file):
64 fns["fatal"]("Missing 'list.txt' for locale '%s'" % locale)
65
66 # Check for region.properties existence
67 region_file = os.path.join(path, ABB_REGION_PROPS_PATH)
68 if not os.path.exists(region_file):
69 fns["fatal"]("Missing 'region.properties' for locale '%s'" % locale)
70
71 # Get whitelist and build regex
72 whitelist = cfg["ordered-whitelist"]
73 white_re = re.compile("^%s.*$" % (".*|".join(whitelist)))
74
75 # Read engine IDs from list.txt, discard engines not on whitelist
76 engine_ids = []
77 for line in open(list_file, "r"):
78 line = line.strip()
79 if len(line) > 0:
80 if white_re.match(line.lower()):
81 engine_ids.append(line)
82 else:
83 fns["info"]("Removing '%s'" % line)
84
85 # Make sure we still have search engines left
86 if len(engine_ids) == 0:
87 fns["fatal"]("No search engines left over for '%s'" % locale)
88
89 # 'Parse' XML to get matching 'ShortName' for all engine IDs
90 engine_names = {}
91 for eid in engine_ids:
92 xml_file = os.path.join(path, ABB_SEARCHPLUGINS_PATH, "%s.xml" % eid)
93 if os.path.exists(xml_file):
94 short_name = None
95 for line in open(xml_file, "r"):
96 line = line.strip()
97 if ABB_SHORTNAME_RE.match(line):
98 short_name = line[11:-12]
99 if not short_name:
100 fns["fatal"]("No ShortName defined for '%s' in '%s" %
101 (eid, locale))
102 engine_names[eid] = short_name
103 else:
104 fns["fatal"]("XML definiton for '%s' in '%s' missing" %
105 (eid, locale))
106
107 fns["info"]("Remaining engine IDs: %s" % ", ".join(engine_ids))
108
109 # Create search engine order with real engine names
110 engine_order = []
111 for eid in whitelist:
112 sn = abb_get_shortname_from_id(eid, engine_ids, engine_names)
113 if sn:
114 engine_order.append(sn)
115
116 fns["info"]("Resulting ordered list: %s" % (", ".join(engine_order)))
117
118 # Read region.properties and remove browser.search.* lines
119 props = []
120 for line in open(region_file, "r"):
121 line = line.rstrip("\r\n")
122 if not ABB_SEARCH_PROPS_RE.match(line.strip()):
123 props.append(line)
124
125 # Append default search engine name
126 props.append("browser.search.defaultenginename=%s" % engine_order[0])
127
128 # Append search engine order
129 for i in range(0, min(3, len(engine_order))):
130 props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i]))
131
132 # Write back list.txt
133 with open(list_file, "w") as fd:
134 for l in engine_ids:
135 fd.write("%s\n" % l)
136
137 # Write back region.properties
138 with open(region_file, "w") as fd:
139 for l in props:
140 fd.write("%s\n" % l)
141
142
143 def abb_print_info(obj):
144 """ Wrapper for 'self.info' (to be self-contained) """
145 def fn(s):
146 if obj:
147 obj.info(s)
148 else:
149 print "INFO: %s" % s
150 return fn
151
152
153 def abb_print_error(obj):
154 """ Wrapper for 'self.error' (to be self-contained) """
155 def fn(s):
156 if obj:
157 obj.error(s)
158 else:
159 print "ERROR: %s" % s
160 return fn
161
162
163 def abb_exit_fatal(obj):
164 """ Wrapper for 'self.fatal' (to be self-contained) """
165 def fn(s):
166 if obj:
167 obj.fatal(s)
168 else:
169 print "FATAL: %s" % s
170 exit(1)
171 return fn
172
173
174 def abb_transform_locales_impl(obj_dir, obj):
175 fns = {"info": abb_print_info(obj),
176 "error": abb_print_error(obj),
177 "fatal": abb_exit_fatal(obj)}
178
179 chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome")
180 if not os.path.exists(chrome_path):
181 fns["fatal"]("'dist/bin/chrome' not existent in '%s'" % obj_dir)
182
183 locales = []
184 for p in next(os.walk(chrome_path))[1]:
185 if ABB_LOCALE_RE.match(p):
186 locales.append(p)
187 locales.sort()
188
189 fns["info"]("Found %d locales" % len(locales))
190
191 for locale in locales:
192 locale_path = os.path.join(chrome_path, locale, "locale", locale)
193 if os.path.exists(locale_path):
194 abb_transform_locale(locale, locale_path, fns)
195 else:
196 fns["error"]("Missing 'locale' folder for '%s'" % locale)
197
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