LEFT | RIGHT |
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 | 5 import base64 |
6 import hashlib | 6 import hashlib |
| 7 import json |
| 8 import mimetypes |
7 import os | 9 import os |
8 import zipfile | 10 import zipfile |
9 | 11 |
10 import packager | 12 import packager |
11 import packagerChrome | 13 import packagerChrome |
12 | 14 |
| 15 # Files and directories expected inside of the .APPX archive. |
13 MANIFEST = 'AppxManifest.xml' | 16 MANIFEST = 'AppxManifest.xml' |
14 CONTENT_TYPES = '[Content_Types].xml' | 17 CONTENT_TYPES = '[Content_Types].xml' |
15 BLOCKMAP = 'AppxBlockMap.xml' | 18 BLOCKMAP = 'AppxBlockMap.xml' |
| 19 EXTENSION_DIR = 'Extension' |
| 20 ASSETS_DIR = 'Assets' |
| 21 |
| 22 # Size of uncompressed block in the APPX block map. |
16 BLOCKSIZE = 64 * 1024 | 23 BLOCKSIZE = 64 * 1024 |
| 24 |
| 25 defaultLocale = packagerChrome.defaultLocale |
17 | 26 |
18 | 27 |
19 def _get_template_for(filename): | 28 def _get_template_for(filename): |
20 return packager.getTemplate('edge/{}.tmpl'.format(filename)) | 29 return packager.getTemplate('edge/{}.tmpl'.format(filename)) |
21 | |
22 | |
23 def _SHA256(block): | |
24 h = hashlib.new('sha256') | |
25 h.update(block) | |
26 return base64.b64encode(h.digest()) | |
27 | 30 |
28 | 31 |
29 def _lfh_size(filename): | 32 def _lfh_size(filename): |
30 """Compute the size of zip local file header for `filename`.""" | 33 """Compute the size of zip local file header for `filename`.""" |
31 try: | 34 try: |
32 filename = filename.encode('utf-8') | 35 filename = filename.encode('utf-8') |
33 except UnicodeDecodeError: | 36 except UnicodeDecodeError: |
34 pass # filename is already a byte string. | 37 pass # filename is already a byte string. |
35 return zipfile.sizeFileHeader + len(filename) | 38 return zipfile.sizeFileHeader + len(filename) |
36 | 39 |
37 | 40 |
38 def _make_blockmap_entry(filename, data): | 41 def _make_blockmap_entry(filename, data): |
39 blocks = [data[i:i + BLOCKSIZE] for i in range(0, len(data), BLOCKSIZE)] | 42 blocks = [data[i:i + BLOCKSIZE] for i in range(0, len(data), BLOCKSIZE)] |
40 return { | 43 return { |
41 'name': filename.replace('/', '\\'), | 44 'name': filename.replace('/', '\\'), |
42 'size': len(data), | 45 'size': len(data), |
43 'lfh_size': _lfh_size(filename), | 46 'lfh_size': _lfh_size(filename), |
44 'blocks': [{'hash': _SHA256(block), 'compressed_size': len(block)} | 47 'blocks': [ |
45 for block in blocks] | 48 {'hash': base64.b64encode(hashlib.sha256(block).digest())} |
| 49 for block in blocks |
| 50 ] |
46 } | 51 } |
47 | 52 |
48 | 53 |
49 def createAppxBlockmap(files): | 54 def create_appx_blockmap(files): |
50 """Create APPX blockmap for the list of files.""" | 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. |
51 template = _get_template_for(BLOCKMAP) | 62 template = _get_template_for(BLOCKMAP) |
52 files = [_make_blockmap_entry(n, d) for n, d in files.items()] | 63 files = [_make_blockmap_entry(n, d) for n, d in files.items()] |
53 return template.render(files=files).encode('utf-8') | 64 return template.render(files=files).encode('utf-8') |
54 | 65 |
55 | 66 |
56 def createAppxManifest(params, files): | 67 def load_translation(files, locale): |
| 68 """Load translation strings for locale from files.""" |
| 69 path = '{}/_locales/{}/messages.json'.format(EXTENSION_DIR, locale) |
| 70 return json.loads(files[path]) |
| 71 |
| 72 |
| 73 def pad_version(version): |
| 74 """Make sure version number has 4 groups of digits.""" |
| 75 groups = (version.split('.') + ['0', '0', '0'])[:4] |
| 76 return '.'.join(groups) |
| 77 |
| 78 |
| 79 def create_appx_manifest(params, files, release_build=False): |
57 """Create AppxManifest.xml.""" | 80 """Create AppxManifest.xml.""" |
58 template = _get_template_for(MANIFEST) | |
59 params = dict(params) | 81 params = dict(params) |
60 metadata = params['metadata'] | 82 metadata = params['metadata'] |
61 params['package_identity'] = dict(metadata.items('package_identity')) | 83 w = params['windows_version'] = {} |
| 84 w['min'], w['max'] = metadata.get('compat', 'windows').split('/') |
62 params.update(metadata.items('general')) | 85 params.update(metadata.items('general')) |
| 86 params['version'] = pad_version(params['version']) |
| 87 |
| 88 translation = load_translation(files, defaultLocale) |
| 89 name_key = 'name' if release_build else 'name_devbuild' |
| 90 params['display_name'] = translation[name_key]['message'] |
| 91 params['description'] = translation['description']['message'] |
| 92 |
63 for size in ['44', '50', '150']: | 93 for size in ['44', '50', '150']: |
64 path = 'Assets/logo_{}.png'.format(size) | 94 path = '{}/logo_{}.png'.format(ASSETS_DIR, size) |
65 if path not in files: | 95 if path not in files: |
66 raise KeyError('{} is not found in files'.format(path)) | 96 raise KeyError(path + 'is not found in files') |
67 params['logo_{}'.format(size)] = path.replace('/', '\\') | 97 params['logo_' + size] = path.replace('/', '\\') |
| 98 |
| 99 template = _get_template_for(MANIFEST) |
68 return template.render(params).encode('utf-8') | 100 return template.render(params).encode('utf-8') |
69 | 101 |
70 | 102 |
71 def moveFilesToExtension(files): | 103 def move_files_to_extension(files): |
72 """Move all files into `Extension` folder for APPX packaging.""" | 104 """Move all files into `Extension` folder for APPX packaging.""" |
| 105 # We sort the files to ensure that 'Extension/xyz' is moved before 'xyz'. |
| 106 # If 'xyz' is moved first, it would overwrite 'Extension/xyz' and its |
| 107 # original content would be lost. |
73 names = sorted(files.keys(), key=len, reverse=True) | 108 names = sorted(files.keys(), key=len, reverse=True) |
74 for filename in names: | 109 for filename in names: |
75 files['Extension/' + filename] = files[filename] | 110 files['{}/{}'.format(EXTENSION_DIR, filename)] = files.pop(filename) |
76 del files[filename] | |
77 | 111 |
78 | 112 |
79 def createContentTypesMap(): | 113 def create_content_types_map(filenames): |
80 """Create [Content_Types].xml -- a mime type map.""" | 114 """Create [Content_Types].xml -- a mime type map.""" |
| 115 params = {'defaults': {}, 'overrides': {}} |
| 116 overrides = { |
| 117 BLOCKMAP: 'application/vnd.ms-appx.blockmap+xml', |
| 118 MANIFEST: 'application/vnd.ms-appx.manifest+xml' |
| 119 } |
| 120 for filename in filenames: |
| 121 ext = os.path.splitext(filename)[1] |
| 122 if ext: |
| 123 content_type = mimetypes.guess_type(filename, strict=False)[0] |
| 124 if content_type is not None: |
| 125 params['defaults'][ext[1:]] = content_type |
| 126 if filename in overrides: |
| 127 params['overrides']['/' + filename] = overrides[filename] |
81 content_types_template = _get_template_for(CONTENT_TYPES) | 128 content_types_template = _get_template_for(CONTENT_TYPES) |
82 return content_types_template.render().encode('utf-8') | 129 return content_types_template.render(params).encode('utf-8') |
83 | 130 |
84 | 131 |
85 class Files(packager.Files): | 132 def createBuild(baseDir, type='edge', outFile=None, # noqa: preserve API. |
86 """Files subclass that zips without compression.""" | 133 buildNum=None, releaseBuild=False, keyFile=None, |
87 | 134 devenv=False): |
88 # We don't support AppxBlockmap.xml generation for compressed zip files at | |
89 # the moment. The only way to reliably calculate the compressed size of | |
90 # each 64k chunk in the zip file is to override the relevant parts of | |
91 # `zipfile` library. We have chosen to not do it for now, so zip() below | |
92 # doesn't perform any compression. | |
93 | |
94 # TODO: Replace zip() below with a compressing version: | |
95 # https://issues.adblockplus.org/ticket/4149 | |
96 | |
97 def zip(self, outFile, sortKey=None): | |
98 """Pack files into zip archive producing matching appx block map.""" | |
99 zf = zipfile.ZipFile(outFile, 'w', zipfile.ZIP_STORED) | |
100 for name in sorted(self, key=sortKey): | |
101 zf.writestr(name, self[name]) | |
102 zf.writestr(BLOCKMAP, createAppxBlockmap(self)) | |
103 zf.close() | |
104 | |
105 | |
106 def createBuild(baseDir, type='edge', outFile=None, buildNum=None, | |
107 releaseBuild=False, keyFile=None, devenv=False): | |
108 | 135 |
109 metadata = packager.readMetadata(baseDir, type) | 136 metadata = packager.readMetadata(baseDir, type) |
110 version = packager.getBuildVersion(baseDir, metadata, releaseBuild, | 137 version = packager.getBuildVersion(baseDir, metadata, releaseBuild, |
111 buildNum) | 138 buildNum) |
112 | 139 |
113 if outFile is None: | 140 outfile = outFile or packager.getDefaultFileName(metadata, version, 'appx') |
114 outFile = packager.getDefaultFileName(metadata, version, 'appx') | |
115 | 141 |
116 params = { | 142 params = { |
117 'type': type, | 143 'type': type, |
118 'baseDir': baseDir, | 144 'baseDir': baseDir, |
119 'releaseBuild': releaseBuild, | 145 'releaseBuild': releaseBuild, |
120 'version': version, | 146 'version': version, |
121 'devenv': devenv, | 147 'devenv': devenv, |
122 'metadata': metadata, | 148 'metadata': metadata, |
123 } | 149 } |
124 | 150 |
125 files = Files(packagerChrome.getPackageFiles(params), | 151 files = packager.Files(packagerChrome.getPackageFiles(params), |
126 packagerChrome.getIgnoredFiles(params)) | 152 packagerChrome.getIgnoredFiles(params)) |
127 | 153 |
128 if metadata.has_section('mapping'): | 154 if metadata.has_section('mapping'): |
129 mapped = metadata.items('mapping') | 155 mapped = metadata.items('mapping') |
130 files.readMappedFiles(mapped) | 156 files.readMappedFiles(mapped) |
131 files.read(baseDir, skip=[filename for filename, _ in mapped]) | 157 files.read(baseDir, skip=[filename for filename, _ in mapped]) |
132 else: | 158 else: |
133 files.read(baseDir) | 159 files.read(baseDir) |
134 | 160 |
135 if metadata.has_section('convert_js'): | 161 if metadata.has_section('convert_js'): |
136 packagerChrome.convertJS(params, files) | 162 packagerChrome.convertJS(params, files) |
137 | 163 |
138 if metadata.has_section('preprocess'): | 164 if metadata.has_section('preprocess'): |
139 files.preprocess(metadata.options('preprocess'), {'needsExt': True}) | 165 files.preprocess(metadata.options('preprocess'), {'needsExt': True}) |
140 | 166 |
141 if metadata.has_section('import_locales'): | 167 if metadata.has_section('import_locales'): |
142 packagerChrome.importGeckoLocales(params, files) | 168 packagerChrome.importGeckoLocales(params, files) |
143 | 169 |
144 files['manifest.json'] = packagerChrome.createManifest(params, files) | 170 files['manifest.json'] = packagerChrome.createManifest(params, files) |
145 | 171 |
146 moveFilesToExtension(files) | 172 move_files_to_extension(files) |
147 | 173 |
148 if metadata.has_section('appx_assets'): | 174 if metadata.has_section('appx_assets'): |
149 for name, path in metadata.items('appx_assets'): | 175 for name, path in metadata.items('appx_assets'): |
150 path = os.path.join(baseDir, path) | 176 path = os.path.join(baseDir, path) |
151 files.read(path, 'Assets/{}'.format(name)) | 177 files.read(path, '{}/{}'.format(ASSETS_DIR, name)) |
152 | 178 |
153 files[MANIFEST] = createAppxManifest(params, files) | 179 files[MANIFEST] = create_appx_manifest(params, files, releaseBuild) |
154 files[CONTENT_TYPES] = createContentTypesMap() | 180 files[BLOCKMAP] = create_appx_blockmap(files) |
| 181 files[CONTENT_TYPES] = create_content_types_map(files.keys() + [BLOCKMAP]) |
155 | 182 |
156 files.zip(outFile) | 183 files.zip(outfile, compression=zipfile.ZIP_STORED) |
LEFT | RIGHT |