Skip to content

Commit a8fbdd1

Browse files
committed
fix: debug
1 parent 9eac7c2 commit a8fbdd1

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

scripts/local/checksum.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Command-line script to calculate and print file checksums.
4+
5+
Usage:
6+
python scripts/local/checksum.py <file_path> [--algorithm <algorithm>]
7+
8+
Examples:
9+
python scripts/local/checksum.py README.md
10+
python scripts/local/checksum.py README.md --algorithm md5
11+
python scripts/local/checksum.py README.md --algorithm sha1
12+
python scripts/local/checksum.py README.md --algorithm sha512
13+
"""
14+
15+
import argparse
16+
import sys
17+
from pathlib import Path
18+
19+
# Add the project root to the Python path
20+
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
21+
22+
from src.utils.file import calculate_file_checksum, print_file_checksum
23+
24+
25+
def main():
26+
parser = argparse.ArgumentParser(
27+
description="Calculate and print file checksums",
28+
formatter_class=argparse.RawDescriptionHelpFormatter,
29+
epilog="""
30+
Examples:
31+
%(prog)s README.md
32+
%(prog)s README.md --algorithm md5
33+
%(prog)s README.md --algorithm sha1
34+
%(prog)s README.md --algorithm sha512
35+
36+
Supported algorithms: md5, sha1, sha256 (default), sha512
37+
"""
38+
)
39+
40+
parser.add_argument(
41+
"file_path",
42+
help="Path to the file to checksum"
43+
)
44+
45+
parser.add_argument(
46+
"--algorithm", "-a",
47+
default="sha256",
48+
choices=["md5", "sha1", "sha256", "sha512"],
49+
help="Hash algorithm to use (default: sha256)"
50+
)
51+
52+
parser.add_argument(
53+
"--quiet", "-q",
54+
action="store_true",
55+
help="Only output the checksum value (no filename or algorithm label)"
56+
)
57+
58+
args = parser.parse_args()
59+
60+
try:
61+
if args.quiet:
62+
checksum = calculate_file_checksum(args.file_path, args.algorithm)
63+
print(checksum)
64+
else:
65+
print_file_checksum(args.file_path, args.algorithm)
66+
except (FileNotFoundError, ValueError) as e:
67+
print(f"Error: {e}", file=sys.stderr)
68+
sys.exit(1)
69+
except KeyboardInterrupt:
70+
print("\nOperation cancelled by user.", file=sys.stderr)
71+
sys.exit(1)
72+
73+
74+
if __name__ == "__main__":
75+
main()

src/entry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from src.schemas.constants import DEFAULT_ANSWERS_SUMMARY_FORMAT_STRING
1414
from src.schemas.defaults import CONFIG_DEFAULTS
1515
from src.utils import constants
16-
from src.utils.file import PathUtils
16+
from src.utils.file import PathUtils, print_file_checksum
1717
from src.utils.image import ImageUtils
1818
from src.utils.interaction import InteractionUtils, Stats
1919
from src.utils.logger import console, logger
@@ -243,7 +243,7 @@ def process_directory_files(
243243
files_counter += 1
244244
file_name = PathUtils.remove_non_utf_characters(file_path.name)
245245
file_id = str(file_name)
246-
246+
print_file_checksum(file_path, "md5")
247247
gray_image, colored_image = ImageUtils.read_image_util(file_path, tuning_config)
248248

249249
logger.info("")

src/utils/file.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hashlib
12
import json
23
import os
34
import string
@@ -23,6 +24,57 @@ def load_json(path, **rest) -> dict[str, Any]:
2324
return loaded
2425

2526

27+
def calculate_file_checksum(file_path: Path | str, algorithm: str = "sha256") -> str:
28+
"""
29+
Calculate the checksum of a file using the specified hashing algorithm.
30+
31+
Args:
32+
file_path: Path to the file
33+
algorithm: Hash algorithm to use (md5, sha1, sha256, sha512)
34+
35+
Returns:
36+
Hexadecimal string representation of the file's checksum
37+
38+
Raises:
39+
FileNotFoundError: If the file doesn't exist
40+
ValueError: If the algorithm is not supported
41+
"""
42+
file_path = Path(file_path)
43+
44+
if not file_path.exists():
45+
msg = f"File not found: {file_path}"
46+
raise FileNotFoundError(msg)
47+
48+
# Validate algorithm
49+
try:
50+
hasher = hashlib.new(algorithm)
51+
except ValueError as e:
52+
msg = f"Unsupported hash algorithm: {algorithm}"
53+
raise ValueError(msg) from e
54+
55+
# Read file in chunks to handle large files efficiently
56+
with file_path.open("rb") as f:
57+
while chunk := f.read(8192):
58+
hasher.update(chunk)
59+
60+
return hasher.hexdigest()
61+
62+
63+
def print_file_checksum(file_path: Path | str, algorithm: str = "md5") -> None:
64+
"""
65+
Calculate and print the checksum of a file.
66+
67+
Args:
68+
file_path: Path to the file
69+
algorithm: Hash algorithm to use (md5, sha1, sha256, sha512)
70+
"""
71+
try:
72+
checksum = calculate_file_checksum(file_path, algorithm)
73+
print(f"{algorithm.upper()} ({file_path}): {checksum}")
74+
except (FileNotFoundError, ValueError) as e:
75+
print(f"Error: {e}")
76+
77+
2678
class PathUtils:
2779
printable_chars: ClassVar = set(string.printable)
2880

0 commit comments

Comments
 (0)