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.
This commit is contained in:
Abdessamad Derraz committed 2026-11-18 20:44:20 +01:00
1 parent 7ae6996133
commit b9c91a897d
4 files changed
+9 -29

No files matched your search

+2 -6
View File
@@ -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
+2 -6
View File
@@ -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]:
+3 -11
View File
@@ -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)
+2 -6
View File
@@ -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]: