refactor: step 5 reads counts both tools record

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 19:50:42 +01:00
1 parent cf7e13808f
commit 9814bb1234
21 files changed
+428 -432

No files matched your search

+94 -97
View File
@@ -2,108 +2,107 @@
from __future__ import annotations
import json
from pathlib import Path
from core.paths import PLATFORMS_DIR
from orchestrate import compare
from packs.archive import is_pack_note
from orchestrate.labels import STEP_LABELS, step_title
from orchestrate.reports import (
match_key,
parse_pack_counts,
parse_pack_exclusions,
parse_pack_extras,
parse_pack_paths,
parse_verify_counts,
parse_verify_extras,
)
from packs.archive import is_pack_note
from packs.packcounts import read_pack_counts
def read_records(destinations_file: str, counts_file: str) -> tuple[dict, list[dict]] | None:
"""verify's records by registry id and the build's full packs, or None
when either file cannot be read (the step that writes it failed)."""
try:
verify = json.loads(Path(destinations_file).read_text())
packs = read_pack_counts(counts_file)
except (OSError, ValueError) as exc:
print(f" unreadable counts ({exc})")
return None
if not isinstance(verify, dict):
print(f" unreadable counts ({destinations_file} holds no record per platform)")
return None
return verify, packs
def _platform_agrees(record: dict, pack: dict) -> bool:
"""One platform's verify record against the pack built for it, printed."""
label, ok = record["label"], True
if record["core_files"] != pack["core_files"]:
# The cores' share: the base check passed RomM while verify
# counted 412 core files and game data and the pack held 60.
print(
f" {label}: MISMATCH core files verify "
f"{record['core_files']} != pack {pack['core_files']}"
)
ok = False
v_ok, v_total, p_ok, p_total = record["ok"], record["total"], pack["ok"], pack["total"]
if v_total != p_total:
print(f" {label}: MISMATCH total verify {v_total} != pack {p_total}")
return False
if p_ok == v_ok:
print(f" {label}: verify {v_ok}/{v_total} == pack {p_ok}/{p_total} OK")
return ok
# Both ways are a disagreement. A pack exclusion never covers a file
# verify counts OK: an existence platform ships a file whose hash
# disagrees, and a hash platform's verify reports it untested, so the
# counts can only differ by a defect.
print(
f" {label}: MISMATCH pack {p_ok}/{p_total} OK "
f"({pack['unsafe_excluded']} unsafe excluded) != verify {v_ok}/{v_total}"
)
return False
def compare_counts(verify: dict, packs: list[dict], released: set[str] | None = None) -> bool:
"""Each platform's verify record against the full pack built for it.
Both sides are keyed by registry id: *verify* as verify.py --destinations
writes it, *packs* as generate_pack.py --counts does. *released* holds
the registry ids of the platforms with a release pack. verify judges
every platform, the build only those: a platform outside it has no pack
to compare, which is no failure.
"""
all_ok = True
if not verify or not packs:
# Nothing read is nothing compared: a build that recorded no pack
# would otherwise pass the check without a single platform in it.
print(f" parsed {len(verify)} verify and {len(packs)} pack results: nothing to compare")
all_ok = False
pack_of = {platform: pack for pack in packs for platform in pack["platforms"]}
for pack in sorted(packs, key=lambda p: p["label"]):
if not any(platform in verify for platform in pack["platforms"]):
print(f" {pack['label']}: pack with no verify result")
all_ok = False
for platform, record in sorted(verify.items(), key=lambda item: (item[1]["label"], item[0])):
if platform in pack_of:
all_ok = _platform_agrees(record, pack_of[platform]) and all_ok
elif released is not None and platform not in released:
print(f" {record['label']}: {record['ok']}/{record['total']} "
"(no release pack, served by the installer)")
else:
print(f" {record['label']}: MISSING pack: verify judged "
f"{record['ok']}/{record['total']} but the build wrote no pack for it")
all_ok = False
return all_ok
def check_consistency(
verify_output: str, pack_output: str, released: set[str] | None = None,
destinations_file: str, counts_file: str, released: set[str] | None = None,
) -> bool:
"""Verify that check counts match between verify and pack for each platform.
*released* holds the identities of the platforms with a release pack
(release_pack_keys). verify judges every platform, the build only those:
a platform outside it has no pack to compare, which is no failure.
"""
v = parse_verify_counts(verify_output)
p = parse_pack_counts(pack_output)
excluded = parse_pack_exclusions(pack_output)
verify_extras = parse_verify_extras(verify_output)
pack_extras = parse_pack_extras(pack_output)
"""Verify that check counts match between verify and pack for each platform."""
print(f"\n--- {step_title(STEP_LABELS['consistency'])} ---")
all_ok = True
if not v or not p:
# Nothing parsed is nothing compared: a changed output format would
# otherwise pass the check without a single platform in it.
print(f" parsed {len(v)} verify and {len(p)} pack results: nothing to compare")
all_ok = False
for p_label in sorted(p):
if not any(match_key(p_label) & match_key(v_label) for v_label in v):
print(f" {p_label}: pack with no verify result")
all_ok = False
for v_label, (v_ok, v_total) in sorted(v.items()):
# Match by normalized name overlap. Platform display labels and
# registry IDs legitimately differ in punctuation and spacing
# (notably ``MiSTer FPGA`` vs ``misterfpga``).
p_match = None
v_names = match_key(v_label)
for p_label in p:
if v_names & match_key(p_label):
p_match = p_label
break
if (
p_match
and p_match in pack_extras
and v_label in verify_extras
and verify_extras[v_label] != pack_extras[p_match]
):
# The cores' share: the base check passed RomM while verify
# counted 412 core files and game data and the pack held 60.
print(
f" {v_label}: MISMATCH core files verify "
f"{verify_extras[v_label]} != pack {pack_extras[p_match]}"
)
all_ok = False
if p_match:
p_ok, p_total = p[p_match]
p_excluded = excluded.get(p_match, 0)
if v_total != p_total:
print(f" {v_label}: MISMATCH total verify {v_total} != pack {p_total}")
all_ok = False
elif p_ok == v_ok:
print(
f" {v_label}: verify {v_ok}/{v_total} == pack {p_ok}/{p_total} OK"
)
else:
# Both ways are a disagreement. A pack exclusion never covers
# a file verify counts OK: an existence platform ships a file
# whose hash disagrees, and a hash platform's verify reports
# it untested, so the counts can only differ by a defect.
print(
f" {v_label}: MISMATCH pack {p_ok}/{p_total} OK "
f"({p_excluded} unsafe excluded) != verify {v_ok}/{v_total}"
)
all_ok = False
elif released is not None and not (v_names & released):
print(f" {v_label}: {v_ok}/{v_total} (no release pack, served by the installer)")
else:
print(f" {v_label}: MISSING pack: verify judged {v_ok}/{v_total} "
"but the build wrote no pack for it")
all_ok = False
records = read_records(destinations_file, counts_file)
all_ok = records is not None and compare_counts(*records, released)
status = "OK" if all_ok else "FAILED"
print(f"--- {step_title(STEP_LABELS['consistency'])}, counts: {status} ---")
return all_ok
def check_pack_destinations(
destinations_file: str, pack_output: str, platforms_dir: str = PLATFORMS_DIR,
destinations_file: str, counts_file: str, platforms_dir: str = PLATFORMS_DIR,
) -> bool:
"""Compare the paths verify credits with the members of each archive.
@@ -112,22 +111,20 @@ def check_pack_destinations(
set aside when its path and size are the cached file's and verify does
not credit the path.
"""
import json
import zipfile
print(f"\n--- {step_title(STEP_LABELS['consistency'])}, destinations ---")
try:
credited_by = json.loads(Path(destinations_file).read_text())
except (OSError, ValueError) as exc:
print(f" no destinations from verify ({exc})")
records = read_records(destinations_file, counts_file)
if records is None:
return False
paths = parse_pack_paths(pack_output)
if not paths:
print(" no archive named in the pack output")
credited_by, packs = records
if not packs:
print(" no archive in the pack counts")
return False
all_ok = True
for label, zip_path in sorted(paths.items()):
names = [n for n in credited_by if match_key(n) & match_key(label)]
for pack in sorted(packs, key=lambda p: p["label"]):
label = pack["label"]
names = [platform for platform in pack["platforms"] if platform in credited_by]
if not names:
print(f" {label}: no verify destinations")
all_ok = False
@@ -135,7 +132,7 @@ def check_pack_destinations(
entry = credited_by[names[0]]
credited = set(entry["pack_destinations"])
trees = compare.tree_members(entry["platform"], platforms_dir)
with zipfile.ZipFile(zip_path) as zf:
with zipfile.ZipFile(pack["archive"]) as zf:
written = {
info.filename for info in zf.infolist()
if not info.is_dir()