Left: | ||
Right: |
OLD | NEW |
---|---|
1 # This Source Code Form is subject to the terms of the Mozilla Public | 1 # This Source Code Form is subject to the terms of the Mozilla Public |
2 # License, v. 2.0. If a copy of the MPL was not distributed with this | 2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
5 import errno | 5 import errno |
6 import io | 6 import io |
7 import json | 7 import json |
8 import os | 8 import os |
9 import re | 9 import re |
10 from StringIO import StringIO | 10 from StringIO import StringIO |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 ).encode('utf-8') | 183 ).encode('utf-8') |
184 | 184 |
185 | 185 |
186 def toJson(data): | 186 def toJson(data): |
187 return json.dumps( | 187 return json.dumps( |
188 data, ensure_ascii=False, sort_keys=True, | 188 data, ensure_ascii=False, sort_keys=True, |
189 indent=2, separators=(',', ': ') | 189 indent=2, separators=(',', ': ') |
190 ).encode('utf-8') + '\n' | 190 ).encode('utf-8') + '\n' |
191 | 191 |
192 | 192 |
193 def importGeckoLocales(params, files): | 193 def import_string_webext(data, key, source): |
194 """Overwrites data[key] with source.""" | |
195 data[key] = source | |
196 | |
197 | |
198 def import_string_gecko(data, key, value): | |
199 """Only sets {'message': value} in data-dictionary, after stripping | |
Vasily Kuznetsov
2017/08/21 10:43:18
Do you think it would be difficult to make this do
tlucas
2017/08/21 11:33:01
Do you mean full conformity, including params?
For
Vasily Kuznetsov
2017/08/21 11:53:15
Yeah, just rephrase and reformat where appropriate
tlucas
2017/08/21 12:28:50
Done.
| |
200 undesired gecko-style access keys. | |
201 """ | |
202 | |
203 # Remove access keys from possible gecko-style | |
Wladimir Palant
2017/08/21 10:57:21
Nit: Gecko should be capitalized (here and in the
tlucas
2017/08/21 11:33:00
Acknowledged.
tlucas
2017/08/21 12:28:50
Done.
| |
204 # translations | |
205 match = re.search(r'^(.*?)\s*\(&.\)$', value) | |
Wladimir Palant
2017/08/21 10:57:21
For reference, the regexp used by Firefox code is
tlucas
2017/08/21 11:33:00
Acknowledged.
| |
206 if match: | |
207 value = match.group(1) | |
208 else: | |
209 index = value.find('&') | |
210 if index >= 0: | |
211 value = value[0:index] + value[index + 1:] | |
212 | |
213 data[key] = {'message': value} | |
214 | |
215 | |
216 def import_locales(params, files): | |
194 import localeTools | 217 import localeTools |
195 | 218 |
196 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as | 219 # FIXME: localeTools doesn't use real Chrome locales, it uses dash as |
197 # separator instead. | 220 # separator instead. |
198 convert_locale_code = lambda code: code.replace('-', '_') | 221 convert_locale_code = lambda code: code.replace('-', '_') |
199 | 222 |
200 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome | 223 # We need to map Chrome locales to Gecko locales. Start by mapping Chrome |
201 # locales to themselves, merely with the dash as separator. | 224 # locales to themselves, merely with the dash as separator. |
202 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es} | 225 locale_mapping = {convert_locale_code(l): l for l in localeTools.chromeLocal es} |
203 | 226 |
(...skipping 16 matching lines...) Expand all Loading... | |
220 fileName, keys = item | 243 fileName, keys = item |
221 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) | 244 parts = map(lambda n: source if n == '*' else n, fileName.split('/') ) |
222 sourceFile = os.path.join(os.path.dirname(item.source), *parts) | 245 sourceFile = os.path.join(os.path.dirname(item.source), *parts) |
223 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') | 246 incompleteMarker = os.path.join(os.path.dirname(sourceFile), '.incom plete') |
224 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): | 247 if not os.path.exists(sourceFile) or os.path.exists(incompleteMarker ): |
225 continue | 248 continue |
226 | 249 |
227 data = json.loads(files[targetFile].decode('utf-8')) | 250 data = json.loads(files[targetFile].decode('utf-8')) |
228 | 251 |
229 try: | 252 try: |
253 # The WebExtensions (.json) and Gecko format provide | |
254 # translations differently and/or provide additional | |
255 # information like e.g. "placeholders". We want to adhere to | |
256 # that and preserve the addtional info. | |
257 | |
230 if sourceFile.endswith('.json'): | 258 if sourceFile.endswith('.json'): |
231 with io.open(sourceFile, 'r', encoding='utf-8') as handle: | 259 with io.open(sourceFile, 'r', encoding='utf-8') as handle: |
232 sourceData = {k: v['message'] for k, v in json.load(hand le).iteritems()} | 260 sourceData = json.load(handle) |
261 | |
Vasily Kuznetsov
2017/08/21 10:43:18
I don't feel very strong about it, but it seems th
tlucas
2017/08/21 11:33:01
Acknowledged.
tlucas
2017/08/21 12:28:50
Done.
| |
262 import_string = import_string_webext | |
233 else: | 263 else: |
234 sourceData = localeTools.readFile(sourceFile) | 264 sourceData = localeTools.readFile(sourceFile) |
235 | 265 |
266 import_string = import_string_gecko | |
267 | |
236 # Resolve wildcard imports | 268 # Resolve wildcard imports |
237 if keys == '*' or keys == '=*': | 269 if keys == '*' or keys == '=*': |
238 importList = sourceData.keys() | 270 importList = sourceData.keys() |
239 importList = filter(lambda k: not k.startswith('_'), importL ist) | 271 importList = filter(lambda k: not k.startswith('_'), importL ist) |
240 if keys == '=*': | 272 if keys == '=*': |
241 importList = map(lambda k: '=' + k, importList) | 273 importList = map(lambda k: '=' + k, importList) |
242 keys = ' '.join(importList) | 274 keys = ' '.join(importList) |
243 | 275 |
244 for stringID in keys.split(): | 276 for stringID in keys.split(): |
245 noMangling = False | 277 noMangling = False |
246 if stringID.startswith('='): | 278 if stringID.startswith('='): |
247 stringID = stringID[1:] | 279 stringID = stringID[1:] |
248 noMangling = True | 280 noMangling = True |
249 | 281 |
250 if stringID in sourceData: | 282 if stringID in sourceData: |
251 if noMangling: | 283 if noMangling: |
252 key = re.sub(r'\W', '_', stringID) | 284 key = re.sub(r'\W', '_', stringID) |
253 else: | 285 else: |
254 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) | 286 key = re.sub(r'\..*', '', parts[-1]) + '_' + re.sub( r'\W', '_', stringID) |
255 if key in data: | 287 if key in data: |
256 print 'Warning: locale string %s defined multiple ti mes' % key | 288 print 'Warning: locale string %s defined multiple ti mes' % key |
257 | 289 |
258 # Remove access keys | 290 import_string(data, key, sourceData[stringID]) |
259 value = sourceData[stringID] | |
260 match = re.search(r'^(.*?)\s*\(&.\)$', value) | |
261 if match: | |
262 value = match.group(1) | |
263 else: | |
264 index = value.find('&') | |
265 if index >= 0: | |
266 value = value[0:index] + value[index + 1:] | |
267 data[key] = {'message': value} | |
268 except Exception as e: | 291 except Exception as e: |
269 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) | 292 print 'Warning: error importing locale data from %s: %s' % (sour ceFile, e) |
270 | 293 |
271 files[targetFile] = toJson(data) | 294 files[targetFile] = toJson(data) |
272 | 295 |
273 | 296 |
274 def truncate(text, length_limit): | 297 def truncate(text, length_limit): |
275 if len(text) <= length_limit: | 298 if len(text) <= length_limit: |
276 return text | 299 return text |
277 return text[:length_limit - 1].rstrip() + u'\u2026' | 300 return text[:length_limit - 1].rstrip() + u'\u2026' |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 if metadata.has_section('convert_js'): | 396 if metadata.has_section('convert_js'): |
374 convertJS(params, files) | 397 convertJS(params, files) |
375 | 398 |
376 if metadata.has_section('preprocess'): | 399 if metadata.has_section('preprocess'): |
377 files.preprocess( | 400 files.preprocess( |
378 [f for f, _ in metadata.items('preprocess')], | 401 [f for f, _ in metadata.items('preprocess')], |
379 {'needsExt': True} | 402 {'needsExt': True} |
380 ) | 403 ) |
381 | 404 |
382 if metadata.has_section('import_locales'): | 405 if metadata.has_section('import_locales'): |
383 importGeckoLocales(params, files) | 406 import_locales(params, files) |
384 | 407 |
385 files['manifest.json'] = createManifest(params, files) | 408 files['manifest.json'] = createManifest(params, files) |
386 if type == 'chrome': | 409 if type == 'chrome': |
387 fixTranslationsForCWS(files) | 410 fixTranslationsForCWS(files) |
388 | 411 |
389 if devenv: | 412 if devenv: |
390 import buildtools | 413 import buildtools |
391 import random | 414 import random |
392 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js') | 415 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js '), relpath='devenvPoller__.js') |
393 files['devenvVersion__'] = str(random.random()) | 416 files['devenvVersion__'] = str(random.random()) |
394 | 417 |
395 if metadata.has_option('general', 'testScripts'): | 418 if metadata.has_option('general', 'testScripts'): |
396 files['qunit/index.html'] = createScriptPage( | 419 files['qunit/index.html'] = createScriptPage( |
397 params, 'testIndex.html.tmpl', ('general', 'testScripts') | 420 params, 'testIndex.html.tmpl', ('general', 'testScripts') |
398 ) | 421 ) |
399 | 422 |
400 zipdata = files.zipToString() | 423 zipdata = files.zipToString() |
401 signature = None | 424 signature = None |
402 pubkey = None | 425 pubkey = None |
403 if keyFile != None: | 426 if keyFile != None: |
404 signature = signBinary(zipdata, keyFile) | 427 signature = signBinary(zipdata, keyFile) |
405 pubkey = getPublicKey(keyFile) | 428 pubkey = getPublicKey(keyFile) |
406 writePackage(outFile, pubkey, signature, zipdata) | 429 writePackage(outFile, pubkey, signature, zipdata) |
OLD | NEW |