diff --git a/scripts/packs/datatrees.py b/scripts/packs/datatrees.py index fc332cb58..508494e0b 100644 --- a/scripts/packs/datatrees.py +++ b/scripts/packs/datatrees.py @@ -247,6 +247,49 @@ def data_directory_members( yield src, full +def carried_trees( + profile: dict, standalone: bool, required_only: bool, data_registry: dict | None, +) -> Iterator[tuple[str, str | None, str]]: + """(registry key, cache folder or None when not cached, destination) of + each data tree an emulator pack carries for a profile. + + A tree only one build reads follows that build, like a file, and one + declared ``required: false`` stays out of a required-only pack. The + destination sits below the build's own folder (``pack_structure``). + """ + structure = profile.get("pack_structure") + prefix = structure.get("standalone" if standalone else "libretro", "") if structure else "" + for dd in deliverable(profile.get("data_directories", []), standalone): + if required_only and dd.get("required") is False: + continue + ref = dd.get("ref", "") + if not ref: + continue + cache = (data_registry or {}).get(ref, {}).get("local_cache", "") + dd_dest = dd.get("destination", "") + if prefix: + dd_dest = f"{prefix}/{dd_dest}" if dd_dest else prefix + yield ref, cache if cache and os.path.isdir(cache) else None, dd_dest + + +def claimed_tree_files( + local_cache: str, dd_dest: str, placed: Placed, +) -> Iterator[tuple[str, str]]: + """(source, destination) of each tree file an emulator pack writes. + + A path another file already holds is skipped, the others are taken in + `placed`. The cache's shared lock is held while the caller reads what + is yielded: a refresh swaps the tree by rename, and a walk half done + read the old tree and wrote files of the new one. + """ + with cache_lock(local_cache, shared=True): + for src, full in _tree_files(local_cache, dd_dest): + if placed.block(full): + continue + placed.take(full) + yield src, full + + def pack_data_tree( zf: zipfile.ZipFile | PackRecord, local_cache: str, @@ -256,26 +299,16 @@ def pack_data_tree( ) -> int: """Write one data directory into an emulator pack, under its shared lock. - A refresh swaps the tree by rename. Without the lock a walk half done - read the old tree and wrote files of the new one. ``packed_at`` records - each member, so a later profile asking for the same bytes there is - served rather than reported missing. + ``packed_at`` records each member, so a later profile asking for the + same bytes there is served rather than reported missing. """ added = 0 - with cache_lock(local_cache, shared=True): - for root, _dirs, filenames in os.walk(local_cache): - for fname in filenames: - src = os.path.join(root, fname) - rel = os.path.relpath(src, local_cache) - full = f"{dd_dest}/{rel}" if dd_dest else rel - if placed.block(full): - continue - placed.take(full) - # The installer writes a data tree member without the bit. - add_pack_member(zf, src, full, executable=False) - if packed_at is not None: - packed_at[full.lower()] = src - added += 1 + for src, full in claimed_tree_files(local_cache, dd_dest, placed): + # The installer writes a data tree member without the bit. + add_pack_member(zf, src, full, executable=False) + if packed_at is not None: + packed_at[full.lower()] = src + added += 1 return added diff --git a/scripts/packs/emulatorcheck.py b/scripts/packs/emulatorcheck.py index 020a70c80..4d501abb9 100644 --- a/scripts/packs/emulatorcheck.py +++ b/scripts/packs/emulatorcheck.py @@ -19,6 +19,7 @@ from model.profiles import ( ) from model.registry import load_data_dir_registry from packs.archive import Placed +from packs.datatrees import carried_trees, claimed_tree_files from packs.emulatorentries import EmulatorPackEntries, EmulatorSelection, emulator_pack_entries from packs.verdicts import ( RELEASE_ASSET_PENDING, @@ -32,7 +33,6 @@ from packs.verdicts import ( from resolve.validation import ( build_validation_index, build_ground_truth, - deliverable, validated_choice, with_archive_members, ) @@ -184,53 +184,24 @@ class _EmulatorPack: self.placed[dest.lower()] = (path, owner, dest) self.taken.take(dest) - def occupy_trees(self, emu_name: str, profile: dict) -> None: + def carry_trees(self, emu_name: str, profile: dict) -> None: """The profile's data trees follow its files in the pack, which writes them even when the system filter leaves it no file: bluemsx's Machines/ tree holds msx-emu's coleco.rom path in a - --system colecovision pack.""" - for dd in deliverable(profile.get("data_directories", []), self.standalone): - if self.required_only and dd.get("required") is False: - continue - cache = (self.data_registry or {}).get(dd.get("ref", ""), {}).get("local_cache", "") - if not cache or not os.path.isdir(cache): - continue - dd_dest = dd.get("destination", "") - structure = profile.get("pack_structure") - prefix = ( - structure.get("standalone" if self.standalone else "libretro", "") - if structure else "" - ) - if prefix: - dd_dest = f"{prefix}/{dd_dest}" if dd_dest else prefix - for root, _dirs, names in os.walk(cache): - for fname in names: - src = os.path.join(root, fname) - rel = os.path.relpath(src, cache).replace(os.sep, "/") - self.occupy(f"{dd_dest}/{rel}" if dd_dest else rel, src, emu_name) - - def note_data_dirs(self, profile: dict) -> None: - # Check data directories (only notice if not cached) - for dd in deliverable( - profile.get("data_directories", []), self.standalone + --system colecovision pack. A tree the cache lacks is noticed.""" + for ref, cache, dd_dest in carried_trees( + profile, self.standalone, self.required_only, self.data_registry, ): - if self.required_only and dd.get("required") is False: + if cache is None: + self.data_dir_notices.append(ref) continue - ref = dd.get("ref", "") - if not ref: - continue - if self.data_registry and ref in self.data_registry: - cache_path = self.data_registry[ref].get("local_cache", "") - if cache_path and os.path.isdir(cache_path): - continue # cached, no notice needed - self.data_dir_notices.append(ref) + for src, full in claimed_tree_files(cache, dd_dest, self.taken): + self.placed[full.lower()] = (src, emu_name, full) def verify_profile( self, emu_name: str, profile: dict, carried: EmulatorPackEntries, ) -> None: """Verify one profile's entries and add them to the report.""" - self.note_data_dirs(profile) - if not carried.carries_entries: self.details.append( { @@ -242,7 +213,7 @@ class _EmulatorPack: "ground_truth": [], } ) - self.occupy_trees(emu_name, profile) + self.carry_trees(emu_name, profile) return # Each destination's outcome, settled by set once the profile is @@ -251,7 +222,7 @@ class _EmulatorPack: placed_results = self._archive_results(emu_name, carried) self._place_loose(emu_name, self._loose_results(emu_name, carried), placed_results) self._settle(emu_name, profile, placed_results) - self.occupy_trees(emu_name, profile) + self.carry_trees(emu_name, profile) def _archive_results(self, emu_name: str, carried: EmulatorPackEntries) -> list[PlacedResult]: # Verify archives as units (e.g., neogeo.zip, aes.zip) diff --git a/scripts/packs/emulatorpack.py b/scripts/packs/emulatorpack.py index 7bf243f2a..2fd27294c 100644 --- a/scripts/packs/emulatorpack.py +++ b/scripts/packs/emulatorpack.py @@ -35,7 +35,7 @@ from packs.archive import ( write_generated_member, writing_pack, ) -from packs.datatrees import pack_data_tree +from packs.datatrees import carried_trees, pack_data_tree from packs.emulatorentries import EmulatorSelection, emulator_pack_entries from packs.extras import emulator_region_drops from packs.naming import ( @@ -45,7 +45,6 @@ from resolve import packfiles as packresolve from resolve.packfiles import download_external, resolve_file from resolve.validation import ( build_validation_index, - deliverable, validated_choice, with_archive_members, ) @@ -128,7 +127,6 @@ class _EmulatorPackBuild: self.record.files.append(dest) def pack_profile(self, emu_name: str, profile: dict) -> None: - pack_structure = profile.get("pack_structure") carried = emulator_pack_entries(emu_name, profile, EmulatorSelection( self.db, self.standalone, self.all_profiles, self.validation_index, self.zip_contents, self.data_registry, system_ids=self.system_ids, @@ -160,7 +158,7 @@ class _EmulatorPackBuild: for position, (_entries, _fills, report) in enumerate(outcomes): if report is not None and position not in excused: report() - self._pack_trees(profile, pack_structure) + self._pack_trees(profile) def _pack_archive(self, own_entry: dict, archive_name: str, archive_dest: str) -> _Outcome: local_path, status = resolve_file( @@ -336,32 +334,16 @@ class _EmulatorPackBuild: if os.path.exists(tmp_path): os.unlink(tmp_path) - def _pack_trees(self, profile: dict, pack_structure: dict | None) -> None: + def _pack_trees(self, profile: dict) -> None: # The trees after the files, as in a platform pack: a dump the # profile declares wins over the copy a tree bundles at the same # path (Dolphin's real dsp_rom.bin over Sys/GC's free one). - # A tree only one build reads follows that build, like a file. - required_only = self.required_only - for dd in deliverable( - profile.get("data_directories", []), self.standalone + for ref_key, local_cache, dd_dest in carried_trees( + profile, self.standalone, self.required_only, self.data_registry, ): - if required_only and dd.get("required") is False: - continue - ref_key = dd.get("ref", "") - if not ref_key or not self.data_registry or ref_key not in self.data_registry: - if ref_key: - self.data_dir_notices.append(ref_key) - continue - local_cache = self.data_registry[ref_key].get("local_cache", "") - if not local_cache or not os.path.isdir(local_cache): + if local_cache is None: self.data_dir_notices.append(ref_key) continue - dd_dest = dd.get("destination", "") - if pack_structure: - mode_key = "standalone" if self.standalone else "libretro" - prefix = pack_structure.get(mode_key, "") - if prefix: - dd_dest = f"{prefix}/{dd_dest}" if dd_dest else prefix self.data_files += pack_data_tree( self.zf, local_cache, dd_dest, self.placed, self.packed_at, )