"""Copy the built catalog and pristine library trees into codedesk/data for the image (file copies only). Usage (on the host, after catalog.py): python3 codedesk/build/package.py """ import ast import json import shutil from pathlib import Path HERE = Path(__file__).resolve().parent OUT, DATA = HERE / "out", HERE.parent / "data" # tests that never finish on their own; removed from the shipped copies STRIP = {"simple_pid": {"tests/test_pid.py": {"test_converge_system"}}} # simple-pid's tests depend on wall-clock timing and gave different results under load: not used DROP = {"simple_pid", "semver"} def strip_tests(path: Path, names: set[str]): src = path.read_text() spans = [(min([n.lineno] + [d.lineno for d in n.decorator_list]), n.end_lineno) for n in ast.walk(ast.parse(src)) if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) and n.name in names] lines = src.splitlines(keepends=True) for first, last in sorted(spans, reverse=True): del lines[first - 1:last] path.write_text("".join(lines)) def main(): catalog = [m for m in json.loads((OUT / "catalog.json").read_text()) if m["lib"] not in DROP] valid = OUT / "valid.json" if valid.exists() and "--validated" in __import__("sys").argv: # keep only mutants that passed validate.py keys = {tuple(k) for k in json.loads(valid.read_text())["ok"]} catalog = [m for m in catalog if (m["lib"], m["file"], m["line"], m["col"], m["new"]) in keys] libs = sorted({m["lib"] for m in catalog}) shutil.rmtree(DATA, ignore_errors=True) (DATA / "libs").mkdir(parents=True) for lib in libs: shutil.copytree(OUT / lib, DATA / "libs" / lib, ignore=shutil.ignore_patterns( "__pycache__", "*.pyc", ".pytest_cache", ".hypothesis", "docs", ".github", "*.egg-info")) for rel, names in STRIP.get(lib, {}).items(): strip_tests(DATA / "libs" / lib / rel, names) shutil.copy(OUT / f"{lib}.baseline.json", DATA / f"{lib}.baseline.json") stripped = {(lib, f"{rel}::{n}") for lib, files in STRIP.items() for rel, names in files.items() for n in names} catalog = [m for m in catalog if not any((m["lib"], k.split("[")[0]) in stripped for k in m["killed"])] (DATA / "catalog.json").write_text(json.dumps(catalog, indent=1)) by = {} for m in catalog: by[m["lib"]] = by.get(m["lib"], 0) + 1 print(len(catalog), "mutants", by) if __name__ == "__main__": main()