forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_sha_filenames.py
executable file
·43 lines (34 loc) · 1.12 KB
/
check_sha_filenames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
"""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()