diff --git a/.github/workflows/launchpad_ppa.yml b/.github/workflows/launchpad_ppa.yml index 0b89c908..db050df6 100644 --- a/.github/workflows/launchpad_ppa.yml +++ b/.github/workflows/launchpad_ppa.yml @@ -138,25 +138,7 @@ jobs: mkdir -p .cargo cargo vendor --locked vendor > .cargo/config.toml python3 debian/sanitize-vendor.py - python3 - <<'PY' - import json - from pathlib import Path, PurePosixPath - - offenders = [] - for checksum_file in Path("vendor").rglob(".cargo-checksum.json"): - data = json.loads(checksum_file.read_text()) - for rel_path in data.get("files", {}): - if rel_path.endswith(".orig") or any( - part.startswith(".") for part in PurePosixPath(rel_path).parts - ): - offenders.append(f"{checksum_file}: {rel_path}") - - if offenders: - raise SystemExit( - "vendor checksum still references hidden/orig files:\n" - + "\n".join(offenders[:50]) - ) - PY + python3 debian/sanitize-vendor.py --check grep -q 'replace-with = "vendored-sources"' .cargo/config.toml grep -q '\[source.vendored-sources\]' .cargo/config.toml grep -q '^directory = "vendor"$' .cargo/config.toml diff --git a/debian/sanitize-vendor.py b/debian/sanitize-vendor.py index 2b82e48a..bf264716 100644 --- a/debian/sanitize-vendor.py +++ b/debian/sanitize-vendor.py @@ -1,40 +1,102 @@ #!/usr/bin/env python3 +"""Make vendored crates survive dpkg-source. +dpkg-source leaves out of the source tarball every file that matches its +default tar-ignore patterns (object files, static and shared libraries, VCS +metadata, editor backups). Cargo verifies each vendored crate against its +.cargo-checksum.json, so a listed file that never reaches the Launchpad +builder fails the offline build ("failed to calculate checksum of ..."). +ring 0.17 ships prebuilt Windows objects in pregenerated/*.o, for example. + +Delete those files from vendor/ and from the checksum lists before the source +package is built. With --check, only report checksum entries that would still +be dropped, and exit non-zero if any remain. +""" + +import fnmatch import json -from pathlib import Path +import subprocess +import sys +from pathlib import Path, PurePosixPath + +# dpkg's built-in defaults (Dpkg::Source::Package::get_default_tar_ignore_pattern), +# used when the running dpkg cannot be asked. +FALLBACK_TAR_IGNORE = [ + "*.a", "*.la", "*.o", "*.so", ".*.sw?", "*/*~", ",,*", ".[#~]*", + ".arch-ids", ".arch-inventory", ".be", ".bzr", ".bzr.backup", ".bzr.tags", + ".bzrignore", ".cvsignore", ".deps", ".git", ".gitattributes", ".gitignore", + ".gitmodules", ".gitreview", ".hg", ".hgignore", ".hgsigs", ".hgtags", + ".mailmap", ".mtn-ignore", ".shelf", ".svn", "CVS", "DEADJOE", "RCS", + "_MTN", "_darcs", "{arch}", +] -def should_strip(rel_path: str) -> bool: - path = Path(rel_path) - return rel_path.endswith(".orig") or any(part.startswith(".") for part in path.parts) +def tar_ignore_patterns() -> list[str]: + try: + out = subprocess.run( + [ + "perl", "-MDpkg::Source::Package", "-e", + 'print join("\\n", Dpkg::Source::Package::get_default_tar_ignore_pattern())', + ], + check=True, capture_output=True, text=True, + ).stdout.split() + except (OSError, subprocess.CalledProcessError): + out = [] + return sorted(set(out) | set(FALLBACK_TAR_IGNORE)) + + +def should_strip(rel_path: str, patterns: list[str]) -> bool: + path = PurePosixPath(rel_path) + if rel_path.endswith(".orig") or any(part.startswith(".") for part in path.parts): + return True + # tar --exclude drops any file or directory whose name matches, which also + # drops everything under an excluded directory; patterns with a slash + # match a trailing run of path components. + parts = path.parts + for p in patterns: + if "/" in p: + if any(fnmatch.fnmatchcase("/".join(parts[i:]), p) for i in range(len(parts))): + return True + elif any(fnmatch.fnmatchcase(part, p) for part in parts): + return True + return False def main() -> int: + check_only = "--check" in sys.argv[1:] vendor_root = Path("vendor") if not vendor_root.is_dir(): raise SystemExit("vendor directory not found") - checksum_files = list(vendor_root.rglob(".cargo-checksum.json")) - for checksum_file in checksum_files: + patterns = tar_ignore_patterns() + offenders = [] + for checksum_file in sorted(vendor_root.rglob(".cargo-checksum.json")): crate_dir = checksum_file.parent data = json.loads(checksum_file.read_text()) files = data.get("files", {}) - removed = [] - for rel_path in list(files.keys()): - if should_strip(rel_path): - removed.append(rel_path) - files.pop(rel_path, None) - target = crate_dir / rel_path - if target.exists(): - target.unlink() + removed = [rel for rel in files if should_strip(rel, patterns)] + if check_only: + offenders.extend(f"{checksum_file}: {rel}" for rel in removed) + continue + + for rel_path in removed: + files.pop(rel_path, None) + target = crate_dir / rel_path + if target.exists(): + target.unlink() if removed: checksum_file.write_text( json.dumps(data, separators=(",", ":"), sort_keys=True) + "\n" ) - print(f"sanitized {checksum_file} ({len(removed)} hidden/orig entries)") + print(f"sanitized {checksum_file} ({len(removed)} entries dpkg-source would drop)") + if offenders: + raise SystemExit( + "vendor checksums still reference files dpkg-source drops:\n" + + "\n".join(offenders[:50]) + ) return 0