fix: read a database record by its path field

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:01:46 +01:00
1 parent db9a153832
commit 2308039452
5 files changed
+16 -33

No files matched your search

+4 -10
View File
@@ -38,10 +38,10 @@ def _entry_for_file(candidates: list[dict], held: dict) -> dict:
if held.get(key)
):
return fe
held_paths = [str(held.get("path", ""))] + [str(p) for p in held.get("paths") or []]
held_path = str(held.get("path", "")).lower()
for fe in candidates:
tail = str(fe.get("path") or fe.get("destination") or "").lower()
if tail and any(path.lower().endswith("/" + tail) for path in held_paths):
if tail and held_path.endswith("/" + tail):
return fe
return candidates[-1]
@@ -151,15 +151,11 @@ def _md5_pack_name(
def _md5_destination(entry: dict, layout: _Md5Layout, opts: _Md5Settings) -> str:
"""Where a held file goes in a hash-selected pack."""
name = entry.get("name", "")
aliases = entry.get("aliases") or []
paths = entry.get("paths") or []
dest = name
matched_fe = None
for lookup_name in [name] + aliases:
if lookup_name.lower() in layout.index:
matched_fe = _entry_for_file(layout.index[lookup_name.lower()], entry)
break
if name.lower() in layout.index:
matched_fe = _entry_for_file(layout.index[name.lower()], entry)
if matched_fe:
if opts.emulator_name:
@@ -168,8 +164,6 @@ def _md5_destination(entry: dict, layout: _Md5Layout, opts: _Md5Settings) -> str
dest = resolve_destination(matched_fe, opts.standalone)
else:
dest = matched_fe.get("destination", matched_fe.get("name", name))
elif paths:
dest = paths[0]
if layout.base_dest and not dest.startswith(layout.base_dest):
return f"{layout.base_dest}/{dest}"
+4 -11
View File
@@ -294,24 +294,17 @@ def lookup_hashes(
entry = files_db[sha1]
name = entry.get("name", "?")
md5 = entry.get("md5", "?")
paths = entry.get("paths") or []
aliases = entry.get("aliases") or []
path = entry.get("path", "")
print(f"\n{hash_type.upper()}: {hash_val}")
print(f" SHA1: {sha1}")
print(f" MD5: {md5}")
print(f" Name: {name}")
if paths:
print(f" Path: {paths[0]}")
if aliases:
print(f" Aliases: {aliases}")
if path:
print(f" Path: {path}")
# Check if file exists in repo (by path or by resolve_local_file)
in_repo = False
if paths:
primary = os.path.join(bios_dir, paths[0])
if os.path.exists(primary):
in_repo = True
in_repo = bool(path) and os.path.exists(path)
if not in_repo:
try:
fe_check = {"name": name, "sha1": sha1, "md5": md5}
+3 -7
View File
@@ -458,11 +458,8 @@ def _check_duplicate(result: ValidationResult, db: dict, relative: str) -> None:
result.add_check(True, "Not a duplicate in database")
return
existing = db["files"][result.sha1]
held_at = {
os.path.normpath(p)
for p in [existing.get("path"), *(existing.get("paths") or [])] if p
}
if os.path.normpath(relative) in held_at:
held_at = existing.get("path")
if held_at and os.path.normpath(relative) == os.path.normpath(held_at):
# The file the database was built from, not a copy of it: a
# PR that regenerated database.json was told each new file
# duplicated itself.
@@ -859,8 +856,7 @@ def _held_directories(db: dict | None) -> frozenset[str]:
_HELD_DIRECTORIES[key] = frozenset(
path.rsplit("/", 1)[0] + "/"
for record in (db or {}).get("files", {}).values()
for path in [record.get("path"), *(record.get("paths") or [])]
if path and "/" in path and "/.variants/" not in path
if "/" in (path := record.get("path") or "") and "/.variants/" not in path
)
return _HELD_DIRECTORIES[key]
+1 -2
View File
@@ -336,8 +336,7 @@ class Placer:
self.platform_hashes = platform_hashes
self.profile_hashes = profile_hashes
self.paths = {
path for record in db.get("files", {}).values()
for path in [record.get("path"), *(record.get("paths") or [])] if path
record["path"] for record in db.get("files", {}).values() if record.get("path")
}
def place(self, hashes: dict[str, list[str]], name: str, systems: Iterable[str] = (),
+4 -3
View File
@@ -2675,8 +2675,7 @@ class TestE2E(unittest.TestCase):
"md5": "md5abc",
"sha1": "sha1abc",
"sha256": "sha256abc",
"paths": ["Mfr/Console/test.bin"],
"aliases": ["alt.bin"],
"path": "bios/Mfr/Console/test.bin",
},
},
"indexes": {
@@ -2690,7 +2689,9 @@ class TestE2E(unittest.TestCase):
output = buf.getvalue()
self.assertIn("test.bin", output)
self.assertIn("sha1abc", output)
self.assertIn("alt.bin", output)
# The record's path, the one field the schema gives it; a read of
# "paths" printed nothing.
self.assertIn("Path: bios/Mfr/Console/test.bin", output)
self.assertEqual(missing, 0)
def test_141_lookup_hashes_not_found(self):