Files
carbon-lang/scripts/check_sha_filenames.py
T
Chandler Carruth b79ce84f33 Switch to uv for all of our Python scripts (#7242)
This removes the need to install any specific version of Python or
figure out how to configure it by instead asking users to install `uv`
and letting it manage Python. Among other advantages, `uv` is designed
to be fast enough to embed directly into our scripts.

We were already using this in `bench_runner.py` so that the script could
import non standard library dependencies. Moving to it for the rest of
our Python unifies the approach and will also enable dependencies
whenever needed.

I've left `github_tools` alone as it has special handling with its own
Bazel setup.

I've updated the contributing tools to explain the approach here.
2026-05-28 18:03:52 +00:00

49 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# ///
"""Requires files be named for their SHA1.
We name fuzzer corpus files for their SHA1. The choice of SHA1 is for
consistency with git.
This maintains the current extension for .textproto, but at some point we might
want to specify the extension by path.
"""
__copyright__ = """
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
Exceptions. See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""
import hashlib
from pathlib import Path
import sys
def main() -> None:
has_errors = False
for arg in sys.argv[1:]:
path = Path(arg)
with path.open("rb") as f:
content = f.read()
if len(content) == 0:
want = "empty"
else:
want = hashlib.sha1(content).hexdigest()
want_path = path.parent.joinpath(want).with_suffix(path.suffix)
if path != want_path:
print(f"Renaming {path} to {want_path}", file=sys.stderr)
path.rename(want_path)
has_errors = True
if has_errors:
exit(1)
if __name__ == "__main__":
main()