vinix-ports / tools / bundle-x11.py
106 lines · 91 sloc · 4.08 KB
Raw
1"""Assemble a release X11 overlay from two audited vpac clients."""
2
3import argparse
4import filecmp
5import gzip
6import hashlib
7from importlib import import_module
8import os
9from pathlib import Path
10import stat
11import subprocess
12import tarfile
13import tempfile
14
15RELEASE_COMMANDS = (
16 "usr/bin/Xorg", "usr/bin/Xvfb", "usr/bin/Xvfb-glx", "usr/bin/startx",
17 "usr/bin/vinix-wine-host", "usr/bin/vinix-xinput",
18)
19
20
21def tree(root):
22 entries = {}
23 for directory, folders, files in os.walk(root):
24 # os.walk puts symlinks to directories in folders but does not traverse them.
25 for name in folders + files:
26 path = Path(directory, name)
27 entries[path.relative_to(root).as_posix()] = path.lstat().st_mode
28 return entries
29
30
31def overlay_paths(base, full):
32 before, after = tree(base), tree(full)
33 for path, mode in before.items():
34 updated = after.get(path)
35 if updated is None or mode != updated:
36 raise ValueError(f"X11 install removed or replaced /{path}")
37 if stat.S_ISDIR(mode):
38 continue
39 original, installed = base / path, full / path
40 matches = (os.readlink(original) == os.readlink(installed) if stat.S_ISLNK(mode)
41 else filecmp.cmp(original, installed, shallow=False))
42 if not matches:
43 raise ValueError(f"X11 install changed /{path} from the desktop root")
44 added = sorted(after.keys() - before.keys())
45 for path in added:
46 mode = after[path]
47 if not (stat.S_ISREG(mode) or stat.S_ISLNK(mode) or stat.S_ISDIR(mode)):
48 raise ValueError(f"unexpected sidecar entry type for /{path}")
49 return added
50
51
52def packages(client):
53 package_fields = import_module("stage-image").package_fields
54 records = (client / "db/local").glob("*/desc")
55 return {record["NAME"]: record["VERSION"]
56 for record in (package_fields(path.read_text()) for path in records)}
57
58
59def owner_root(entry):
60 entry.uid = entry.gid = 0
61 entry.uname = entry.gname = "root"
62 return entry
63
64
65def bundle(args):
66 base, full = args.desktop.resolve(), args.x11.resolve()
67 output = args.output.resolve()
68 if output.is_relative_to(base) or output.is_relative_to(full):
69 raise ValueError("put sidecar output outside both clients")
70 if output.exists():
71 raise FileExistsError(output)
72 for client in (base, full):
73 subprocess.run([args.auditor, args.arch, client], check=True)
74 common, combined = packages(base), packages(full)
75 if ("vinix-release-desktop" not in common or "vinix-release-x11" in common
76 or "vinix-release-x11" not in combined or not common.items() < combined.items()):
77 raise ValueError("expected desktop and desktop-plus-X11 package roots")
78 base, full = base / "root", full / "root"
79 paths = overlay_paths(base, full)
80 for name in RELEASE_COMMANDS:
81 if name not in paths or not os.access(full / name, os.X_OK):
82 raise ValueError(f"sidecar is missing executable /{name}")
83
84 suffix = "amd64" if args.arch == "x86_64" else "arm64"
85 name = f"vinix-x11-{suffix}.tar.gz"
86 with tempfile.TemporaryDirectory(dir=output.parent) as work:
87 staging = Path(work, "sidecar")
88 staging.mkdir(mode=0o755)
89 archive = staging / name
90 with archive.open("xb") as data:
91 with gzip.GzipFile(filename="", mode="wb", fileobj=data, mtime=0) as compressed:
92 with tarfile.open(fileobj=compressed, mode="w|", format=tarfile.USTAR_FORMAT) as tar:
93 for path in paths:
94 tar.add(full / path, arcname=f"./{path}", recursive=False, filter=owner_root)
95 with archive.open("rb") as data:
96 digest = hashlib.file_digest(data, "sha256").hexdigest()
97 (staging / (name + ".sha256")).write_text(f"{digest} {name}\n")
98 staging.rename(output)
99
100
101if __name__ == "__main__":
102 parser = argparse.ArgumentParser(description=__doc__)
103 parser.add_argument("arch", choices=("x86_64", "aarch64"))
104 for label in ("desktop", "x11", "auditor", "output"):
105 parser.add_argument(label, type=Path)
106 bundle(parser.parse_args())
107