refactor: read archive members in one place

core.ziptools.zip_members lists the name, size and crc32 of each member once per archive revision. The site lookup and cards, the gap report, the pack check, the romset importer, the recipe identification and build, provenance, derivations and the atom pool read it instead of opening each archive again.

Each reader also tested a .zip suffix and skipped the .variants/ copies (x.zip.1a2b3c4d): the site lookup listed no member of a copy, the gap report's member index never saw one, the pack check never compared a copy's members, and the pack builders copied a copy without its CRC test. They now use is_archive_path. The inner ROM index of the resolver reads copies too, and a copy answers only for a member no primary holds.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 10:30:40 +01:00
1 parent 331f244aac
commit 74fcdd2bc5
16 files changed
+213 -103

No files matched your search

+13 -5
View File
@@ -14,6 +14,7 @@ from typing import TYPE_CHECKING, TypedDict
from core.hashing import parse_hash_list, stream_hashes
from core.paths import BIOS_DIR, PLATFORMS_DIR, REPO_ROOT
from core.ziptools import is_archive_path, zip_members
from model.alternatives import set_keys, set_requirements, settle_sets
from model.database import get_mame_clone_map, sha1_at
from model.profiles import lists_files
@@ -158,10 +159,14 @@ def _zip_members(bios_root: str | Path) -> list[tuple[str, list[str]]]:
return _zip_members_cache[key]
found: list[tuple[str, list[str]]] = []
root = Path(bios_root)
for zpath in root.rglob("*.zip") if root.is_dir() else ():
# .variants/ copies (x.zip.1a2b3c4d) are archives too.
archives = sorted(
p for p in (root.rglob("*") if root.is_dir() else ())
if is_archive_path(p.name) and p.is_file()
)
for zpath in archives:
try:
with zipfile.ZipFile(zpath) as zf:
members = [m for m in zf.namelist() if not m.endswith("/")]
members = [member.name for member in zip_members(zpath)]
except (zipfile.BadZipFile, OSError) as exc:
print(f" WARNING: unreadable archive {zpath}: {exc}", file=sys.stderr)
continue
@@ -345,10 +350,13 @@ def _content_matches(location: tuple[str, str], declared: dict[str, set[str]]) -
"""Whether the file or member at *location* carries a declared hash."""
path, member = location
try:
if member and set(declared) == {"crc32"}:
return any(
held.name == member and held.crc32 in declared["crc32"]
for held in zip_members(path)
)
if member:
with zipfile.ZipFile(path) as zf:
if set(declared) == {"crc32"}:
return f"{zf.getinfo(member).CRC:08x}" in declared["crc32"]
data = zf.read(member)
else:
data = Path(path).read_bytes()
+2 -2
View File
@@ -15,7 +15,7 @@ from typing import TYPE_CHECKING, TypedDict
from core.options import reject_unknown
from core.paths import REPO_ROOT
from core.ziptools import build_zip_contents_index
from core.ziptools import build_zip_contents_index, is_archive_path
from model.alternatives import settle_sets, slot_set_keys
from model.profiles import (
ProfileSelectionError,
@@ -119,7 +119,7 @@ class _EmulatorPackBuild:
def _add(self, local_path: str, dest: str, fe: dict | None) -> None:
"""Write a loose file or an archive at dest and count it."""
if fe is not None and local_path.endswith(".zip"):
if fe is not None and is_archive_path(local_path):
add_zip_to_pack(local_path, dest, self.zf, fe)
else:
add_pack_member(self.zf, local_path, dest)
+8 -9
View File
@@ -12,7 +12,7 @@ from typing import TYPE_CHECKING, TypedDict
from core.artifacts import build_timestamp
from packs.paths import MEDIA_MARKER, has_path_conflict
from core.ziptools import build_zip_contents_index, is_archive_path
from core.ziptools import build_zip_contents_index, is_archive_path, zip_members
from core.ziptools import safe_member_parts
from resolve.nativemode import digest_algorithm, platform_mode
from resolve.validation import (
@@ -106,10 +106,6 @@ def _settle_untracked(
return "untracked", file_name
def _member_crcs(archive: zipfile.ZipFile) -> dict[str, int]:
return {i.filename: i.CRC for i in archive.infolist() if not i.is_dir()}
def _same_members(pack: zipfile.ZipFile, name: str, source: str) -> bool:
"""Whether a ZIP inside the pack holds the members of a ZIP on disk.
@@ -118,9 +114,12 @@ def _same_members(pack: zipfile.ZipFile, name: str, source: str) -> bool:
each carried their own copy.
"""
try:
with zipfile.ZipFile(io.BytesIO(pack.read(name))) as packed, \
zipfile.ZipFile(source) as held:
return _member_crcs(packed) == _member_crcs(held)
held = {member.name: member.crc32 for member in zip_members(source)}
with zipfile.ZipFile(io.BytesIO(pack.read(name))) as packed:
return held == {
info.filename: format(info.CRC, "08x")
for info in packed.infolist() if not info.is_dir()
}
except (zipfile.BadZipFile, OSError):
return False
@@ -622,7 +621,7 @@ def _same_content(zf: zipfile.ZipFile, member: str, local: str) -> bool:
with open(local, "rb") as handle:
if zf.read(member) == handle.read():
return True
return local.lower().endswith(".zip") and _same_members(zf, member, local)
return is_archive_path(local) and _same_members(zf, member, local)
class PlatformCheckOptions(TypedDict, total=False):
+4 -3
View File
@@ -15,6 +15,7 @@ from model.registry import load_platform_registry
from core.hashing import compute_hashes
from core.options import reject_unknown
from core.paths import EMULATORS_DIR, declared_destination, sanitize_pack_path
from core.ziptools import is_archive_path
from model.alternatives import core_file_spares
from model.declarations import OUTER_ROOTS, declared_root
from model.platforms import load_platform_config
@@ -778,9 +779,9 @@ class _PlatformPackBuild:
extract = file_entry.get("extract", False)
flat_dest = self._flat(full_dest)
if extract and local_path.endswith(".zip"):
if extract and is_archive_path(local_path):
extract_zip_to_archive(local_path, flat_dest, self.zf)
elif local_path.endswith(".zip"):
elif is_archive_path(local_path):
add_zip_to_pack(local_path, flat_dest, self.zf, file_entry)
else:
add_pack_member(self.zf, local_path, flat_dest)
@@ -876,7 +877,7 @@ class _PlatformPackBuild:
within = plan.opts.within
if within is not None and not same_bytes(local_path, within.get(flat_dest)):
return
if local_path.endswith(".zip"):
if is_archive_path(local_path):
add_zip_to_pack(local_path, flat_dest, self.zf, fe)
else:
add_pack_member(self.zf, local_path, flat_dest)