vinix-ports / tools / release-inputs.py
64 lines · 58 sloc · 3.0 KB · 7f608e7c598a36c9e4d491b034047aee5046269e
Raw
1"""Prepare the audited initramfs and X11 sidecar consumed by deploy-iso.sh."""
2
3import argparse
4import hashlib
5import os
6from pathlib import Path
7import re
8import subprocess
9import sys
10import tempfile
11
12
13def assemble(args):
14 output = args.output
15 if output.exists() or output.is_symlink():
16 raise FileExistsError(output)
17 arch = "amd64" if args.arch == "x86_64" else "arm64"
18 if not re.fullmatch(r"[0-9a-f]{40}", args.commit):
19 raise ValueError("expected the full Vinix source commit")
20 if args.release and not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._-]*", args.release):
21 raise ValueError("invalid Vinix release tag")
22 programs = ["usr/bin/vinix-desktop", "usr/sbin/vinix-account", "usr/sbin/vinix-authd",
23 "usr/sbin/vinix-session", "usr/sbin/vinix-system"]
24 machine = 62 if args.arch == "x86_64" else 183
25 for program in programs:
26 path = args.native / program
27 if path.is_symlink() or not path.is_file() or not os.access(path, os.X_OK):
28 raise ValueError(f"native layer is missing executable /{program}")
29 with path.open("rb") as stream:
30 header = stream.read(20)
31 if header[:6] != b"\x7fELF\x02\x01" or int.from_bytes(header[18:20], "little") != machine:
32 raise ValueError(f"native layer executable has the wrong ELF architecture: /{program}")
33
34 tools = Path(__file__).resolve().parent
35 with tempfile.TemporaryDirectory(prefix=".ports-inputs-", dir=output.parent) as temporary:
36 work = Path(temporary)
37 bundle = work / "bundle"
38 stage = work / "image"
39 subprocess.run([sys.executable, tools / "bundle-x11.py", args.arch, args.desktop,
40 args.graphics, args.auditor, bundle], check=True)
41 subprocess.run([sys.executable, tools / "stage-image.py", args.arch, args.desktop,
42 args.repo, stage, args.auditor, args.fingerprint, "--native-layer",
43 args.native, "--initramfs", f"--release={args.release}",
44 "--seed-client", args.graphics], check=True)
45 (stage / "initramfs.tar").rename(bundle / "initramfs.tar")
46 info = bundle / "build-info"
47 info.write_text(f"commit={args.commit}\nrelease={args.release}\narch={arch}\n")
48 assets = sorted(bundle.iterdir())
49 with (bundle / "SHA256SUMS").open("x") as manifest:
50 for path in assets:
51 with path.open("rb") as stream:
52 digest = hashlib.file_digest(stream, "sha256").hexdigest()
53 manifest.write(f"{digest} {path.name}\n")
54 bundle.rename(output)
55
56
57if __name__ == "__main__":
58 parser = argparse.ArgumentParser(description=__doc__)
59 parser.add_argument("arch", choices=("x86_64", "aarch64"))
60 for name in ("repo", "desktop", "graphics", "native", "auditor", "output"):
61 parser.add_argument(name, type=lambda path: Path(path).absolute())
62 for name in ("fingerprint", "commit", "release"):
63 parser.add_argument(name)
64 assemble(parser.parse_args())
65