refactor: one digest loop for files and streams

This commit is contained in:
Abdessamad Derraz committed 2026-11-18 21:42:28 +01:00
1 parent 88581cf95d
commit 2c8a9cfad8
11 files changed
+88 -98

No files matched your search

+3 -11
View File
@@ -15,10 +15,11 @@ almost never is.
from __future__ import annotations
import hashlib
from dataclasses import dataclass
from pathlib import Path
from core.hashing import file_digest
# A manufacturer directory holding files that genuinely belong to several
# machines, where a shared dump says nothing about a misfiling.
@@ -40,7 +41,6 @@ KNOWN_SHARED: dict[str, tuple[frozenset[str], str]] = {
),
}
CHUNK = 1 << 20
@dataclass
@@ -63,14 +63,6 @@ def _manufacturer(path: Path, root: Path) -> str:
return parts[0] if len(parts) > 1 else ""
def _sha1(path: Path) -> str:
digest = hashlib.sha1()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(CHUNK), b""):
digest.update(chunk)
return digest.hexdigest()
def scan(bios_dir: str = "bios") -> list[Duplicate]:
"""Group the tree by content and keep the groups that cross makers.
@@ -87,7 +79,7 @@ def scan(bios_dir: str = "bios") -> list[Duplicate]:
if len(paths) < 2:
continue
for path in paths:
by_content.setdefault(_sha1(path), []).append(path)
by_content.setdefault(file_digest(path), []).append(path)
found: list[Duplicate] = []
for sha1, paths in by_content.items():
+3 -7
View File
@@ -2,7 +2,6 @@
from __future__ import annotations
import hashlib
import os
import sys
import urllib.parse
@@ -16,7 +15,7 @@ try:
except ImportError:
yaml = None
from core.hashing import compute_hashes, parse_hash_list, parse_md5_list
from core.hashing import compute_hashes, parse_hash_list, parse_md5_list, stream_hashes
from model.nodedup import _in_nodedup_dir
from model.platforms import load_platform_config
from model.profiles import load_emulator_profiles
@@ -355,12 +354,9 @@ def _archive_members(
if info.file_size > MAX_FILE_SIZE:
described.append(f"`{info.filename}` too large to read here")
continue
sha1, md5 = hashlib.sha1(), hashlib.md5()
with archive.open(info) as handle:
for chunk in iter(lambda: handle.read(1 << 20), b""):
sha1.update(chunk)
md5.update(chunk)
sha1_hex, md5_hex = sha1.hexdigest(), md5.hexdigest()
digests = stream_hashes(handle, frozenset({"sha1", "md5"}))
sha1_hex, md5_hex = digests["sha1"], digests["md5"]
crc = f"{info.CRC:08x}"
if sha1_hex in held:
state = f"already held at `{held[sha1_hex]['path']}`"