-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
41 changed files
with
707 additions
and
271 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
This file was deleted.
Oops, something went wrong.
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,115 @@ | ||
#!/usr/bin/env python3 | ||
from __future__ import annotations | ||
|
||
import os | ||
import glob | ||
import sys | ||
import json | ||
import hashlib | ||
|
||
|
||
IGNORED_PREFIXES = [] | ||
|
||
|
||
def find_fingerprints_and_check_dirs(base_dir): | ||
all_fingerprints = set(glob.glob(os.path.join(base_dir, '**', 'test*', '**', '*.fingerprint'), recursive=True)) | ||
|
||
all_fingerprints = {os.path.relpath(fp) for fp in all_fingerprints | ||
if not any(fp.startswith(prefix) for prefix in IGNORED_PREFIXES)} | ||
|
||
if not all_fingerprints: | ||
show("No .fingerprint files or cache directories found.") | ||
exit(1) | ||
|
||
missing_content = [] | ||
valid_paths = set() | ||
fingerprint_contents = [] | ||
|
||
for fingerprint in all_fingerprints: | ||
path = fingerprint.replace('.fingerprint', '') | ||
|
||
if not os.path.exists(path): | ||
missing_content.append(path) | ||
continue | ||
|
||
if not os.path.isdir(path): | ||
valid_paths.add(path) | ||
continue | ||
|
||
if os.listdir(path): | ||
valid_paths.add(path) | ||
else: | ||
missing_content.append(path) | ||
|
||
with open(fingerprint, 'r') as f: | ||
content = f.read().strip() | ||
fingerprint_contents.append((fingerprint, content)) | ||
|
||
return sorted(valid_paths), missing_content, fingerprint_contents | ||
|
||
|
||
def calculate_sha256(fingerprint_contents): | ||
sorted_fingerprint_contents = sorted(fingerprint_contents, key=lambda x: x[0]) | ||
|
||
concatenated_contents = ''.join(content for _, content in sorted_fingerprint_contents) | ||
|
||
sha256_hash = hashlib.sha256(concatenated_contents.encode()).hexdigest() | ||
|
||
return sha256_hash | ||
|
||
|
||
def calculate_file_sha256(file_path): | ||
"""Calculate the sha256 digest of a file.""" | ||
sha256_hash = hashlib.sha256() | ||
with open(file_path, 'rb') as f: | ||
for byte_block in iter(lambda: f.read(4096), b""): | ||
sha256_hash.update(byte_block) | ||
return sha256_hash.hexdigest() | ||
|
||
|
||
def show(*s: str): | ||
print(*s, file=sys.stderr) | ||
|
||
|
||
def main(file_path: str | None): | ||
base_dir = '.' | ||
valid_paths, missing_content, fingerprint_contents = find_fingerprints_and_check_dirs(base_dir) | ||
|
||
if missing_content: | ||
show("The following paths are missing or have no content, but have corresponding .fingerprint files:") | ||
for path in sorted(missing_content): | ||
show(f"- {path}") | ||
show("Please ensure these paths exist and have content if they are directories.") | ||
exit(1) | ||
|
||
sha256_hash = calculate_sha256(fingerprint_contents) | ||
|
||
paths_with_digests = [] | ||
for path in sorted(valid_paths): | ||
fingerprint_file = f"{path}.fingerprint" | ||
if os.path.exists(fingerprint_file): | ||
file_digest = calculate_file_sha256(fingerprint_file) | ||
paths_with_digests.append({ | ||
"path": path, | ||
"digest": file_digest | ||
}) | ||
|
||
output = { | ||
"digest": sha256_hash, | ||
"paths": paths_with_digests | ||
} | ||
|
||
content = json.dumps(output, indent=2) | ||
|
||
if file_path: | ||
with open(file_path, 'w') as f: | ||
f.write(content) | ||
|
||
print(content) | ||
|
||
|
||
if __name__ == "__main__": | ||
file_path = None | ||
if len(sys.argv) > 1: | ||
file_path = sys.argv[1] | ||
main(file_path) |
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
Oops, something went wrong.