diff --git a/scripts/collection/duplicates.py b/scripts/collection/duplicates.py index 856e9938a..eeadb9ace 100644 --- a/scripts/collection/duplicates.py +++ b/scripts/collection/duplicates.py @@ -7,6 +7,7 @@ from collections import defaultdict from pathlib import Path from collection.scan import primary_rank +from core.collectionfiles import collection_files from core.hashing import compute_hashes from model.nodedup import in_nodedup_dir @@ -18,7 +19,10 @@ def _is_mame_dir(path: str) -> bool: def scan_duplicates(bios_dir: str) -> dict[str, list[str]]: - """Files grouped by SHA1, excluding no-dedup directories. + """Collection files grouped by SHA1, excluding no-dedup directories. + + The walk is the database scan's: a copy the index never sees (under + `.cache/`, a hidden file) is no candidate to keep or to remove. Two equal files have the same size, so only files that share a size are read, and for the sha1 alone: hashing the whole tree with every digest @@ -26,12 +30,12 @@ def scan_duplicates(bios_dir: str) -> dict[str, list[str]]: size is in no group of two and is left out. """ by_size: dict[int, list[str]] = defaultdict(list) - for root, _dirs, files in os.walk(bios_dir): - for name in files: - path = os.path.join(root, name) - if in_nodedup_dir(path): - continue - by_size[os.path.getsize(path)].append(path) + root = Path(bios_dir) + for found in collection_files(root): + path = os.path.join(bios_dir, found.relative_to(root)) + if in_nodedup_dir(path): + continue + by_size[os.path.getsize(path)].append(path) sha1_to_paths: dict[str, list[str]] = defaultdict(list) for paths in by_size.values(): diff --git a/scripts/collection/scan.py b/scripts/collection/scan.py index fa73c749f..ab5e62cb4 100644 --- a/scripts/collection/scan.py +++ b/scripts/collection/scan.py @@ -1,15 +1,14 @@ -"""The walk of bios/: which files count, their digests, and which copy of a content is primary.""" +"""The walk of bios/: each collection file's digests, and which copy of a content is primary.""" from __future__ import annotations import sys from pathlib import Path +from core.collectionfiles import collection_files from core.hashing import compute_hashes from resolve.localfile import variant_base_name -SKIP_PATTERNS = {".git", ".github", "__pycache__", ".cache", ".DS_Store", "desktop.ini"} - # Every digest database.json publishes per file, in the order compute_hashes # returns them. The schema requires all five, so a cache entry missing any of # them cannot serve a database entry, and the order is fixed here so a warm @@ -17,18 +16,6 @@ SKIP_PATTERNS = {".git", ".github", "__pycache__", ".cache", ".DS_Store", "deskt CACHED_HASHES = ("sha1", "md5", "sha256", "crc32", "adler32") -def should_skip(path: Path) -> bool: - """Whether a path stays out of the collection. - - Tooling directories and hidden files do. A hidden directory inside the - collection does not: `.variants/` holds alternate dumps, and an engine - tree can carry one of its own (C-Dogs SDL reads `data/.wolf3d/`). - """ - if any(part in SKIP_PATTERNS for part in path.parts): - return True - return path.name.startswith(".") - - def _canonical_name(filepath: Path) -> str: """Get canonical filename, stripping .variants/ hash suffix.""" if ".variants" in filepath.parts: @@ -135,12 +122,7 @@ def scan_bios_dir(bios_dir: Path, cache: dict, force: bool) -> tuple[dict, dict, new_cache = {} root = Path(bios_dir.name) - for filepath in sorted(bios_dir.rglob("*")): - if not filepath.is_file(): - continue - if should_skip(filepath.relative_to(bios_dir)): - continue - + for filepath in collection_files(bios_dir): rel_path = str(filepath.relative_to(bios_dir.parent)) stat = filepath.stat() mtime = stat.st_mtime diff --git a/scripts/core/collectionfiles.py b/scripts/core/collectionfiles.py new file mode 100644 index 000000000..3b995e8a3 --- /dev/null +++ b/scripts/core/collectionfiles.py @@ -0,0 +1,32 @@ +"""Which files under bios/ belong to the collection.""" + +from __future__ import annotations + +from collections.abc import Iterator +from pathlib import Path + +SKIP_PATTERNS = {".git", ".github", "__pycache__", ".cache", ".DS_Store", "desktop.ini"} + + +def should_skip(path: Path) -> bool: + """Whether a path stays out of the collection. + + Tooling directories and hidden files do. A hidden directory inside the + collection does not: `.variants/` holds alternate dumps, and an engine + tree can carry one of its own (C-Dogs SDL reads `data/.wolf3d/`). + """ + if any(part in SKIP_PATTERNS for part in path.parts): + return True + return path.name.startswith(".") + + +def collection_files(bios_dir: Path) -> Iterator[Path]: + """Every file of the collection under *bios_dir*, in walk order. + + The database scan and the dedup both read this walk. The dedup walked + every file and ranked a copy under `bios/.cache/` with the others: it + could keep that copy, which no index names, and delete the indexed one. + """ + for path in sorted(bios_dir.rglob("*")): + if path.is_file() and not should_skip(path.relative_to(bios_dir)): + yield path diff --git a/tests/sources.py b/tests/sources.py index 1830cd1e6..2affc0678 100644 --- a/tests/sources.py +++ b/tests/sources.py @@ -49,7 +49,8 @@ COMMANDS = { "retired_files.py": ("model/retired.py", "collection/retiredhistory.py"), "region_audit.py": ("resolve/regionaudit.py",), "dedup.py": ( - "model/nodedup.py", "collection/dedup.py", "collection/duplicates.py", + "model/nodedup.py", "collection/dedup.py", + "collection/duplicates.py", "core/collectionfiles.py", "collection/clonemap.py", "model/database.py", ), "release_record.py": ("model/releaserecord.py", "release/record.py"), @@ -134,7 +135,8 @@ COMMANDS = { "schemas/romfolders.py", "schemas/games.py", "schemas/run.py", ), "generate_db.py": ( - "collection/scan.py", "collection/hashcache.py", "collection/releaseassets.py", + "collection/scan.py", "core/collectionfiles.py", "collection/hashcache.py", + "collection/releaseassets.py", "collection/declared.py", "collection/aliases.py", "collection/indexes.py", "collection/record.py", "collection/run.py", ), diff --git a/tests/test_directory_entries.py b/tests/test_directory_entries.py index 04f2d606d..dd2024d8f 100644 --- a/tests/test_directory_entries.py +++ b/tests/test_directory_entries.py @@ -28,7 +28,7 @@ except ImportError: from model.profiles import load_emulator_profiles, emulator_profiles_cache from resolve.directories import directory_members from collection.indexes import build_indexes -from collection.scan import should_skip +from core.collectionfiles import should_skip from packs import emulatorpack from packs import manifest as packs_manifest from packs import platformpack