File size: 1,654 Bytes
9368cc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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()}