| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 glob | |
| 6 import io | 7 import io |
| 7 import json | 8 import json |
| 8 import os | 9 import os |
| 9 import re | 10 import re |
| 10 from StringIO import StringIO | 11 from StringIO import StringIO |
| 11 import struct | 12 import struct |
| 13 import subprocess | |
| 12 import sys | 14 import sys |
| 13 import collections | 15 import random |
| 14 import glob | |
| 15 | 16 |
| 16 from packager import (readMetadata, getDefaultFileName, getBuildVersion, | 17 from packager import (readMetadata, getDefaultFileName, getBuildVersion, |
| 17 getTemplate, Files) | 18 getTemplate, Files) |
| 18 | 19 |
| 19 defaultLocale = 'en_US' | 20 defaultLocale = 'en_US' |
| 20 | 21 |
| 21 | 22 |
| 22 def getIgnoredFiles(params): | 23 def getIgnoredFiles(params): |
| 23 return {'store.description'} | 24 return {'store.description'} |
| 24 | 25 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 # Normalize JSON structure | 134 # Normalize JSON structure |
| 134 licenseComment = re.compile(r'/\*.*?\*/', re.S) | 135 licenseComment = re.compile(r'/\*.*?\*/', re.S) |
| 135 data = json.loads(re.sub(licenseComment, '', manifest, 1)) | 136 data = json.loads(re.sub(licenseComment, '', manifest, 1)) |
| 136 if '_dummy' in data: | 137 if '_dummy' in data: |
| 137 del data['_dummy'] | 138 del data['_dummy'] |
| 138 manifest = json.dumps(data, sort_keys=True, indent=2) | 139 manifest = json.dumps(data, sort_keys=True, indent=2) |
| 139 | 140 |
| 140 return manifest.encode('utf-8') | 141 return manifest.encode('utf-8') |
| 141 | 142 |
| 142 | 143 |
| 143 def convertJS(params, files): | |
| 144 output_files = collections.OrderedDict() | |
| 145 args = {} | |
| 146 | |
| 147 for item in params['metadata'].items('convert_js'): | |
| 148 name, value = item | |
| 149 filename, arg = re.search(r'^(.*?)(?:\[(.*)\])?$', name).groups() | |
| 150 if arg is None: | |
| 151 output_files[filename] = (value.split(), item.source) | |
| 152 else: | |
| 153 args.setdefault(filename, {})[arg] = value | |
| 154 | |
| 155 template = getTemplate('modules.js.tmpl') | |
| 156 | |
| 157 for filename, (input_files, origin) in output_files.iteritems(): | |
| 158 if '/' in filename and not files.isIncluded(filename): | |
| 159 continue | |
| 160 | |
| 161 current_args = args.get(filename, {}) | |
| 162 current_args['autoload'] = [module for module in | |
| 163 current_args.get('autoload', '').split(',') | |
| 164 if module != ''] | |
| 165 | |
| 166 base_dir = os.path.dirname(origin) | |
| 167 modules = [] | |
| 168 | |
| 169 for input_filename in input_files: | |
| 170 module_name = os.path.splitext(os.path.basename(input_filename))[0] | |
| 171 prefix = os.path.basename(os.path.dirname(input_filename)) | |
| 172 if prefix != 'lib': | |
| 173 module_name = '{}_{}'.format(prefix, module_name) | |
| 174 with open(os.path.join(base_dir, input_filename), 'r') as file: | |
| 175 modules.append((module_name, file.read().decode('utf-8'))) | |
| 176 files.pop(input_filename, None) | |
| 177 | |
| 178 files[filename] = template.render( | |
| 179 args=current_args, | |
| 180 basename=params['metadata'].get('general', 'basename'), | |
| 181 modules=modules, | |
| 182 type=params['type'], | |
| 183 version=params['metadata'].get('general', 'version') | |
| 184 ).encode('utf-8') | |
| 185 | |
| 186 | |
| 187 def toJson(data): | 144 def toJson(data): |
| 188 return json.dumps( | 145 return json.dumps( |
| 189 data, ensure_ascii=False, sort_keys=True, | 146 data, ensure_ascii=False, sort_keys=True, |
| 190 indent=2, separators=(',', ': ') | 147 indent=2, separators=(',', ': ') |
| 191 ).encode('utf-8') + '\n' | 148 ).encode('utf-8') + '\n' |
| 149 | |
| 150 | |
| 151 def create_bundles(params, files): | |
| 152 base_extension_path = params['baseDir'] | |
| 153 info_templates = { | |
| 154 'chrome': 'chromeInfo.js.tmpl', | |
| 155 'edge': 'edgeInfo.js.tmpl', | |
| 156 'gecko': 'geckoInfo.js.tmpl' | |
| 157 } | |
| 158 | |
| 159 # Historically we didn't use relative paths when requiring modules, so in | |
| 160 # order for webpack to know where to find them we need to pass in a list of | |
| 161 # resolve paths. Going forward we should always use relative paths, once we | |
| 162 # do that consistently this can be removed. See issues 5760, 5761 and 5762. | |
| 163 resolve_paths = [os.path.join(base_extension_path, dir, 'lib') | |
| 164 for dir in ['', 'adblockpluscore', 'adblockplusui']] | |
| 165 | |
| 166 info_template = getTemplate(info_templates[params['type']]) | |
| 167 info_module = info_template.render( | |
| 168 basename=params['metadata'].get('general', 'basename'), | |
| 169 version=params['metadata'].get('general', 'version') | |
| 170 ).encode('utf-8') | |
| 171 | |
| 172 configuration = { | |
| 173 'bundles': [], | |
| 174 'extension_path': base_extension_path, | |
| 175 'info_module': info_module, | |
| 176 'resolve_paths': resolve_paths, | |
| 177 } | |
| 178 | |
| 179 for item in params['metadata'].items('bundles'): | |
| 180 name, value = item | |
| 181 base_item_path = os.path.dirname(item.source) | |
| 182 | |
| 183 bundle_file = os.path.relpath(os.path.join(base_item_path, name), | |
| 184 base_extension_path) | |
| 185 entry_files = [os.path.join(base_item_path, module_path) | |
| 186 for module_path in value.split()] | |
| 187 configuration['bundles'].append({ | |
| 188 'bundle_name': bundle_file, | |
| 189 'entry_points': entry_files, | |
| 190 }) | |
| 191 | |
| 192 cmd = ['node', os.path.join(os.path.dirname(__file__), 'webpack_runner.js')] | |
| 193 process = subprocess.Popen(cmd, stdout=subprocess.PIPE, | |
| 194 stdin=subprocess.PIPE) | |
| 195 output = process.communicate(input=toJson(configuration))[0] | |
| 196 if process.returncode != 0: | |
| 197 raise subprocess.CalledProcessError(process.returncode, cmd=cmd) | |
| 198 output = json.loads(output) | |
| 199 | |
| 200 # Clear the mapping for any files included in a bundle, to avoid them being | |
| 201 # duplicated in the build. | |
| 202 for to_ignore in output['included']: | |
| 203 files.pop(to_ignore, None) | |
| 204 | |
| 205 for bundle in output['files']: | |
| 206 files[bundle] = output['files'][bundle].encode('utf-8') | |
| 192 | 207 |
| 193 | 208 |
| 194 def import_locales(params, files): | 209 def import_locales(params, files): |
| 195 for item in params['metadata'].items('import_locales'): | 210 for item in params['metadata'].items('import_locales'): |
| 196 filename, keys = item | 211 filename, keys = item |
| 197 for sourceFile in glob.glob(os.path.join(os.path.dirname(item.source), | 212 for sourceFile in glob.glob(os.path.join(os.path.dirname(item.source), |
| 198 *filename.split('/'))): | 213 *filename.split('/'))): |
| 199 locale = sourceFile.split(os.path.sep)[-2] | 214 locale = sourceFile.split(os.path.sep)[-2] |
| 200 targetFile = os.path.join('_locales', locale, 'messages.json') | 215 targetFile = os.path.join('_locales', locale, 'messages.json') |
| 201 data = json.loads(files.get(targetFile, '{}').decode('utf-8')) | 216 data = json.loads(files.get(targetFile, '{}').decode('utf-8')) |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 else: | 314 else: |
| 300 file = outputFile | 315 file = outputFile |
| 301 if pubkey != None and signature != None: | 316 if pubkey != None and signature != None: |
| 302 file.write(struct.pack('<4sIII', 'Cr24', 2, len(pubkey), len(signature)) ) | 317 file.write(struct.pack('<4sIII', 'Cr24', 2, len(pubkey), len(signature)) ) |
| 303 file.write(pubkey) | 318 file.write(pubkey) |
| 304 file.write(signature) | 319 file.write(signature) |
| 305 file.write(zipdata) | 320 file.write(zipdata) |
| 306 | 321 |
| 307 | 322 |
| 308 def add_devenv_requirements(files, metadata, params): | 323 def add_devenv_requirements(files, metadata, params): |
| 309 import buildtools | 324 files.read( |
|
Sebastian Noack
2017/10/17 21:29:52
If we determine the path relative to this module (
tlucas
2017/10/18 09:27:49
Done.
| |
| 310 import random | 325 os.path.join(os.path.dirname(__file__), 'chromeDevenvPoller__.js'), |
|
Sebastian Noack
2017/10/17 21:29:52
This import can go to the top of the module.
tlucas
2017/10/18 09:27:49
Done.
| |
| 311 files.read(os.path.join(buildtools.__path__[0], 'chromeDevenvPoller__.js'), | 326 relpath='devenvPoller__.js', |
| 312 relpath='devenvPoller__.js') | 327 ) |
| 313 files['devenvVersion__'] = str(random.random()) | 328 files['devenvVersion__'] = str(random.random()) |
| 314 | 329 |
| 315 if metadata.has_option('general', 'testScripts'): | 330 if metadata.has_option('general', 'testScripts'): |
| 316 files['qunit/index.html'] = createScriptPage( | 331 files['qunit/index.html'] = createScriptPage( |
| 317 params, 'testIndex.html.tmpl', ('general', 'testScripts') | 332 params, 'testIndex.html.tmpl', ('general', 'testScripts') |
| 318 ) | 333 ) |
| 319 | 334 |
| 320 | 335 |
| 321 def createBuild(baseDir, type='chrome', outFile=None, buildNum=None, releaseBuil d=False, keyFile=None, devenv=False): | 336 def createBuild(baseDir, type='chrome', outFile=None, buildNum=None, releaseBuil d=False, keyFile=None, devenv=False): |
| 322 metadata = readMetadata(baseDir, type) | 337 metadata = readMetadata(baseDir, type) |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 338 'metadata': metadata, | 353 'metadata': metadata, |
| 339 } | 354 } |
| 340 | 355 |
| 341 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ] | 356 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ] |
| 342 files = Files(getPackageFiles(params), getIgnoredFiles(params), | 357 files = Files(getPackageFiles(params), getIgnoredFiles(params), |
| 343 process=lambda path, data: processFile(path, data, params)) | 358 process=lambda path, data: processFile(path, data, params)) |
| 344 | 359 |
| 345 files.readMappedFiles(mapped) | 360 files.readMappedFiles(mapped) |
| 346 files.read(baseDir, skip=[opt for opt, _ in mapped]) | 361 files.read(baseDir, skip=[opt for opt, _ in mapped]) |
| 347 | 362 |
| 348 if metadata.has_section('convert_js'): | 363 if metadata.has_section('bundles'): |
| 349 convertJS(params, files) | 364 create_bundles(params, files) |
| 350 | 365 |
| 351 if metadata.has_section('preprocess'): | 366 if metadata.has_section('preprocess'): |
| 352 files.preprocess( | 367 files.preprocess( |
| 353 [f for f, _ in metadata.items('preprocess')], | 368 [f for f, _ in metadata.items('preprocess')], |
| 354 {'needsExt': True} | 369 {'needsExt': True} |
| 355 ) | 370 ) |
| 356 | 371 |
| 357 if metadata.has_section('import_locales'): | 372 if metadata.has_section('import_locales'): |
| 358 import_locales(params, files) | 373 import_locales(params, files) |
| 359 | 374 |
| 360 files['manifest.json'] = createManifest(params, files) | 375 files['manifest.json'] = createManifest(params, files) |
| 361 if type == 'chrome': | 376 if type == 'chrome': |
| 362 fix_translations_for_chrome(files) | 377 fix_translations_for_chrome(files) |
| 363 | 378 |
| 364 if devenv: | 379 if devenv: |
| 365 add_devenv_requirements(files, metadata, params) | 380 add_devenv_requirements(files, metadata, params) |
| 366 | 381 |
| 367 zipdata = files.zipToString() | 382 zipdata = files.zipToString() |
| 368 signature = None | 383 signature = None |
| 369 pubkey = None | 384 pubkey = None |
| 370 if keyFile != None: | 385 if keyFile != None: |
| 371 signature = signBinary(zipdata, keyFile) | 386 signature = signBinary(zipdata, keyFile) |
| 372 pubkey = getPublicKey(keyFile) | 387 pubkey = getPublicKey(keyFile) |
| 373 writePackage(outFile, pubkey, signature, zipdata) | 388 writePackage(outFile, pubkey, signature, zipdata) |
| LEFT | RIGHT |