| 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 _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())} | |
|
Sebastian Noack
2016/07/11 16:29:45
Wrapping the value into a dict seems unnecessary.
Vasily Kuznetsov
2016/07/12 09:38:09
The dictionary might also contain the `compressed_
| |
| 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('{} is not found in files'.format(path)) | |
|
Sebastian Noack
2016/07/11 16:29:45
Use the + operator here?
Vasily Kuznetsov
2016/07/12 09:38:09
Done.
| |
| 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' | |
|
Sebastian Noack
2016/07/11 16:29:44
Isn't that overridden below?
Vasily Kuznetsov
2016/07/12 09:38:08
Indeed it is, but since I'm not sure how the conte
Sebastian Noack
2016/07/12 21:15:25
I mean, isn't the value here overridden by the log
Vasily Kuznetsov
2016/07/13 10:35:31
My thinking was that we are normally adding the co
Sebastian Noack
2016/07/13 16:29:22
Do we even need to consider AppxBlockMap.xml and A
Vasily Kuznetsov
2016/07/13 17:36:45
Yeah, this is possible, but we'll later have other
Sebastian Noack
2016/07/13 17:42:39
The package is signed by the Windows Store. In fac
| |
| 86 }, | |
| 87 'overrides': { | |
| 88 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', | |
| 89 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' | |
| 90 } | |
| 91 } | |
| 92 for filename in files: | |
| 93 if '.' in filename[1:]: | |
| 94 ext = filename.split('.')[-1] | |
|
Sebastian Noack
2016/07/11 16:29:45
Use os.path.splitext?
Vasily Kuznetsov
2016/07/12 09:38:10
Done.
| |
| 95 content_type = mimetypes.guess_type(filename, strict=False) | |
| 96 # The return value can be a string or a tuple: | |
|
Sebastian Noack
2016/07/11 16:29:44
Can it? I think it is always a tuple, or do I miss
Vasily Kuznetsov
2016/07/12 09:38:09
"The return value is a tuple (type, encoding) wher
| |
| 97 # https://docs.python.org/2/library/mimetypes.html#mimetypes.guess_t ype | |
| 98 if isinstance(content_type, tuple): | |
| 99 content_type = content_type[0] | |
| 100 if content_type is not None: | |
| 101 params['defaults'][ext] = content_type | |
| 102 content_types_template = _get_template_for(CONTENT_TYPES) | |
| 103 return content_types_template.render(params).encode('utf-8') | |
| 104 | |
| 105 | |
| 106 def createBuild(baseDir, type='edge', outFile=None, # noqa: preserve API. | |
| 107 buildNum=None, releaseBuild=False, keyFile=None, | |
| 108 devenv=False): | |
| 109 | |
| 110 metadata = packager.readMetadata(baseDir, type) | |
| 111 version = packager.getBuildVersion(baseDir, metadata, releaseBuild, | |
| 112 buildNum) | |
| 113 | |
| 114 outfile = outFile or packager.getDefaultFileName(metadata, version, 'appx') | |
| 115 | |
| 116 params = { | |
| 117 'type': type, | |
| 118 'baseDir': baseDir, | |
| 119 'releaseBuild': releaseBuild, | |
| 120 'version': version, | |
| 121 'devenv': devenv, | |
| 122 'metadata': metadata, | |
| 123 } | |
| 124 | |
| 125 files = packager.Files(packagerChrome.getPackageFiles(params), | |
| 126 packagerChrome.getIgnoredFiles(params)) | |
| 127 | |
| 128 if metadata.has_section('mapping'): | |
| 129 mapped = metadata.items('mapping') | |
| 130 files.readMappedFiles(mapped) | |
| 131 files.read(baseDir, skip=[filename for filename, _ in mapped]) | |
| 132 else: | |
| 133 files.read(baseDir) | |
| 134 | |
| 135 if metadata.has_section('convert_js'): | |
| 136 packagerChrome.convertJS(params, files) | |
| 137 | |
| 138 if metadata.has_section('preprocess'): | |
| 139 files.preprocess(metadata.options('preprocess'), {'needsExt': True}) | |
| 140 | |
| 141 if metadata.has_section('import_locales'): | |
| 142 packagerChrome.importGeckoLocales(params, files) | |
| 143 | |
| 144 files['manifest.json'] = packagerChrome.createManifest(params, files) | |
| 145 | |
| 146 move_files_to_extension(files) | |
| 147 | |
| 148 if metadata.has_section('appx_assets'): | |
| 149 for name, path in metadata.items('appx_assets'): | |
| 150 path = os.path.join(baseDir, path) | |
| 151 files.read(path, 'Assets/' + name) | |
| 152 | |
| 153 files[MANIFEST] = create_appx_manifest(params, files) | |
| 154 files[CONTENT_TYPES] = create_content_types_map(files) | |
| 155 | |
| 156 # We don't support AppxBlockmap.xml generation for compressed zip files at | |
| 157 # the moment. The only way to reliably calculate the compressed size of | |
| 158 # each 64k chunk in the zip file is to override the relevant parts of | |
| 159 # `zipfile` library. We have chosen to not do it for now, so we produce | |
| 160 # an uncompressed zip file. | |
| 161 files[BLOCKMAP] = create_appx_blockmap(files) | |
| 162 | |
| 163 # TODO: Implement AppxBlockmap.xml generation for compressed zip files. | |
| 164 # https://issues.adblockplus.org/ticket/4149 | |
| 165 files.zip(outfile, compress=False) | |
| OLD | NEW |