#!/usr/bin/python3 """Refresh the XS-Vendored-Sources-Rust field of debian/control. Ubuntu generates that field with dh-cargo-vendored-sources, which is not part of Debian's dh-cargo, so read the vendored crates directly instead: every directory of debian/rust-vendor/ is one crate, and its Cargo.toml carries the authoritative name and version in its [package] section. Run through "debian/rules vendor"; it is a maintainer-side script, not part of the package build. """ import pathlib import sys import tomllib VENDOR_DIR = pathlib.Path("debian/rust-vendor") CONTROL = pathlib.Path("debian/control") FIELD = "XS-Vendored-Sources-Rust" def crates(): for manifest in sorted(VENDOR_DIR.glob("*/Cargo.toml")): with manifest.open("rb") as fh: package = tomllib.load(fh).get("package") if not package: print(f"warning: {manifest} has no [package] section", file=sys.stderr) continue yield f"{package['name']}@{package['version']}" def main(): if not VENDOR_DIR.is_dir(): sys.exit(f"{VENDOR_DIR} does not exist, run 'debian/rules vendor' first") value = f"{FIELD}: " + ", ".join(sorted(set(crates()), key=str.casefold)) lines = CONTROL.read_text(encoding="utf-8").splitlines(keepends=True) for i, line in enumerate(lines): if line.startswith(f"{FIELD}:"): lines[i] = value + "\n" break else: # Keep the field where Ubuntu has it: just above Maintainer. i = next(i for i, l in enumerate(lines) if l.startswith("Maintainer:")) lines.insert(i, value + "\n") CONTROL.write_text("".join(lines), encoding="utf-8") if __name__ == "__main__": main()