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

Side by Side Diff: packagerChrome.py

Issue 29549786: Issue 5535 - Replace our module system with webpack (Closed)
Patch Set: Delete all temporary files at the same time Created Sept. 22, 2017, 2 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 | « package-lock.json ('k') | packagerEdge.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import shutil
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 tempfile
14 16
15 from packager import (readMetadata, getDefaultFileName, getBuildVersion, 17 from packager import (readMetadata, getDefaultFileName, getBuildVersion,
16 getTemplate, Files) 18 getTemplate, Files)
17 19
18 defaultLocale = 'en_US' 20 defaultLocale = 'en_US'
19 21
20 22
21 def getIgnoredFiles(params): 23 def getIgnoredFiles(params):
22 return {'store.description'} 24 return {'store.description'}
23 25
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 # Normalize JSON structure 134 # Normalize JSON structure
133 licenseComment = re.compile(r'/\*.*?\*/', re.S) 135 licenseComment = re.compile(r'/\*.*?\*/', re.S)
134 data = json.loads(re.sub(licenseComment, '', manifest, 1)) 136 data = json.loads(re.sub(licenseComment, '', manifest, 1))
135 if '_dummy' in data: 137 if '_dummy' in data:
136 del data['_dummy'] 138 del data['_dummy']
137 manifest = json.dumps(data, sort_keys=True, indent=2) 139 manifest = json.dumps(data, sort_keys=True, indent=2)
138 140
139 return manifest.encode('utf-8') 141 return manifest.encode('utf-8')
140 142
141 143
142 def convertJS(params, files): 144 def create_bundles(params, files):
143 output_files = collections.OrderedDict() 145 base_extension_path = params['baseDir']
144 args = {} 146 info_templates = {
147 'chrome': 'chromeInfo.js.tmpl',
148 'edge': 'edgeInfo.js.tmpl',
149 'gecko-webext': 'geckoInfo.js.tmpl'
150 }
151 info_module = None
145 152
146 for item in params['metadata'].items('convert_js'): 153 # Once we use relative paths when requiring modules we can remove these,
147 name, value = item 154 # but in the mean time Webpack needs to know where to look.
148 filename, arg = re.search(r'^(.*?)(?:\[(.*)\])?$', name).groups() 155 resolve_paths = ' '.join(['./lib',
149 if arg is None: 156 './adblockpluscore/lib',
150 output_files[filename] = (value.split(), item.source) 157 './adblockplusui/lib'])
Sebastian Noack 2017/09/22 19:28:02 Mind filing a follow up issue (and referring to it
Sebastian Noack 2017/09/22 22:21:12 Never mind, these issues have already been filed,
kzar 2017/09/23 20:26:36 OK since we're now considering removing buildtools
Sebastian Noack 2017/09/23 20:49:01 It seems you missed this part?
kzar 2017/09/24 09:47:53 Oh, I forgot to mention that I tried to file the i
151 else: 158 try:
152 args.setdefault(filename, {})[arg] = value 159 temp_dir = tempfile.mkdtemp()
Sebastian Noack 2017/09/22 19:28:02 This should be outside of the try-finally block. O
kzar 2017/09/23 20:26:35 Done.
160 template = getTemplate(info_templates[params['type']])
161 with tempfile.NamedTemporaryFile(delete=False,
Sebastian Noack 2017/09/22 19:28:02 Do we need to use NamedTemporaryFile here, or can'
kzar 2017/09/23 20:26:35 Done. (But I wonder if the file name is more likel
162 dir=temp_dir) as info_file:
163 info_file.write(
164 template.render(
165 basename=params['metadata'].get('general', 'basename'),
166 version=params['metadata'].get('general', 'version')
167 ).encode('utf-8')
168 )
169 info_module = info_file.name
153 170
154 template = getTemplate('modules.js.tmpl') 171 for item in params['metadata'].items('bundles'):
172 name, value = item
173 base_item_path = os.path.dirname(item.source)
155 174
156 for filename, (input_files, origin) in output_files.iteritems(): 175 bundle_file = os.path.relpath(os.path.join(base_item_path, name),
157 if '/' in filename and not files.isIncluded(filename): 176 base_extension_path)
158 continue 177 entry_files = [
159 178 os.path.join(
160 current_args = args.get(filename, {}) 179 '.',
161 current_args['autoload'] = [module for module in 180 os.path.relpath(os.path.join(base_item_path, module_path),
162 current_args.get('autoload', '').split(',') 181 base_extension_path)
163 if module != ''] 182 )
164 183 for module_path in value.split()
165 base_dir = os.path.dirname(origin) 184 ]
166 modules = [] 185 subprocess.check_call(
167 186 ['npm', 'run-script', 'webpack', '--silent'],
168 for input_filename in input_files: 187 cwd=os.path.dirname(__file__),
169 module_name = os.path.splitext(os.path.basename(input_filename))[0] 188 env={
170 prefix = os.path.basename(os.path.dirname(input_filename)) 189 'EXTENSION_PATH': base_extension_path,
171 if prefix != 'lib': 190 'ENTRY_POINTS': ' '.join(entry_files),
172 module_name = '{}_{}'.format(prefix, module_name) 191 'OUTPUT_PATH': temp_dir,
173 with open(os.path.join(base_dir, input_filename), 'r') as file: 192 'BUNDLE_NAME': bundle_file,
174 modules.append((module_name, file.read().decode('utf-8'))) 193 'RESOLVE_PATHS': resolve_paths,
175 files.pop(input_filename, None) 194 'INFO_PATH': info_module,
176 195 'PATH': os.environ['PATH']
177 files[filename] = template.render( 196 }
178 args=current_args, 197 )
179 basename=params['metadata'].get('general', 'basename'), 198 for file_name in [bundle_file, bundle_file + '.map']:
180 modules=modules, 199 with open(os.path.join(temp_dir, file_name), 'r') as f:
181 type=params['type'], 200 files[file_name] = f.read()
182 version=params['metadata'].get('general', 'version') 201 finally:
183 ).encode('utf-8') 202 shutil.rmtree(temp_dir)
184 203
185 204
186 def toJson(data): 205 def toJson(data):
187 return json.dumps( 206 return json.dumps(
188 data, ensure_ascii=False, sort_keys=True, 207 data, ensure_ascii=False, sort_keys=True,
189 indent=2, separators=(',', ': ') 208 indent=2, separators=(',', ': ')
190 ).encode('utf-8') + '\n' 209 ).encode('utf-8') + '\n'
191 210
192 211
193 def import_string_webext(data, key, source): 212 def import_string_webext(data, key, source):
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 'metadata': metadata, 401 'metadata': metadata,
383 } 402 }
384 403
385 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ] 404 mapped = metadata.items('mapping') if metadata.has_section('mapping') else [ ]
386 files = Files(getPackageFiles(params), getIgnoredFiles(params), 405 files = Files(getPackageFiles(params), getIgnoredFiles(params),
387 process=lambda path, data: processFile(path, data, params)) 406 process=lambda path, data: processFile(path, data, params))
388 407
389 files.readMappedFiles(mapped) 408 files.readMappedFiles(mapped)
390 files.read(baseDir, skip=[opt for opt, _ in mapped]) 409 files.read(baseDir, skip=[opt for opt, _ in mapped])
391 410
392 if metadata.has_section('convert_js'): 411 if metadata.has_section('bundles'):
393 convertJS(params, files) 412 create_bundles(params, files)
394 413
395 if metadata.has_section('preprocess'): 414 if metadata.has_section('preprocess'):
396 files.preprocess( 415 files.preprocess(
397 [f for f, _ in metadata.items('preprocess')], 416 [f for f, _ in metadata.items('preprocess')],
398 {'needsExt': True} 417 {'needsExt': True}
399 ) 418 )
400 419
401 if metadata.has_section('import_locales'): 420 if metadata.has_section('import_locales'):
402 import_locales(params, files) 421 import_locales(params, files)
403 422
(...skipping 12 matching lines...) Expand all
416 params, 'testIndex.html.tmpl', ('general', 'testScripts') 435 params, 'testIndex.html.tmpl', ('general', 'testScripts')
417 ) 436 )
418 437
419 zipdata = files.zipToString() 438 zipdata = files.zipToString()
420 signature = None 439 signature = None
421 pubkey = None 440 pubkey = None
422 if keyFile != None: 441 if keyFile != None:
423 signature = signBinary(zipdata, keyFile) 442 signature = signBinary(zipdata, keyFile)
424 pubkey = getPublicKey(keyFile) 443 pubkey = getPublicKey(keyFile)
425 writePackage(outFile, pubkey, signature, zipdata) 444 writePackage(outFile, pubkey, signature, zipdata)
OLDNEW
« no previous file with comments | « package-lock.json ('k') | packagerEdge.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld