File size: 2,474 Bytes
e042d72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
"""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()