mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-25 02:57:52 -06:00
refactor: move pack library code into packs/
This commit is contained in:
1 parent
4d4052a8ab
commit
0b532366d0
167 files changed
+14476
-13882
No files matched your search
@@ -0,0 +1,230 @@
|
||||
"""Destinations a platform declares twice and resolves to two different files."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import zlib
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from core.hashing import compute_hashes, parse_md5_list
|
||||
from core.ziptools import check_inside_zip
|
||||
from resolve.localfile import resolve_local_file, variant_base_name
|
||||
from resolve.packfiles import preferred_entries
|
||||
|
||||
|
||||
def _normalize(destination: str, fold: bool = True) -> str:
|
||||
"""Comparable form of a destination, since layers write it differently.
|
||||
|
||||
Case is folded only where the platform's filesystem folds it: on Linux
|
||||
RetroDECK ships bios/DISK.ROM and bios/disk.rom side by side.
|
||||
"""
|
||||
cleaned = destination.strip().strip("/").replace("\\", "/")
|
||||
return cleaned.casefold() if fold else cleaned
|
||||
|
||||
|
||||
@dataclass
|
||||
class Collision:
|
||||
"""One destination a platform fills with two different files."""
|
||||
|
||||
destination: str
|
||||
resolved: list[str]
|
||||
# Each declaration with the file it resolved to, in the platform's order.
|
||||
declarations: list[tuple[dict, str | None]] = field(default_factory=list)
|
||||
# The system of each declaration, in the same order.
|
||||
systems: list[str] = field(default_factory=list)
|
||||
# The file the pack ships at the destination, as the builder decides it.
|
||||
shipped: str | None = None
|
||||
|
||||
|
||||
def find_collisions(
|
||||
config: dict,
|
||||
db: dict,
|
||||
zip_contents: dict | None = None,
|
||||
data_dir_registry: dict | None = None,
|
||||
) -> list[Collision]:
|
||||
"""Destinations a platform declares twice and resolves two ways.
|
||||
|
||||
One path holds one file, so whichever declaration the builder reaches
|
||||
first decides in silence. Declaring an archive several times to name its
|
||||
inner ROMs is the documented zipped_file pattern and resolves to one
|
||||
archive. Two declarations landing on two files name two files for one
|
||||
path. RetroDECK aims a PC-88 disk subsystem ROM and a
|
||||
CoCo disk ROM at one bios/disk.rom.
|
||||
"""
|
||||
fold = bool(config.get("case_insensitive_fs", False))
|
||||
systems = config.get("systems") or {}
|
||||
# The builder lets a hash-constrained declaration that resolves by
|
||||
# content take the path, else the first it reaches walking the systems
|
||||
# sorted: the same choice says which file ships.
|
||||
preferred = set(
|
||||
preferred_entries(systems, db, "bios", "", False, zip_contents, True).values()
|
||||
)
|
||||
by_dest: dict[str, list[tuple[str, dict]]] = {}
|
||||
for sys_id, system in systems.items():
|
||||
for entry in system.get("files") or []:
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
dest = entry.get("destination") or entry.get("name") or ""
|
||||
if dest:
|
||||
by_dest.setdefault(_normalize(dest, fold), []).append((sys_id, entry))
|
||||
|
||||
collisions = []
|
||||
for key, claims in sorted(by_dest.items()):
|
||||
if len(claims) < 2:
|
||||
continue
|
||||
entries = [entry for _sys_id, entry in claims]
|
||||
resolved = []
|
||||
declarations = []
|
||||
for entry in entries:
|
||||
local, _ = resolve_local_file(
|
||||
entry,
|
||||
db,
|
||||
zip_contents,
|
||||
dest_hint=entry.get("destination", ""),
|
||||
data_dir_registry=data_dir_registry,
|
||||
)
|
||||
declarations.append((entry, local))
|
||||
if local and local not in resolved:
|
||||
resolved.append(local)
|
||||
if len(resolved) > 1 and not _same_file_family(resolved):
|
||||
# A file every declaration accepts settles the path: RetroDECK
|
||||
# declares ATARIOSB.ROM twice, the second with an md5 list that
|
||||
# also names the first one's file.
|
||||
if any(_accepted_by_all(path, entries, db) for path in resolved):
|
||||
continue
|
||||
reached = sorted(
|
||||
(sys_id, local)
|
||||
for (sys_id, _entry), (_e, local) in zip(claims, declarations) if local
|
||||
)
|
||||
shipped = next(
|
||||
(local for entry, local in declarations if local and id(entry) in preferred),
|
||||
reached[0][1],
|
||||
)
|
||||
collisions.append(
|
||||
Collision(
|
||||
destination=key,
|
||||
resolved=resolved,
|
||||
declarations=declarations,
|
||||
systems=[sys_id for sys_id, _entry in claims],
|
||||
shipped=shipped,
|
||||
)
|
||||
)
|
||||
return collisions
|
||||
|
||||
|
||||
def _lists_hashes(entry: dict) -> bool:
|
||||
"""Whether a declaration says anything about content at all."""
|
||||
return any(
|
||||
_declared_hashes(entry, field_name)
|
||||
for field_name in ("md5", "sha1", "sha256", "crc32")
|
||||
)
|
||||
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
def _record_at(path: str, db: dict) -> dict | None:
|
||||
"""The database record stored at path, from an index built once per db.
|
||||
|
||||
A scan of db['files'] on every call was O(calls x 19 000 files) on the
|
||||
path verify --all and slots.py both take.
|
||||
"""
|
||||
files = db.get("files", {})
|
||||
cached = _PATH_INDEX.get(id(files))
|
||||
if cached is None or cached[0] is not files:
|
||||
cached = (files, {e.get("path"): e for e in files.values()})
|
||||
_PATH_INDEX[id(files)] = cached
|
||||
return cached[1].get(path)
|
||||
|
||||
|
||||
_PATH_INDEX: dict[int, tuple[dict, dict[str, dict]]] = {}
|
||||
|
||||
|
||||
def _content_sha1(path: str, db: dict) -> str | None:
|
||||
"""The sha1 of the file at path, from the database or read from disk.
|
||||
|
||||
A data directory cache is not in the database, so its files are hashed.
|
||||
"""
|
||||
record = _record_at(path, db)
|
||||
if record is not None:
|
||||
return record.get("sha1")
|
||||
if not os.path.isfile(path):
|
||||
return None
|
||||
return compute_hashes(path, frozenset({"sha1"}))["sha1"]
|
||||
|
||||
|
||||
def _accepted_by_all(path: str, entries: list[dict], db: dict) -> bool:
|
||||
"""Whether every declaration's own hashes accept the file at path."""
|
||||
record = _record_at(path, db)
|
||||
if record is None:
|
||||
return False
|
||||
for entry in entries:
|
||||
if entry.get("zipped_file"):
|
||||
declared = parse_md5_list(entry.get("md5"))
|
||||
if declared and not any(
|
||||
check_inside_zip(path, entry["zipped_file"], m) == "ok" for m in declared
|
||||
):
|
||||
return False
|
||||
continue
|
||||
for field_name in ("md5", "sha1", "sha256", "crc32"):
|
||||
declared = _declared_hashes(entry, field_name)
|
||||
if declared and str(record.get(field_name, "")).lower() not in declared:
|
||||
if field_name == "crc32" and _headerless_crc32(path, record, entry) in declared:
|
||||
continue
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
COPIER_HEADER = 512
|
||||
|
||||
|
||||
def _headerless_crc32(path: str, record: dict, entry: dict) -> str | None:
|
||||
"""The crc32 of a file past its 512-byte copier header, when the entry
|
||||
declares both the file's size and the size without the header.
|
||||
|
||||
Geargrafx drops the header when the file is 512 bytes over the expected
|
||||
size and computes the crc32 on the rest (src/media.cpp:754-773). The
|
||||
profile lists both sizes. EmuELEC's headered syscard3.pce (crc32
|
||||
64f78e3c) is the 6d9a73ef image the profile names.
|
||||
"""
|
||||
sizes = entry.get("size")
|
||||
declared = sizes if isinstance(sizes, list) else [sizes]
|
||||
size = record.get("size")
|
||||
if not isinstance(size, int) or not {size, size - COPIER_HEADER} <= set(declared):
|
||||
return None
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
f.seek(COPIER_HEADER)
|
||||
return f"{zlib.crc32(f.read()):08x}"
|
||||
except OSError:
|
||||
return None
|
||||
|
||||
|
||||
def _same_file_family(paths: list[str]) -> bool:
|
||||
"""Whether the paths are one file and its own pinned variants.
|
||||
|
||||
A platform list built on an older romset pins bytes the primary no longer
|
||||
carries, which the repository keeps side by side under .variants. That is
|
||||
the documented multi-version case, not two machines fighting for a path.
|
||||
"""
|
||||
canonical = set()
|
||||
for path in paths:
|
||||
if "/.variants/" in path:
|
||||
directory, _, name = path.rpartition("/")
|
||||
directory = directory[: -len("/.variants")]
|
||||
name = variant_base_name(name)
|
||||
path = f"{directory}/{name}"
|
||||
canonical.add(path.casefold())
|
||||
return len(canonical) == 1
|
||||
@@ -0,0 +1,549 @@
|
||||
"""Finding the bytes a pack entry names.
|
||||
|
||||
Resolution proper lives in common. This layer adds what only a build
|
||||
needs: storage tiers, release assets for files too large to commit,
|
||||
and the offline switch that forbids reaching for the network."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
import re
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
from model.largefiles import fetch_large_file
|
||||
from core.hashing import cut_md5_owner
|
||||
from resolve.content import declared_hash_verdict
|
||||
from resolve.localfile import resolution_is_hash_exact, resolve_local_file
|
||||
from core.paths import REPO_ROOT, sanitize_pack_path
|
||||
from resolve.validation import inner_rom_check
|
||||
# CLI-wide network policy. Public helpers can override it explicitly, while
|
||||
# every CLI mode inherits --offline (including large-file release fallback).
|
||||
_OFFLINE = False
|
||||
|
||||
|
||||
def set_offline(value: bool) -> None:
|
||||
"""Forbid every network reach for the rest of the run."""
|
||||
global _OFFLINE
|
||||
_OFFLINE = value
|
||||
|
||||
|
||||
def scratch_dir() -> str:
|
||||
"""The repository's tmp/, never the system /tmp (a 4 GB tmpfs on WSL)."""
|
||||
path = REPO_ROOT / "tmp"
|
||||
path.mkdir(exist_ok=True)
|
||||
return str(path)
|
||||
|
||||
|
||||
def download_external(
|
||||
file_entry: dict, dest_path: str, offline: bool | None = None
|
||||
) -> bool:
|
||||
"""Download a `storage: external` file, verify its hash, write dest_path.
|
||||
|
||||
The run's network policy applies: under --offline nothing is fetched and
|
||||
the entry stays missing, as verify reports it. The body is streamed to
|
||||
dest_path and hashed on the way, and removed when the hash disagrees.
|
||||
"""
|
||||
url = file_entry.get("source_url")
|
||||
if not url:
|
||||
return False
|
||||
name = file_entry.get("name", url)
|
||||
declared = next(
|
||||
(
|
||||
(algo, str(file_entry[algo]).lower())
|
||||
for algo in ("sha256", "sha1", "md5")
|
||||
if file_entry.get(algo)
|
||||
),
|
||||
None,
|
||||
)
|
||||
if declared is None:
|
||||
print(f" WARNING: no hash for {name}, skipping unverifiable download")
|
||||
return False
|
||||
if _OFFLINE if offline is None else offline:
|
||||
print(f" WARNING: {name} is external and the run is offline, not fetched")
|
||||
return False
|
||||
algo, expected = declared
|
||||
digest = hashlib.new(algo)
|
||||
Path(dest_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
req = urllib.request.Request(url, headers={"User-Agent": "retrobios-pack-gen/1.0"})
|
||||
with urllib.request.urlopen(req, timeout=120) as resp, open(dest_path, "wb") as out:
|
||||
for chunk in iter(lambda: resp.read(1 << 20), b""):
|
||||
digest.update(chunk)
|
||||
out.write(chunk)
|
||||
except (urllib.error.URLError, OSError) as exc:
|
||||
print(f" WARNING: Failed to download {url}: {exc}")
|
||||
Path(dest_path).unlink(missing_ok=True)
|
||||
return False
|
||||
if digest.hexdigest() != expected:
|
||||
print(f" WARNING: {algo.upper()} mismatch for {name}")
|
||||
Path(dest_path).unlink(missing_ok=True)
|
||||
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))
|
||||
for field in ("sha1", "md5", "sha256", "crc32")
|
||||
}
|
||||
|
||||
|
||||
def _verified_asset(
|
||||
name: str, declared: dict[str, list[str]], file_entry: dict, offline: bool
|
||||
) -> str | None:
|
||||
"""The release asset of that name, if it is a copy the entry accepts.
|
||||
|
||||
The store is asked once per accepted sha1 (or md5), since it keeps one
|
||||
revision under a name. What it returns is held against every hash the
|
||||
entry declares, sha256 and crc32 included.
|
||||
"""
|
||||
attempts = (
|
||||
[(sha1, "") for sha1 in declared["sha1"]]
|
||||
or [("", md5) for md5 in declared["md5"]]
|
||||
or [("", "")]
|
||||
)
|
||||
for sha1, md5 in attempts:
|
||||
cached = fetch_large_file(
|
||||
name, expected_sha1=sha1, expected_md5=md5, offline=offline,
|
||||
)
|
||||
if not cached:
|
||||
continue
|
||||
verdict = declared_hash_verdict(
|
||||
cached,
|
||||
has_strong_hash=any(declared.values()),
|
||||
sha1_candidates=set(declared["sha1"]),
|
||||
sha256_candidates=set(declared["sha256"]),
|
||||
md5_list=declared["md5"],
|
||||
crc_candidates=set(declared["crc32"]),
|
||||
zipped_file=file_entry.get("zipped_file"),
|
||||
declared_size=file_entry.get("size"),
|
||||
file_entry=file_entry,
|
||||
)
|
||||
if verdict != "hash_mismatch":
|
||||
return cached
|
||||
return None
|
||||
|
||||
|
||||
def run_is_offline(offline: bool | None = None) -> bool:
|
||||
"""Whether this run fetches nothing: --offline, or RETROBIOS_OFFLINE."""
|
||||
return bool(
|
||||
(_OFFLINE if offline is None else offline)
|
||||
or os.environ.get("RETROBIOS_OFFLINE")
|
||||
)
|
||||
|
||||
|
||||
_RECORDS_BY_PATH: list[tuple[dict, int, dict[str, dict]]] = []
|
||||
|
||||
|
||||
def _records_by_path(db: dict) -> dict[str, dict]:
|
||||
"""The database records by path, built once per database."""
|
||||
files = db.get("files", {})
|
||||
if _RECORDS_BY_PATH:
|
||||
cached, size, index = _RECORDS_BY_PATH[0]
|
||||
if cached is files and size == len(files):
|
||||
return index
|
||||
index = {record.get("path"): record for record in files.values()}
|
||||
_RECORDS_BY_PATH[:] = [(files, len(files), index)]
|
||||
return index
|
||||
|
||||
|
||||
def pending_release_asset(
|
||||
file_entry: dict, db: dict, registered: list[str] | None = None,
|
||||
) -> str | None:
|
||||
"""The release asset an online build fetches for an entry, if one does.
|
||||
|
||||
The registered path whose database record carries what the entry
|
||||
declares, read without a download: verify never fetches, and a release
|
||||
asset the cache lacks was reported missing while an online build packed
|
||||
it. ``registered`` is the .gitignore registry (load_registered_paths).
|
||||
"""
|
||||
from model.largefiles import asset_candidates, asset_names, load_registered_paths
|
||||
|
||||
name = file_entry.get("name", "")
|
||||
if not name or name.endswith("/") or file_entry.get("type") == "directory":
|
||||
return None
|
||||
declared = _declared_hashes(file_entry)
|
||||
if file_entry.get("unsourceable") and not any(declared.values()):
|
||||
return None
|
||||
if file_entry.get("zipped_file") and not any(declared.values()):
|
||||
return None
|
||||
if registered is None:
|
||||
registered = load_registered_paths()
|
||||
assets = set(asset_candidates(name, registered))
|
||||
if not assets:
|
||||
return None
|
||||
records = _records_by_path(db)
|
||||
declared_size = file_entry.get("size")
|
||||
for path, asset in sorted(asset_names(registered).items()):
|
||||
record = records.get(path)
|
||||
if asset not in assets or record is None:
|
||||
continue
|
||||
if isinstance(declared_size, int) and record.get("size") != declared_size:
|
||||
continue
|
||||
if all(
|
||||
not values or str(record.get(field, "")).lower() in values
|
||||
for field, values in declared.items()
|
||||
):
|
||||
return path
|
||||
return None
|
||||
|
||||
|
||||
# Digest length in hex digits -> kind. A 29-digit MD5 is a value cut short,
|
||||
# looked up as the resolver looks one up (cut_md5_owner).
|
||||
_HASH_KINDS = {8: "crc32", 29: "md5", 32: "md5", 40: "sha1", 64: "sha256"}
|
||||
_HEX_DIGITS = re.compile(r"[0-9a-f]+")
|
||||
_WORD = re.compile(r"[0-9A-Za-z]+")
|
||||
ACCEPTED_HASHES = (
|
||||
"CRC32 (8 hex digits), MD5 (32, or the 29 Batocera writes), "
|
||||
"SHA-1 (40) or SHA-256 (64)"
|
||||
)
|
||||
|
||||
|
||||
def _detect_hash_type(h: str) -> str | None:
|
||||
"""The digest a hex string is, by its length, or None."""
|
||||
return _HASH_KINDS.get(len(h))
|
||||
|
||||
|
||||
def _hash_token(token: str) -> tuple[str, str] | None:
|
||||
"""(kind, value) when a token is a whole digest of a known length."""
|
||||
value = token.strip().lower().removeprefix("*").removeprefix("0x")
|
||||
kind = _detect_hash_type(value)
|
||||
if kind and _HEX_DIGITS.fullmatch(value):
|
||||
return kind, value
|
||||
return None
|
||||
|
||||
|
||||
def _refuse(rejected: list[str]) -> None:
|
||||
shown = ", ".join(rejected[:5]) + (", ..." if len(rejected) > 5 else "")
|
||||
raise ValueError(f"not a hash: {shown}. Accepted: {ACCEPTED_HASHES}")
|
||||
|
||||
|
||||
def parse_hash_input(raw: str) -> list[tuple[str, str]]:
|
||||
"""Parse a comma-separated hash string into (type, hash) tuples.
|
||||
|
||||
A part that is no digest is refused rather than dropped: a 12-digit
|
||||
value was looked up as an MD5 and reported NOT FOUND, and a SHA-256
|
||||
vanished without a word.
|
||||
"""
|
||||
results: list[tuple[str, str]] = []
|
||||
rejected: list[str] = []
|
||||
for part in raw.split(","):
|
||||
if not part.strip():
|
||||
continue
|
||||
parsed = _hash_token(part)
|
||||
if parsed:
|
||||
results.append(parsed)
|
||||
else:
|
||||
rejected.append(part.strip())
|
||||
if rejected:
|
||||
_refuse(rejected)
|
||||
return results
|
||||
|
||||
|
||||
def parse_hash_file(path: str) -> list[tuple[str, str]]:
|
||||
"""Parse a hash file: one hash per line, alone or beside a file name as
|
||||
md5sum writes it, ``#`` for comments. A line holding no digest is
|
||||
refused with its line number."""
|
||||
results: list[tuple[str, str]] = []
|
||||
rejected: list[str] = []
|
||||
with open(path) as f:
|
||||
for number, line in enumerate(f, 1):
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
parsed = next(
|
||||
(h for h in map(_hash_token, _WORD.findall(line)) if h), None
|
||||
)
|
||||
if parsed:
|
||||
results.append(parsed)
|
||||
else:
|
||||
rejected.append(f"line {number} ({line[:40]})")
|
||||
if rejected:
|
||||
_refuse(rejected)
|
||||
return results
|
||||
|
||||
|
||||
def sha1_for_hash(hash_type: str, hash_val: str, db: dict) -> str | None:
|
||||
"""The database key a looked-up hash names, or None."""
|
||||
files_db = db.get("files", {})
|
||||
indexes = db.get("indexes", {})
|
||||
sha1: str | None
|
||||
if hash_type == "sha1":
|
||||
sha1 = hash_val
|
||||
elif hash_type == "crc32":
|
||||
sha1 = indexes.get("by_crc32", {}).get(hash_val)
|
||||
elif hash_type == "md5":
|
||||
by_md5 = indexes.get("by_md5", {})
|
||||
sha1 = by_md5.get(hash_val)
|
||||
if sha1 is None:
|
||||
owner = cut_md5_owner(hash_val, by_md5)
|
||||
sha1 = by_md5.get(owner) if owner else None
|
||||
else:
|
||||
sha1 = next(
|
||||
(k for k, e in files_db.items() if e.get("sha256") == hash_val), None
|
||||
)
|
||||
return sha1 if sha1 in files_db else None
|
||||
|
||||
def lookup_hashes(
|
||||
hashes: list[tuple[str, str]],
|
||||
db: dict,
|
||||
bios_dir: str,
|
||||
emulators_dir: str,
|
||||
platforms_dir: str,
|
||||
) -> int:
|
||||
"""Print diagnostic info for each hash and return how many were not found."""
|
||||
missing = 0
|
||||
files_db = db.get("files", {})
|
||||
|
||||
for hash_type, hash_val in hashes:
|
||||
sha1 = sha1_for_hash(hash_type, hash_val, db)
|
||||
if not sha1:
|
||||
print(f"\n{hash_type.upper()}: {hash_val}")
|
||||
print(" NOT FOUND in database")
|
||||
missing += 1
|
||||
continue
|
||||
|
||||
entry = files_db[sha1]
|
||||
name = entry.get("name", "?")
|
||||
md5 = entry.get("md5", "?")
|
||||
paths = entry.get("paths") or []
|
||||
aliases = entry.get("aliases") or []
|
||||
|
||||
print(f"\n{hash_type.upper()}: {hash_val}")
|
||||
print(f" SHA1: {sha1}")
|
||||
print(f" MD5: {md5}")
|
||||
print(f" Name: {name}")
|
||||
if paths:
|
||||
print(f" Path: {paths[0]}")
|
||||
if aliases:
|
||||
print(f" Aliases: {aliases}")
|
||||
|
||||
# Check if file exists in repo (by path or by resolve_local_file)
|
||||
in_repo = False
|
||||
if paths:
|
||||
primary = os.path.join(bios_dir, paths[0])
|
||||
if os.path.exists(primary):
|
||||
in_repo = True
|
||||
if not in_repo:
|
||||
try:
|
||||
fe_check = {"name": name, "sha1": sha1, "md5": md5}
|
||||
local, status = resolve_file(fe_check, db, bios_dir, {})
|
||||
if local and status != "not_found":
|
||||
in_repo = True
|
||||
except (KeyError, OSError):
|
||||
pass
|
||||
print(f" In repo: {'YES' if in_repo else 'NO'}")
|
||||
if missing:
|
||||
print(f"\n{missing} of {len(hashes)} hash(es) not in the database")
|
||||
return missing
|
||||
|
||||
def resolve_file(
|
||||
file_entry: dict,
|
||||
db: dict,
|
||||
bios_dir: str,
|
||||
zip_contents: dict | None = None,
|
||||
dest_hint: str = "",
|
||||
data_dir_registry: dict | None = None,
|
||||
*,
|
||||
offline: bool | None = None,
|
||||
) -> tuple[str | None, str]:
|
||||
"""Resolve a BIOS file with storage tiers and release asset fallback.
|
||||
|
||||
Wraps common.resolve_local_file() with pack-specific logic for
|
||||
storage tiers (external/user_provided), large file release assets,
|
||||
and MAME clone mapping (deduped ZIPs).
|
||||
"""
|
||||
storage = file_entry.get("storage", "embedded")
|
||||
if storage == "user_provided":
|
||||
return None, "user_provided"
|
||||
if storage == "external":
|
||||
return None, "external"
|
||||
|
||||
path, status = resolve_local_file(
|
||||
file_entry,
|
||||
db,
|
||||
zip_contents,
|
||||
dest_hint=dest_hint,
|
||||
data_dir_registry=data_dir_registry,
|
||||
)
|
||||
if path and status != "hash_mismatch":
|
||||
return path, status
|
||||
|
||||
# Large files from GitHub release assets: tried when local file is
|
||||
# missing OR has a hash mismatch (wrong variant on disk)
|
||||
name = file_entry.get("name", "")
|
||||
if file_entry.get("type") == "directory" or name.endswith("/"):
|
||||
# A tree the collection does not hold is missing, as verify says,
|
||||
# since the asset store holds files, not trees.
|
||||
return (path, status) if path else (None, "not_found")
|
||||
declared = _declared_hashes(file_entry)
|
||||
if file_entry.get("unsourceable") and not any(declared.values()):
|
||||
# The asset store is addressed by file name: with no hash to check,
|
||||
# it would hand back the same-named file of another game, which is
|
||||
# exactly what the flag forbids.
|
||||
return (path, status) if path else (None, "not_found")
|
||||
cached = _verified_asset(
|
||||
name, declared, file_entry, _OFFLINE if offline is None else offline,
|
||||
)
|
||||
if cached:
|
||||
return cached, "release_asset"
|
||||
|
||||
# Fall back to hash_mismatch local file if release asset unavailable
|
||||
if path:
|
||||
return path, status
|
||||
|
||||
return None, "not_found"
|
||||
|
||||
|
||||
def apply_override(
|
||||
override: str | None, local_path: str | None, status: str
|
||||
) -> tuple[str | None, str]:
|
||||
"""The file a pack serves at a destination once its slot is settled.
|
||||
|
||||
An entry the platform stores outside the collection, or a release asset
|
||||
fetched for its declared hash, keeps its own answer.
|
||||
"""
|
||||
if override and status not in ("external", "release_asset"):
|
||||
return override, "slot_arbitrated"
|
||||
return local_path, status
|
||||
|
||||
|
||||
def resolve_declaration(
|
||||
file_entry: dict,
|
||||
db: dict,
|
||||
bios_dir: str,
|
||||
zip_contents: dict | None,
|
||||
data_dir_registry: dict | None,
|
||||
override: str | None,
|
||||
*,
|
||||
offline: bool | None = None,
|
||||
) -> tuple[str | None, str]:
|
||||
"""The file a platform pack serves for one of the platform's declarations.
|
||||
|
||||
Read by the builder, the install manifest, verify and the region pass.
|
||||
verify resolved with common.resolve_local_file and put the slot override
|
||||
first: without the release-asset tier a gitignored file absent from bios/
|
||||
read as missing there while the pack shipped the cached asset.
|
||||
"""
|
||||
return apply_override(override, *resolve_file(
|
||||
file_entry,
|
||||
db,
|
||||
bios_dir,
|
||||
zip_contents,
|
||||
data_dir_registry=data_dir_registry,
|
||||
offline=offline,
|
||||
))
|
||||
|
||||
|
||||
def _declarations_by_destination(
|
||||
pack_systems: dict, base_dest: str, required_only: bool,
|
||||
) -> dict[str, list[dict]]:
|
||||
"""The declarations of each full destination, in system order."""
|
||||
dest_entries: dict[str, list[dict]] = {}
|
||||
for _sys_id, system in sorted(pack_systems.items()):
|
||||
for file_entry in system.get("files", []):
|
||||
if required_only and file_entry.get("required") is False:
|
||||
continue
|
||||
dest = sanitize_pack_path(
|
||||
file_entry.get("destination", file_entry.get("name", ""))
|
||||
)
|
||||
if not dest:
|
||||
continue
|
||||
full = f"{base_dest}/{dest}" if base_dest else dest
|
||||
dest_entries.setdefault(full, []).append(file_entry)
|
||||
return dest_entries
|
||||
|
||||
|
||||
def _archive_choice(
|
||||
resolved: list[tuple[dict, str | None, str]], members: list[dict],
|
||||
) -> dict | None:
|
||||
"""The declaration whose archive the most per-ROM declarations accept."""
|
||||
# One archive declared once per ROM it must hold: the archive
|
||||
# to ship is the one the most declarations accept, not the first
|
||||
# that answers one of them (adam_fdc.zip: a one-member MAME set
|
||||
# answered first, the eight-member set sat beside it).
|
||||
def accepted(path: str | None) -> int:
|
||||
if not path or not path.endswith(".zip"):
|
||||
return -1
|
||||
return sum(inner_rom_check(fe, path) == "ok" for fe in members)
|
||||
|
||||
fe, path, _st = max(resolved, key=lambda item: accepted(item[1]))
|
||||
return fe if accepted(path) > 0 else None
|
||||
|
||||
|
||||
def _hash_choice(resolved: list[tuple[dict, str | None, str]]) -> dict | None:
|
||||
"""The first declaration resolved by md5, else the first by any hash."""
|
||||
best = None
|
||||
for fe, _lp, _st in resolved:
|
||||
if _lp and _st == "md5_exact":
|
||||
return fe
|
||||
if best is None and _lp and resolution_is_hash_exact(_st):
|
||||
best = fe
|
||||
return best
|
||||
|
||||
|
||||
def preferred_entries(
|
||||
pack_systems: dict,
|
||||
db: dict,
|
||||
bios_dir: str,
|
||||
base_dest: str,
|
||||
required_only: bool,
|
||||
zip_contents: dict | None,
|
||||
offline: bool | None,
|
||||
) -> dict[str, int]:
|
||||
"""Which declaration wins when several claim the same destination.
|
||||
|
||||
A platform can declare one destination in more than one system, bare
|
||||
in one and hash-constrained in another. First-come dedup would let
|
||||
the bare entry pack whatever answers to the name, so a constrained
|
||||
sibling that resolves by content claims the destination instead.
|
||||
The choice is about the baseline and reads no data directory, for the
|
||||
pack, the manifest and verify alike: verify alone passed the registry
|
||||
and could crown a declaration only a data/ cache satisfies.
|
||||
"""
|
||||
preferred_entries: dict[str, int] = {}
|
||||
dest_entries = _declarations_by_destination(pack_systems, base_dest, required_only)
|
||||
for full, entries in dest_entries.items():
|
||||
if len(entries) < 2:
|
||||
continue
|
||||
constrained = [
|
||||
fe
|
||||
for fe in entries
|
||||
if fe.get("md5") or fe.get("sha1") or fe.get("zipped_file")
|
||||
]
|
||||
if not constrained:
|
||||
continue
|
||||
resolved = [
|
||||
(fe, *resolve_file(
|
||||
fe,
|
||||
db,
|
||||
bios_dir,
|
||||
zip_contents,
|
||||
offline=offline,
|
||||
))
|
||||
for fe in constrained
|
||||
]
|
||||
members = [fe for fe in entries if fe.get("zipped_file")]
|
||||
if members:
|
||||
chosen = _archive_choice(resolved, members)
|
||||
if chosen is not None:
|
||||
preferred_entries[full] = id(chosen)
|
||||
continue
|
||||
best = _hash_choice(resolved)
|
||||
if best is not None:
|
||||
preferred_entries[full] = id(best)
|
||||
|
||||
return preferred_entries
|
||||
@@ -0,0 +1,915 @@
|
||||
"""Reviewing a contributed file: whether the collection knows it, accepts it, and where in bios/ it goes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
import sys
|
||||
import urllib.parse
|
||||
import zipfile
|
||||
from collections.abc import Callable, Iterable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, TYPE_CHECKING, TypedDict
|
||||
|
||||
try:
|
||||
import yaml
|
||||
except ImportError:
|
||||
yaml = None
|
||||
|
||||
from core.hashing import compute_hashes, parse_hash_list, parse_md5_list
|
||||
from model.nodedup import _in_nodedup_dir
|
||||
from model.platforms import load_platform_config
|
||||
from model.profiles import load_emulator_profiles
|
||||
from model.registry import list_registered_platforms
|
||||
from resolve.nativemode import digest_algorithm
|
||||
from resolve.validation import frontend_digest_matches, inner_rom_check
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import Unpack
|
||||
|
||||
|
||||
BLOCKED_EXTENSIONS = {
|
||||
".exe",
|
||||
".bat",
|
||||
".cmd",
|
||||
".sh",
|
||||
".ps1",
|
||||
".vbs",
|
||||
".js",
|
||||
".msi",
|
||||
".dll",
|
||||
".so",
|
||||
".dylib",
|
||||
".py",
|
||||
".rb",
|
||||
".pl",
|
||||
}
|
||||
|
||||
MAX_FILE_SIZE = 100 * 1024 * 1024
|
||||
LARGE_FILE_ISSUE_URL = (
|
||||
"https://github.com/Abdess/retrobios/issues/new?template=missing-bios.yml"
|
||||
)
|
||||
|
||||
|
||||
def large_file_issue_url(result: ValidationResult) -> str:
|
||||
"""The issue form with the values this run computed already filled in.
|
||||
|
||||
GitHub fills an issue form field from the query parameter named by its
|
||||
id: filename, sha1, md5 and size in missing-bios.yml.
|
||||
"""
|
||||
fields = {
|
||||
"title": f"[BIOS] Missing: {result.filename}",
|
||||
"filename": result.filename,
|
||||
"sha1": result.sha1,
|
||||
"md5": result.md5,
|
||||
"size": str(result.size),
|
||||
}
|
||||
return f"{LARGE_FILE_ISSUE_URL}&{urllib.parse.urlencode(fields)}"
|
||||
|
||||
|
||||
class ValidationResult:
|
||||
def __init__(self, filepath: str) -> None:
|
||||
self.filepath = filepath
|
||||
self.filename = os.path.basename(filepath)
|
||||
self.checks: list[tuple[str, str]] = [] # (status, message) tuples
|
||||
self.sha1 = ""
|
||||
self.md5 = ""
|
||||
self.crc32 = ""
|
||||
self.size = 0
|
||||
# "held" (a copy of a file the collection has) or "unknown" (no list
|
||||
# or profile declares it): no check fails, and neither is a file
|
||||
# the pull request adds to the collection.
|
||||
self.outcome = ""
|
||||
# Where --place copies the file: one folder and one declared name, or
|
||||
# the .variants/ slot of a name the collection already holds.
|
||||
self.destination = ""
|
||||
|
||||
def add_check(self, passed: bool, message: str) -> None:
|
||||
self.checks.append(("PASS" if passed else "FAIL", message))
|
||||
|
||||
def add_warning(self, message: str) -> None:
|
||||
self.checks.append(("WARN", message))
|
||||
|
||||
def add_info(self, message: str) -> None:
|
||||
self.checks.append(("INFO", message))
|
||||
|
||||
@property
|
||||
def passed(self) -> bool:
|
||||
return all(s != "FAIL" for s, _ in self.checks)
|
||||
|
||||
@property
|
||||
def status(self) -> str:
|
||||
return "FAIL" if not self.passed else "REVIEW" if self.outcome else "PASS"
|
||||
|
||||
def to_markdown(self) -> str:
|
||||
status = self.status
|
||||
lines = [f"### {status} `{self.filename}`"]
|
||||
lines.append("")
|
||||
lines.append(f"- **Path**: `{self.filepath}`")
|
||||
# A path refused before it was read has no size or digest to show:
|
||||
# empty fields read as an empty file contributed.
|
||||
if self.sha1:
|
||||
lines.append(f"- **Size**: {self.size:,} bytes")
|
||||
lines.append(f"- **SHA1**: `{self.sha1}`")
|
||||
lines.append(f"- **MD5**: `{self.md5}`")
|
||||
lines.append(f"- **CRC32**: `{self.crc32}`")
|
||||
lines.append("")
|
||||
|
||||
for status_str, message in self.checks:
|
||||
if status_str == "PASS":
|
||||
lines.append(f"- Pass: {message}")
|
||||
elif status_str == "FAIL":
|
||||
lines.append(f"- Fail: {message}")
|
||||
elif status_str == "WARN":
|
||||
lines.append(f"- Warning: {message}")
|
||||
else:
|
||||
lines.append(f"- Info: {message}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _note_sizes(sizes: dict[str, list], names: list[str], entry: dict) -> None:
|
||||
"""Record the sizes an entry accepts under each of its names.
|
||||
|
||||
An entry that declares no size says nothing about it, so a name is
|
||||
judged only by the declarations that do.
|
||||
"""
|
||||
size = entry.get("size")
|
||||
accepted = []
|
||||
if isinstance(size, list):
|
||||
accepted = [(int(v), int(v)) for v in size]
|
||||
elif isinstance(size, int) and size > 0:
|
||||
accepted = [(size, size)]
|
||||
if entry.get("min_size") or entry.get("max_size"):
|
||||
accepted.append((int(entry.get("min_size") or 0),
|
||||
int(entry.get("max_size") or 2 ** 63)))
|
||||
for name in names if accepted else []:
|
||||
sizes.setdefault(name, []).extend(accepted)
|
||||
|
||||
|
||||
def load_platform_hashes(platforms_dir: str, db: dict | None = None) -> dict:
|
||||
"""Load all known hashes from platform configs.
|
||||
|
||||
With the database, `folders` maps each declared hash to the
|
||||
bios/<Manufacturer>/<Console>/ folders the collection files its system
|
||||
under, read from the other files of that system it holds.
|
||||
"""
|
||||
known: dict[str, Any] = {"sha1": set(), "md5": set(), "sha256": set(), "crc32": set(),
|
||||
"names": set(), "folders": {}, "sizes": {}, "entries": {}, "named": {},
|
||||
"name_systems": {}, "system_dirs": {}, "system_folders": {}}
|
||||
systems_of: dict[str, set[str]] = {}
|
||||
folders_of: dict[str, dict[str, int]] = {}
|
||||
files = (db or {}).get("files", {})
|
||||
by_md5 = (db or {}).get("indexes", {}).get("by_md5", {})
|
||||
|
||||
if not os.path.isdir(platforms_dir) or yaml is None:
|
||||
return known
|
||||
|
||||
# Read the way verify reads them: inheritance and shared groups resolved,
|
||||
# and a Recalbox md5 field split into the revisions it lists. The raw
|
||||
# file kept "a,b,c" as one value, so a PR adding the seventh accepted
|
||||
# falcon.img was told its hash differs.
|
||||
for name in list_registered_platforms(platforms_dir):
|
||||
try:
|
||||
config = load_platform_config(name, platforms_dir)
|
||||
except (OSError, yaml.YAMLError) as exc:
|
||||
print(f"WARNING: {name}: {exc}", file=sys.stderr)
|
||||
continue
|
||||
|
||||
mode, rule = config.get("verification_mode"), config.get("hash_rule", "")
|
||||
for system_id, system in config.get("systems", {}).items():
|
||||
for file_entry in system.get("files", []):
|
||||
sha1s = parse_hash_list(file_entry.get("sha1"))
|
||||
md5s = parse_md5_list(file_entry.get("md5"))
|
||||
sha256s = parse_hash_list(file_entry.get("sha256"))
|
||||
known["sha1"].update(sha1s)
|
||||
known["md5"].update(md5s)
|
||||
known["sha256"].update(sha256s)
|
||||
if not (sha1s or md5s or sha256s) and isinstance(file_entry.get("size"), int):
|
||||
# The resolver's reading: a crc32 counts where no stronger
|
||||
# hash is declared, bounded by the declared size.
|
||||
known["crc32"].update(
|
||||
(crc.zfill(8), file_entry["size"])
|
||||
for crc in parse_hash_list(file_entry.get("crc32"))
|
||||
)
|
||||
if "name" in file_entry:
|
||||
known["names"].add(file_entry["name"])
|
||||
_note_sizes(known["sizes"], [file_entry["name"]], file_entry)
|
||||
# A frontend that hashes a ZIP's member or its members
|
||||
# together, or that takes any hash at the declared size,
|
||||
# answers by its own rule, read per entry.
|
||||
known["entries"].setdefault(file_entry["name"].lower(), []).append(
|
||||
(file_entry, mode, rule, name)
|
||||
)
|
||||
known["name_systems"].setdefault(file_entry["name"].lower(), set()).add(system_id)
|
||||
for value in (*sha1s, *md5s):
|
||||
systems_of.setdefault(value, set()).add(system_id)
|
||||
for value in (*sha1s, *md5s, *sha256s):
|
||||
if "name" in file_entry:
|
||||
known["named"].setdefault(value, set()).add(file_entry["name"])
|
||||
_note_folder(folders_of, system_id, files, by_md5, sha1s, md5s,
|
||||
known["system_dirs"])
|
||||
|
||||
known["folders"] = _system_homes(systems_of, folders_of)
|
||||
known["system_folders"] = _single_maker_folders(folders_of)
|
||||
return known
|
||||
|
||||
|
||||
def _note_folder(
|
||||
folders_of: dict[str, dict[str, int]], system_id: str, files: dict,
|
||||
by_md5: dict, sha1s: list[str], md5s: list[str],
|
||||
dirs_of: dict[str, set[str]] | None = None,
|
||||
) -> None:
|
||||
"""Count the bios/<Manufacturer>/<Console>/ folder a held file sits in,
|
||||
and note the directory it sits in under *dirs_of*."""
|
||||
held = next((s for s in sha1s if s in files), None) or next(
|
||||
(by_md5[m] for m in md5s if m in by_md5), None
|
||||
)
|
||||
path = str(files.get(held, {}).get("path", ""))
|
||||
if dirs_of is not None and "/" in path and "/.variants/" not in path:
|
||||
dirs_of.setdefault(system_id, set()).add(path.rsplit("/", 1)[0] + "/")
|
||||
parts = path.split("/")
|
||||
if len(parts) >= 4 and parts[0] == "bios" and not parts[2].startswith("."):
|
||||
folder = "/".join(parts[:3]) + "/"
|
||||
counts = folders_of.setdefault(system_id, {})
|
||||
counts[folder] = counts.get(folder, 0) + 1
|
||||
|
||||
|
||||
def _single_maker_folders(folders_of: dict[str, dict[str, int]]) -> dict[str, set[str]]:
|
||||
"""System id to the folders the collection files it under, for a system
|
||||
filed under one manufacturer only, the rule _system_homes applies."""
|
||||
return {
|
||||
system_id: set(held_in) for system_id, held_in in folders_of.items()
|
||||
if len({folder.split("/")[1] for folder in held_in}) == 1
|
||||
}
|
||||
|
||||
|
||||
def _system_homes(
|
||||
systems_of: dict[str, set[str]], folders_of: dict[str, dict[str, int]]
|
||||
) -> dict[str, set[str]]:
|
||||
"""Hash value to the folders the collection files its systems under.
|
||||
|
||||
A system whose files sit under several manufacturers (arcade sets,
|
||||
computers sharing ROM chips) names no single home: only a system filed
|
||||
under one manufacturer says where a newcomer belongs.
|
||||
"""
|
||||
homes: dict[str, set[str]] = {}
|
||||
for value, systems in systems_of.items():
|
||||
folders: set[str] = set()
|
||||
for system_id in systems:
|
||||
held_in = folders_of.get(system_id, {})
|
||||
if len({folder.split("/")[1] for folder in held_in}) == 1:
|
||||
folders.update(held_in)
|
||||
if folders:
|
||||
homes[value] = folders
|
||||
return homes
|
||||
|
||||
|
||||
def load_profile_hashes(
|
||||
emulators_dir: str, db: dict | None = None
|
||||
) -> dict[str, dict[str, set[str]]]:
|
||||
"""Hash value to the emulators whose profile declares it, per algorithm.
|
||||
|
||||
A file only a core reads (a standalone emulator's firmware, a game
|
||||
engine's data) is in no platform list: the review called every such
|
||||
contribution "not referenced, needs manual review". With the database,
|
||||
`folders` maps each declared hash to the folders the collection files
|
||||
the entry's system under, as load_platform_hashes does: a contributor
|
||||
holding a file only a profile declares was told to pick a
|
||||
bios/Manufacturer/Console/ folder alone.
|
||||
"""
|
||||
known: dict[str, dict] = {
|
||||
"sha1": {}, "sha256": {}, "md5": {}, "crc32": {}, "sizes": {}, "folders": {},
|
||||
"named": {}, "name_systems": {}, "system_dirs": {}, "system_folders": {},
|
||||
}
|
||||
systems_of: dict[str, set[str]] = {}
|
||||
folders_of: dict[str, dict[str, int]] = {}
|
||||
files = (db or {}).get("files", {})
|
||||
indexes = (db or {}).get("indexes", {})
|
||||
by_md5, by_sha256 = indexes.get("by_md5", {}), indexes.get("by_sha256", {})
|
||||
for emulator, profile in load_emulator_profiles(emulators_dir).items():
|
||||
declared_systems = profile.get("systems") or []
|
||||
for entry in profile.get("files") or []:
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
names = [entry.get("name"), *(entry.get("aliases") or [])]
|
||||
_note_sizes(known["sizes"], [n for n in names if isinstance(n, str)], entry)
|
||||
# The resolver's reading: comma lists split (mesence's four
|
||||
# sha256), and a crc32 counts only where no stronger hash is
|
||||
# declared, bounded by the declared size (626 entries name a
|
||||
# file by crc32 alone, clk's Electron os.rom among them).
|
||||
hashes = {
|
||||
algo: parse_hash_list(entry.get(algo)) for algo in ("sha1", "sha256", "md5")
|
||||
}
|
||||
for algo, values in hashes.items():
|
||||
for value in values:
|
||||
known[algo].setdefault(value, set()).add(emulator)
|
||||
if isinstance(entry.get("name"), str):
|
||||
known["named"].setdefault(value, set()).add(entry["name"])
|
||||
# The entry's own system, or the profile's when it emulates one.
|
||||
system = entry.get("system") or (
|
||||
declared_systems[0] if len(declared_systems) == 1 else None
|
||||
)
|
||||
if isinstance(system, str):
|
||||
for value in (v for values in hashes.values() for v in values):
|
||||
systems_of.setdefault(value, set()).add(system)
|
||||
for n in names:
|
||||
if isinstance(n, str):
|
||||
known["name_systems"].setdefault(n.lower(), set()).add(system)
|
||||
sha1s = [*hashes["sha1"], *(
|
||||
by_sha256[v] for v in hashes["sha256"] if v in by_sha256
|
||||
)]
|
||||
_note_folder(folders_of, system, files, by_md5, sha1s, hashes["md5"],
|
||||
known["system_dirs"])
|
||||
if not any(entry.get(algo) for algo in ("sha1", "sha256", "md5")):
|
||||
size = entry.get("size")
|
||||
sizes = [int(v) for v in size] if isinstance(size, list) else (
|
||||
[int(size)] if size else [None]
|
||||
)
|
||||
for value in parse_hash_list(entry.get("crc32")):
|
||||
for accepted in sizes:
|
||||
key = (value.zfill(8), accepted)
|
||||
known["crc32"].setdefault(key, set()).add(emulator)
|
||||
known["folders"] = _system_homes(systems_of, folders_of)
|
||||
known["system_folders"] = _single_maker_folders(folders_of)
|
||||
return known
|
||||
|
||||
|
||||
def _archive_members(
|
||||
filepath: str, db: dict | None, platform_hashes: dict, profile_hashes: dict
|
||||
) -> list[str] | None:
|
||||
"""What each file of a ZIP is, None when filepath is not a ZIP.
|
||||
|
||||
Read only for an archive no list declares as a whole: MAME and FBNeo
|
||||
sets are ZIP BIOS files in their own right.
|
||||
"""
|
||||
if not zipfile.is_zipfile(filepath):
|
||||
return None
|
||||
held = (db or {}).get("files", {})
|
||||
described = []
|
||||
try:
|
||||
with zipfile.ZipFile(filepath) as archive:
|
||||
for info in archive.infolist():
|
||||
if info.is_dir():
|
||||
continue
|
||||
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()
|
||||
crc = f"{info.CRC:08x}"
|
||||
if sha1_hex in held:
|
||||
state = f"already held at `{held[sha1_hex]['path']}`"
|
||||
elif (
|
||||
sha1_hex in platform_hashes.get("sha1", set())
|
||||
or md5_hex in platform_hashes.get("md5", set())
|
||||
or (crc, info.file_size) in platform_hashes.get("crc32", set())
|
||||
or profile_hashes.get("sha1", {}).get(sha1_hex)
|
||||
or profile_hashes.get("md5", {}).get(md5_hex)
|
||||
or profile_hashes.get("crc32", {}).get((crc, info.file_size))
|
||||
):
|
||||
state = "declared and not held"
|
||||
else:
|
||||
state = "not recognised"
|
||||
described.append(f"`{info.filename}` {state}")
|
||||
except (zipfile.BadZipFile, RuntimeError, OSError) as exc:
|
||||
# An encrypted member raises RuntimeError.
|
||||
described.append(f"unreadable ({exc})")
|
||||
return described
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _Review:
|
||||
"""One file under review and the tables it is read against."""
|
||||
|
||||
filepath: str
|
||||
db: dict | None
|
||||
platform_hashes: dict
|
||||
profile_hashes: dict
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _Known:
|
||||
"""What declares the file: a platform's hash, its own check, a profile, its name."""
|
||||
|
||||
sha1: bool
|
||||
md5: bool
|
||||
other: bool
|
||||
accepted_by: set[str]
|
||||
by_profile: set[str] | None
|
||||
name: bool
|
||||
|
||||
@property
|
||||
def by_platform(self) -> bool:
|
||||
return bool(self.sha1 or self.md5 or self.other or self.accepted_by)
|
||||
|
||||
|
||||
def _path_problem(filepath: str) -> str:
|
||||
"""Why the path cannot be reviewed, empty when it can."""
|
||||
# Whoever opened the pull request chose this path, so its shape is
|
||||
# settled before anything is read. A symlink to an endless device would
|
||||
# otherwise be hashed until the job timed out, and a symlink out of the
|
||||
# checkout would be reported as though its target had been contributed.
|
||||
if os.path.islink(filepath):
|
||||
return "Symlinks are not allowed"
|
||||
if not os.path.exists(filepath):
|
||||
return (f"File not found: {filepath}. A path is read from the folder "
|
||||
"the command runs in, or pass --changed to check the files "
|
||||
"changed under bios/")
|
||||
if os.path.isdir(filepath):
|
||||
return "Folder with no file in it"
|
||||
if not os.path.isfile(filepath):
|
||||
return "Not a regular file: a device, a pipe or a socket"
|
||||
return ""
|
||||
|
||||
|
||||
def _check_size(result: ValidationResult) -> bool:
|
||||
"""The size checks, True when the file is too large for git."""
|
||||
too_large = result.size > MAX_FILE_SIZE
|
||||
if too_large:
|
||||
# GitHub refuses a pushed file over 100 MiB, so a pull request cannot
|
||||
# carry it. Only a maintainer can add an asset to the release.
|
||||
result.add_check(
|
||||
False,
|
||||
f"File too large for git ({result.size:,} > {MAX_FILE_SIZE:,} bytes). "
|
||||
"Leave it out of the pull request and open an issue, its hashes "
|
||||
f"filled in ({large_file_issue_url(result)}), saying where the file "
|
||||
"can be obtained: a maintainer adds it to the `large-files` release.",
|
||||
)
|
||||
elif result.size == 0:
|
||||
result.add_check(False, "File is empty (0 bytes)")
|
||||
else:
|
||||
result.add_check(True, f"File size within limits ({result.size:,} bytes)")
|
||||
return too_large
|
||||
|
||||
|
||||
def _relative_path(filepath: str) -> str:
|
||||
# Paths are compared as the database writes them, relative to the
|
||||
# checkout: an absolute path to the file itself read as its duplicate.
|
||||
if os.path.isabs(filepath):
|
||||
candidate = os.path.relpath(filepath, os.getcwd())
|
||||
if not candidate.startswith(".."):
|
||||
return candidate
|
||||
return filepath
|
||||
|
||||
|
||||
def _check_duplicate(result: ValidationResult, db: dict, relative: str) -> None:
|
||||
if result.sha1 not in db.get("files", {}):
|
||||
result.add_check(True, "Not a duplicate in database")
|
||||
return
|
||||
existing = db["files"][result.sha1]
|
||||
held_at = {
|
||||
os.path.normpath(p)
|
||||
for p in [existing.get("path"), *(existing.get("paths") or [])] if p
|
||||
}
|
||||
if os.path.normpath(relative) in held_at:
|
||||
# The file the database was built from, not a copy of it: a
|
||||
# PR that regenerated database.json was told each new file
|
||||
# duplicated itself.
|
||||
result.add_check(True, "Already in the database at this path")
|
||||
elif _in_nodedup_dir(relative):
|
||||
result.add_info(
|
||||
f"Identical to `{existing['path']}`, kept: this tree is read by path"
|
||||
)
|
||||
else:
|
||||
result.outcome = "held"
|
||||
result.add_warning(
|
||||
f"Already held at `{existing['path']}`: nothing to add, "
|
||||
"leave this copy out of the pull request"
|
||||
)
|
||||
|
||||
|
||||
def _identify(review: _Review, result: ValidationResult, sha256: str) -> _Known:
|
||||
platform_hashes, profile_hashes = review.platform_hashes, review.profile_hashes
|
||||
sha1_known = result.sha1 in platform_hashes.get("sha1", set())
|
||||
md5_known = result.md5 in platform_hashes.get("md5", set())
|
||||
other_known = (
|
||||
sha256 in platform_hashes.get("sha256", set())
|
||||
or (result.crc32, result.size) in platform_hashes.get("crc32", set())
|
||||
)
|
||||
accepted_by = (
|
||||
set() if sha1_known or md5_known or other_known
|
||||
else _platforms_accepting(review.filepath, result.filename, platform_hashes)
|
||||
)
|
||||
by_profile = (
|
||||
profile_hashes.get("sha1", {}).get(result.sha1)
|
||||
or profile_hashes.get("sha256", {}).get(sha256)
|
||||
or profile_hashes.get("md5", {}).get(result.md5)
|
||||
or profile_hashes.get("crc32", {}).get((result.crc32, result.size))
|
||||
or profile_hashes.get("crc32", {}).get((result.crc32, None))
|
||||
)
|
||||
return _Known(sha1_known, md5_known, other_known, accepted_by, by_profile,
|
||||
result.filename in platform_hashes.get("names", set()))
|
||||
|
||||
|
||||
def _check_executable(result: ValidationResult, sha256: str, profile_hashes: dict, ext: str) -> None:
|
||||
# An executable is refused unless an emulator profile pins its exact
|
||||
# bytes: PICO-8's pico8.exe and SDL2.dll are declared by sha1 from the
|
||||
# official archive. A weak hash (md5, crc32) never vouches for code.
|
||||
pinned = (
|
||||
profile_hashes.get("sha1", {}).get(result.sha1)
|
||||
or profile_hashes.get("sha256", {}).get(sha256)
|
||||
)
|
||||
if pinned:
|
||||
result.add_check(
|
||||
True,
|
||||
f"Executable whose bytes an emulator profile pins by sha1: {', '.join(sorted(pinned))}",
|
||||
)
|
||||
else:
|
||||
result.add_check(False, f"Blocked file extension: {ext}")
|
||||
|
||||
|
||||
def _check_known(review: _Review, result: ValidationResult, known: _Known, too_large: bool) -> list[str] | None:
|
||||
"""What declares the file, and the files of a ZIP no list declares
|
||||
whole, when the upload is one."""
|
||||
if known.sha1:
|
||||
result.add_check(True, "SHA1 matches known platform requirement")
|
||||
elif known.md5:
|
||||
result.add_check(True, "MD5 matches known platform requirement")
|
||||
elif known.other:
|
||||
result.add_check(True, "Hash matches known platform requirement")
|
||||
elif known.accepted_by:
|
||||
result.add_check(
|
||||
True, "Accepted by the platform's own check: " + ", ".join(sorted(known.accepted_by))
|
||||
)
|
||||
elif known.by_profile:
|
||||
names = sorted(known.by_profile)
|
||||
shown = ", ".join(names[:5]) + (f" and {len(names) - 5} more" if len(names) > 5 else "")
|
||||
result.add_check(True, f"Hash matches a file the emulator profile declares: {shown}")
|
||||
elif known.name:
|
||||
result.add_warning(
|
||||
"Filename matches a known requirement but hash differs - may be a variant"
|
||||
)
|
||||
else:
|
||||
result.outcome = "unknown"
|
||||
members = None if too_large else _archive_members(
|
||||
review.filepath, review.db, review.platform_hashes, review.profile_hashes
|
||||
)
|
||||
_report_unknown(result, members, too_large)
|
||||
return members
|
||||
return None
|
||||
|
||||
|
||||
def _report_unknown(result: ValidationResult, members: list[str] | None, too_large: bool) -> None:
|
||||
if members is not None:
|
||||
# A folder zipped to upload many files at once was hashed as one
|
||||
# file and its contributor asked where that dump came from.
|
||||
shown = "; ".join(members[:10]) + (
|
||||
f"; and {len(members) - 10} more" if len(members) > 10 else ""
|
||||
)
|
||||
result.add_warning(
|
||||
"A ZIP no platform list or emulator profile declares as a whole: "
|
||||
"add the files it holds, unzipped, not the archive. Inside: "
|
||||
+ (shown or "nothing")
|
||||
)
|
||||
else:
|
||||
result.add_warning(
|
||||
"Not recognised: no platform list or emulator profile declares this "
|
||||
f"file. Say in the {'issue' if too_large else 'pull request'} which "
|
||||
"emulator reads it, for which system, and where the dump comes from"
|
||||
)
|
||||
|
||||
|
||||
def _check_bios_placement(result: ValidationResult, normalized: str, expected: list[str]) -> None:
|
||||
parts = normalized.split(os.sep)
|
||||
here = f"bios/{parts[1]}/{parts[2]}/" if len(parts) >= 4 else ""
|
||||
makers = {folder.split("/")[1] for folder in expected}
|
||||
if expected and (len(parts) < 4 or parts[1] not in makers):
|
||||
# Depth alone passed a PS1 BIOS filed under bios/Nintendo/Game Boy/.
|
||||
result.add_warning(
|
||||
"Placed under " + (here or "the bios/ root") + ", while the other files of "
|
||||
"its system are in " + " or ".join(f"`{f}`" for f in expected)
|
||||
)
|
||||
elif len(parts) >= 4:
|
||||
result.add_check(True, f"Correct placement: bios/{parts[1]}/{parts[2]}/")
|
||||
else:
|
||||
result.add_warning("File should be in bios/Manufacturer/Console/ structure")
|
||||
|
||||
|
||||
def _suggest_destination(review: _Review, result: ValidationResult, values: tuple[str, ...],
|
||||
known: _Known) -> None:
|
||||
# The contributor asks before forking: say where it goes.
|
||||
spot = placement(
|
||||
values, result.filename, review.db, review.platform_hashes, review.profile_hashes,
|
||||
declared=bool(known.by_platform or known.by_profile),
|
||||
)
|
||||
result.destination = spot.destination(result.sha1)
|
||||
copies = f". --place copies it to `{result.destination}`" if result.destination else ""
|
||||
if spot.folders:
|
||||
result.add_warning(
|
||||
"File is not under bios/ yet. Its system's files are in "
|
||||
+ " or ".join(f"`{f}`" for f in spot.folders) + copies
|
||||
)
|
||||
elif spot.home:
|
||||
result.add_warning(
|
||||
f"File is not under bios/ yet. The collection keeps this name in `{spot.home}`"
|
||||
+ copies
|
||||
)
|
||||
else:
|
||||
result.add_warning(
|
||||
"File is not under bios/ yet. Place it in bios/Manufacturer/Console/"
|
||||
)
|
||||
|
||||
|
||||
def _check_declared_size(result: ValidationResult, platform_hashes: dict, profile_hashes: dict) -> bool:
|
||||
"""True when the size fits every declaration of the name."""
|
||||
declared = [
|
||||
*platform_hashes.get("sizes", {}).get(result.filename, []),
|
||||
*profile_hashes.get("sizes", {}).get(result.filename, []),
|
||||
]
|
||||
size_fits = not declared or any(lo <= result.size <= hi for lo, hi in declared)
|
||||
if not size_fits:
|
||||
expected = sorted({lo if lo == hi else f"{lo:,} to {hi:,}" for lo, hi in declared},
|
||||
key=str)
|
||||
shown = ", ".join(f"{v:,}" if isinstance(v, int) else v for v in expected)
|
||||
result.add_warning(
|
||||
f"Every declaration of {result.filename} expects {shown} bytes, "
|
||||
f"this file has {result.size:,}: truncated or another file"
|
||||
)
|
||||
return size_fits
|
||||
|
||||
|
||||
def _note_variant(review: _Review, result: ValidationResult, normalized: str) -> None:
|
||||
platform_hashes, profile_hashes = review.platform_hashes, review.profile_hashes
|
||||
owners = name_systems(result.filename, platform_hashes, profile_hashes)
|
||||
home = _variant_home(review.db, result.filename, "", set() if len(owners) > 1 else {
|
||||
d for table in (platform_hashes, profile_hashes) for s in owners
|
||||
for d in table.get("system_dirs", {}).get(s, ())
|
||||
} if owners else None)
|
||||
if not home and normalized.startswith("bios" + os.sep):
|
||||
# Beside the file, in the folder the contributor chose.
|
||||
home = os.path.dirname(normalized).replace(os.sep, "/") + "/"
|
||||
slot = f"{home}.variants/{result.filename}.{result.sha1[:8]}"
|
||||
if home and not normalized.startswith("bios" + os.sep):
|
||||
result.destination = slot
|
||||
if home:
|
||||
result.add_info(
|
||||
f"This may be a valid variant. If accepted, it will be placed in `{slot}`"
|
||||
)
|
||||
else:
|
||||
result.add_info("This may be a valid variant of a file the collection lacks")
|
||||
|
||||
|
||||
def validate_file(
|
||||
filepath: str,
|
||||
db: dict | None,
|
||||
platform_hashes: dict,
|
||||
profile_hashes: dict | None = None,
|
||||
) -> ValidationResult:
|
||||
"""Run all validation checks on a file."""
|
||||
result = ValidationResult(filepath)
|
||||
problem = _path_problem(filepath)
|
||||
if problem:
|
||||
result.add_check(False, problem)
|
||||
return result
|
||||
|
||||
result.size = os.path.getsize(filepath)
|
||||
hashes = compute_hashes(filepath)
|
||||
result.sha1 = hashes["sha1"]
|
||||
result.md5 = hashes["md5"]
|
||||
result.crc32 = hashes["crc32"]
|
||||
sha256 = hashes.get("sha256", "")
|
||||
|
||||
ext = os.path.splitext(filepath)[1].lower()
|
||||
too_large = _check_size(result)
|
||||
relative = _relative_path(filepath)
|
||||
if db:
|
||||
_check_duplicate(result, db, relative)
|
||||
|
||||
review = _Review(filepath, db, platform_hashes, profile_hashes or {})
|
||||
known = _identify(review, result, sha256)
|
||||
if ext in BLOCKED_EXTENSIONS:
|
||||
_check_executable(result, sha256, review.profile_hashes, ext)
|
||||
members = _check_known(review, result, known, too_large)
|
||||
|
||||
normalized = os.path.normpath(relative)
|
||||
values = (result.sha1, result.md5, sha256)
|
||||
if too_large:
|
||||
# The file stays out of the pull request, so no folder of bios/ is
|
||||
# the contributor's to choose: the release asset is placed by hand.
|
||||
pass
|
||||
elif normalized.startswith("bios" + os.sep):
|
||||
_check_bios_placement(result, normalized,
|
||||
expected_folders(values, platform_hashes, review.profile_hashes))
|
||||
elif result.outcome == "held" or members is not None:
|
||||
# Nothing to place: a copy, or an archive whose files go in unzipped.
|
||||
pass
|
||||
else:
|
||||
_suggest_destination(review, result, values, known)
|
||||
|
||||
size_fits = _check_declared_size(result, platform_hashes, review.profile_hashes)
|
||||
if known.name and not known.by_platform and not known.by_profile and size_fits:
|
||||
_note_variant(review, result, normalized)
|
||||
return result
|
||||
|
||||
|
||||
def _platforms_accepting(filepath: str, filename: str, platform_hashes: dict) -> set[str]:
|
||||
"""Platforms whose declaration of this name accepts the file by their
|
||||
own check, as verify and the pack builder read it.
|
||||
|
||||
Batocera pins a ROM inside the ZIP (zipped_file), Recalbox hashes a
|
||||
ZIP's members together, RomM takes any hash at the declared size: the
|
||||
container's sha1 or md5 is in none of those lists, and adam_fdc.zip,
|
||||
which Batocera accepts, was called an unknown variant.
|
||||
"""
|
||||
accepted: set[str] = set()
|
||||
for entry, mode, rule, platform in platform_hashes.get("entries", {}).get(
|
||||
filename.lower(), []
|
||||
):
|
||||
if platform in accepted:
|
||||
continue
|
||||
if entry.get("zipped_file"):
|
||||
ok = inner_rom_check(entry, filepath) == "ok"
|
||||
else:
|
||||
algorithm = digest_algorithm(mode)
|
||||
ok = bool(algorithm or rule) and frontend_digest_matches(
|
||||
entry, filepath, algorithm or "", rule
|
||||
)
|
||||
if ok:
|
||||
accepted.add(platform)
|
||||
return accepted
|
||||
|
||||
|
||||
def expected_folders(
|
||||
values: Iterable[str], platform_hashes: dict, profile_hashes: dict | None
|
||||
) -> list[str]:
|
||||
"""The bios/<Manufacturer>/<Console>/ folders the files of a hash's
|
||||
system are in, read from the first of *values* a table knows.
|
||||
|
||||
A platform's declaration first, then a profile's: most files the gaps
|
||||
page lists are declared by a profile alone.
|
||||
"""
|
||||
for table in (platform_hashes, profile_hashes or {}):
|
||||
for value in values:
|
||||
if value and value in table.get("folders", {}):
|
||||
return sorted(table["folders"][value])
|
||||
return []
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Placement:
|
||||
"""Where a file not under bios/ yet goes.
|
||||
|
||||
``folders`` are those of its system, ``home`` the folder the collection
|
||||
keeps its name in when its system names none. ``name`` is the name it
|
||||
is copied under, empty when the declarations do not settle one.
|
||||
``variant`` says a file of that name is already in the folder: a copy
|
||||
goes beside it in .variants/, never over it.
|
||||
"""
|
||||
|
||||
folders: tuple[str, ...] = ()
|
||||
home: str = ""
|
||||
name: str = ""
|
||||
variant: bool = False
|
||||
|
||||
@property
|
||||
def folder(self) -> str:
|
||||
return self.folders[0] if self.folders else self.home
|
||||
|
||||
def destination(self, sha1: str) -> str:
|
||||
if not self.name:
|
||||
return ""
|
||||
if self.variant:
|
||||
return f"{self.folder}.variants/{self.name}.{sha1[:8]}"
|
||||
return self.folder + self.name
|
||||
|
||||
|
||||
class PlacementOptions(TypedDict, total=False):
|
||||
"""How placement() tests a path, and the systems and directory a
|
||||
declaration names."""
|
||||
|
||||
exists: Callable[[str], bool]
|
||||
systems: Iterable[str]
|
||||
directory: str
|
||||
|
||||
|
||||
def placement(
|
||||
values: Iterable[str],
|
||||
filename: str,
|
||||
db: dict | None,
|
||||
platform_hashes: dict,
|
||||
profile_hashes: dict | None,
|
||||
*,
|
||||
declared: bool,
|
||||
**options: Unpack[PlacementOptions],
|
||||
) -> Placement:
|
||||
"""Where a file goes, by its hashes and its name.
|
||||
|
||||
The review and the site's wanted page both read this one rule, so the
|
||||
folder a contributor is sent to is the folder the review accepts.
|
||||
*declared* says a list or a profile declares one of the hashes: a file
|
||||
known by its name alone gets a slot only once its size fits.
|
||||
|
||||
Without a folder from its hashes, the folder comes from its system:
|
||||
*systems*, or every system that declares the name (several of them
|
||||
name no copy to sit beside). A file of that name held elsewhere is a
|
||||
homonym and says nothing: AmberELEC's MSX config.ini
|
||||
was sent beside msx.emu's ColecoVision config.ini. A declared
|
||||
*directory* (Machines/MSX2/ for one machine's config.ini) narrows it: a
|
||||
copy by name counts only in a directory of that tail, and a held
|
||||
directory of that tail is the folder.
|
||||
"""
|
||||
exists = options.get("exists", os.path.exists)
|
||||
systems = options.get("systems", ())
|
||||
directory = options.get("directory", "")
|
||||
values = [value for value in values if value]
|
||||
folders = expected_folders(values, platform_hashes, profile_hashes)
|
||||
if folders:
|
||||
names = {
|
||||
name
|
||||
for table in (platform_hashes, profile_hashes or {})
|
||||
for value in values
|
||||
for name in table.get("named", {}).get(value, ())
|
||||
}
|
||||
name = next(iter(names)) if len(names) == 1 else ""
|
||||
if len(folders) == 1 and name and "/" not in name:
|
||||
return Placement(tuple(folders), name=name, variant=exists(folders[0] + name))
|
||||
return Placement(tuple(folders))
|
||||
held = next((value for value in values if value in (db or {}).get("files", {})), "")
|
||||
owners = set(systems) or name_systems(filename, platform_hashes, profile_hashes)
|
||||
tables = (platform_hashes, profile_hashes or {})
|
||||
# A name several systems declare does not say which one a file is:
|
||||
# a copy held for one of them is no answer.
|
||||
dirs: set[str] = set() if not systems and len(owners) > 1 else {
|
||||
d for table in tables for s in owners for d in table.get("system_dirs", {}).get(s, ())
|
||||
}
|
||||
tail = _directory_tail(db, directory)
|
||||
within: set[str] | None = dirs
|
||||
if tail:
|
||||
within = {d for d in dirs if d.endswith("/" + tail) or d == tail} if owners else None
|
||||
home = _variant_home(db, filename, held, within if owners or tail else None)
|
||||
if declared and home and filename and "/" not in filename:
|
||||
# The folder holds a file of that name already.
|
||||
return Placement(home=home, name=filename, variant=True)
|
||||
if home:
|
||||
return Placement(home=home)
|
||||
if tail:
|
||||
return Placement(home=next(d for d in _held_directories(db) if d.endswith("/" + tail) or d == tail))
|
||||
homes = {f for table in tables for s in owners for f in table.get("system_folders", {}).get(s, ())}
|
||||
return Placement(tuple(sorted(homes)))
|
||||
|
||||
|
||||
_HELD_DIRECTORIES: dict[int, frozenset[str]] = {}
|
||||
|
||||
|
||||
def _held_directories(db: dict | None) -> frozenset[str]:
|
||||
"""Every directory a file of the database sits in, .variants/ aside."""
|
||||
key = id(db)
|
||||
if key not in _HELD_DIRECTORIES:
|
||||
_HELD_DIRECTORIES[key] = frozenset(
|
||||
path.rsplit("/", 1)[0] + "/"
|
||||
for record in (db or {}).get("files", {}).values()
|
||||
for path in [record.get("path"), *(record.get("paths") or [])]
|
||||
if path and "/" in path and "/.variants/" not in path
|
||||
)
|
||||
return _HELD_DIRECTORIES[key]
|
||||
|
||||
|
||||
def _directory_tail(db: dict | None, directory: str) -> str:
|
||||
"""The longest tail of a declared directory, two segments at least,
|
||||
that exactly one held directory ends with, empty when none does."""
|
||||
parts = [p for p in directory.strip("/").split("/") if p]
|
||||
held = _held_directories(db)
|
||||
for size in range(len(parts), 1, -1):
|
||||
tail = "/".join(parts[-size:]) + "/"
|
||||
found = [d for d in held if d.endswith("/" + tail) or d == tail]
|
||||
if len(found) == 1:
|
||||
return tail
|
||||
if found:
|
||||
return ""
|
||||
return ""
|
||||
|
||||
|
||||
def name_systems(filename: str, platform_hashes: dict, profile_hashes: dict | None) -> set[str]:
|
||||
"""The systems whose platform or profile entries declare this name."""
|
||||
return {
|
||||
system for table in (platform_hashes, profile_hashes or {})
|
||||
for system in table.get("name_systems", {}).get(filename.lower(), ())
|
||||
}
|
||||
|
||||
|
||||
def _variant_home(db: dict | None, name: str, held: str = "",
|
||||
within: set[str] | None = None) -> str:
|
||||
"""The folder the collection keeps a name in, for its variants beside it.
|
||||
|
||||
The folder of an identical copy comes first: adam_fdc.zip, held in
|
||||
bios/Coleco/Adam/, was sent to bios/Arcade/MAME/, the first folder
|
||||
the name index gave. A copy found by name alone counts only in a
|
||||
directory of *within*, those the file's own systems are held in, when
|
||||
the systems are known.
|
||||
"""
|
||||
files = (db or {}).get("files", {})
|
||||
by_name = (db or {}).get("indexes", {}).get("by_name", {}).get(name, [])
|
||||
copies = [(held, True)] if held in files else []
|
||||
for sha1, identical in copies + [(sha1, False) for sha1 in by_name]:
|
||||
path = str(files.get(sha1, {}).get("path", ""))
|
||||
folder = path.rsplit("/", 1)[0] + "/" if "/" in path else ""
|
||||
if not folder or "/.variants" in "/" + folder:
|
||||
continue
|
||||
if identical or within is None or folder in within:
|
||||
return folder
|
||||
return ""
|
||||
Reference in new issue
Block a user