toolchain_tar_test.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. """Check that a release tar contains the same files as a prefix root."""
  3. __copyright__ = """
  4. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  5. Exceptions. See /LICENSE for license information.
  6. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. """
  8. import argparse
  9. import sys
  10. from pathlib import Path
  11. import tarfile
  12. def main() -> None:
  13. parser = argparse.ArgumentParser(description=__doc__)
  14. parser.add_argument(
  15. "tar_file",
  16. type=Path,
  17. help="The tar file to test.",
  18. )
  19. parser.add_argument(
  20. "install_marker",
  21. type=Path,
  22. help="The path of the install marker in a prefix root to test against.",
  23. )
  24. args = parser.parse_args()
  25. # Locate the prefix root from the install marker.
  26. if not args.install_marker.exists():
  27. sys.exit("ERROR: No install marker: " + args.install_marker)
  28. prefix_root_path = args.install_marker.parent.parent.parent
  29. # First check that every file and directory in the tar file exists in our
  30. # prefix root, and build a set of those paths.
  31. installed_paths = set()
  32. with tarfile.open(args.tar_file) as tar:
  33. for tarinfo in tar:
  34. relative_path = Path(*Path(tarinfo.name).parts[1:])
  35. installed_paths.add(relative_path)
  36. if not prefix_root_path.joinpath(relative_path).exists():
  37. sys.exit(
  38. "ERROR: File `{0}` is not in prefix root: `{1}`".format(
  39. tarinfo.name, prefix_root_path
  40. )
  41. )
  42. # If we found an empty tar file, it's always an error.
  43. if len(installed_paths) == 0:
  44. sys.exit("ERROR: Tar file `{0}` was empty.".format(args.tar_file))
  45. # Now check that every file and directory in the prefix root is in that set.
  46. for prefix_path in prefix_root_path.glob("**/*"):
  47. relative_path = prefix_path.relative_to(prefix_root_path)
  48. if relative_path not in installed_paths:
  49. sys.exit(
  50. "ERROR: File `{0}` is not in tar file.".format(relative_path)
  51. )
  52. if __name__ == "__main__":
  53. main()