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