Source code

Revision control

Copy as Markdown

Other Tools

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at
import hashlib
import os
import shutil
import stat
import sys
import tempfile
from urllib.request import urlopen
from mozpack.macpkg import Pbzx, uncpio, unxar
def unpack_sdk(url, sha256, extract_prefix, out_dir="."):
with tempfile.TemporaryFile() as pkg:
hash = hashlib.sha256()
with urlopen(url) as fh:
# Equivalent to shutil.copyfileobj, but computes sha256 at the same time.
while True:
buf = * 1024)
if not buf:
digest = hash.hexdigest()
if digest != sha256:
raise Exception(f"(actual) {digest} != (expected) {sha256}"), os.SEEK_SET)
for name, content in unxar(pkg):
if name == "Payload":
extract_payload(content, extract_prefix, out_dir)
def extract_payload(fileobj, extract_prefix, out_dir="."):
for path, mode, content in uncpio(Pbzx(fileobj)):
if not path:
path = path.decode()
if not path.startswith(extract_prefix):
path = os.path.join(out_dir, path[len(extract_prefix) :].lstrip("/"))
if stat.S_ISDIR(mode):
os.makedirs(path, exist_ok=True)
parent = os.path.dirname(path)
if parent:
os.makedirs(parent, exist_ok=True)
if stat.S_ISLNK(mode):
os.symlink(, path)
elif stat.S_ISREG(mode):
with open(path, "wb") as out:
shutil.copyfileobj(content, out)
raise Exception(f"File mode {mode:o} is not supported")
if __name__ == "__main__":