From b9c91a897d92ba826f284b30d4b306deada41e61 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:35:05 +0100 Subject: [PATCH] refactor: release tools hash through core.hashing Five streamed hashlib loops in check, assets, sync and restore read the digest through compute_hashes with cache=False: the same fresh read of the bytes, written once. --- scripts/release/assets.py | 8 ++------ scripts/release/check.py | 8 ++------ scripts/release/restore.py | 14 +++----------- scripts/release/sync.py | 8 ++------ 4 files changed, 9 insertions(+), 29 deletions(-) diff --git a/scripts/release/assets.py b/scripts/release/assets.py index 943bceb50..ead6d1b85 100644 --- a/scripts/release/assets.py +++ b/scripts/release/assets.py @@ -2,7 +2,6 @@ from __future__ import annotations -import hashlib import json import os import re @@ -10,6 +9,7 @@ import subprocess from collections.abc import Iterable from typing import NamedTuple +from core.hashing import compute_hashes from model.composition import composition_tier from model.largefiles import LARGE_FILES_RELEASE, LARGE_FILES_REPO, asset_names, registered_paths @@ -268,11 +268,7 @@ def cache_hashes(names: set[str], cache_dir: str = ".cache/large") -> dict[str, for name in names: path = os.path.join(cache_dir, name) if os.path.isfile(path): - digest = hashlib.sha1() - with open(path, "rb") as handle: - for chunk in iter(lambda: handle.read(1 << 20), b""): - digest.update(chunk) - hashes[name] = digest.hexdigest() + hashes[name] = compute_hashes(path, frozenset({"sha1"}), cache=False)["sha1"] return hashes diff --git a/scripts/release/check.py b/scripts/release/check.py index aa0cc6723..67b559dc5 100644 --- a/scripts/release/check.py +++ b/scripts/release/check.py @@ -2,12 +2,12 @@ from __future__ import annotations -import hashlib import json import subprocess from dataclasses import dataclass from pathlib import Path +from core.hashing import compute_hashes from release.assets import digests_of from model.largefiles import LARGE_FILES_REPO @@ -114,11 +114,7 @@ def compare_release( def _sha256(path: Path) -> str: - digest = hashlib.sha256() - with open(path, "rb") as handle: - for chunk in iter(lambda: handle.read(1 << 20), b""): - digest.update(chunk) - return digest.hexdigest() + return compute_hashes(path, frozenset({"sha256"}), cache=False)["sha256"] def fetch_assets(tag: str) -> tuple[dict[str, Asset], bool]: diff --git a/scripts/release/restore.py b/scripts/release/restore.py index a87fea85c..75e6d9053 100644 --- a/scripts/release/restore.py +++ b/scripts/release/restore.py @@ -3,12 +3,12 @@ from __future__ import annotations import contextlib -import hashlib import os import sys import tempfile from core.artifacts import copy_file_atomic, set_plain_mode +from core.hashing import compute_hashes from model.database import load_database @@ -36,11 +36,7 @@ def index_cache(cache_dir: str) -> dict[str, str]: path = os.path.join(cache_dir, name) if not os.path.isfile(path): continue - digest = hashlib.sha1() - with open(path, "rb") as f: - for chunk in iter(lambda: f.read(1 << 20), b""): - digest.update(chunk) - index[digest.hexdigest()] = path + index[compute_hashes(path, frozenset({"sha1"}), cache=False)["sha1"]] = path return index @@ -59,11 +55,7 @@ def _copy_verified(source: str, path: str, sha1: str) -> bool: os.close(handle) try: copy_file_atomic(source, scratch) - digest = hashlib.sha1() - with open(scratch, "rb") as f: - for chunk in iter(lambda: f.read(1 << 20), b""): - digest.update(chunk) - if digest.hexdigest() != sha1: + if compute_hashes(scratch, frozenset({"sha1"}), cache=False)["sha1"] != sha1: print(f"Changed in the cache while restoring: {source}", file=sys.stderr) return False set_plain_mode(scratch) diff --git a/scripts/release/sync.py b/scripts/release/sync.py index 869403877..10897c277 100644 --- a/scripts/release/sync.py +++ b/scripts/release/sync.py @@ -2,7 +2,6 @@ from __future__ import annotations -import hashlib import os import shutil import subprocess @@ -10,6 +9,7 @@ from collections.abc import Callable from dataclasses import dataclass from pathlib import Path +from core.hashing import compute_hashes from model.largefiles import LARGE_FILES_RELEASE, LARGE_FILES_REPO, asset_names, registered_paths from release.assets import compare, expected_assets, expected_digests, fetch_release @@ -28,11 +28,7 @@ class Upload: def sha1_of(path: Path) -> str: - digest = hashlib.sha1() - with path.open("rb") as handle: - for chunk in iter(lambda: handle.read(1 << 20), b""): - digest.update(chunk) - return digest.hexdigest() + return compute_hashes(path, frozenset({"sha1"}), cache=False)["sha1"] def database_sha1(db: dict) -> dict[str, str]: