SACB / source /tools /make_tasks.py
ilintar's picture
Add corpus source: base repos, task overlays, hidden tests, and the authoring tools
9368cc4 verified
Raw
History Blame Contribute Delete
1.65 kB
#!/usr/bin/env python3
"""Generate task directories from a spec list.
Break overlays are produced by exact string replacement against the base repo,
so a defect can never silently fail to be injected -- a replacement that does
not match is an error, not a quiet no-op.
"""
import json, sys
from pathlib import Path
CORPUS = Path(__file__).parent / "corpus"
def make(repo: str, task_id: str, meta: dict, edits: list, tests: dict):
"""edits: [(relative_path, old, new), ...] tests: {filename: content}"""
base = CORPUS / repo
tdir = base / "tasks" / task_id
(tdir / "tests").mkdir(parents=True, exist_ok=True)
by_file = {}
for rel, old, new in edits:
src = by_file.get(rel)
if src is None:
src = (base / "repo" / rel).read_text()
if old not in src:
raise SystemExit(f"{task_id}: anchor not found in {rel}:\n{old[:200]}")
if src.count(old) != 1:
raise SystemExit(f"{task_id}: anchor occurs {src.count(old)}x in {rel}")
by_file[rel] = src.replace(old, new)
for rel, content in by_file.items():
out = tdir / "break" / rel
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(content)
(tdir / "task.json").write_text(json.dumps(meta, indent=2) + "\n")
for name, body in tests.items():
(tdir / "tests" / name).write_text(body)
print(f" wrote {repo}/{task_id} ({len(by_file)} file(s) broken, {len(tests)} test file(s))")
def spec(lang, category, difficulty, instruction):
return {"lang": lang, "category": category, "difficulty": difficulty,
"instruction": instruction.strip()}