refactor: hash fields and pins read one way

This commit is contained in:
Abdessamad Derraz committed 2026-11-18 19:58:21 +01:00
1 parent 6172cb65c0
commit 41da592291
6 files changed
+59 -89

No files matched your search

+2 -10
View File
@@ -2,12 +2,12 @@
from __future__ import annotations
import hashlib
import os
import zipfile
from dataclasses import dataclass, field
from pathlib import Path
from core.hashing import compute_hashes
from core.paths import REPO_ROOT
from core.torrentzip import write_torrentzip, zlib_ng_version
from recipes.ledger import is_archive_path
@@ -88,14 +88,6 @@ def _members_match(built: Path, members: list[dict]) -> str:
return f"members differ: expected {missing[:3]}, built {extra[:3]}"
def _file_sha1(path: Path) -> str:
digest = hashlib.sha1()
with path.open("rb") as handle:
while chunk := handle.read(16 * 1024 * 1024):
digest.update(chunk)
return digest.hexdigest()
def destination(record_path: str, output_dir: Path) -> Path:
"""Where a build lands: the archive's path below ``bios/``, under *output_dir*."""
parts = Path(record_path).parts
@@ -145,7 +137,7 @@ def build_archive(
[(name, _opener(source.path, source.member)) for name, source in sources],
)
if exact:
written = _file_sha1(scratch)
written = compute_hashes(scratch, frozenset({"sha1"}), cache=False)["sha1"]
detail = "" if written == record["sha1"] else (
f"sha1 {written} is not the recorded {record['sha1']}"
)
+8 -23
View File
@@ -10,9 +10,11 @@ from concurrent.futures import ProcessPoolExecutor
from pathlib import Path
from typing import BinaryIO
from core.hashing import parse_hash_list
from core.torrentzip import write_torrentzip, zlib_ng_version
from recipes.ledger import database_archives
from recipes.members import _member_sha1, _opener
from recipes.pins import container_entries
from recipes.romsets import collect_recipes, load_dat_recipes
_TORRENTZIP_COMMENT = b"TORRENTZIPPED-"
@@ -96,36 +98,19 @@ def _read_job(job: tuple[str, str]) -> tuple[str, dict | str]:
return path, f"{type(exc).__name__}: {exc}"
def _hash_values(value: object) -> set[str]:
raw = value if isinstance(value, list) else str(value or "").split(",")
return {str(item).strip().lower() for item in raw if str(item).strip()}
def container_pins(platforms_dir: str, emulators_dir: str) -> dict[str, set[str]]:
"""Container hash -> who pins it: ``platform:<name>`` or ``profile:<name>``.
A ``zipped_file`` entry pins a ROM inside the archive, and a Recalbox
archive md5 is the composite of its members (``zip_digest: composite``,
Bios.cpp:64-68): neither pins the container bytes.
"""
from model.registry import list_registered_platforms
"""Container hash -> who pins it: ``platform:<name>`` or ``profile:<name>``."""
from model.profiles import load_emulator_profiles
from model.platforms import load_platform_config
pins: dict[str, set[str]] = defaultdict(set)
for platform in list_registered_platforms(platforms_dir):
config = load_platform_config(platform, platforms_dir)
for system in config.get("systems", {}).values():
for entry in system.get("files", []) or []:
if entry.get("zipped_file") or entry.get("zip_digest") == "composite":
continue
for key in ("md5", "sha1", "alt_md5"):
for value in _hash_values(entry.get(key)):
pins[value].add(f"platform:{platform}")
for platform, entry in container_entries(platforms_dir):
for key in ("md5", "sha1", "alt_md5"):
for value in parse_hash_list(entry.get(key)):
pins[value].add(f"platform:{platform}")
for name, profile in load_emulator_profiles(emulators_dir, skip_aliases=False).items():
for entry in profile.get("files", []) or []:
for key in ("md5", "sha1", "sha256"):
for value in _hash_values(entry.get(key)):
for value in parse_hash_list(entry.get(key)):
pins[value].add(f"profile:{name}")
return dict(pins)
+10 -24
View File
@@ -9,8 +9,7 @@ from collections import defaultdict
from pathlib import Path
from core.torrentzip import is_torrentzip
from model.platforms import load_platform_config
from model.registry import list_registered_platforms
from recipes.pins import container_entries
from recipes.pool import AtomPool
from recipes.romsets import Recipe
@@ -65,29 +64,16 @@ def reconstruct(
def platform_targets(platforms_dir: str) -> dict[str, set[str]]:
"""Container MD5s the platforms pin, per archive name.
``zipped_file`` entries pin an inner ROM instead of the container, so
their MD5 is not an archive hash and is skipped. So is a Recalbox md5,
the composite of the members (``zip_digest: composite``, Bios.cpp:64-68):
no container build can ever give it.
"""
"""Container MD5s the platforms pin, per archive name."""
targets: dict[str, set[str]] = defaultdict(set)
for name in list_registered_platforms(platforms_dir):
config = load_platform_config(name, platforms_dir)
for system in config.get("systems", {}).values():
for entry in system.get("files", []) or []:
archive = entry.get("name", "")
if (
not archive.endswith(".zip")
or entry.get("zipped_file")
or entry.get("zip_digest") == "composite"
):
continue
for value in str(entry.get("md5") or "").split(","):
value = value.strip().lower()
if len(value) == 32:
targets[archive].add(value)
for _platform, entry in container_entries(platforms_dir):
archive = entry.get("name", "")
if not archive.endswith(".zip"):
continue
for value in str(entry.get("md5") or "").split(","):
value = value.strip().lower()
if len(value) == 32:
targets[archive].add(value)
return dict(targets)
+24
View File
@@ -0,0 +1,24 @@
"""The platform entries whose hash pins an archive's container bytes."""
from __future__ import annotations
from collections.abc import Iterator
from model.platforms import load_platform_config
from model.registry import list_registered_platforms
def container_entries(platforms_dir: str) -> Iterator[tuple[str, dict]]:
"""(platform, entry) for each platform entry whose hash is the container's.
A ``zipped_file`` entry pins a ROM inside the archive, and a Recalbox
archive md5 is the composite of its members (``zip_digest: composite``,
Bios.cpp:64-68): neither pins the container bytes, and no container
build can ever give them.
"""
for platform in list_registered_platforms(platforms_dir):
config = load_platform_config(platform, platforms_dir)
for system in config.get("systems", {}).values():
for entry in system.get("files", []) or []:
if not (entry.get("zipped_file") or entry.get("zip_digest") == "composite"):
yield platform, entry