"""`structure_mirror` task generator.""" import json import random import shutil from collections import defaultdict from pathlib import Path from ..base import ( Generator, _VERIFY_HEADER, _render_verify, pad_to, _write, _para, _AUTHORS, _SONG_TITLES, _STUDENTS, _WORDS, ) class StructureMirror(Generator): KEY = "structure_mirror" CATEGORY_NAME = "Structure Mirror" DIFFICULTY = "L2" TAGS = ["folder structure", "organization"] SRC = "source" DST = "mirror" def build(self, env_dir, llm, rng): subdirs = ["docs", "docs/api", "src", "src/utils", "tests", "data/raw"] rng.shuffle(subdirs) subdirs = subdirs[: rng.randint(4, 6)] # ensure parents implied full = set() for d in subdirs: parts = d.split("/") for i in range(1, len(parts) + 1): full.add("/".join(parts[:i])) for d in sorted(full): (env_dir / self.SRC / d).mkdir(parents=True, exist_ok=True) _write(env_dir / self.SRC / d / "note.txt", "placeholder\n") _write(env_dir / self.SRC / "readme.txt", "root file\n") return {"src": self.SRC, "dst": self.DST, "dirs": sorted(full)} def description(self, spec): return ( "Please use FileSystem tools to finish the following task:\n\n" "### Task: Mirror a directory structure\n\n" f"Create a folder `{self.DST}/` in the test directory root that " f"replicates the **directory structure** of `{self.SRC}/` — every " f"subdirectory of `{self.SRC}/` (at every depth) must exist at the same " f"relative path under `{self.DST}/`.\n\n" f"- Recreate directories only. `{self.DST}/` must contain **no files**.\n" f"- Do not modify `{self.SRC}/`." ) def verify_src(self, spec): body = ''' C = json.loads(__CONSTS__) def main(): t = get_test_dir() dst = t / C["dst"] if not dst.is_dir(): fail(f"missing directory: {C['dst']}") for d in C["dirs"]: if not (dst / d).is_dir(): fail(f"missing mirrored directory: {C['dst']}/{d}") # No files anywhere under dst. for p in dst.rglob("*"): if p.is_file() and p.name not in SYSTEM_FILES: fail(f"{C['dst']} should contain no files, found: {p.relative_to(dst)}") ok(f"directory structure mirrored ({len(C['dirs'])} dirs, no files)") print("\\U0001f389 All checks passed!") sys.exit(0) if __name__ == "__main__": main() ''' return _render_verify(body, {"src": spec["src"], "dst": spec["dst"], "dirs": spec["dirs"]}) def solve(self, work_dir, spec): for d in spec["dirs"]: (work_dir / spec["dst"] / d).mkdir(parents=True, exist_ok=True)