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: Created June 13, 2016, 12:31 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
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 os
8 import zipfile
9
10 import packager
11 import packagerChrome
12
13 MANIFEST = 'AppxManifest.xml'
14 CONTENT_TYPES = '[Content_Types].xml'
15 BLOCKMAP = 'AppxBlockMap.xml'
16 BLOCKSIZE = 65536
Wladimir Palant 2016/06/17 09:53:46 Maybe more obvious if hexadecimal - 0x10000?
Vasily Kuznetsov 2016/06/17 17:53:26 I'm not sure if it's actually more obvious. Becaus
17
18 # Size of the fixed part of Zip local file header.
19 # See https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers
20 LFH_FIXED_SIZE = 30
Wladimir Palant 2016/06/17 09:53:46 zipfile.sizeFileHeader?
Vasily Kuznetsov 2016/06/17 17:53:27 Oh, I didn't know about this constant. It's not me
21
22
23 def _get_template_for(filename):
24 return packager.getTemplate('edge/{}.tmpl'.format(filename))
25
26
27 def _SHA256(block):
28 h = hashlib.new('sha256')
29 h.update(block)
30 return base64.b64encode(h.digest())
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': str(len(data)),
38 'lfh_size': str(LFH_FIXED_SIZE + len(filename)),
Wladimir Palant 2016/06/17 09:53:46 Assumption here: filename is ASCII. Maybe enforce
Vasily Kuznetsov 2016/06/17 17:53:26 If it's not ASCII, it gets encoded as utf-8. I imp
39 'blocks': [{'hash': _SHA256(block), 'compressed_size': str(len(block))}
40 for block in blocks]
Wladimir Palant 2016/06/17 09:53:46 No point stringifying all the numbers, the represe
Vasily Kuznetsov 2016/06/17 17:53:27 Done.
41 }
42
43
44 def createAppxBlockmap(files):
45 """Create APPX blockmap for the list of files."""
46 template = _get_template_for(BLOCKMAP)
47 files = [_make_blockmap_entry(n, d) for n, d in files.items()]
48 return template.render(files=files).encode('utf-8')
49
50
51 def createAppxManifest(params):
52 """Create AppxManifest.xml."""
53 template = _get_template_for(MANIFEST)
54 params = dict(params)
55 metadata = params['metadata']
56 params['package_identity'] = dict(metadata.items('package_identity'))
57 params.update(metadata.items('general'))
58 for size in ['44', '50', '150']:
59 params['logo_{}'.format(size)] = 'Assets\\logo_{}.png'.format(size)
Wladimir Palant 2016/06/17 09:53:46 We need to verify that these files actually exist.
Vasily Kuznetsov 2016/06/17 17:53:27 I started checking that they exist.
60 return template.render(params).encode('utf-8')
61
62
63 def convertToAppx(files):
64 """Move all files into `Extension` folder for APPX packaging."""
65 names = sorted(files.keys(), key=len, reverse=True)
Wladimir Palant 2016/06/17 09:53:45 Why sort here? If the point is creating a copy of
Vasily Kuznetsov 2016/06/17 17:53:26 Sorting is to make sure that moving 'foo' to 'Exte
Wladimir Palant 2016/06/29 19:33:44 This makes sense but it's also non-obvious. You be
Vasily Kuznetsov 2016/07/01 19:51:27 Done
66 for filename in names:
67 files['Extension/' + filename] = files[filename]
68 del files[filename]
69 content_types_template = _get_template_for(CONTENT_TYPES)
70 files[CONTENT_TYPES] = content_types_template.render().encode('utf-8')
Wladimir Palant 2016/06/17 09:53:46 This function does two things - prefix file names
Vasily Kuznetsov 2016/06/17 17:53:27 Yeah, makes sense. I broke it up.
71
72
73 class Files(packager.Files):
74 """Files subclass that zips without compression."""
75
76 # We don't support AppxBlockmap.xml generation for compressed zip files at
77 # the moment. The only way to reliably calculate the compressed size of
78 # each 64k chunk in the zip file is to override the relevant parts of
79 # `zipfile` library. We have chosen to not do it for now, so zip() below
80 # doesn't perform any compression.
81
82 def zip(self, outFile, sortKey=None):
Vasily Kuznetsov 2016/06/13 12:57:31 This can eventually be replaced with a compressing
Wladimir Palant 2016/06/17 09:53:46 Maybe add a TODO comment on that, so it's obvious?
Vasily Kuznetsov 2016/06/17 17:53:27 Done.
83 """Pack files into zip archive producing matching appx block map."""
84 zf = zipfile.ZipFile(outFile, 'w', zipfile.ZIP_STORED)
85 for name in sorted(self, key=sortKey):
86 zf.writestr(name, self[name])
87 zf.writestr(BLOCKMAP, createAppxBlockmap(self))
88 zf.close()
89
90
91 def createBuild(baseDir, type='edge', outFile=None, buildNum=None,
Vasily Kuznetsov 2016/06/13 12:57:31 This function has been copied from `packagerChrome
92 releaseBuild=False, keyFile=None, devenv=False):
93
94 metadata = packager.readMetadata(baseDir, type)
95 version = packager.getBuildVersion(baseDir, metadata, releaseBuild,
96 buildNum)
97
98 if outFile is None:
99 outFile = packager.getDefaultFileName(metadata, version, 'appx')
100
101 params = {
102 'type': type,
103 'baseDir': baseDir,
104 'releaseBuild': releaseBuild,
105 'version': version,
106 'devenv': devenv,
107 'metadata': metadata,
108 }
109
110 files = Files(packagerChrome.getPackageFiles(params),
111 packagerChrome.getIgnoredFiles(params))
112
113 if metadata.has_section('mapping'):
114 mapped = metadata.items('mapping')
115 files.readMappedFiles(mapped)
116 files.read(baseDir, skip=[filename for filename, _ in mapped])
117 else:
118 files.read(baseDir)
119
120 if metadata.has_section('convert_js'):
121 packagerChrome.convertJS(params, files)
122
123 if metadata.has_section('preprocess'):
124 files.preprocess(metadata.options('preprocess'), {'needsExt': True})
125
126 if metadata.has_section('import_locales'):
127 packagerChrome.importGeckoLocales(params, files)
128
129 files['manifest.json'] = packagerChrome.createManifest(params, files)
130
131 convertToAppx(files)
132
133 if metadata.has_section('appx_assets'):
134 for name, path in metadata.items('appx_assets'):
135 path = os.path.join(baseDir, path)
136 files.read(path, 'Assets/{}'.format(name))
137
138 files[MANIFEST] = createAppxManifest(params)
139 files.zip(outFile)
OLDNEW

Powered by Google App Engine
This is Rietveld