File size: 4,027 Bytes
5154c4e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | from __future__ import annotations
import re
import sys
import xml.etree.ElementTree as ET
from collections import Counter
from pathlib import Path
INIT_BRD_NAME = "mystery.brd"
def _locate_init_board(start: Path) -> Path | None:
for cand in (
start / INIT_BRD_NAME,
start / "init_file" / INIT_BRD_NAME,
start.parent / "init_file" / INIT_BRD_NAME,
start.parent.parent / "init_file" / INIT_BRD_NAME,
):
if cand.exists():
return cand
return None
def _expected_signals(brd_path: Path) -> list[tuple[str, list[tuple[str, str]]]]:
root = ET.parse(brd_path).getroot()
out: list[tuple[str, list[tuple[str, str]]]] = []
for sig in root.findall(".//signal"):
name = sig.get("name") or ""
refs = [(cr.get("element") or "", cr.get("pad") or "") for cr in sig.findall("contactref")]
out.append((name, refs))
return out
def _parse_netlist(text: str, signal_names: list[str]) -> list[tuple[str, list[tuple[str, str]]]]:
sections: list[tuple[str, list[tuple[str, str]]]] = []
current_name: str | None = None
current_refs: list[tuple[str, str]] = []
ordered_names = sorted(signal_names, key=len, reverse=True)
ref_pat = re.compile(
r"([A-Za-z0-9_+$./-]+)(?:\s+|\s*\.\s*)([A-Za-z0-9_+$./-]+|[+-])(?=$|\s|[,\);])"
)
def flush() -> None:
nonlocal current_name, current_refs
if current_name is not None:
sections.append((current_name, current_refs))
current_name = None
current_refs = []
def collect_refs(fragment: str) -> None:
nonlocal current_refs
for match in ref_pat.finditer(fragment):
current_refs.append((match.group(1), match.group(2)))
for raw in text.splitlines():
line = raw.rstrip()
if not line.strip():
continue
if line.startswith("Netlist") or line.startswith("Exported from") or line.startswith("EAGLE Version"):
continue
if line.startswith("Net") and "Part" in line and "Pad" in line:
continue
matched = None
for name in ordered_names:
if re.match(rf"^{re.escape(name)}(?:\s|$)", line):
matched = name
break
if matched is not None:
flush()
current_name = matched
collect_refs(line[len(matched):])
continue
if current_name is not None:
collect_refs(line)
flush()
return sections
def evaluate(submission_dir: str) -> bool:
sub = Path(submission_dir).resolve()
target = None
for name in ("mystery_netlist.txt",):
cand = sub / name
if cand.is_file():
target = cand
break
if target is None:
return False
try:
text = target.read_text(encoding="utf-8")
except UnicodeDecodeError:
text = target.read_text(encoding="latin-1")
if not text.strip():
return False
brd = _locate_init_board(Path(__file__).resolve().parent)
if brd is None:
return False
expected = _expected_signals(brd)
actual = _parse_netlist(text, [name for name, _ in expected])
if {name for name, _ in actual} != {name for name, _ in expected}:
return False
expected_map = {name: Counter(refs) for name, refs in expected}
actual_map = {name: Counter(refs) for name, refs in actual}
if set(actual_map) != set(expected_map):
return False
for name, exp_refs in expected_map.items():
if actual_map.get(name) != exp_refs:
return False
return True
def main(argv: list[str]) -> int:
if len(argv) > 2:
print("usage: python eval.py SUBMISSION_DIR", file=sys.stderr)
return 2
submission_dir = argv[1] if len(argv) == 2 else Path(__file__).resolve().parent
ok = evaluate(submission_dir)
print("True" if ok else "False")
return 0 if ok else 1
if __name__ == "__main__":
raise SystemExit(main(sys.argv))
|