-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Andrew Svetlov <[email protected]>
- Loading branch information
1 parent
43f3fda
commit b66205a
Showing
5 changed files
with
102 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
.envrc | ||
.flake | ||
.gitconfig | ||
.hash | ||
.idea | ||
.install-cython | ||
.install-deps | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python | ||
|
||
import argparse | ||
import hashlib | ||
import pathlib | ||
import sys | ||
|
||
PARSER = argparse.ArgumentParser( | ||
description="Helper for check file hashes in Makefile instead of bare timestamps" | ||
) | ||
PARSER.add_argument("dst", metavar="DST", type=pathlib.Path) | ||
PARSER.add_argument("-d", "--debug", action="store_true", default=False) | ||
|
||
|
||
def main(argv): | ||
args = PARSER.parse_args(argv) | ||
dst = args.dst | ||
assert dst.suffix == ".hash" | ||
dirname = dst.parent | ||
if dirname.name != ".hash": | ||
if args.debug: | ||
print(f"Invalid name {dst} -> dirname {dirname}", file=sys.stderr) | ||
return 0 | ||
dirname.mkdir(exist_ok=True) | ||
src_dir = dirname.parent | ||
src_name = dst.stem # drop .hash | ||
full_src = src_dir / src_name | ||
hasher = hashlib.sha256() | ||
try: | ||
hasher.update(full_src.read_bytes()) | ||
except OSError: | ||
if args.debug: | ||
print(f"Cannot open {full_src}", file=sys.stderr) | ||
return 0 | ||
src_hash = hasher.hexdigest() | ||
if dst.exists(): | ||
dst_hash = dst.read_text() | ||
else: | ||
dst_hash = "" | ||
if src_hash != dst_hash: | ||
dst.write_text(src_hash) | ||
print(f"re-hash {src_hash}") | ||
else: | ||
if args.debug: | ||
print(f"Skip {src_hash} checksum, up-to-date") | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main(sys.argv[1:])) |