4
0

ctlwake-known.py 2.3 KB

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