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