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: Now really writing region.properties to region.properties. Created Sept. 15, 2015, 4:48 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 "wikipedia"]}
40 }
41
42
43 def abb_get_shortname_from_id(needle, engine_ids, engine_map):
44 regex = re.compile("^%s.*$" % needle)
45 for engine in engine_ids:
46 if regex.match(engine.lower()):
47 return engine_map[engine]
48 return None
49
50
51 def abb_transform_locale(locale, path, fns):
52 fns["info"]("Processing locale '%s'..." % locale)
53
54 # Get configuration for current locale
55 cfg = ABB_CFG.get(locale, ABB_CFG[ABB_DEFAULT_LOCALE])
56
57 # Check for list.txt existence
58 list_file = os.path.join(path, ABB_LIST_TXT_PATH)
59 if not os.path.exists(list_file):
60 fns["fatal"]("Missing 'list.txt' for locale '%s'" % locale)
61
62 # Check for region.properties existence
63 region_file = os.path.join(path, ABB_REGION_PROPS_PATH)
64 if not os.path.exists(region_file):
65 fns["fatal"]("Missing 'region.properties' for locale '%s'" % locale)
66
67 # Get whitelist and build regex
68 whitelist = cfg["ordered-whitelist"]
69 white_re = re.compile("^%s.*$" % (".*|".join(whitelist)))
70
71 # Read engine IDs from list.txt, discard engines not on whitelist
72 engine_ids = []
73 for line in open(list_file, "r"):
74 line = line.strip()
75 if len(line) > 0:
76 if white_re.match(line.lower()):
77 engine_ids.append(line)
78 else:
79 fns["info"]("Removing '%s'" % line)
80
81 # Make sure we still have search engines left
82 if len(engine_ids) == 0:
83 fns["fatal"]("No search engines left over for '%s'" % locale)
84
85 # 'Parse' XML to get matching 'ShortName' for all engine IDs
86 engine_names = {}
87 for eid in engine_ids:
88 xml_file = os.path.join(path, ABB_SEARCHPLUGINS_PATH, "%s.xml" % eid)
89 if os.path.exists(xml_file):
90 short_name = None
91 for line in open(xml_file, "r"):
92 line = line.strip()
93 if ABB_SHORTNAME_RE.match(line):
94 short_name = line[11:-12]
95 if not short_name:
96 fns["fatal"]("No ShortName defined for '%s' in '%s" %
97 (eid, locale))
98 engine_names[eid] = short_name
99 else:
100 fns["fatal"]("XML definiton for '%s' in '%s' missing" %
101 (eid, locale))
102
103 fns["info"]("Remaining engine IDs: %s" % ", ".join(engine_ids))
104
105 # Create search engine order with real engine names
106 engine_order = []
107 for eid in whitelist:
108 sn = abb_get_shortname_from_id(eid, engine_ids, engine_names)
109 if sn:
110 engine_order.append(sn)
111
112 fns["info"]("Resulting ordered list %s" % (", ".join(engine_order)))
113
114 # Read region.properties and remove browser.search.* lines
115 props = []
116 for line in open(region_file, "r"):
117 line = line.rstrip("\r\n")
118 if not ABB_SEARCH_PROPS_RE.match(line.strip()):
119 props.append(line)
120
121 # Append default search engine name
122 props.append("browser.search.defaultenginename=%s" % engine_order[0])
123
124 # Append search engine order
125 for i in range(0, len(engine_order)):
126 props.append("browser.search.order.%d=%s" % (i + 1, engine_order[i]))
127
128 # Write back list.txt
129 with open(list_file, "w") as fd:
130 for l in engine_ids:
131 fd.write("%s\n" % l)
132
133 # Write back region.properties
134 with open(region_file, "w") as fd:
135 for l in props:
136 fd.write("%s\n" % l)
137
138
139 def abb_print_info(obj):
140 """ Wrapper for 'self.info' (to be self-contained) """
141 def fn(s):
142 if obj:
143 obj.info(s)
144 else:
145 print "INFO: %s" % s
146 return fn
147
148
149 def abb_print_error(obj):
150 """ Wrapper for 'self.error' (to be self-contained) """
151 def fn(s):
152 if obj:
153 obj.error(s)
154 else:
155 print "ERROR: %s" % s
156 return fn
157
158
159 def abb_exit_fatal(obj):
160 """ Wrapper for 'self.fatal' (to be self-contained) """
161 def fn(s):
162 if obj:
163 obj.fatal(s)
164 else:
165 print "FATAL: %s" % s
166 exit(1)
167 return fn
168
169
170 def abb_transform_locales_impl(obj_dir, obj):
171 fns = {"info": abb_print_info(obj),
172 "error": abb_print_error(obj),
173 "fatal": abb_exit_fatal(obj)}
174
175 chrome_path = os.path.join(obj_dir, "dist", "bin", "chrome")
176 if not os.path.exists(chrome_path):
177 fns["fatal"]("'dist/bin/chrome' not existent in '%s'" % obj_dir)
178
179 locales = []
180 for p in next(os.walk(chrome_path))[1]:
181 if ABB_LOCALE_RE.match(p):
182 locales.append(p)
183 locales.sort()
184
185 fns["info"]("Found %d locales" % len(locales))
186
187 for locale in locales:
188 locale_path = os.path.join(chrome_path, locale, "locale", locale)
189 if os.path.exists(locale_path):
190 abb_transform_locale(locale, locale_path, fns)
191 else:
192 fns["error"]("Missing 'locale' folder for '%s'" % locale)
193
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