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

Unified Diff: packagerGecko.py

Issue 6590020945182720: Issue 1707 - Allow importing Chrome-style locales in Firefox extensions (Closed)
Patch Set: Addressed comment Created Dec. 17, 2014, 1:25 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packagerGecko.py
===================================================================
--- a/packagerGecko.py
+++ b/packagerGecko.py
@@ -1,15 +1,22 @@
# coding: utf-8
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
-import os, sys, re, hashlib, base64, urllib, json
+import os
+import sys
+import re
+import hashlib
+import base64
+import urllib
+import json
+import io
from ConfigParser import SafeConfigParser
from StringIO import StringIO
import xml.dom.minidom as minidom
import buildtools.localeTools as localeTools
import packager
from packager import readMetadata, getMetadataPath, getDefaultFileName, getBuildVersion, getTemplate, Files
@@ -51,16 +58,19 @@ def getPackageFiles(params):
for file in os.listdir(baseDir):
if file.endswith('.js') or file.endswith('.xml'):
result.add(file)
return result
def getIgnoredFiles(params):
return {'.incomplete', 'meta.properties'}
+def archive_path(path, baseDir):
+ return '/'.join(os.path.split(os.path.relpath(path, baseDir)))
+
def isValidLocale(localesDir, dir, includeIncomplete=False):
if re.search(r'[^\w\-]', dir):
return False
curLocaleDir = os.path.join(localesDir, dir)
if not os.path.isdir(curLocaleDir):
return False
if len(os.listdir(curLocaleDir)) == 0:
return False
@@ -138,30 +148,59 @@ def createManifest(params):
template = getTemplate('install.rdf.tmpl', autoEscape=True)
templateData = dict(params)
templateData['localeMetadata'] = readLocaleMetadata(params['baseDir'], params['locales'])
initTranslators(templateData['localeMetadata'])
templateData['KNOWN_APPS'] = KNOWN_APPS
templateData['defaultLocale'] = defaultLocale
return template.render(templateData).encode('utf-8')
+def importLocales(params, files):
+ SECTION = 'import_locales'
+ if not params['metadata'].has_section(SECTION):
+ return
+
+ import localeTools
+
+ for locale in params['locales']:
+ for item in params['metadata'].items(SECTION):
+ path, keys = item
+ parts = [locale if p == '*' else p for p in path.split('/')]
+ source = os.path.join(os.path.dirname(item.source), *parts)
+ if not os.path.exists(source):
+ continue
+
+ with io.open(source, 'r', encoding='utf-8') as handle:
+ data = json.load(handle)
+
+ target_name = os.path.splitext(os.path.basename(source))[0] + '.properties'
+ target = archive_path(os.path.join(getLocalesDir(params['baseDir']), locale, target_name), params['baseDir'])
+
+ files[target] = ''
+ for key, value in sorted(data.items()):
+ message = value['message']
+ files[target] += localeTools.generateStringEntry(key, message, target).encode('utf-8')
+
def fixupLocales(params, files):
global defaultLocale
- # Read in default locale data, it might not be included in files
+ # Read in default locale data, it might not be included in package files
defaultLocaleDir = os.path.join(getLocalesDir(params['baseDir']), defaultLocale)
+ reference_files = Files(getPackageFiles(params), getIgnoredFiles(params))
+ reference_files.read(defaultLocaleDir, archive_path(defaultLocaleDir, params['baseDir']))
+ reference_params = dict(params)
+ reference_params['locales'] = [defaultLocale]
+ importLocales(reference_params, reference_files)
+
reference = {}
- ignoredFiles = getIgnoredFiles(params)
- for file in os.listdir(defaultLocaleDir):
- path = os.path.join(defaultLocaleDir, file)
- if file in ignoredFiles or not os.path.isfile(path):
- continue
- data = localeTools.readFile(path)
+ for path, data in reference_files.iteritems():
+ filename = path.split('/')[-1]
+ data = localeTools.parseString(data.decode('utf-8'), filename)
if data:
- reference[file] = data
+ reference[filename] = data
for locale in params['locales']:
for file in reference.iterkeys():
path = 'chrome/locale/%s/%s' % (locale, file)
if path in files:
data = localeTools.parseString(files[path].decode('utf-8'), path)
for key, value in reference[file].iteritems():
if not key in data:
@@ -303,16 +342,17 @@ def createBuild(baseDir, type="gecko", o
process=lambda path, data: processFile(path, data, params))
files['install.rdf'] = createManifest(params)
if metadata.has_section('mapping'):
files.readMappedFiles(metadata.items('mapping'))
files.read(baseDir, skip=('chrome'))
for name, path in getChromeSubdirs(baseDir, params['locales']).iteritems():
if os.path.isdir(path):
files.read(path, 'chrome/%s' % name)
+ importLocales(params, files)
fixupLocales(params, files)
if not 'bootstrap.js' in files:
addMissingFiles(params, files)
if metadata.has_section('preprocess'):
files.preprocess([f for f, _ in metadata.items('preprocess')])
if keyFile:
signFiles(files, keyFile)
files.zip(outFile, sortKey=lambda x: '!' if x == 'META-INF/zigbert.rsa' else x)
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld