Bug 1821117 [wpt PR 38888] - Expose desired{Execution|Render}Start in LoAF+ScriptTimi...
[gecko.git] / taskcluster / scripts / misc / unpack-sdk.py
blob118560bdb8950737af597e46cb1a7aa1c185f5bd
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/.
5 import hashlib
6 import os
7 import shutil
8 import stat
9 import sys
10 import tempfile
11 from urllib.request import urlopen
13 from mozpack.macpkg import Pbzx, uncpio, unxar
16 def unpack_sdk(url, sha256, extract_prefix, out_dir="."):
17 with tempfile.TemporaryFile() as pkg:
18 hash = hashlib.sha256()
19 with urlopen(url) as fh:
20 # Equivalent to shutil.copyfileobj, but computes sha256 at the same time.
21 while True:
22 buf = fh.read(1024 * 1024)
23 if not buf:
24 break
25 hash.update(buf)
26 pkg.write(buf)
27 digest = hash.hexdigest()
28 if digest != sha256:
29 raise Exception(f"(actual) {digest} != (expected) {sha256}")
31 pkg.seek(0, os.SEEK_SET)
33 for name, content in unxar(pkg):
34 if name == "Payload":
35 extract_payload(content, extract_prefix, out_dir)
38 def extract_payload(fileobj, extract_prefix, out_dir="."):
39 for path, mode, content in uncpio(Pbzx(fileobj)):
40 if not path:
41 continue
42 path = path.decode()
43 if not path.startswith(extract_prefix):
44 continue
45 path = os.path.join(out_dir, path[len(extract_prefix) :].lstrip("/"))
46 if stat.S_ISDIR(mode):
47 os.makedirs(path, exist_ok=True)
48 else:
49 parent = os.path.dirname(path)
50 if parent:
51 os.makedirs(parent, exist_ok=True)
53 if stat.S_ISLNK(mode):
54 os.symlink(content.read(), path)
55 elif stat.S_ISREG(mode):
56 with open(path, "wb") as out:
57 shutil.copyfileobj(content, out)
58 else:
59 raise Exception(f"File mode {mode:o} is not supported")
62 if __name__ == "__main__":
63 unpack_sdk(*sys.argv[1:])