mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-26 19:38:52 -06:00
refactor: read hashes and the registry one way
Hash fields go through core.hashing.parse_hash_list, which also splits comma-joined list items: the truth model compared a comma-joined sha256 list as one value. _registry.yml goes through load_platform_registry (required= where an absent file must stop the run), database.json through load_database, file and member digests through file_digest and stream_hashes. download.py and check_release read SHA256SUMS.txt with one parser: a sha256sum -b line matched nothing in download.py and the pack went unchecked.
This commit is contained in:
1 parent
7cd499531d
commit
348d5b51c3
32 files changed
+130
-217
No files matched your search
@@ -6,7 +6,7 @@ import os
|
||||
import zlib
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from core.hashing import compute_hashes, parse_md5_list
|
||||
from core.hashing import compute_hashes, parse_hash_list, parse_md5_list
|
||||
from core.ziptools import check_inside_zip
|
||||
from resolve.localfile import resolve_local_file, variant_base_name
|
||||
from resolve.packfiles import records_by_path, preferred_entries
|
||||
@@ -123,17 +123,7 @@ def lists_hashes(entry: dict) -> bool:
|
||||
|
||||
def _declared_hashes(entry: dict, field_name: str) -> set[str]:
|
||||
"""Hashes an entry accepts for one field, declared as a string or a list."""
|
||||
value = entry.get(field_name)
|
||||
values = value if isinstance(value, list) else [value]
|
||||
# Recalbox writes its md5 list comma separated, and profiles do the
|
||||
# same for sha256 (mesence names four BS-X dumps in one string).
|
||||
return {
|
||||
part.strip().lower()
|
||||
for v in values
|
||||
if v
|
||||
for part in str(v).split(",")
|
||||
if part.strip()
|
||||
}
|
||||
return set(parse_hash_list(entry.get(field_name)))
|
||||
|
||||
|
||||
def _record_at(path: str, db: dict) -> dict | None:
|
||||
|
||||
@@ -20,6 +20,7 @@ import re
|
||||
from collections.abc import Callable, Iterable, Mapping
|
||||
from dataclasses import dataclass
|
||||
|
||||
from core.hashing import parse_hash_list
|
||||
from model.declarations import CONFIGS_ROOT, ROMS_ROOT, declared_root, outer_layout
|
||||
from resolve.content import name_match_size_ok
|
||||
from resolve.localfile import resolve_local_file
|
||||
@@ -1267,8 +1268,7 @@ def _hashes(entry: dict) -> dict[str, set[str]]:
|
||||
value = entry.get(field) or (entry.get("expected") or {}).get(field)
|
||||
if not value:
|
||||
continue
|
||||
values = value if isinstance(value, list) else str(value).split(",")
|
||||
found[field] = {str(v).strip().lower() for v in values if str(v).strip()}
|
||||
found[field] = set(parse_hash_list(value))
|
||||
return found
|
||||
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
from model.largefiles import fetch_large_file
|
||||
from core.hashing import cut_md5_owner
|
||||
from core.hashing import cut_md5_owner, parse_hash_list
|
||||
from resolve.content import declared_hash_verdict
|
||||
from resolve.localfile import resolution_is_hash_exact, resolve_local_file
|
||||
from core.paths import REPO_ROOT, declared_destination
|
||||
@@ -84,20 +84,9 @@ def download_external(
|
||||
return False
|
||||
return True
|
||||
|
||||
def _values(raw: object) -> list[str]:
|
||||
"""A hash field's values: one, a list, or a comma-separated string."""
|
||||
items = raw if isinstance(raw, list) else [raw] if raw else []
|
||||
return [
|
||||
value.strip().lower().removeprefix("0x")
|
||||
for item in items
|
||||
for value in str(item).split(",")
|
||||
if value.strip()
|
||||
]
|
||||
|
||||
|
||||
def _declared_hashes(file_entry: dict) -> dict[str, list[str]]:
|
||||
return {
|
||||
field: _values(file_entry.get(field))
|
||||
field: parse_hash_list(file_entry.get(field))
|
||||
for field in ("sha1", "md5", "sha256", "crc32")
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ from collections.abc import Callable
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
from core.hashing import parse_hash_list
|
||||
from model.systems import norm_system_id
|
||||
from core.paths import REPO_ROOT
|
||||
|
||||
@@ -25,12 +26,7 @@ HASH_FIELDS = ("sha1", "md5", "sha256", "crc32")
|
||||
|
||||
def hash_values(entry: dict, field_name: str) -> list[str]:
|
||||
"""Return a hash field as a list, whatever shape it was written in."""
|
||||
raw = entry.get(field_name)
|
||||
if not raw:
|
||||
return []
|
||||
if isinstance(raw, list):
|
||||
return [str(v).strip().lower() for v in raw if str(v).strip()]
|
||||
return [v.strip().lower() for v in str(raw).split(",") if v.strip()]
|
||||
return parse_hash_list(entry.get(field_name))
|
||||
|
||||
|
||||
def checked_hashes(entry: dict | None) -> dict | None:
|
||||
|
||||
@@ -17,7 +17,7 @@ from model.registry import load_data_dir_registry
|
||||
from resolve.standalone import platform_layouts, standalone_set as platform_standalone_set
|
||||
from resolve.cores import resolve_platform_cores
|
||||
from model.alternatives import groups_of
|
||||
from core.hashing import compute_hashes
|
||||
from core.hashing import compute_hashes, parse_hash_list
|
||||
from resolve.validation import (
|
||||
filter_files_by_mode,
|
||||
installed_with_emulator,
|
||||
@@ -249,10 +249,8 @@ def _shares_hash(a: dict, b: dict) -> bool:
|
||||
|
||||
|
||||
def _declared(entry: dict, field: str) -> set[str]:
|
||||
"""The values an entry declares for a hash field, one or a list."""
|
||||
value = entry.get(field)
|
||||
values = value if isinstance(value, list) else [value]
|
||||
return {str(v).lower() for v in values if v}
|
||||
"""The values an entry declares for a hash field, one, a list or comma-joined."""
|
||||
return set(parse_hash_list(entry.get(field)))
|
||||
|
||||
|
||||
def _contents_disagree(a: dict, b: dict) -> bool:
|
||||
@@ -350,15 +348,15 @@ def _merge_hashes(
|
||||
existing[h] = theirs
|
||||
existing.setdefault("_hash_by", {})[h] = emu_name
|
||||
continue
|
||||
t_list = theirs if isinstance(theirs, list) else [theirs]
|
||||
o_list = ours if isinstance(ours, list) else [ours]
|
||||
# A field holds one value, a list or a comma-joined string (mesence
|
||||
# names twelve BS-X dumps in one sha256): compared value by value.
|
||||
t_list, o_list = parse_hash_list(theirs), parse_hash_list(ours)
|
||||
if revision:
|
||||
known = {str(v).lower() for v in o_list}
|
||||
added = [v for v in t_list if str(v).lower() not in known]
|
||||
added = [v for v in t_list if v not in o_list]
|
||||
if added:
|
||||
existing[h] = [*o_list, *added]
|
||||
continue
|
||||
if not {str(v).lower() for v in t_list} & {str(v).lower() for v in o_list}:
|
||||
if not set(t_list) & set(o_list):
|
||||
# The value kept is one revision among those the cores accept:
|
||||
# it pairs an entry, it never overrules one (checked_hashes).
|
||||
existing["hash_conflict"] = sorted({*existing.get("hash_conflict", ()), h})
|
||||
|
||||
@@ -22,9 +22,9 @@ from resolve.content import (
|
||||
)
|
||||
from resolve.archives import archive_members
|
||||
from model.profiles import CONTENT_CHECKS, entry_system_ids
|
||||
from core.hashing import compute_hashes
|
||||
from core.paths import sanitize_pack_path
|
||||
from core.hashing import md5_composite, parse_hash_list, parse_md5_list
|
||||
from core.hashing import compute_hashes, parse_hash_list
|
||||
from core.paths import BIOS_DIR, sanitize_pack_path
|
||||
from core.hashing import md5_composite, parse_md5_list
|
||||
from resolve.nativemode import SIZE_AND_ANY_HASH, digest_algorithm, hash_mismatch_excludes_file
|
||||
from core.ziptools import check_inside_zip
|
||||
|
||||
@@ -281,35 +281,19 @@ def _merge_sizes(item: dict, f: dict) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _hex_values(value: object) -> list[str]:
|
||||
"""Hash values without their 0x prefix, lowercased."""
|
||||
values = value if isinstance(value, list) else [value] if value else []
|
||||
out = []
|
||||
for item in values:
|
||||
norm = str(item).lower()
|
||||
if norm.startswith("0x"):
|
||||
norm = norm[2:]
|
||||
out.append(norm)
|
||||
return out
|
||||
|
||||
|
||||
def _merge_hashes(item: dict, f: dict, checks: list[str]) -> None:
|
||||
# Hash checks: collect all accepted hashes as sets (multiple valid
|
||||
# versions of the same file, e.g. MT-32 ROM versions)
|
||||
if "crc32" in checks and f.get("crc32"):
|
||||
item["crc32"].update(_hex_values(f["crc32"]))
|
||||
item["crc32"].update(parse_hash_list(f["crc32"]))
|
||||
# A hash field may carry several accepted values, as a list or as
|
||||
# the comma-separated string the schema spells out
|
||||
for hash_type in ("md5", "sha1", "sha256"):
|
||||
if hash_type in checks and f.get(hash_type):
|
||||
val = f[hash_type]
|
||||
values = val if isinstance(val, list) else str(val).split(",")
|
||||
item[hash_type].update(
|
||||
str(h).strip().lower() for h in values if str(h).strip()
|
||||
)
|
||||
item[hash_type].update(parse_hash_list(f[hash_type]))
|
||||
# Adler32: stored as known_hash_adler32 field (not in validation: list
|
||||
# for Dolphin, but support it in both forms for future profiles)
|
||||
item["adler32"].update(_hex_values(f.get("known_hash_adler32") or f.get("adler32")))
|
||||
item["adler32"].update(parse_hash_list(f.get("known_hash_adler32") or f.get("adler32")))
|
||||
if f.get("adler32_byteswap"):
|
||||
item["adler32_byteswap"] = True
|
||||
|
||||
@@ -487,7 +471,7 @@ def check_file_validation(
|
||||
local_path: str,
|
||||
filename: str,
|
||||
validation_index: dict[str, dict],
|
||||
bios_dir: str = "bios",
|
||||
bios_dir: str = BIOS_DIR,
|
||||
emulators: set[str] | None = None,
|
||||
strict: bool = False,
|
||||
) -> tuple[str, list[str]] | None:
|
||||
@@ -894,7 +878,7 @@ def find_validated_variant(
|
||||
db: dict,
|
||||
current_path: str,
|
||||
validation_index: dict,
|
||||
bios_dir: str = "bios",
|
||||
bios_dir: str = BIOS_DIR,
|
||||
platform_digest: str | None = None,
|
||||
emulators: set[str] | None = None,
|
||||
strict: bool = False,
|
||||
|
||||
Reference in new issue
Block a user