mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-23 10:07:48 -06:00
refactor: read dat packs through one helper
This commit is contained in:
1 parent
e16caf57b4
commit
8d58a9edfd
5 files changed
+76
-82
No files matched your search
@@ -16,15 +16,13 @@ from __future__ import annotations
|
||||
import argparse
|
||||
import sys
|
||||
import zipfile
|
||||
from collections.abc import Iterator
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from model.dumpcatalog import write_provenance_snapshot
|
||||
from .listxml import iter_dat_documents
|
||||
from .logiqx_parser import LogiqxDat, parse_logiqx
|
||||
|
||||
MAX_MEMBER_SIZE = 100 * 1024 * 1024
|
||||
|
||||
SOURCES = ("no-intro", "tosec")
|
||||
|
||||
|
||||
@@ -40,33 +38,12 @@ def _bios_entries(source: str, dat: LogiqxDat) -> list[dict]:
|
||||
return dat.provenance_entries(roms)
|
||||
|
||||
|
||||
def _iter_dat_contents(pack: Path) -> Iterator[tuple[str, bytes]]:
|
||||
"""Yield (member name, content bytes) for DAT files in a pack.
|
||||
|
||||
Accepts a ZIP archive or a directory of extracted DATs.
|
||||
"""
|
||||
if pack.is_dir():
|
||||
for path in sorted(pack.rglob("*")):
|
||||
if path.suffix.lower() in (".dat", ".xml") and path.is_file():
|
||||
yield str(path), path.read_bytes()
|
||||
return
|
||||
with zipfile.ZipFile(pack) as zf:
|
||||
for info in sorted(zf.infolist(), key=lambda i: i.filename):
|
||||
suffix = Path(info.filename).suffix.lower()
|
||||
if info.is_dir() or suffix not in (".dat", ".xml"):
|
||||
continue
|
||||
if info.file_size > MAX_MEMBER_SIZE:
|
||||
print(f" Skipping {info.filename}: exceeds {MAX_MEMBER_SIZE} bytes")
|
||||
continue
|
||||
yield info.filename, zf.read(info)
|
||||
|
||||
|
||||
def import_pack(source: str, pack: Path) -> tuple[dict, list[dict], int]:
|
||||
"""Import BIOS entries from a pack. Returns (dats, entries, skipped)."""
|
||||
dats = {}
|
||||
entries = []
|
||||
skipped = 0
|
||||
for _member, content in _iter_dat_contents(pack):
|
||||
for _member, content in iter_dat_documents(pack):
|
||||
try:
|
||||
dat = parse_logiqx(content)
|
||||
except (ValueError, SyntaxError):
|
||||
|
||||
@@ -29,8 +29,6 @@ import gzip
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
import zipfile
|
||||
from collections.abc import Iterator
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
@@ -44,6 +42,7 @@ from model.region import REGIONS, WORLD
|
||||
from model.region import NOINTRO
|
||||
|
||||
from .base_scraper import download, github_json
|
||||
from .listxml import iter_dat_documents
|
||||
|
||||
SOURCES = ("no-intro", "redump")
|
||||
CACHE_DIR = Path(".cache") / "game-dats"
|
||||
@@ -341,24 +340,9 @@ def fetch_redump(code: str, cache_dir: Path, offline: bool, refresh: bool) -> tu
|
||||
# Reading packs ----------------------------------------------------------
|
||||
|
||||
|
||||
def _iter_dats(pack: Path) -> Iterator[tuple[str, bytes]]:
|
||||
"""(member name, bytes) of every .dat in a ZIP, a directory or a file."""
|
||||
if pack.is_dir():
|
||||
for path in sorted(pack.rglob("*.dat")):
|
||||
yield path.name, path.read_bytes()
|
||||
return
|
||||
if zipfile.is_zipfile(pack):
|
||||
with zipfile.ZipFile(pack) as archive:
|
||||
for info in sorted(archive.infolist(), key=lambda i: i.filename):
|
||||
if info.filename.lower().endswith(".dat"):
|
||||
yield Path(info.filename).name, archive.read(info)
|
||||
return
|
||||
yield pack.name, pack.read_bytes()
|
||||
|
||||
|
||||
def import_nointro(pack: Path, meta: dict) -> list[dict]:
|
||||
docs = []
|
||||
for member, data in _iter_dats(pack):
|
||||
for member, data in iter_dat_documents(pack, (".dat",)):
|
||||
match = _NOINTRO_MEMBER.match(member)
|
||||
name = match["name"] if match else None
|
||||
if name not in NOINTRO_DATS:
|
||||
@@ -382,7 +366,7 @@ def import_nointro(pack: Path, meta: dict) -> list[dict]:
|
||||
def import_redump(pack: Path, meta: dict) -> list[dict]:
|
||||
docs = []
|
||||
by_name = {name: (code, system) for code, (name, system, _) in REDUMP_DATS.items()}
|
||||
for member, data in _iter_dats(pack):
|
||||
for member, data in iter_dat_documents(pack, (".dat",)):
|
||||
dat_name, version, games = parse_dat(data)
|
||||
if dat_name not in by_name:
|
||||
continue
|
||||
|
||||
@@ -9,6 +9,7 @@ from __future__ import annotations
|
||||
|
||||
import html
|
||||
import re
|
||||
import sys
|
||||
import zipfile
|
||||
from collections.abc import Callable, Collection, Iterator
|
||||
from contextlib import AbstractContextManager, contextmanager
|
||||
@@ -101,6 +102,42 @@ def rom_members(
|
||||
return members
|
||||
|
||||
|
||||
# A member past this is refused aloud rather than read into memory.
|
||||
MAX_DAT_MEMBER = 200 * 1024 * 1024
|
||||
|
||||
|
||||
def iter_dat_documents(
|
||||
pack: Path,
|
||||
suffixes: tuple[str, ...] = (".dat", ".xml"),
|
||||
max_member: int = MAX_DAT_MEMBER,
|
||||
) -> Iterator[tuple[str, bytes]]:
|
||||
"""(base name, bytes) of every DAT in a ZIP, a directory or a lone file.
|
||||
|
||||
A suffix matches in any case. A member past `max_member` is skipped and
|
||||
said on stderr: a DAT left out silently ended a run on "Unchanged" with
|
||||
a version missing.
|
||||
"""
|
||||
if pack.is_dir():
|
||||
for path in sorted(pack.rglob("*")):
|
||||
if path.suffix.lower() in suffixes and path.is_file():
|
||||
yield path.name, path.read_bytes()
|
||||
return
|
||||
if not zipfile.is_zipfile(pack):
|
||||
yield pack.name, pack.read_bytes()
|
||||
return
|
||||
with zipfile.ZipFile(pack) as archive:
|
||||
for info in sorted(archive.infolist(), key=lambda i: i.filename):
|
||||
if info.is_dir() or Path(info.filename).suffix.lower() not in suffixes:
|
||||
continue
|
||||
if info.file_size > max_member:
|
||||
print(
|
||||
f" Skipping {info.filename}: {info.file_size} bytes exceeds {max_member}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
continue
|
||||
yield Path(info.filename).name, archive.read(info)
|
||||
|
||||
|
||||
def xml_members(archive: zipfile.ZipFile) -> list[str]:
|
||||
return [n for n in archive.namelist() if n.lower().endswith(".xml")]
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ import json
|
||||
import sys
|
||||
import urllib.parse
|
||||
import zipfile
|
||||
from collections.abc import Callable, Iterator
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
@@ -49,11 +49,16 @@ from model.database import load_database
|
||||
from model.dumpcatalog import build_snapshot, load_provenance_snapshots, write_snapshot
|
||||
from resolve.localfile import variant_base_name
|
||||
from .base_scraper import download, fetch_github_latest_version, github_json
|
||||
from .listxml import Opener, iter_elements, open_listxml, rom_members, xml_members
|
||||
from .listxml import (
|
||||
Opener,
|
||||
iter_dat_documents,
|
||||
iter_elements,
|
||||
open_listxml,
|
||||
rom_members,
|
||||
xml_members,
|
||||
)
|
||||
from .logiqx_parser import LogiqxDat, parse_logiqx
|
||||
|
||||
MAX_MEMBER_SIZE = 200 * 1024 * 1024
|
||||
|
||||
SOURCES = ("mame", "fbneo")
|
||||
DEFAULT_OUTPUT = "recipes/{source}.json"
|
||||
|
||||
@@ -383,34 +388,6 @@ def merge_snapshot(
|
||||
return write_snapshot(output, snapshot)
|
||||
|
||||
|
||||
def _iter_dat_contents(pack: Path) -> Iterator[tuple[str, bytes]]:
|
||||
"""Yield (member name, content bytes) for DAT files in *pack*."""
|
||||
if pack.is_dir():
|
||||
for path in sorted(pack.rglob("*")):
|
||||
if path.suffix.lower() in (".dat", ".xml") and path.is_file():
|
||||
yield path.name, path.read_bytes()
|
||||
return
|
||||
if pack.suffix.lower() == ".zip":
|
||||
with zipfile.ZipFile(pack) as archive:
|
||||
for info in sorted(archive.infolist(), key=lambda i: i.filename):
|
||||
if info.is_dir():
|
||||
continue
|
||||
if Path(info.filename).suffix.lower() not in (".dat", ".xml"):
|
||||
continue
|
||||
if info.file_size > MAX_MEMBER_SIZE:
|
||||
# Said out loud: a DAT left out silently ended the run
|
||||
# on "Unchanged" with a version missing.
|
||||
print(
|
||||
f" Skipping {info.filename}: {info.file_size} bytes "
|
||||
f"exceeds {MAX_MEMBER_SIZE}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
continue
|
||||
yield info.filename, archive.read(info)
|
||||
return
|
||||
yield pack.name, pack.read_bytes()
|
||||
|
||||
|
||||
MAME_LISTXML_URL = (
|
||||
"https://github.com/mamedev/mame/releases/download/{tag}/{tag}lx.zip"
|
||||
)
|
||||
@@ -593,7 +570,7 @@ def _dat_recipes(
|
||||
dats: dict[str, str] = {}
|
||||
entries: list[dict] = []
|
||||
skipped = 0
|
||||
for member, content in _iter_dat_contents(pack):
|
||||
for member, content in iter_dat_documents(pack):
|
||||
try:
|
||||
dat = parse_logiqx(content)
|
||||
except (ValueError, SyntaxError) as exc:
|
||||
|
||||
Reference in new issue
Block a user