mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-12-01 13:58:55 -06:00
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:
1 parent
331f244aac
commit
74fcdd2bc5
16 files changed
+213
-103
No files matched your search
@@ -3,14 +3,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
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 core.ziptools import is_archive_path
|
||||
from core.ziptools import is_archive_path, zip_members
|
||||
from romrecipes.members import member_sha1, member_opener
|
||||
|
||||
|
||||
@@ -74,12 +73,9 @@ class BuildResult:
|
||||
def _members_match(built: Path, members: list[dict]) -> str:
|
||||
"""Empty when *built* holds exactly *members*, else what differs."""
|
||||
expected = sorted((m["name"], m["size"], m["crc32"], m["sha1"]) for m in members)
|
||||
with zipfile.ZipFile(built) as archive:
|
||||
names = [info.filename for info in archive.infolist() if not info.is_dir()]
|
||||
sizes = {info.filename: info.file_size for info in archive.infolist()}
|
||||
crcs = {info.filename: f"{info.CRC:08x}" for info in archive.infolist()}
|
||||
held = sorted(
|
||||
(name, sizes[name], crcs[name], member_sha1(built, name)) for name in names
|
||||
(member.name, member.size, member.crc32, member_sha1(built, member.name))
|
||||
for member in zip_members(built)
|
||||
)
|
||||
if held == expected:
|
||||
return ""
|
||||
|
||||
@@ -9,6 +9,7 @@ from collections import defaultdict
|
||||
from pathlib import Path
|
||||
|
||||
from core.torrentzip import is_torrentzip
|
||||
from core.ziptools import zip_members
|
||||
from romrecipes.pins import container_entries
|
||||
from romrecipes.pool import AtomPool
|
||||
from romrecipes.romsets import Recipe
|
||||
@@ -39,11 +40,7 @@ def identify_archive(
|
||||
def _member_list(path: Path) -> list[tuple[str, str]] | None:
|
||||
"""Sorted (name, crc32) of an archive's members, from its directory."""
|
||||
try:
|
||||
with zipfile.ZipFile(path) as archive:
|
||||
return sorted(
|
||||
(info.filename, format(info.CRC, "08x"))
|
||||
for info in archive.infolist() if not info.is_dir()
|
||||
)
|
||||
return sorted((member.name, member.crc32) for member in zip_members(path))
|
||||
except (zipfile.BadZipFile, OSError) as exc:
|
||||
print(f" UNREADABLE: {path}: {exc}", file=sys.stderr)
|
||||
return None
|
||||
@@ -84,10 +81,10 @@ def _is_archive(path: Path) -> bool:
|
||||
ROM, not an archive. Recipes only describe archives.
|
||||
"""
|
||||
try:
|
||||
with zipfile.ZipFile(path):
|
||||
return True
|
||||
zip_members(path)
|
||||
except (OSError, zipfile.BadZipFile):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _local_paths(archive: str, db: dict) -> list[Path]:
|
||||
|
||||
@@ -10,7 +10,7 @@ from pathlib import Path
|
||||
|
||||
from core.paths import REPO_ROOT
|
||||
from core.torrentzip import build_torrentzip
|
||||
from core.ziptools import is_archive_path
|
||||
from core.ziptools import is_archive_path, zip_members
|
||||
from romrecipes.romsets import Recipe
|
||||
|
||||
|
||||
@@ -42,12 +42,8 @@ class AtomPool:
|
||||
archives = (p for p in bios_dir.rglob("*") if is_archive_path(p.name) and p.is_file())
|
||||
for archive in sorted(archives, key=lambda p: (".variants" in p.parts, p)):
|
||||
try:
|
||||
with zipfile.ZipFile(archive) as handle:
|
||||
for info in handle.infolist():
|
||||
if not info.is_dir():
|
||||
self._index.setdefault(
|
||||
f"{info.CRC:08x}", (archive, info.filename)
|
||||
)
|
||||
for member in zip_members(archive):
|
||||
self._index.setdefault(member.crc32, (archive, member.name))
|
||||
except (OSError, zipfile.BadZipFile) as exc:
|
||||
self.unreadable.append(f"{archive}: {exc}")
|
||||
# Loose ROMs, by the crc32 the database already computed: nmk004.zip
|
||||
|
||||
Reference in new issue
Block a user