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

Side by Side Diff: packagerEdge.py

Issue 29345751: Issue 4028 - Add support for Edge extensions to buildtools (Closed)
Patch Set: Address comments on patch set 5 Created July 12, 2016, 3:41 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 | « packagerChrome.py ('k') | templates/Info.plist.tmpl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 import base64
6 import hashlib
7 import mimetypes
8 import os
9 import zipfile
10
11 import packager
12 import packagerChrome
13
14 MANIFEST = 'AppxManifest.xml'
15 CONTENT_TYPES = '[Content_Types].xml'
16 BLOCKMAP = 'AppxBlockMap.xml'
17 BLOCKSIZE = 64 * 1024
18
19
20 def _get_template_for(filename):
21 return packager.getTemplate('edge/{}.tmpl'.format(filename))
22
23
24 def _lfh_size(filename):
25 """Compute the size of zip local file header for `filename`."""
26 try:
27 filename = filename.encode('utf-8')
28 except UnicodeDecodeError:
29 pass # filename is already a byte string.
30 return zipfile.sizeFileHeader + len(filename)
31
32
33 def _make_blockmap_entry(filename, data):
34 blocks = [data[i:i + BLOCKSIZE] for i in range(0, len(data), BLOCKSIZE)]
35 return {
36 'name': filename.replace('/', '\\'),
37 'size': len(data),
38 'lfh_size': _lfh_size(filename),
39 'blocks': [
40 {'hash': base64.b64encode(hashlib.sha256(block).digest())}
41 for block in blocks
42 ]
43 }
44
45
46 def create_appx_blockmap(files):
47 """Create APPX blockmap for the list of files."""
48 template = _get_template_for(BLOCKMAP)
49 files = [_make_blockmap_entry(n, d) for n, d in files.items()]
50 return template.render(files=files).encode('utf-8')
51
52
53 def create_appx_manifest(params, files):
54 """Create AppxManifest.xml."""
55 template = _get_template_for(MANIFEST)
56 params = dict(params)
57 metadata = params['metadata']
58 params['package_identity'] = dict(metadata.items('package_identity'))
59 w = params['windows_version'] = {}
60 w['min'], w['max'] = metadata.get('compat', 'windows').split('/')
61 params.update(metadata.items('general'))
62 for size in ['44', '50', '150']:
63 path = 'Assets/logo_{}.png'.format(size)
64 if path not in files:
65 raise KeyError(path + 'is not found in files')
66 params['logo_' + size] = path.replace('/', '\\')
67 return template.render(params).encode('utf-8')
68
69
70 def move_files_to_extension(files):
71 """Move all files into `Extension` folder for APPX packaging."""
72 # We sort the files to ensure that 'Extension/xyz' is moved before 'xyz'.
73 # If 'xyz' is moved first, it would overwrite 'Extension/xyz' and its
74 # original content would be lost.
75 names = sorted(files.keys(), key=len, reverse=True)
76 for filename in names:
77 files['Extension/' + filename] = files.pop(filename)
78
79
80 def create_content_types_map(files):
81 """Create [Content_Types].xml -- a mime type map."""
82 # We always have a manifest and a blockmap, so we include those by default.
83 params = {
84 'defaults': {
85 'xml': 'text/xml'
86 },
87 'overrides': {
88 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml',
Sebastian Noack 2016/07/12 21:15:26 I just noticed that you define those filenames by
Vasily Kuznetsov 2016/07/13 10:35:31 Done.
89 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml'
90 }
91 }
92 for filename in files:
93 if '.' in filename[1:]:
Sebastian Noack 2016/07/12 21:15:26 It seems, now where you use os.path.splitext(), th
Vasily Kuznetsov 2016/07/13 10:35:32 Done.
94 ext = os.path.splitext(filename)[1][1:]
95 content_type = mimetypes.guess_type(filename, strict=False)[0]
96 if content_type is not None:
97 params['defaults'][ext] = content_type
98 content_types_template = _get_template_for(CONTENT_TYPES)
99 return content_types_template.render(params).encode('utf-8')
100
101
102 def createBuild(baseDir, type='edge', outFile=None, # noqa: preserve API.
103 buildNum=None, releaseBuild=False, keyFile=None,
104 devenv=False):
105
106 metadata = packager.readMetadata(baseDir, type)
107 version = packager.getBuildVersion(baseDir, metadata, releaseBuild,
108 buildNum)
109
110 outfile = outFile or packager.getDefaultFileName(metadata, version, 'appx')
111
112 params = {
113 'type': type,
114 'baseDir': baseDir,
115 'releaseBuild': releaseBuild,
116 'version': version,
117 'devenv': devenv,
118 'metadata': metadata,
119 }
120
121 files = packager.Files(packagerChrome.getPackageFiles(params),
122 packagerChrome.getIgnoredFiles(params))
123
124 if metadata.has_section('mapping'):
125 mapped = metadata.items('mapping')
126 files.readMappedFiles(mapped)
127 files.read(baseDir, skip=[filename for filename, _ in mapped])
128 else:
129 files.read(baseDir)
130
131 if metadata.has_section('convert_js'):
132 packagerChrome.convertJS(params, files)
133
134 if metadata.has_section('preprocess'):
135 files.preprocess(metadata.options('preprocess'), {'needsExt': True})
136
137 if metadata.has_section('import_locales'):
138 packagerChrome.importGeckoLocales(params, files)
139
140 files['manifest.json'] = packagerChrome.createManifest(params, files)
141
142 move_files_to_extension(files)
143
144 if metadata.has_section('appx_assets'):
145 for name, path in metadata.items('appx_assets'):
146 path = os.path.join(baseDir, path)
147 files.read(path, 'Assets/' + name)
148
149 files[MANIFEST] = create_appx_manifest(params, files)
150 files[CONTENT_TYPES] = create_content_types_map(files)
151
152 # We don't support AppxBlockmap.xml generation for compressed zip files at
153 # the moment. The only way to reliably calculate the compressed size of
154 # each 64k chunk in the zip file is to override the relevant parts of
155 # `zipfile` library. We have chosen to not do it for now, so we produce
156 # an uncompressed zip file.
157 files[BLOCKMAP] = create_appx_blockmap(files)
158
159 # TODO: Implement AppxBlockmap.xml generation for compressed zip files.
160 # https://issues.adblockplus.org/ticket/4149
161 files.zip(outfile, compress=False)
OLDNEW
« no previous file with comments | « packagerChrome.py ('k') | templates/Info.plist.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld