#!/usr/bin/env python3 """SOURCES: what the published page renderer distributes, and where each part's source is. scripts/renderer-sources.py check PIN SOURCES The renderer archive (scripts/build-renderer-tree.py) holds Debian packages, Chromium's headless shell among them. Publishing it makes us their distributor, so its forge release carries SOURCES beside it. SOURCES is made by build-renderer-tree.py in the same run as the archive, from the same pinned list (internal/release/renderer.json) and the same checked debs, so it cannot drift: sources(pin, debs) here. It is plain text: The page renderer: Chromium headless shell , from Debian. Chromium source: https://snapshot.debian.org/package/chromium// ... one line per pinned amd64 package, in the pin's order. A package's source name and version are its deb's control file's Source field (the package's own name and version when it has none). `check` refuses SOURCES unless it is exactly what the pin's packages give for every field the pin holds: the header's version and, per line and in order, package, version, architecture and URL, with a source URL on snapshot.debian.org. publish-renderer.sh runs it before an upload. """ import io import json import re import sys import tarfile from urllib.parse import quote SNAPSHOT_PACKAGE = "https://snapshot.debian.org/package/" class Refused(Exception): pass def ar_members(name, deb): """The members of a deb (an ar archive), by name.""" if deb[:8] != b"!\n": raise Refused("%s: not a deb" % name) i, members = 8, {} while i < len(deb): header = deb[i:i + 60] if len(header) != 60 or header[58:60] != b"`\n": raise Refused("%s: a broken ar header" % name) member = header[:16].decode("ascii", "replace").strip().rstrip("/") size = int(header[48:58]) members.setdefault(member, deb[i + 60:i + 60 + size]) i += 60 + size + (size & 1) return members def control_of(name, deb): """The deb's control fields (Package, Version, Source, ...).""" members = ar_members(name, deb) modes = {"control.tar": "r:", "control.tar.gz": "r:gz", "control.tar.xz": "r:xz"} found = [m for m in members if m in modes] if len(found) != 1: raise Refused("%s: want one control.tar, .tar.gz or .tar.xz member" % name) with tarfile.open(fileobj=io.BytesIO(members[found[0]]), mode=modes[found[0]]) as tar: for m in tar: if m.name in ("./control", "control") and m.isfile(): text = tar.extractfile(m).read().decode("utf-8") break else: raise Refused("%s: no control file" % name) fields, last = {}, None for line in text.splitlines(): if line[:1] in (" ", "\t") and last: continue key, sep, value = line.partition(":") if sep: last = key.strip() fields[last] = value.strip() return fields def source_of(p, control): """(source name, source version) of a pinned package, from its control.""" if control.get("Package") != p["package"] or control.get("Version") != p["version"]: raise Refused("%s: its control says %s %s" % (p["package"], control.get("Package"), control.get("Version"))) m = re.fullmatch(r"([a-z0-9][a-z0-9+.-]*)(?: \(([^()\s]+)\))?", control.get("Source", p["package"])) if not m: raise Refused("%s: a Source field %r" % (p["package"], control.get("Source"))) return m.group(1), m.group(2) or p["version"] def source_url(name, version): return SNAPSHOT_PACKAGE + quote(name, safe="") + "/" + quote(version, safe="~:+") + "/" def header(pin, chromium_version): return ("The page renderer: Chromium headless shell %s, from Debian.\n" "Chromium source: %s\n\n" % (pin["version"], source_url("chromium", chromium_version))) def sources(pin, debs): """SOURCES for pin's amd64 packages, debs their checked bytes in order.""" packages = pin["amd64"] if len(debs) != len(packages): raise Refused("want one deb per pinned package") lines, chromium = [], None for p, deb in zip(packages, debs): name, version = source_of(p, control_of(p["package"], deb)) if p["package"] == "chromium-headless-shell": if name != "chromium": raise Refused("chromium-headless-shell is built from %s, not chromium" % name) chromium = version lines.append(" ".join([p["package"], p["version"], p["architecture"], p["url"], source_url(name, version)])) if chromium is None: raise Refused("the pin has no chromium-headless-shell") return header(pin, chromium) + "\n".join(lines) + "\n" def check(pin, text): """Refuse SOURCES unless it is what pin's packages give.""" packages = pin["amd64"] head, sep, body = text.partition("\n\n") if not sep or not head.startswith("The page renderer: Chromium headless shell %s, from Debian.\nChromium source: %schromium/" % (pin["version"], SNAPSHOT_PACKAGE)): raise Refused("SOURCES' header is not the pin's") rows = body.splitlines() if len(rows) != len(packages) or not text.endswith("\n"): raise Refused("SOURCES lists %d packages, the pin %d" % (len(rows), len(packages))) for p, row in zip(packages, rows): f = row.split(" ") if len(f) != 5 or f[:4] != [p["package"], p["version"], p["architecture"], p["url"]] or not f[4].startswith(SNAPSHOT_PACKAGE): raise Refused("SOURCES' line for %s is not the pin's: %r" % (p["package"], row)) def main(argv): if len(argv) != 3 or argv[0] != "check": print(__doc__, file=sys.stderr) return 2 with open(argv[1]) as f: pin = json.load(f) with open(argv[2], encoding="utf-8") as f: text = f.read() try: check(pin, text) except Refused as r: print("renderer-sources: refused: %s" % r, file=sys.stderr) return 1 return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))