|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Compare that multiple installs of Python don't have conflicting files |
| 3 | +# |
| 4 | +# This is a requirement for Debian's Multi-Arch installs of Python |
| 5 | +# https://www.debian.org/doc/debian-policy/ch-controlfields.html#multi-arch |
| 6 | +# |
| 7 | +# Excluded from this should be: |
| 8 | +# * /usr/bin/*: only libraries are multi-arch co-installed, one arch's binaries |
| 9 | +# are installed at a time |
| 10 | +# * pyconfig.h: Varies according to config, installed into a tag-specific |
| 11 | +# directory. |
| 12 | +# * Non-tag suffixed .pc files: Only the suffixed versions are co-installable |
| 13 | +# * .dist-info/RECORD: Contains hashes, not co-installable. |
| 14 | +# * .dist-info/WHEEL: Contains arch and version tags. Can be merged in some |
| 15 | +# cases, but not typically co-installable. |
| 16 | + |
| 17 | +from argparse import ArgumentParser |
| 18 | +from hashlib import file_digest |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | + |
| 22 | +def hash_tree(base: Path, algorithm: str = "sha512") -> dict[str, str]: |
| 23 | + print(f"Hashing {base}") |
| 24 | + seen: dict[str, str] = {} |
| 25 | + for dirpath, dirnames, filenames in base.walk(): |
| 26 | + if dirpath.name == "__pycache__": |
| 27 | + # Includes a timestamp, we expect a mismatch |
| 28 | + continue |
| 29 | + for file in filenames: |
| 30 | + filepath = dirpath / file |
| 31 | + with filepath.open("rb") as f: |
| 32 | + digest = file_digest(f, algorithm) |
| 33 | + seen[str(filepath.relative_to(base))] = digest.hexdigest() |
| 34 | + return seen |
| 35 | + |
| 36 | + |
| 37 | +def compare_trees(base: Path) -> bool: |
| 38 | + seen: dict[str, str] = {} |
| 39 | + success: bool = True |
| 40 | + for tree in base.iterdir(): |
| 41 | + if not tree.is_dir(): |
| 42 | + continue |
| 43 | + hashes = hash_tree(tree) |
| 44 | + for path, digest in hashes.items(): |
| 45 | + if path not in seen: |
| 46 | + seen[path] = digest |
| 47 | + continue |
| 48 | + if digest != seen[path]: |
| 49 | + print(f"Mismatch found in {tree}: {path}") |
| 50 | + print(f"{digest} != {seen[path]}") |
| 51 | + success = False |
| 52 | + return success |
| 53 | + |
| 54 | + |
| 55 | +def main() -> None: |
| 56 | + p = ArgumentParser("Compare multiple installs of Python") |
| 57 | + p.add_argument( |
| 58 | + "base_directory", |
| 59 | + type=Path, |
| 60 | + help=( |
| 61 | + "Directory below which multiple Pythons are installed, " |
| 62 | + "each inside their own directory." |
| 63 | + ), |
| 64 | + ) |
| 65 | + args = p.parse_args() |
| 66 | + if not compare_trees(args.base_directory): |
| 67 | + raise SystemExit(1) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments