| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #!/usr/bin/env python3
- # Writes tests/CtlWake173dKnown.inc from tools/ctlwake surveys (--tsv files, oldest first: a later survey's row for a
- # control replaces an earlier one): for every generator control found dependent-ok, the context that woke it (the
- # regression test tries only that). With --hints <file> (the output of `ctlwake --hints`), a key of an older survey
- # that names a hint by its index ("hint:12") is rewritten to the hint's stable id, matched by the note it printed.
- # Usage: scripts/ctlwake-known.py [--hints hints.tsv] survey1.tsv [survey2.tsv …] > tests/CtlWake173dKnown.inc
- import csv
- import re
- import sys
- args = sys.argv[1:]
- hints = {} # engine -> [(note, id)]
- if args[:1] == ["--hints"]:
- with open(args[1]) as f:
- for line in f:
- c = line.rstrip("\n").split("\t")
- if len(c) >= 3:
- hints.setdefault(c[0], []).append((c[2], c[1]))
- args = args[2:]
- def stable(engine, key, note):
- m = re.match(r"hint:(\d{1,5})(\+.*)?$", key) # (an older survey's index; a stable id is 8 hex digits, some all digits)
- if not m:
- return key
- best = None
- for n, i in hints.get(engine, []):
- if note.startswith(n) and (best is None or len(n) > len(best[0])):
- best = (n, i)
- if best is None: # (a hint renamed or removed since: the control needs a new survey)
- print("dropped: %s | %s" % (engine, note), file=sys.stderr)
- return None
- return "hint:" + best[1] + (m.group(2) or "")
- rows = {}
- for path in args:
- with open(path, newline="") as f:
- for r in csv.DictReader(f, delimiter="\t"):
- k = (r["engine"], r["control"])
- if r.get("status") == "dependent-ok" and r.get("key"):
- key = stable(r["engine"], r["key"], r.get("note", ""))
- if key is None:
- rows.pop(k, None)
- else:
- rows[k] = key
- elif r.get("status") == "ok":
- rows.pop(k, None)
- def q(s):
- return '"' + s.replace("\\", "\\\\").replace('"', '\\"') + '"'
- print("// Generated by scripts/ctlwake-known.py from tools/ctlwake surveys (round 173d2): the context each dependent")
- print("// generator control needs (engine, control, key of ctlwake::context). Regenerate after a survey.")
- for (e, c), k in sorted(rows.items()):
- print("{ %s, %s, %s }," % (q(e), q(c), q(k)))
|