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 io import BytesIO
from urllib.request import urlopen
from mozpack.macpkg import Pbzx, uncpio, unxar
def unpack_sdk(url, sha512, extract_prefix, out_dir="."):
if "MOZ_AUTOMATION" in os.environ:
with tempfile.TemporaryFile() as pkg:
hash = hashlib.sha512()
for attempt in range(3):
if attempt != 0:
print(f"Failed to download from {url}. Retrying", file=sys.stderr)
with urlopen(url) as fh:
# Equivalent to shutil.copyfileobj, but computes sha512 at the same time.
while True:
buf = * 1024)
if not buf:
digest = hash.hexdigest()
if digest == sha512:
raise Exception(f"(actual) {digest} != (expected) {sha512}"), os.SEEK_SET)
for name, content in unxar(pkg):
if name in ("Payload", "Content"):
extract_payload(content, extract_prefix, out_dir)
def extract_payload(fileobj, extract_prefix, out_dir="."):
hardlinks = {}
for path, st, content in uncpio(Pbzx(fileobj)):
if not path:
path = path.decode()
matches = path.startswith(extract_prefix)
if matches:
path = os.path.join(out_dir, path[len(extract_prefix) :].lstrip("/"))
# When there are hardlinks, normally a cpio stream is supposed to
# contain the data for all of them, but, even with compression, that
# can be a waste of space, so in some cpio streams (*cough* *cough*,
# Apple's, e.g. in Xcode), the files after the first one have dummy
# data.
# As we may be filtering the first file out (if it doesn't match
# extract_prefix), we need to keep its data around (we're not going
# to be able to rewind).
if stat.S_ISREG(st.mode) and st.nlink > 1:
key = (, st.ino)
hardlink = hardlinks.get(key)
if hardlink:
hardlink[0] -= 1
if hardlink[0] == 0:
del hardlinks[key]
content = hardlink[1]
if isinstance(content, BytesIO):
if matches:
hardlink[1] = path
elif matches:
hardlink = hardlinks[key] = [st.nlink - 1, path]
hardlink = hardlinks[key] = [st.nlink - 1, BytesIO(]
content = hardlink[1]
if not matches:
if stat.S_ISDIR(st.mode):
os.makedirs(path, exist_ok=True)
parent = os.path.dirname(path)
if parent:
os.makedirs(parent, exist_ok=True)
if stat.S_ISLNK(st.mode):
os.symlink(, path)
elif stat.S_ISREG(st.mode):
if isinstance(content, str):, path)
with open(path, "wb") as out:
shutil.copyfileobj(content, out)
raise Exception(f"File mode {st.mode:o} is not supported")
if __name__ == "__main__":