| 1 | #!/usr/bin/env python3 |
| 2 | """Pin the Chromium the CI box runs browser tests with, and the page |
| 3 | renderer runners ship: write scripts/ci-chromium.json and |
| 4 | internal/release/renderer.json. |
| 5 | |
| 6 | scripts/ci-chromium-pin.py VERSION DEBIAN_SNAPSHOT SECURITY_SNAPSHOT |
| 7 | |
| 8 | for example |
| 9 | |
| 10 | scripts/ci-chromium-pin.py 156.0.8078.79-1~deb13u1 20261107T024144Z 20261103T013555Z |
| 11 | |
| 12 | The browser is Debian 13's chromium-headless-shell, the system the box |
| 13 | image runs, because Debian builds it for arm64 (hosted CI) and amd64 |
| 14 | alike; Chrome for Testing has no Linux arm64 build. It is a program and |
| 15 | some sixty libraries, not one file, so this reads the Packages indexes of |
| 16 | trixie and trixie-security at two snapshot.debian.org timestamps and |
| 17 | pins every package the headless shell needs that a Debian image does not |
| 18 | already have: what Priority: required pulls in is in every Debian image, |
| 19 | the box's too, and is left out. Each package is pinned by the SHA-256 the |
| 20 | signed index gives it, at a snapshot URL that never changes; |
| 21 | scripts/ci-box.py fetches and checks them and unpacks them into one tree. |
| 22 | |
| 23 | internal/release/renderer.json is the same browser for Linux amd64 with |
| 24 | nothing left out: the same resolution without pruning the base, so a |
| 25 | tree built from it runs with nothing from the host (the loader and libc |
| 26 | included). Its packages marked "base" are the ones ci-chromium.json leaves |
| 27 | to the image; the rest are ci-chromium.json's amd64 packages, entry for |
| 28 | entry. The release build fetches and checks them (sha256) and builds the |
| 29 | renderer tree; nothing downloads them at run time. |
| 30 | |
| 31 | Raise the pin with each Chromium security release: the newest version is |
| 32 | at https://snapshot.debian.org/mr/binary/chromium-headless-shell/, and |
| 33 | its first_seen there (binfiles?fileinfo=1) is the security timestamp. |
| 34 | """ |
| 35 | import json |
| 36 | import lzma |
| 37 | import os |
| 38 | import re |
| 39 | import sys |
| 40 | import urllib.request |
| 41 | |
| 42 | ARCHES = ("amd64", "arm64") |
| 43 | TOP = ("chromium-headless-shell", "chromium-common") |
| 44 | # chromium-common's own dependencies are the full browser's (x11-utils for |
| 45 | # xmessage, xdg-utils for opening links); the headless shell runs neither. |
| 46 | SKIP = {"x11-utils", "xdg-utils"} |
| 47 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 48 | OUT = os.path.join(HERE, "ci-chromium.json") |
| 49 | RENDERER_OUT = os.path.join(HERE, os.pardir, "internal", "release", "renderer.json") |
| 50 | RENDERER_ARCH = "amd64" |
| 51 | SNAPSHOT = "https://snapshot.debian.org/archive/%s/%s/" |
| 52 | |
| 53 | |
| 54 | def fetch(url): |
| 55 | request = urllib.request.Request(url, headers={"User-Agent": "equipo-ci-chromium-pin"}) |
| 56 | with urllib.request.urlopen(request, timeout=300) as response: |
| 57 | return response.read() |
| 58 | |
| 59 | |
| 60 | def stanzas(text): |
| 61 | for block in text.split("\n\n"): |
| 62 | fields, last = {}, None |
| 63 | for line in block.splitlines(): |
| 64 | if line[:1] in (" ", "\t") and last: |
| 65 | fields[last] += "\n" + line |
| 66 | elif ":" in line: |
| 67 | last, value = line.split(":", 1) |
| 68 | fields[last] = value.strip() |
| 69 | if "Package" in fields: |
| 70 | yield fields |
| 71 | |
| 72 | |
| 73 | def index(archive, stamp, suite, arch): |
| 74 | url = SNAPSHOT % (archive, stamp) + "dists/%s/main/binary-%s/Packages.xz" % (suite, arch) |
| 75 | packages = {} |
| 76 | for p in stanzas(lzma.decompress(fetch(url)).decode()): |
| 77 | p["_base"] = SNAPSHOT % (archive, stamp) |
| 78 | packages[p["Package"]] = p |
| 79 | return packages |
| 80 | |
| 81 | |
| 82 | def relations(p): |
| 83 | """The alternatives of each Depends and Pre-Depends group, names only.""" |
| 84 | groups = [] |
| 85 | for field in ("Pre-Depends", "Depends"): |
| 86 | for group in filter(None, (g.strip() for g in p.get(field, "").split(","))): |
| 87 | groups.append([re.split(r"[\s(\[:]", alt.strip(), maxsplit=1)[0] for alt in group.split("|")]) |
| 88 | return groups |
| 89 | |
| 90 | |
| 91 | def resolver(packages): |
| 92 | provides = {} |
| 93 | for name, p in packages.items(): |
| 94 | for virtual in filter(None, (v.strip() for v in p.get("Provides", "").split(","))): |
| 95 | provides.setdefault(re.split(r"[\s(]", virtual, maxsplit=1)[0], name) |
| 96 | |
| 97 | def pick(alternatives): |
| 98 | for alt in alternatives: |
| 99 | if alt in packages: |
| 100 | return alt |
| 101 | for alt in alternatives: |
| 102 | if alt in provides: |
| 103 | return provides[alt] |
| 104 | raise SystemExit("no package for any of %s" % " | ".join(alternatives)) |
| 105 | |
| 106 | def closure(start, skip=frozenset(), prune=frozenset()): |
| 107 | seen, todo = set(), list(start) |
| 108 | while todo: |
| 109 | name = todo.pop() |
| 110 | if name in seen or name in prune: |
| 111 | continue |
| 112 | seen.add(name) |
| 113 | if name in skip: |
| 114 | continue |
| 115 | for group in relations(packages[name]): |
| 116 | todo.append(pick(group)) |
| 117 | return seen |
| 118 | return closure |
| 119 | |
| 120 | |
| 121 | def archive(debian, security, arch): |
| 122 | """Every package of trixie at the debian snapshot, with trixie-security's |
| 123 | updates at the security snapshot in place of the release's builds.""" |
| 124 | packages = index("debian", debian, "trixie", arch) |
| 125 | for name, p in index("debian-security", security, "trixie-security", arch).items(): |
| 126 | packages[name] = p # a security update supersedes the release's build |
| 127 | return packages |
| 128 | |
| 129 | |
| 130 | def entry(p): |
| 131 | return {"package": p["Package"], "version": p["Version"], |
| 132 | "url": p["_base"] + p["Filename"], "sha256": p["SHA256"], "size": int(p["Size"])} |
| 133 | |
| 134 | |
| 135 | def renderer_entry(p, base): |
| 136 | """A renderer.json entry: base is true for a package ci-chromium.json |
| 137 | leaves to the image.""" |
| 138 | return {"package": p["Package"], "version": p["Version"], "architecture": p["Architecture"], |
| 139 | "url": p["_base"] + p["Filename"], "sha256": p["SHA256"], "size": int(p["Size"]), "base": base} |
| 140 | |
| 141 | |
| 142 | def pins(packages, version, arch): |
| 143 | """(the browser packages a Debian image lacks, those plus the base they |
| 144 | need): one resolution, pruned at the required base and not.""" |
| 145 | for name in TOP: |
| 146 | if packages[name]["Version"] != version: |
| 147 | sys.exit("%s %s: the snapshots have %s, not %s" % (name, arch, packages[name]["Version"], version)) |
| 148 | closure = resolver(packages) |
| 149 | required = [n for n, p in packages.items() if p.get("Priority") == "required" or p.get("Essential") == "yes"] |
| 150 | base = closure(required) |
| 151 | browser = closure(TOP, skip=SKIP, prune=base) - SKIP |
| 152 | full = closure(TOP, skip=SKIP) - SKIP |
| 153 | if not browser <= full: |
| 154 | sys.exit("%s: the pruned closure has %s beyond the full one" % (arch, sorted(browser - full))) |
| 155 | return browser, full |
| 156 | |
| 157 | |
| 158 | def main(argv): |
| 159 | if len(argv) != 3: |
| 160 | print(__doc__, file=sys.stderr) |
| 161 | return 2 |
| 162 | version, debian, security = argv |
| 163 | head = { |
| 164 | "version": version, |
| 165 | "snapshots": {"debian": debian, "debian-security": security}, |
| 166 | "program": "usr/lib/chromium/chromium-headless-shell", |
| 167 | } |
| 168 | pinned, renderer = dict(head), dict(head) |
| 169 | for arch in ARCHES: |
| 170 | packages = archive(debian, security, arch) |
| 171 | browser, full = pins(packages, version, arch) |
| 172 | pinned[arch] = [entry(packages[n]) for n in sorted(browser)] |
| 173 | print("%s: %d packages, %.0f MB" % (arch, len(pinned[arch]), sum(p["size"] for p in pinned[arch]) / 1e6)) |
| 174 | if arch == RENDERER_ARCH: |
| 175 | renderer[arch] = [renderer_entry(packages[n], n not in browser) for n in sorted(full)] |
| 176 | print("renderer %s: %d packages, %d of them base, %.0f MB" % ( |
| 177 | arch, len(full), len(full - browser), sum(p["size"] for p in renderer[arch]) / 1e6)) |
| 178 | for path, data in ((OUT, pinned), (RENDERER_OUT, renderer)): |
| 179 | with open(path, "w") as f: |
| 180 | json.dump(data, f, indent=2) |
| 181 | f.write("\n") |
| 182 | print("wrote " + os.path.normpath(path)) |
| 183 | return 0 |
| 184 | |
| 185 | |
| 186 | if __name__ == "__main__": |
| 187 | sys.exit(main(sys.argv[1:])) |
| 188 | |