Left: | ||
Right: |
OLD | NEW |
---|---|
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 base64 | |
6 import hashlib | |
7 import json | |
8 import mimetypes | |
9 import os | 5 import os |
10 import zipfile | 6 import shutil |
7 from StringIO import StringIO | |
8 import subprocess | |
9 import tempfile | |
10 from xml.etree import ElementTree | |
11 from zipfile import ZipFile | |
11 | 12 |
12 import packager | 13 import packager |
13 import packagerChrome | 14 import packagerChrome |
14 | 15 |
15 # Files and directories expected inside of the .APPX archive. | 16 MANIFEST = 'appxmanifest.xml' |
16 MANIFEST = 'AppxManifest.xml' | |
17 CONTENT_TYPES = '[Content_Types].xml' | |
18 BLOCKMAP = 'AppxBlockMap.xml' | |
19 EXTENSION_DIR = 'Extension' | |
20 ASSETS_DIR = 'Assets' | 17 ASSETS_DIR = 'Assets' |
21 | 18 |
22 # Size of uncompressed block in the APPX block map. | |
23 BLOCKSIZE = 64 * 1024 | |
24 | |
25 defaultLocale = packagerChrome.defaultLocale | 19 defaultLocale = packagerChrome.defaultLocale |
26 | 20 |
27 | 21 |
28 def _get_template_for(filename): | 22 def _get_template_for(filename): |
29 return packager.getTemplate('edge/{}.tmpl'.format(filename)) | 23 return packager.getTemplate('edge/{}.tmpl'.format(filename)) |
30 | 24 |
31 | 25 |
32 def _lfh_size(filename): | 26 def register_xml_namespaces(manifest_path): |
33 """Compute the size of zip local file header for `filename`.""" | 27 """Register namespaces of the given file, in order to preserve defaults.""" |
34 try: | 28 with open(manifest_path, 'r') as fp: |
35 filename = filename.encode('utf-8') | 29 ns = dict([node for _, node in ElementTree.iterparse( |
36 except UnicodeDecodeError: | 30 fp, events=['start-ns'])]) |
37 pass # filename is already a byte string. | 31 for prefix, uri in ns.items(): |
38 return zipfile.sizeFileHeader + len(filename) | 32 ElementTree.register_namespace(prefix, uri) |
33 | |
34 # Make the default namespace available in an xpath expression | |
35 ns['_d'] = ns[''] | |
36 | |
37 return ns | |
39 | 38 |
40 | 39 |
41 def _make_blockmap_entry(filename, data): | 40 def update_appx_manifest(manifest_path, base_dir, files, metadata, |
42 blocks = [data[i:i + BLOCKSIZE] for i in range(0, len(data), BLOCKSIZE)] | 41 release_build): |
43 return { | 42 namespaces = register_xml_namespaces(manifest_path) |
44 'name': filename.replace('/', '\\'), | |
45 'size': len(data), | |
46 'lfh_size': _lfh_size(filename), | |
47 'blocks': [ | |
48 {'hash': base64.b64encode(hashlib.sha256(block).digest())} | |
49 for block in blocks | |
50 ], | |
51 } | |
52 | 43 |
44 v_min, v_max = metadata.get('compat', 'windows').split('/') | |
53 | 45 |
54 def create_appx_blockmap(files): | 46 filenames = [] |
55 """Create APPX blockmap for the list of files.""" | |
56 # We don't support AppxBlockmap.xml generation for compressed zip files at | |
57 # the moment. The only way to reliably calculate the compressed size of | |
58 # each 64k chunk in the zip file is to override the relevant parts of | |
59 # `zipfile` library. We have chosen to not do it so we produce an | |
60 # uncompressed zip file that is later repackaged by Windows Store with | |
61 # compression. | |
62 template = _get_template_for(BLOCKMAP) | |
63 files = [_make_blockmap_entry(n, d) for n, d in files.items()] | |
64 return template.render(files=files).encode('utf-8') | |
65 | 47 |
48 for name, path in metadata.items('appx_assets'): | |
49 path = os.path.join(base_dir, path) | |
50 icon_path = '{}/{}'.format(ASSETS_DIR, name) | |
66 | 51 |
67 def load_translation(files, locale): | 52 files.read(path, icon_path) |
68 """Load translation strings for locale from files.""" | 53 filenames.append(icon_path) |
69 path = '{}/_locales/{}/messages.json'.format(EXTENSION_DIR, locale) | |
70 return json.loads(files[path]) | |
71 | 54 |
55 assets = packagerChrome.makeIcons(files, filenames) | |
72 | 56 |
73 def get_appx_version(metadata, build_num): | 57 author = metadata.get('general', 'author') |
74 """Get the version number for usage in AppxManifest.xml. | |
75 | 58 |
76 As required by the Windows Store, the returned version string has four | 59 overrides = [ |
77 components, where the 3rd component is replaced with the build number | 60 ('_d:Identity', None, [ |
78 if available, and the 4th component is always zero (e.g. 1.2.1000.0). | 61 ('Name', packager.get_app_id(release_build, metadata)), |
79 """ | 62 ('Publisher', metadata.get('general', 'publisher_id')), |
80 components = metadata.get('general', 'version').split('.')[:3] | 63 ]), |
81 components.extend(['0'] * (4 - len(components))) | 64 ('_d:Properties/_d:PublisherDisplayName', author, []), |
82 if build_num: | 65 ('_d:Properties/_d:Logo', assets[50], []), |
83 components[2] = build_num | 66 ('_d:Dependencies/_d:TargetDeviceFamily', None, [ |
84 return '.'.join(components) | 67 ('MaxVersionTested', v_max), |
68 ('MinVersion', v_min), | |
69 ]), | |
70 ('_d:Applications/_d:Application/uap:VisualElements', None, [ | |
71 ('Square150x150Logo', assets[150]), | |
72 ('Square44x44Logo', assets[44]), | |
73 ]), | |
74 ] | |
85 | 75 |
76 tree = ElementTree.parse(manifest_path) | |
77 root = tree.getroot() | |
86 | 78 |
87 def create_appx_manifest(params, files, build_num, release_build): | 79 for xpath, text, attributes in overrides: |
88 """Create AppxManifest.xml.""" | 80 element = root.find(xpath, namespaces) |
89 params = dict(params) | 81 element.text = text |
Sebastian Noack
2018/08/08 22:27:32
We should check if text is not None. It may not be
tlucas
2018/08/09 07:09:24
Done.
| |
90 metadata = params['metadata'] | 82 for attr, value in attributes: |
91 w = params['windows_version'] = {} | 83 element.set(attr, value) |
92 w['min'], w['max'] = metadata.get('compat', 'windows').split('/') | |
93 params['version'] = get_appx_version(metadata, build_num) | |
94 | 84 |
95 metadata_suffix = 'release' if release_build else 'devbuild' | 85 tree.write(manifest_path, encoding='utf-8', xml_declaration=True) |
96 app_extension_id = 'extension_id_' + metadata_suffix | |
97 if metadata.has_option('general', app_extension_id): | |
98 params['app_extension_id'] = metadata.get('general', app_extension_id) | |
99 else: | |
100 params['app_extension_id'] = 'EdgeExtension' | |
101 | |
102 params['app_id'] = packager.get_app_id(release_build, metadata) | |
103 | |
104 translation = load_translation(files, defaultLocale) | |
105 name_key = 'name' if release_build else 'name_devbuild' | |
106 params['display_name'] = translation[name_key]['message'] | |
107 params['description'] = translation['description']['message'] | |
108 | |
109 for size in ['44', '50', '150']: | |
110 path = '{}/logo_{}.png'.format(ASSETS_DIR, size) | |
111 if path not in files: | |
112 raise KeyError(path + ' is not found in files') | |
113 params['logo_' + size] = path.replace('/', '\\') | |
114 | |
115 template = _get_template_for(MANIFEST) | |
116 return template.render(params).encode('utf-8') | |
117 | |
118 | |
119 def move_files_to_extension(files): | |
120 """Move all files into `Extension` folder for APPX packaging.""" | |
121 # We sort the files to ensure that 'Extension/xyz' is moved before 'xyz'. | |
122 # If 'xyz' is moved first, it would overwrite 'Extension/xyz' and its | |
123 # original content would be lost. | |
124 names = sorted(files.keys(), key=len, reverse=True) | |
125 for filename in names: | |
126 files['{}/{}'.format(EXTENSION_DIR, filename)] = files.pop(filename) | |
127 | |
128 | |
129 def create_content_types_map(filenames): | |
130 """Create [Content_Types].xml -- a mime type map.""" | |
131 params = {'defaults': {}, 'overrides': {}} | |
132 overrides = { | |
133 BLOCKMAP: 'application/vnd.ms-appx.blockmap+xml', | |
134 MANIFEST: 'application/vnd.ms-appx.manifest+xml', | |
135 } | |
136 types = mimetypes.MimeTypes() | |
137 types.add_type('application/octet-stream', '.otf') | |
138 for filename in filenames: | |
139 ext = os.path.splitext(filename)[1] | |
140 if ext: | |
141 content_type = types.guess_type(filename, strict=False)[0] | |
142 if content_type is not None: | |
143 params['defaults'][ext[1:]] = content_type | |
144 if filename in overrides: | |
145 params['overrides']['/' + filename] = overrides[filename] | |
146 content_types_template = _get_template_for(CONTENT_TYPES) | |
147 return content_types_template.render(params).encode('utf-8') | |
148 | 86 |
149 | 87 |
150 def createBuild(baseDir, type='edge', outFile=None, # noqa: preserve API. | 88 def createBuild(baseDir, type='edge', outFile=None, # noqa: preserve API. |
151 buildNum=None, releaseBuild=False, keyFile=None, | 89 buildNum=None, releaseBuild=False, keyFile=None, |
152 devenv=False): | 90 devenv=False): |
153 | 91 |
154 metadata = packager.readMetadata(baseDir, type) | 92 metadata = packager.readMetadata(baseDir, type) |
155 version = packager.getBuildVersion(baseDir, metadata, releaseBuild, | 93 version = packager.getBuildVersion(baseDir, metadata, releaseBuild, |
156 buildNum) | 94 buildNum) |
157 | 95 |
(...skipping 26 matching lines...) Expand all Loading... | |
184 files.preprocess(metadata.options('preprocess'), {'needsExt': True}) | 122 files.preprocess(metadata.options('preprocess'), {'needsExt': True}) |
185 | 123 |
186 if metadata.has_section('import_locales'): | 124 if metadata.has_section('import_locales'): |
187 packagerChrome.import_locales(params, files) | 125 packagerChrome.import_locales(params, files) |
188 | 126 |
189 files['manifest.json'] = packagerChrome.createManifest(params, files) | 127 files['manifest.json'] = packagerChrome.createManifest(params, files) |
190 | 128 |
191 if devenv: | 129 if devenv: |
192 packagerChrome.add_devenv_requirements(files, metadata, params) | 130 packagerChrome.add_devenv_requirements(files, metadata, params) |
193 | 131 |
194 move_files_to_extension(files) | 132 zipped = StringIO() |
133 files.zip(zipped) | |
195 | 134 |
196 if metadata.has_section('appx_assets'): | 135 zipped.seek(0) |
197 for name, path in metadata.items('appx_assets'): | |
198 path = os.path.join(baseDir, path) | |
199 files.read(path, '{}/{}'.format(ASSETS_DIR, name)) | |
200 | 136 |
201 files[MANIFEST] = create_appx_manifest(params, files, | 137 if devenv: |
202 buildNum, releaseBuild) | 138 shutil.copyfileobj(zipped, outfile) |
203 files[BLOCKMAP] = create_appx_blockmap(files) | 139 return |
204 files[CONTENT_TYPES] = create_content_types_map(files.keys() + [BLOCKMAP]) | |
205 | 140 |
206 files.zip(outfile, compression=zipfile.ZIP_STORED) | 141 tmp_dir = tempfile.mkdtemp('adblockplus_package') |
142 try: | |
143 src_dir = os.path.join(tmp_dir, 'src') | |
144 ext_dir = os.path.join(tmp_dir, 'ext') | |
145 | |
146 with ZipFile(zipped, 'r') as zip_file: | |
147 zip_file.extractall(src_dir) | |
148 | |
149 cmd_env = os.environ.copy() | |
150 cmd_env['SRC_FOLDER'] = src_dir | |
151 cmd_env['EXT_FOLDER'] = ext_dir | |
152 | |
153 manifold_folder = os.path.join(ext_dir, 'MSGname', 'edgeextension') | |
154 manifest_folder = os.path.join(manifold_folder, 'manifest') | |
155 asset_folder = os.path.join(manifest_folder, ASSETS_DIR) | |
156 | |
157 # prepare the extension with manifoldjs | |
158 cmd = ['npm', 'run', '--silent', 'build-edge'] | |
159 subprocess.check_call(cmd, env=cmd_env, cwd=os.path.dirname(__file__)) | |
160 | |
161 # update incomplete appxmanifest | |
162 intermediate_manifest = os.path.join(manifest_folder, MANIFEST) | |
163 update_appx_manifest(intermediate_manifest, baseDir, files, metadata, | |
164 releaseBuild) | |
165 | |
166 # cleanup placeholders, copy actual images | |
167 shutil.rmtree(asset_folder) | |
168 os.mkdir(asset_folder) | |
169 if metadata.has_section('appx_assets'): | |
170 for name, path in metadata.items('appx_assets'): | |
171 path = os.path.join(baseDir, path) | |
172 target = os.path.join(asset_folder, name) | |
173 shutil.copyfile(path, target) | |
174 | |
175 # package app with manifoldjs | |
176 cmd = ['npm', 'run', '--silent', 'package-edge'] | |
177 | |
178 subprocess.check_call(cmd, env=cmd_env, cwd=os.path.dirname(__file__)) | |
179 | |
180 package = os.path.join(manifold_folder, 'package', | |
181 'edgeExtension.appx') | |
182 | |
183 shutil.copyfile(package, outfile) | |
184 finally: | |
185 shutil.rmtree(tmp_dir, ignore_errors=True) | |
OLD | NEW |