Left: | ||
Right: |
OLD | NEW |
---|---|
(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 _sha256(block): | |
25 h = hashlib.sha256() | |
26 h.update(block) | |
27 return base64.b64encode(h.digest()) | |
28 | |
29 | |
30 def _lfh_size(filename): | |
31 """Compute the size of zip local file header for `filename`.""" | |
32 try: | |
33 filename = filename.encode('utf-8') | |
34 except UnicodeDecodeError: | |
35 pass # filename is already a byte string. | |
36 return zipfile.sizeFileHeader + len(filename) | |
37 | |
38 | |
39 def _make_blockmap_entry(filename, data): | |
40 blocks = [data[i:i + BLOCKSIZE] for i in range(0, len(data), BLOCKSIZE)] | |
41 return { | |
42 'name': filename.replace('/', '\\'), | |
43 'size': len(data), | |
44 'lfh_size': _lfh_size(filename), | |
45 'blocks': [{'hash': _sha256(block)} for block in blocks] | |
46 } | |
47 | |
48 | |
49 def create_appx_blockmap(files): | |
50 """Create APPX blockmap for the list of files.""" | |
51 template = _get_template_for(BLOCKMAP) | |
52 files = [_make_blockmap_entry(n, d) for n, d in files.items()] | |
53 return template.render(files=files).encode('utf-8') | |
54 | |
55 | |
56 def create_appx_manifest(params, files): | |
57 """Create AppxManifest.xml.""" | |
58 template = _get_template_for(MANIFEST) | |
59 params = dict(params) | |
60 metadata = params['metadata'] | |
61 params['package_identity'] = dict(metadata.items('package_identity')) | |
62 w = params['windows_version'] = {} | |
63 w['min'], w['max'] = metadata.get('compat', 'windows').split('/') | |
64 params.update(metadata.items('general')) | |
65 for size in ['44', '50', '150']: | |
66 path = 'Assets/logo_{}.png'.format(size) | |
67 if path not in files: | |
68 raise KeyError('{} is not found in files'.format(path)) | |
69 params['logo_' + size] = path.replace('/', '\\') | |
70 return template.render(params).encode('utf-8') | |
71 | |
72 | |
73 def move_files_to_extension(files): | |
74 """Move all files into `Extension` folder for APPX packaging.""" | |
75 # We sort the files to ensure that 'Extension/xyz' is moved before 'xyz'. | |
76 # If 'xyz' is moved first, it would overwrite 'Extension/xyz' and its | |
77 # original content would be lost. | |
78 names = sorted(files.keys(), key=len, reverse=True) | |
79 for filename in names: | |
80 files['Extension/' + filename] = files.pop(filename) | |
81 | |
82 | |
83 def create_content_types_map(files): | |
84 """Create [Content_Types].xml -- a mime type map.""" | |
85 # We always have a manifest and a blockmap, so we include those by default. | |
86 params = { | |
87 'defaults': { | |
88 'xml': 'text/xml' | |
89 }, | |
90 'overrides': { | |
91 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', | |
92 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' | |
93 } | |
94 } | |
95 for filename in files: | |
96 if '.' in filename[1:]: | |
97 ext = filename.split('.')[-1] | |
98 content_type = mimetypes.guess_type(filename, strict=False) | |
99 # The return value can be a string or a tuple: | |
100 # https://docs.python.org/2/library/mimetypes.html#mimetypes.guess_t ype | |
101 if isinstance(content_type, tuple): | |
102 content_type = content_type[0] | |
103 if content_type is not None: | |
104 params['defaults'][ext] = content_type | |
105 content_types_template = _get_template_for(CONTENT_TYPES) | |
106 return content_types_template.render(params).encode('utf-8') | |
107 | |
108 | |
109 def createBuild(baseDir, type='edge', outFile=None, # noqa: preserve API. | |
110 buildNum=None, releaseBuild=False, keyFile=None, | |
111 devenv=False): | |
112 | |
113 metadata = packager.readMetadata(baseDir, type) | |
114 version = packager.getBuildVersion(baseDir, metadata, releaseBuild, | |
115 buildNum) | |
116 | |
117 outfile = outFile or packager.getDefaultFileName(metadata, version, 'appx') | |
118 | |
119 params = { | |
120 'type': type, | |
121 'baseDir': baseDir, | |
122 'releaseBuild': releaseBuild, | |
123 'version': version, | |
124 'devenv': devenv, | |
125 'metadata': metadata, | |
126 } | |
127 | |
128 files = packager.Files(packagerChrome.getPackageFiles(params), | |
129 packagerChrome.getIgnoredFiles(params)) | |
130 | |
131 if metadata.has_section('mapping'): | |
132 mapped = metadata.items('mapping') | |
133 files.readMappedFiles(mapped) | |
134 files.read(baseDir, skip=[filename for filename, _ in mapped]) | |
135 else: | |
136 files.read(baseDir) | |
137 | |
138 if metadata.has_section('convert_js'): | |
139 packagerChrome.convertJS(params, files) | |
140 | |
141 if metadata.has_section('preprocess'): | |
142 files.preprocess(metadata.options('preprocess'), {'needsExt': True}) | |
143 | |
144 if metadata.has_section('import_locales'): | |
145 packagerChrome.importGeckoLocales(params, files) | |
146 | |
147 files['manifest.json'] = packagerChrome.createManifest(params, files) | |
148 | |
149 move_files_to_extension(files) | |
150 | |
151 if metadata.has_section('appx_assets'): | |
152 for name, path in metadata.items('appx_assets'): | |
153 path = os.path.join(baseDir, path) | |
154 files.read(path, 'Assets/{}'.format(name)) | |
Sebastian Noack
2016/07/08 13:59:12
Use the + operator here?
Vasily Kuznetsov
2016/07/08 16:47:39
Done.
| |
155 | |
156 files[MANIFEST] = create_appx_manifest(params, files) | |
157 files[CONTENT_TYPES] = create_content_types_map(files) | |
158 | |
159 # We don't support AppxBlockmap.xml generation for compressed zip files at | |
160 # the moment. The only way to reliably calculate the compressed size of | |
161 # each 64k chunk in the zip file is to override the relevant parts of | |
162 # `zipfile` library. We have chosen to not do it for now, so we produce | |
163 # an uncompressed zip file. | |
164 files[BLOCKMAP] = create_appx_blockmap(files) | |
165 | |
166 # TODO: Implement AppxBlockmap.xml generation for compressed zip files. | |
167 # https://issues.adblockplus.org/ticket/4149 | |
168 files.zip(outfile, compress=False) | |
OLD | NEW |