File size: 2,682 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
from __future__ import annotations

import re
import sys
import xml.etree.ElementTree as ET
from collections import Counter
from pathlib import Path


def _board_contactrefs(path: Path) -> list[tuple[str, str]]:
    root = ET.parse(path).getroot()
    refs: list[tuple[str, str]] = []
    for sig in root.findall(".//signal"):
        name = sig.get("name") or ""
        for cr in sig.findall("contactref"):
            refs.append((name, cr.get("element") or ""))
    return refs


def _ipc_records(text: str) -> list[tuple[str, str]]:
    out: list[tuple[str, str]] = []
    for line in text.splitlines():
        if not line.startswith("317"):
            continue
        m = re.match(r"^317\s*([^\s]+)\s+([^\s]+)", line)
        if not m:
            continue
        out.append((m.group(1), m.group(2)))
    return out


def evaluate(submission_dir: str) -> bool:
    sub = Path(submission_dir).resolve()
    target = None
    for name in ("board.ipc",):
        cand = sub / name
        if cand.exists():
            target = cand
            break
    if target is None:
        return False

    try:
        text = target.read_text(encoding="utf-8")
    except Exception:
        return False

    lines = [ln.rstrip() for ln in text.splitlines() if ln.strip()]
    if not lines or not lines[0].startswith("C"):
        return False
    if not any(re.match(r"^P\s+JOB\b", ln) for ln in lines):
        return False
    if not any(re.match(r"^P\s+UNITS\b", ln) for ln in lines):
        return False
    if lines[-1] != "999":
        return False

    test_records = [ln for ln in lines if re.match(r"^317", ln)]
    if not test_records:
        return False
    for ln in test_records:
        # EAGLE emits both spaced and compact IPC-D-356A drill/access tokens.
        if not re.search(r"D\s*\d+\s*A\d+", ln, re.IGNORECASE):
            return False

    eval_dir = Path(__file__).resolve().parent
    brd = next(
        (path for path in (eval_dir / "board.brd", eval_dir / "init_file" / "board.brd") if path.exists()),
        eval_dir / "board.brd",
    )
    expected_refs = _board_contactrefs(brd)
    if len(test_records) != len(expected_refs):
        return False

    if Counter(_ipc_records(text)) != Counter(expected_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))