Skip to content

Commit 253c929

Browse files
committed
fix(python): Use python 3.10 hashlib equivalent
Function file_digest in haslib was introduced in Python 3.11, breaking the minimal target of Python 3.10. Replace the current code with a more backward compatible code. Signed-off-by: Helio Chissini de Castro <[email protected]>
1 parent 6395b09 commit 253c929

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/orthw/utils/checksum.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class FolderType(Enum):
3131

3232
def check_evaluation_md5_sum() -> bool:
3333
"""Checks whether the evaluator inputs changed."""
34-
evaluation_md5_sum_file: Path = config.path("evaluation_md5_sum_file")
35-
package_configuration_md5_sum_file: Path = config.path("package_configuration_md5_sum_file")
36-
package_curations_md5_sum_file: Path = config.path("package_curations_md5_sum_file")
34+
evaluation_md5_sum_file: Path = config.evaluation_md5_sum_file
35+
package_configuration_md5_sum_file: Path = config.package_configuration_md5_sum_file
36+
package_curations_md5_sum_file: Path = config.package_curations_md5_sum_file
3737

3838
if evaluation_md5_sum_file.exists():
3939
md5_res = get_folder_md5(FolderType.CONFIGURATION)
@@ -79,24 +79,23 @@ def get_folder_md5(folder_type: str | FolderType) -> str | None:
7979

8080
if isinstance(folder_type, FolderType):
8181
if folder_type == FolderType.CONFIGURATION:
82-
folder = config.path("ort_config_package_configuration_dir")
82+
folder = config.ort_config_package_configurations_dir
8383
elif folder_type == FolderType.CURATIONS:
84-
folder = config.path("ort_config_package_curations_dir")
84+
folder = config.ort_config_package_curations_dir
8585
elif isinstance(folder_type, str):
8686
folder = Path(folder_type)
8787

8888
if not folder or not folder.exists():
8989
logging.error("No valid entry for folder_type.")
9090
return None
9191

92-
if folder:
93-
sorted_file_list: list[Path] = sorted(folder.glob("**/*.yml"))
92+
sorted_file_list: list[Path] = sorted(folder.glob("**/*.yml"))
9493

95-
hashed_file_list = hashlib.md5() # noqa: S324
96-
for file in sorted_file_list:
97-
with Path.open(file, mode="rb") as f:
98-
digest = hashlib.file_digest(f, "md5")
99-
logging.debug(f"{digest.hexdigest()} {file.as_posix()}".encode())
100-
hashed_file_list.update(f"{digest.hexdigest()} {file.as_posix()}".encode())
94+
hashed_file_list = hashlib.md5() # noqa: S324
95+
for file in sorted_file_list:
96+
with file.open(mode="rb") as f:
97+
for chunk in iter(lambda: f.read(4096), b""):
98+
hashed_file_list.update(chunk)
99+
logging.debug(f"{hashed_file_list.hexdigest()} {file.as_posix()}")
101100

102101
return hashed_file_list.hexdigest()

0 commit comments

Comments
 (0)