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

Unified Diff: sitescripts/extensions/bin/legacy/packagerSafari.py

Issue 29589691: Issue 5757 - Update buildtools dependency, (re)move legacy extensions (Closed)
Patch Set: Created Oct. 26, 2017, 10:50 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
Index: sitescripts/extensions/bin/legacy/packagerSafari.py
diff --git a/sitescripts/extensions/bin/legacy/packagerSafari.py b/sitescripts/extensions/bin/legacy/packagerSafari.py
new file mode 100644
index 0000000000000000000000000000000000000000..35ab6d63bb4c4a386c512b6e9eb01227a2e355f2
--- /dev/null
+++ b/sitescripts/extensions/bin/legacy/packagerSafari.py
@@ -0,0 +1,223 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
tlucas 2017/11/02 10:03:29 This code was (apart from the below exception) com
Vasily Kuznetsov 2017/11/30 17:34:00 Acknowledged.
+# 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 base64
+import collections
+import ConfigParser
+import json
+import os
+import re
+from urlparse import urlparse
+
+from buildtools.packager import readMetadata, getDefaultFileName, getBuildVersion, getTemplate, Files
+from buildtools.packagerChrome import import_locales, getIgnoredFiles, getPackageFiles, defaultLocale, createScriptPage
+
+
+def processFile(path, data, params):
+ return data
+
+
+def createManifest(params, files):
+ template = getTemplate('Info.plist.tmpl', autoEscape=True)
+ metadata = params['metadata']
+ catalog = json.loads(files['_locales/%s/messages.json' % defaultLocale])
+
+ def parse_section(section, depth=1):
+ result = {}
+
+ if not metadata.has_section(section):
+ return result
+
+ for opt in metadata.options(section):
+ bits = opt.split('_', depth)
+ key = bits.pop().replace('_', ' ').title()
+ val = metadata.get(section, opt)
+
+ try:
+ val = int(val)
+ except ValueError:
+ try:
+ val = float(val)
+ except ValueError:
+ pass
+
+ reduce(lambda d, x: d.setdefault(x, {}), bits, result)[key] = val
+
+ return result
+
+ def get_optional(*args):
+ try:
+ return metadata.get(*args)
+ except ConfigParser.Error:
+ return None
+
+ allowedDomains = set()
+ allowAllDomains = False
+ allowSecurePages = False
+
+ for perm in metadata.get('general', 'permissions').split():
+ if perm == '<all_urls>':
+ allowAllDomains = True
+ allowSecurePages = True
+ continue
+
+ url = urlparse(perm)
+
+ if url.scheme == 'https':
+ allowSecurePages = True
+ elif url.scheme != 'http':
+ continue
+
+ if '*' in url.hostname:
+ allowAllDomains = True
+ continue
+
+ allowedDomains.add(url.hostname)
+
+ return template.render(
+ basename=metadata.get('general', 'basename'),
+ version=params['version'],
+ releaseBuild=params['releaseBuild'],
+ name=catalog['name']['message'],
+ description=catalog['description']['message'],
+ author=get_optional('general', 'author'),
+ homepage=get_optional('general', 'homepage'),
+ updateURL=get_optional('general', 'updateURL'),
+ allowedDomains=allowedDomains,
+ allowAllDomains=allowAllDomains,
+ allowSecurePages=allowSecurePages,
+ startScripts=(get_optional('contentScripts', 'document_start') or '').split(),
+ endScripts=(get_optional('contentScripts', 'document_end') or '').split(),
+ menus=parse_section('menus', 2),
+ toolbarItems=parse_section('toolbar_items'),
+ popovers=parse_section('popovers'),
+ developerIdentifier=params.get('developerIdentifier')
+ ).encode('utf-8')
+
+
+def createInfoModule(params):
+ template = getTemplate('safariInfo.js.tmpl')
+ return template.render(params).encode('utf-8')
+
+
+def _get_sequence(data):
+ from Crypto.Util import asn1
+ sequence = asn1.DerSequence()
+ sequence.decode(data)
+ return sequence
+
+
+def get_developer_identifier(certs):
+ for cert in certs:
+ # See https://tools.ietf.org/html/rfc5280#section-4
+ tbscertificate = _get_sequence(base64.b64decode(cert))[0]
+ subject = _get_sequence(tbscertificate)[5]
+
+ # We could decode the subject but since we have to apply a regular
+ # expression on CN entry anyway we can just skip that.
+ m = re.search(r'Safari Developer: \((\S*?)\)', subject)
+ if m:
+ return m.group(1)
+
+ raise Exception('No Safari developer certificate found in chain')
+
+
+def convertJS(params, files):
tlucas 2017/11/02 10:03:29 Thins function was copied from packagerChrome.py i
+ output_files = collections.OrderedDict()
+ args = {}
+
+ for item in params['metadata'].items('convert_js'):
+ name, value = item
+ filename, arg = re.search(r'^(.*?)(?:\[(.*)\])?$', name).groups()
+ if arg is None:
+ output_files[filename] = (value.split(), item.source)
+ else:
+ args.setdefault(filename, {})[arg] = value
+
+ template = getTemplate('modules.js.tmpl')
+
+ for filename, (input_files, origin) in output_files.iteritems():
+ if '/' in filename and not files.isIncluded(filename):
+ continue
+
+ current_args = args.get(filename, {})
+ current_args['autoload'] = [module for module in
+ current_args.get('autoload', '').split(',')
+ if module != '']
+
+ base_dir = os.path.dirname(origin)
+ modules = []
+
+ for input_filename in input_files:
+ module_name = os.path.splitext(os.path.basename(input_filename))[0]
+ prefix = os.path.basename(os.path.dirname(input_filename))
+ if prefix != 'lib':
+ module_name = '{}_{}'.format(prefix, module_name)
+ with open(os.path.join(base_dir, input_filename), 'r') as file:
+ modules.append((module_name, file.read().decode('utf-8')))
+ files.pop(input_filename, None)
+
+ files[filename] = template.render(
+ args=current_args,
+ modules=modules
+ ).encode('utf-8')
+
+
+def createBuild(baseDir, type, outFile=None, buildNum=None, releaseBuild=False, keyFile=None, devenv=False):
+ metadata = readMetadata(baseDir, type)
+ version = getBuildVersion(baseDir, metadata, releaseBuild, buildNum)
+
+ if not outFile:
+ outFile = getDefaultFileName(metadata, version, 'safariextz' if keyFile else 'zip')
+
+ params = {
+ 'type': type,
+ 'baseDir': baseDir,
+ 'releaseBuild': releaseBuild,
+ 'version': version,
+ 'devenv': devenv,
+ 'metadata': metadata,
+ }
+
+ mapped = metadata.items('mapping') if metadata.has_section('mapping') else []
+ files = Files(getPackageFiles(params), getIgnoredFiles(params),
+ process=lambda path, data: processFile(path, data, params))
+ files.readMappedFiles(mapped)
+ files.read(baseDir, skip=[opt for opt, _ in mapped])
+
+ if metadata.has_section('convert_js'):
+ convertJS(params, files)
+
+ if metadata.has_section('preprocess'):
+ files.preprocess(
+ [f for f, _ in metadata.items('preprocess')],
+ {'needsExt': True}
+ )
+
+ if metadata.has_section('import_locales'):
+ import_locales(params, files)
+
+ if metadata.has_option('general', 'testScripts'):
+ files['qunit/index.html'] = createScriptPage(params, 'testIndex.html.tmpl',
+ ('general', 'testScripts'))
+
+ if keyFile:
+ from buildtools import xarfile
+ certs, key = xarfile.read_certificates_and_key(keyFile)
+ params['developerIdentifier'] = get_developer_identifier(certs)
+
+ files['lib/info.js'] = createInfoModule(params)
+ files['background.html'] = createScriptPage(params, 'background.html.tmpl',
+ ('general', 'backgroundScripts'))
+ files['Info.plist'] = createManifest(params, files)
+
+ dirname = metadata.get('general', 'basename') + '.safariextension'
+ for filename in files.keys():
+ files[os.path.join(dirname, filename)] = files.pop(filename)
+
+ if not devenv and keyFile:
+ from buildtools import xarfile
+ xarfile.create(outFile, files, keyFile)
+ else:
+ files.zip(outFile)

Powered by Google App Engine
This is Rietveld