mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-26 19:38:52 -06:00
refactor: campaign rules and editor into probes
This commit is contained in:
1 parent
fdb7b6f912
commit
853a36c79f
6 files changed
+8
-6
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Real-condition probes: the campaign, the pack and region probes, and what they share."""
|
||||
@@ -0,0 +1,164 @@
|
||||
"""Write a settled field into one entry of a profile, and nothing else.
|
||||
|
||||
The profile is edited as text, so its comments, order and quoting stay as
|
||||
the maintainer wrote them. Every edit is checked by parsing the result: the
|
||||
edited entry must equal the old one with the new fields, and every other
|
||||
part of the profile must parse to what it did before. An edit that cannot
|
||||
hold both is refused (ProfileEditError), never written.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
ITEM = re.compile(r"^(?P<indent>\s*)- ")
|
||||
KEY = re.compile(r"^(?P<indent>\s*)(?P<key>[A-Za-z_][A-Za-z0-9_]*):(?P<rest>.*)$")
|
||||
# Where a new key goes: after the first of these the entry has, else last.
|
||||
ANCHORS = {
|
||||
"required": ("name",),
|
||||
"hle_fallback": ("required", "name"),
|
||||
"path": ("name",),
|
||||
"aliases": ("path", "name"),
|
||||
"validation": ("sha1", "md5", "crc32", "size", "name"),
|
||||
"on_mismatch": ("validation", "sha1", "md5", "crc32", "size", "name"),
|
||||
"found_by": ("path", "name"),
|
||||
}
|
||||
|
||||
|
||||
class ProfileEditError(ValueError):
|
||||
"""An edit the profile's layout does not allow to make safely."""
|
||||
|
||||
|
||||
def scalar(value: object) -> str:
|
||||
if isinstance(value, bool):
|
||||
return "true" if value else "false"
|
||||
if value is None:
|
||||
return "null"
|
||||
if isinstance(value, (int, float)):
|
||||
return str(value)
|
||||
if isinstance(value, str):
|
||||
return json.dumps(value, ensure_ascii=False)
|
||||
if isinstance(value, list):
|
||||
return "[" + ", ".join(scalar(v) for v in value) + "]"
|
||||
raise ProfileEditError(f"no scalar form for {value!r}")
|
||||
|
||||
|
||||
def item_spans(lines: list[str]) -> list[tuple[int, int, int]]:
|
||||
"""(first line, end line, item indent) of each item of the top-level files: list."""
|
||||
try:
|
||||
start = next(i for i, line in enumerate(lines) if line.rstrip() == "files:")
|
||||
except StopIteration as exc:
|
||||
raise ProfileEditError("no top-level files: list") from exc
|
||||
spans: list[tuple[int, int, int]] = []
|
||||
indent: int | None = None
|
||||
current: int | None = None
|
||||
end = len(lines)
|
||||
for i in range(start + 1, len(lines)):
|
||||
line = lines[i]
|
||||
stripped = line.strip()
|
||||
if not stripped or stripped.startswith("#"):
|
||||
continue
|
||||
width = len(line) - len(line.lstrip(" "))
|
||||
if width == 0 and not line.startswith("-"):
|
||||
end = i
|
||||
break
|
||||
match = ITEM.match(line)
|
||||
if match and (indent is None or width == indent):
|
||||
indent = width
|
||||
if current is not None:
|
||||
spans.append((current, i, indent))
|
||||
current = i
|
||||
if current is not None:
|
||||
spans.append((current, end, indent or 0))
|
||||
# An item ends at its last line of content: the comments and blank lines
|
||||
# before the next item belong to that item.
|
||||
trimmed = []
|
||||
for first, stop, width in spans:
|
||||
last = stop
|
||||
while last > first + 1 and (not lines[last - 1].strip() or lines[last - 1].strip().startswith("#")):
|
||||
last -= 1
|
||||
trimmed.append((first, last, width))
|
||||
return trimmed
|
||||
|
||||
|
||||
def key_lines(lines: list[str], first: int, stop: int, key_indent: int) -> dict[str, tuple[int, int]]:
|
||||
"""key -> (line, end line) for each key of one item, its continuation lines included."""
|
||||
keys: dict[str, tuple[int, int]] = {}
|
||||
order: list[tuple[str, int]] = []
|
||||
for i in range(first, stop):
|
||||
# The item's first line carries the dash where its first key's indent is.
|
||||
line = " " * key_indent + lines[i][key_indent:] if i == first else lines[i]
|
||||
match = KEY.match(line)
|
||||
if match and len(match["indent"]) == key_indent:
|
||||
order.append((match["key"], i))
|
||||
for position, (key, start) in enumerate(order):
|
||||
end = order[position + 1][1] if position + 1 < len(order) else stop
|
||||
while end > start + 1 and (not lines[end - 1].strip() or lines[end - 1].strip().startswith("#")):
|
||||
end -= 1
|
||||
keys[key] = (start, end)
|
||||
return keys
|
||||
|
||||
|
||||
def _render(key: str, value: object, indent: int, block: bool = False) -> list[str]:
|
||||
"""One key as lines. A list keeps the block style it was written in."""
|
||||
pad = " " * indent
|
||||
if isinstance(value, dict):
|
||||
return [f"{pad}{key}:"] + [f"{pad} {k}: {scalar(v)}" for k, v in value.items()]
|
||||
if block and isinstance(value, list):
|
||||
return [f"{pad}{key}:"] + [f"{pad} - {scalar(v)}" for v in value]
|
||||
return [f"{pad}{key}: {scalar(value)}"]
|
||||
|
||||
|
||||
def set_fields(text: str, index: int, updates: dict[str, object]) -> str:
|
||||
"""The profile with these keys of files[index] set, the rest untouched."""
|
||||
before = yaml.safe_load(text)
|
||||
entries = before.get("files") or []
|
||||
if not 0 <= index < len(entries):
|
||||
raise ProfileEditError(f"no files[{index}]")
|
||||
lines = text.splitlines()
|
||||
spans = item_spans(lines)
|
||||
if len(spans) != len(entries):
|
||||
raise ProfileEditError(f"{len(spans)} items read as text, {len(entries)} parsed")
|
||||
first, stop, item_indent = spans[index]
|
||||
key_indent = item_indent + 2
|
||||
for key, value in updates.items():
|
||||
keys = key_lines(lines, first, stop, key_indent)
|
||||
if key in keys:
|
||||
start, end = keys[key]
|
||||
rendered = _render(key, value, key_indent, block=lines[start].rstrip().endswith(":"))
|
||||
if start == first:
|
||||
raise ProfileEditError(f"files[{index}]: {key} is the item's first key")
|
||||
lines[start:end] = rendered
|
||||
stop += len(rendered) - (end - start)
|
||||
continue
|
||||
rendered = _render(key, value, key_indent)
|
||||
anchor = next((keys[a][1] for a in ANCHORS.get(key, ()) if a in keys), None)
|
||||
at = anchor if anchor is not None else stop
|
||||
lines[at:at] = rendered
|
||||
stop += len(rendered)
|
||||
out = "\n".join(lines) + ("\n" if text.endswith("\n") else "")
|
||||
after = yaml.safe_load(out)
|
||||
expected = {**entries[index], **updates}
|
||||
if after.get("files", [None] * (index + 1))[index] != expected:
|
||||
raise ProfileEditError(f"files[{index}]: the edit does not parse to the intended entry")
|
||||
rest_before = {k: v for k, v in before.items() if k != "files"}
|
||||
rest_after = {k: v for k, v in after.items() if k != "files"}
|
||||
others_before = [e for i, e in enumerate(entries) if i != index]
|
||||
others_after = [e for i, e in enumerate(after.get("files") or []) if i != index]
|
||||
if rest_before != rest_after or others_before != others_after:
|
||||
raise ProfileEditError(f"files[{index}]: the edit changes another part of the profile")
|
||||
return out
|
||||
|
||||
|
||||
def write_fields(path: Path, index: int, updates: dict[str, object]) -> bool:
|
||||
"""Apply the edit to the file. False when it changes nothing."""
|
||||
text = path.read_text(encoding="utf-8")
|
||||
out = set_fields(text, index, updates)
|
||||
if out == text:
|
||||
return False
|
||||
path.write_text(out, encoding="utf-8")
|
||||
return True
|
||||
@@ -0,0 +1,312 @@
|
||||
"""The campaign's fixed rule table: which verdicts settle which profile field.
|
||||
|
||||
Each rule is a pure function of verdicts read from core probe records (the
|
||||
boot oracle of core_probe.py: boots, bios_error, crash, hang, unknown) and
|
||||
a profile entry. It returns verdicts on profile fields. One with `apply` set
|
||||
is settled by the table and written into the profile with the record ids as
|
||||
`evidence:`. Any other goes to the review queue (measurements/findings.jsonl).
|
||||
|
||||
Three runs are compared for a file, all on the same content: `none` with no
|
||||
BIOS at all, `present` with the file alone, and wrong files staged under its
|
||||
name (zeros of its size, its first half, another region's dump).
|
||||
|
||||
runs field settled
|
||||
none boots, the file changes the run hle_fallback true
|
||||
none fails, present boots required true
|
||||
hle_fallback false
|
||||
zeros refused, present boots validation a content check
|
||||
zeros used, present boots validation no content check
|
||||
half refused, zeros used validation size
|
||||
half used validation no size check
|
||||
opens the staged file under another path path that path
|
||||
opens the staged file under another name aliases name added
|
||||
a staged file the content should use, never - flagged only
|
||||
read
|
||||
copies of one file that boot and fail - queued
|
||||
|
||||
"Fails" is bios_error, crash or hang. A wrong file is refused when the
|
||||
core reports a BIOS error, when its log says it ran its built-in BIOS
|
||||
(bios_used builtin), or when it boots without the BIOS screens the right
|
||||
file shows. It is used when the log names the file it loaded. It is used when it
|
||||
turns the run into a hang or a crash that the run without any file does not
|
||||
show. Anything else settles nothing. A check the entry declares with
|
||||
`on_mismatch: warn` is one the code only logs: a wrong file that runs
|
||||
agrees with it. Of validation, the table writes only a
|
||||
missing `size`. A contradicted content check goes to review: which hash a
|
||||
core computes is read in its code.
|
||||
|
||||
Standard library only.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Iterable, Sequence
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
BOOTS = "boots"
|
||||
BIOS_ERROR = "bios_error"
|
||||
CRASH = "crash"
|
||||
HANG = "hang"
|
||||
UNKNOWN = "unknown"
|
||||
FAILS = (BIOS_ERROR, CRASH, HANG)
|
||||
REFUSED = "refused"
|
||||
USED = "used"
|
||||
CONTENT_CHECKS = ("md5", "sha1", "sha256", "crc32", "adler32", "signature")
|
||||
|
||||
# A field an entry leaves out reads as this value.
|
||||
FIELD_DEFAULTS: dict[str, object] = {"hle_fallback": False, "path": None, "aliases": [],
|
||||
"validation": []}
|
||||
SETTLED_FIELDS = ("required", "hle_fallback", "path", "aliases", "validation")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Run:
|
||||
"""The verdict of one variant of a record: what a rule reads."""
|
||||
|
||||
verdict: str
|
||||
record_id: str
|
||||
label: str
|
||||
stages: tuple[str, ...] = ()
|
||||
bios_used: str = "unknown"
|
||||
|
||||
@property
|
||||
def shows_bios(self) -> bool:
|
||||
return "bios" in self.stages
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Verdict:
|
||||
rule: str
|
||||
field: str
|
||||
value: object
|
||||
statement: str
|
||||
evidence: tuple[str, ...]
|
||||
apply: bool = False
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Opened:
|
||||
"""Where a run found the staged file opened."""
|
||||
|
||||
dest: str
|
||||
record_id: str
|
||||
extra: dict = field(default_factory=dict)
|
||||
|
||||
|
||||
def variant(record: dict, label: str) -> dict:
|
||||
for item in record.get("variants") or ():
|
||||
if item.get("label") == label:
|
||||
return item
|
||||
raise KeyError(f"record {record.get('id')}: no variant {label!r}")
|
||||
|
||||
|
||||
def run_of(record: dict, label: str) -> Run:
|
||||
"""A variant's verdict as a rule reads it. A record from before the
|
||||
oracle has none, and reads as unknown."""
|
||||
item = variant(record, label)
|
||||
evidence = item.get("verdict_evidence") or {}
|
||||
return Run(item.get("verdict", UNKNOWN), record["id"], label, tuple(evidence.get("stages") or ()),
|
||||
item.get("bios_used", "unknown"))
|
||||
|
||||
|
||||
def ids(*runs: Run) -> tuple[str, ...]:
|
||||
return tuple(dict.fromkeys(run.record_id for run in runs))
|
||||
|
||||
|
||||
def current(entry: dict, name: str) -> object:
|
||||
if name in entry:
|
||||
return entry[name]
|
||||
return FIELD_DEFAULTS.get(name)
|
||||
|
||||
|
||||
def agrees(entry: dict, verdict: Verdict) -> bool:
|
||||
"""True when the profile already says what the run showed."""
|
||||
if verdict.field == "aliases":
|
||||
shown = verdict.value if isinstance(verdict.value, (list, tuple)) else ()
|
||||
return set(shown) <= set(entry.get("aliases") or ())
|
||||
if verdict.field == "validation":
|
||||
return verdict.rule == "validation_agrees"
|
||||
return current(entry, verdict.field) == verdict.value
|
||||
|
||||
|
||||
def read(none: Run, *runs: Run) -> bool:
|
||||
"""True when the file changes the run: alone or as a wrong file, one
|
||||
variant ends otherwise than the run with no BIOS, or reaches other
|
||||
stages. A core can open a file without using it for this content (a
|
||||
scan that hashes every file of the folder)."""
|
||||
return any(run.verdict != none.verdict or run.stages != none.stages for run in runs if run)
|
||||
|
||||
|
||||
def rule_need(none: Run, present: Run, entry: dict, used: bool = True) -> list[Verdict]:
|
||||
"""No BIOS at all against the file alone: required and hle_fallback. A
|
||||
file the content never uses settles nothing."""
|
||||
name = entry["name"]
|
||||
if none.verdict == BOOTS and not used:
|
||||
return []
|
||||
if none.verdict == BOOTS:
|
||||
on = " on its built-in BIOS" if none.bios_used == "builtin" else ""
|
||||
out = [Verdict("boots_without_file", "hle_fallback", True,
|
||||
f"the core boots the content{on} with no BIOS at all, {name} included",
|
||||
ids(none), True)]
|
||||
if entry.get("required") is True:
|
||||
out.append(Verdict(
|
||||
"boots_without_file", "required", False,
|
||||
f"the profile says {name} is required, and the core boots without it. The table "
|
||||
f"settles hle_fallback from this run, not required", ids(none)))
|
||||
return out
|
||||
if none.verdict in FAILS and present.verdict == BOOTS:
|
||||
return [
|
||||
Verdict("fails_without_file", "required", True,
|
||||
f"with no BIOS the run ends in {none.verdict}, with {name} alone it boots",
|
||||
ids(none, present), True),
|
||||
Verdict("fails_without_file", "hle_fallback", False,
|
||||
f"no stand-in for {name}: with no BIOS the run ends in {none.verdict}",
|
||||
ids(none, present), True),
|
||||
]
|
||||
return [Verdict("unsettled", "hle_fallback", None,
|
||||
f"with no BIOS the run is {none.verdict} and with {name} alone {present.verdict}: "
|
||||
f"no rule settles a field from that", ids(none, present))]
|
||||
|
||||
|
||||
def file_use(wrong: Run, none: Run, present: Run) -> str | None:
|
||||
"""Whether the core refused a wrong file or ran it, or None."""
|
||||
if present.verdict != BOOTS:
|
||||
return None
|
||||
if wrong.verdict == BIOS_ERROR:
|
||||
return REFUSED
|
||||
# The log names the BIOS the core ran: that settles it before any screen.
|
||||
if wrong.verdict == BOOTS and wrong.bios_used == "builtin":
|
||||
return REFUSED
|
||||
if wrong.verdict == BOOTS and wrong.bios_used.startswith("staged:"):
|
||||
return USED
|
||||
if wrong.verdict == BOOTS:
|
||||
if wrong.shows_bios:
|
||||
return USED
|
||||
return REFUSED if present.shows_bios else None
|
||||
if wrong.verdict in (HANG, CRASH) and none.verdict in (BOOTS, BIOS_ERROR):
|
||||
return USED
|
||||
return None
|
||||
|
||||
|
||||
def declared_checks(entry: dict) -> tuple[set[str], bool]:
|
||||
"""The checks the entry's validation names, and whether it is one list
|
||||
(a per-build dict is never edited by the table)."""
|
||||
value = entry.get("validation") or []
|
||||
if isinstance(value, dict):
|
||||
return {str(v) for items in value.values() for v in items or ()}, False
|
||||
return {str(v) for v in value}, True
|
||||
|
||||
|
||||
def warned_checks(entry: dict, checks: set[str]) -> set[str]:
|
||||
"""The declared checks whose failure the code only logs (`on_mismatch:
|
||||
warn`, for all of them or check by check): a wrong file still runs."""
|
||||
level = entry.get("on_mismatch")
|
||||
if level == "warn":
|
||||
return set(checks)
|
||||
if isinstance(level, dict):
|
||||
return {check for check, value in level.items() if value == "warn"} & checks
|
||||
return set()
|
||||
|
||||
|
||||
def rule_validation(zeros: Run | None, half: Run | None, none: Run, present: Run,
|
||||
entry: dict) -> Verdict | None:
|
||||
"""Same-size and short wrong files against the right one: validation."""
|
||||
name = entry["name"]
|
||||
zeros_use = file_use(zeros, none, present) if zeros else None
|
||||
content = {REFUSED: True, USED: False}.get(zeros_use) if zeros_use else None
|
||||
half_use = file_use(half, none, present) if half else None
|
||||
size: bool | None = None
|
||||
if half_use == USED:
|
||||
size, content = False, False if content is None else content
|
||||
elif half_use == REFUSED and content is False:
|
||||
size = True
|
||||
if content is None and size is None:
|
||||
return None
|
||||
declared, editable = declared_checks(entry)
|
||||
enforced = declared - warned_checks(entry, declared)
|
||||
evidence = ids(*(run for run in (none, present, zeros, half) if run))
|
||||
said = sorted(declared)
|
||||
shown = []
|
||||
problems = []
|
||||
if content is not None:
|
||||
shown.append("checks the content" if content else "does not check the content")
|
||||
if content != bool(enforced & set(CONTENT_CHECKS)):
|
||||
problems.append("a same-size file of zeros is " + ("refused" if content else "run")
|
||||
+ f", the profile declares validation {said}")
|
||||
missing_size = size is True and "size" not in declared
|
||||
if size is not None:
|
||||
shown.append("checks the size" if size else "does not check the size")
|
||||
if size is False and "size" in enforced:
|
||||
problems.append(f"the first half of {name} is run, the profile declares a size check")
|
||||
statement = f"the core {' and '.join(shown)} of {name}"
|
||||
# What the runs show, for a finding beside what the profile says.
|
||||
checks = {"content": content, "size": size}
|
||||
if problems:
|
||||
return Verdict("validation_contradicted", "validation", checks,
|
||||
f"{statement}: {'; '.join(problems)}", evidence)
|
||||
if missing_size:
|
||||
if not editable:
|
||||
return Verdict("validation_contradicted", "validation", checks,
|
||||
f"{statement}: the per-build validation has no size check", evidence)
|
||||
value = [*(entry.get("validation") or []), "size"]
|
||||
return Verdict("validation_size_added", "validation", value,
|
||||
f"{statement}: half of the file is refused while zeros of its size are run",
|
||||
evidence, True)
|
||||
return Verdict("validation_agrees", "validation", entry.get("validation") or [], statement,
|
||||
evidence, editable)
|
||||
|
||||
|
||||
def rule_region(dump: Run, present: Run, entry: dict, game_region: str | None) -> str:
|
||||
"""What the core did with another region's dump under this file's name."""
|
||||
return (f"{dump.label} staged as {entry['name']} with a {game_region or 'region-less'} game: "
|
||||
f"{dump.verdict}, {entry['name']} itself: {present.verdict}")
|
||||
|
||||
|
||||
def rule_path(entry: dict, declared: str, opened: Opened) -> Verdict | None:
|
||||
"""The core opened the file at another place under the system directory."""
|
||||
if opened.dest == declared:
|
||||
return None
|
||||
return Verdict("opened_under_other_path", "path", opened.dest,
|
||||
f"the core opened {entry['name']} as {opened.dest}, the profile declares "
|
||||
f"{declared}", (opened.record_id,), True)
|
||||
|
||||
|
||||
def rule_alias(entry: dict, opened: Opened) -> Verdict | None:
|
||||
"""The core opened the file under a name the entry does not declare."""
|
||||
name = opened.dest.rsplit("/", 1)[-1]
|
||||
known = [entry["name"], *(entry.get("aliases") or ())]
|
||||
if name in known:
|
||||
return None
|
||||
return Verdict("opened_other_name", "aliases", [*(entry.get("aliases") or ()), name],
|
||||
f"the core opened {entry['name']} under the name {name}",
|
||||
(opened.record_id,), True)
|
||||
|
||||
|
||||
def rule_never_opened(entry: dict, record_ids: Sequence[str], content: str) -> Verdict:
|
||||
return Verdict("never_opened", "opened", False,
|
||||
f"{entry['name']} was staged and the core never opened it while running "
|
||||
f"{content}", tuple(record_ids))
|
||||
|
||||
|
||||
def rule_variants(runs: Iterable[Run], entry: dict) -> Verdict | None:
|
||||
"""Copies of one file whose verdicts differ: a pack choice to review."""
|
||||
runs = list(runs)
|
||||
if len({run.verdict for run in runs}) < 2:
|
||||
return None
|
||||
shown = ", ".join(f"{run.label} {run.verdict}" for run in runs)
|
||||
return Verdict("variant_changes_verdict", "variants", sorted(run.label for run in runs),
|
||||
f"the copies of {entry['name']} do not boot alike: {shown}", ids(*runs))
|
||||
|
||||
|
||||
def merge_evidence(existing: dict | None, verdict: Verdict,
|
||||
keep: Callable[[str], bool] = lambda rid: True) -> dict:
|
||||
"""The entry's evidence: map with the verdict's record ids added under its
|
||||
field. An id `keep` refuses is dropped from that field: a record from
|
||||
before the oracle no longer proves anything."""
|
||||
out = {key: list(value) for key, value in (existing or {}).items()}
|
||||
ids_ = [rid for rid in out.get(verdict.field, []) if keep(rid)]
|
||||
for rid in verdict.evidence:
|
||||
if rid not in ids_:
|
||||
ids_.append(rid)
|
||||
out[verdict.field] = ids_
|
||||
return {key: out[key] for key in sorted(out) if out[key]}
|
||||
Reference in new issue
Block a user