refactor: reuse the shared helpers left copied

Copies of helpers the first pass created, replaced by the helper:
- source_yaml_file for our YAML sources (generate_db read them in the
  locale's encoding, a syntax error named no file);
- data_dir_registry_at in registry_bundles, platform_registry_at (new,
  beside load_platform_registry) in the manifest;
- DATA_DIRS_FILE for the registry path five modules spelled;
- load_optional_database, sha1_at, VERSIONS_FILE, installer_module;
- DerivedView where a cache was keyed by id() of a dict;
- count_text and series where a plural or a list was written by hand.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 10:31:29 +01:00
1 parent 32a4ca1fa1
commit 036a6ebf29
35 files changed
+141 -170

No files matched your search

+7 -8
View File
@@ -2,10 +2,12 @@
from __future__ import annotations
from core.memo import DerivedView
from resolve.validation import read_by_build
_directory_members_cache: dict[int, tuple[dict, dict[str, dict[str, str]]]] = {}
# The members found under each tail, kept per by_path_suffix index.
_directory_members_cache: DerivedView[dict[str, dict[str, str]]] = DerivedView(lambda _index: {})
def directory_members(db: dict, directory: str) -> dict[str, str]:
@@ -24,12 +26,9 @@ def directory_members(db: dict, directory: str) -> dict[str, str]:
if not tail:
return {}
by_path_suffix = db.get("indexes", {}).get("by_path_suffix", {})
cached = _directory_members_cache.get(id(by_path_suffix))
if cached is None or cached[0] is not by_path_suffix:
cached = (by_path_suffix, {})
_directory_members_cache[id(by_path_suffix)] = cached
if tail in cached[1]:
return cached[1][tail]
found = _directory_members_cache(by_path_suffix)
if tail in found:
return found[tail]
prefix = f"{tail}/"
infix = f"/{prefix}"
@@ -54,7 +53,7 @@ def directory_members(db: dict, directory: str) -> dict[str, str]:
if one_tree and one_content
else {}
)
cached[1][tail] = members
found[tail] = members
return members
+11 -9
View File
@@ -16,6 +16,7 @@ except ImportError:
yaml = None
from core.hashing import compute_hashes, parse_hash_list, stream_hashes
from core.memo import DerivedView
from model.nodedup import in_nodedup_dir
from model.platforms import load_platform_config
from model.profiles import load_emulator_profiles
@@ -846,19 +847,20 @@ def placement(
return Placement(tuple(sorted(homes)))
_HELD_DIRECTORIES: dict[int, frozenset[str]] = {}
def _directories_of(files: dict) -> frozenset[str]:
return frozenset(
path.rsplit("/", 1)[0] + "/"
for record in files.values()
if "/" in (path := record.get("path") or "") and "/.variants/" not in path
)
_held_view = DerivedView(_directories_of)
def _held_directories(db: dict | None) -> frozenset[str]:
"""Every directory a file of the database sits in, .variants/ aside."""
key = id(db)
if key not in _HELD_DIRECTORIES:
_HELD_DIRECTORIES[key] = frozenset(
path.rsplit("/", 1)[0] + "/"
for record in (db or {}).get("files", {}).values()
if "/" in (path := record.get("path") or "") and "/.variants/" not in path
)
return _HELD_DIRECTORIES[key]
return _held_view((db or {}).get("files", {}))
def _directory_tail(db: dict | None, directory: str) -> str: