Debian Codegraph @

debian/update-vendored-sources-field.py

raw
1 #!/usr/bin/python3
2 """Refresh the XS-Vendored-Sources-Rust field of debian/control.
3
4 Ubuntu generates that field with dh-cargo-vendored-sources, which is not part
5 of Debian's dh-cargo, so read the vendored crates directly instead: every
6 directory of debian/rust-vendor/ is one crate, and its Cargo.toml carries the
7 authoritative name and version in its [package] section.
8
9 Run through "debian/rules vendor"; it is a maintainer-side script, not part of
10 the package build.
11 """
12
13 import pathlib
14 import sys
15 import tomllib
16
17 VENDOR_DIR = pathlib.Path("debian/rust-vendor")
18 CONTROL = pathlib.Path("debian/control")
19 FIELD = "XS-Vendored-Sources-Rust"
20
21
22 def crates():
23 for manifest in sorted(VENDOR_DIR.glob("*/Cargo.toml")):
24 with manifest.open("rb") as fh:
25 package = tomllib.load(fh).get("package")
26 if not package:
27 print(f"warning: {manifest} has no [package] section", file=sys.stderr)
28 continue
29 yield f"{package['name']}@{package['version']}"
30
31
32 def main():
33 if not VENDOR_DIR.is_dir():
34 sys.exit(f"{VENDOR_DIR} does not exist, run 'debian/rules vendor' first")
35
36 value = f"{FIELD}: " + ", ".join(sorted(set(crates()), key=str.casefold))
37
38 lines = CONTROL.read_text(encoding="utf-8").splitlines(keepends=True)
39 for i, line in enumerate(lines):
40 if line.startswith(f"{FIELD}:"):
41 lines[i] = value + "\n"
42 break
43 else:
44 # Keep the field where Ubuntu has it: just above Maintainer.
45 i = next(i for i, l in enumerate(lines) if l.startswith("Maintainer:"))
46 lines.insert(i, value + "\n")
47 CONTROL.write_text("".join(lines), encoding="utf-8")
48
49
50 if __name__ == "__main__":
51 main()