oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1#!/usr/bin/env python3
2"""SOURCES: what the published page renderer distributes, and where each
3part's source is.
4
5 scripts/renderer-sources.py check PIN SOURCES
6
7The renderer archive (scripts/build-renderer-tree.py) holds Debian
8packages, Chromium's headless shell among them. Publishing it makes us
9their distributor, so its forge release carries SOURCES beside it. SOURCES
10is made by build-renderer-tree.py in the same run as the archive, from the
11same pinned list (internal/release/renderer.json) and the same checked
12debs, so it cannot drift: sources(pin, debs) here. It is plain text:
13
14 The page renderer: Chromium headless shell <version>, from Debian.
15 Chromium source: https://snapshot.debian.org/package/chromium/<version>/
16
17 <package> <version> <architecture> <deb URL> <source package URL>
18 ...
19
20one line per pinned amd64 package, in the pin's order. A package's source
21name and version are its deb's control file's Source field (the package's
22own name and version when it has none).
23
24`check` refuses SOURCES unless it is exactly what the pin's packages give
25for every field the pin holds: the header's version and, per line and in
26order, package, version, architecture and URL, with a source URL on
27snapshot.debian.org. publish-renderer.sh runs it before an upload.
28"""
29import io
30import json
31import re
32import sys
33import tarfile
34from urllib.parse import quote
35
36SNAPSHOT_PACKAGE = "https://snapshot.debian.org/package/"
37
38
39class Refused(Exception):
40 pass
41
42
43def ar_members(name, deb):
44 """The members of a deb (an ar archive), by name."""
45 if deb[:8] != b"!<arch>\n":
46 raise Refused("%s: not a deb" % name)
47 i, members = 8, {}
48 while i < len(deb):
49 header = deb[i:i + 60]
50 if len(header) != 60 or header[58:60] != b"`\n":
51 raise Refused("%s: a broken ar header" % name)
52 member = header[:16].decode("ascii", "replace").strip().rstrip("/")
53 size = int(header[48:58])
54 members.setdefault(member, deb[i + 60:i + 60 + size])
55 i += 60 + size + (size & 1)
56 return members
57
58
59def control_of(name, deb):
60 """The deb's control fields (Package, Version, Source, ...)."""
61 members = ar_members(name, deb)
62 modes = {"control.tar": "r:", "control.tar.gz": "r:gz", "control.tar.xz": "r:xz"}
63 found = [m for m in members if m in modes]
64 if len(found) != 1:
65 raise Refused("%s: want one control.tar, .tar.gz or .tar.xz member" % name)
66 with tarfile.open(fileobj=io.BytesIO(members[found[0]]), mode=modes[found[0]]) as tar:
67 for m in tar:
68 if m.name in ("./control", "control") and m.isfile():
69 text = tar.extractfile(m).read().decode("utf-8")
70 break
71 else:
72 raise Refused("%s: no control file" % name)
73 fields, last = {}, None
74 for line in text.splitlines():
75 if line[:1] in (" ", "\t") and last:
76 continue
77 key, sep, value = line.partition(":")
78 if sep:
79 last = key.strip()
80 fields[last] = value.strip()
81 return fields
82
83
84def source_of(p, control):
85 """(source name, source version) of a pinned package, from its control."""
86 if control.get("Package") != p["package"] or control.get("Version") != p["version"]:
87 raise Refused("%s: its control says %s %s" % (p["package"], control.get("Package"), control.get("Version")))
88 m = re.fullmatch(r"([a-z0-9][a-z0-9+.-]*)(?: \(([^()\s]+)\))?", control.get("Source", p["package"]))
89 if not m:
90 raise Refused("%s: a Source field %r" % (p["package"], control.get("Source")))
91 return m.group(1), m.group(2) or p["version"]
92
93
94def source_url(name, version):
95 return SNAPSHOT_PACKAGE + quote(name, safe="") + "/" + quote(version, safe="~:+") + "/"
96
97
98def header(pin, chromium_version):
99 return ("The page renderer: Chromium headless shell %s, from Debian.\n"
100 "Chromium source: %s\n\n" % (pin["version"], source_url("chromium", chromium_version)))
101
102
103def sources(pin, debs):
104 """SOURCES for pin's amd64 packages, debs their checked bytes in order."""
105 packages = pin["amd64"]
106 if len(debs) != len(packages):
107 raise Refused("want one deb per pinned package")
108 lines, chromium = [], None
109 for p, deb in zip(packages, debs):
110 name, version = source_of(p, control_of(p["package"], deb))
111 if p["package"] == "chromium-headless-shell":
112 if name != "chromium":
113 raise Refused("chromium-headless-shell is built from %s, not chromium" % name)
114 chromium = version
115 lines.append(" ".join([p["package"], p["version"], p["architecture"], p["url"], source_url(name, version)]))
116 if chromium is None:
117 raise Refused("the pin has no chromium-headless-shell")
118 return header(pin, chromium) + "\n".join(lines) + "\n"
119
120
121def check(pin, text):
122 """Refuse SOURCES unless it is what pin's packages give."""
123 packages = pin["amd64"]
124 head, sep, body = text.partition("\n\n")
125 if not sep or not head.startswith("The page renderer: Chromium headless shell %s, from Debian.\nChromium source: %schromium/" % (pin["version"], SNAPSHOT_PACKAGE)):
126 raise Refused("SOURCES' header is not the pin's")
127 rows = body.splitlines()
128 if len(rows) != len(packages) or not text.endswith("\n"):
129 raise Refused("SOURCES lists %d packages, the pin %d" % (len(rows), len(packages)))
130 for p, row in zip(packages, rows):
131 f = row.split(" ")
132 if len(f) != 5 or f[:4] != [p["package"], p["version"], p["architecture"], p["url"]] or not f[4].startswith(SNAPSHOT_PACKAGE):
133 raise Refused("SOURCES' line for %s is not the pin's: %r" % (p["package"], row))
134
135
136def main(argv):
137 if len(argv) != 3 or argv[0] != "check":
138 print(__doc__, file=sys.stderr)
139 return 2
140 with open(argv[1]) as f:
141 pin = json.load(f)
142 with open(argv[2], encoding="utf-8") as f:
143 text = f.read()
144 try:
145 check(pin, text)
146 except Refused as r:
147 print("renderer-sources: refused: %s" % r, file=sys.stderr)
148 return 1
149 return 0
150
151
152if __name__ == "__main__":
153 sys.exit(main(sys.argv[1:]))
154