check_sha_filenames.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python3
  2. """Requires files be named for their SHA1.
  3. We name fuzzer corpus files for their SHA1. The choice of SHA1 is for
  4. consistency with git.
  5. This maintains the current extension for .textproto, but at some point we might
  6. want to specify the extension by path.
  7. """
  8. __copyright__ = """
  9. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  10. Exceptions. See /LICENSE for license information.
  11. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  12. """
  13. import hashlib
  14. from pathlib import Path
  15. import sys
  16. def main() -> None:
  17. has_errors = False
  18. for arg in sys.argv[1:]:
  19. path = Path(arg)
  20. with path.open("rb") as f:
  21. content = f.read()
  22. if len(content) == 0:
  23. want = "empty"
  24. else:
  25. want = hashlib.sha1(content).hexdigest()
  26. want_path = path.parent.joinpath(want).with_suffix(path.suffix)
  27. if path != want_path:
  28. print(f"Renaming {path} to {want_path}", file=sys.stderr)
  29. path.rename(want_path)
  30. has_errors = True
  31. if has_errors:
  32. exit(1)
  33. if __name__ == "__main__":
  34. main()