#!/usr/bin/env python3
"""Run mutmut with prepared coverage and stats artifacts in a fresh interpreter."""

from __future__ import annotations

import argparse
import os
from pathlib import Path
import shutil

PROJECT_ROOT = Path(__file__).resolve().parents[1]
SITECUSTOMIZE = PROJECT_ROOT / "scripts" / "mutmut_coverage_sitecustomize"


def _parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("mutmut_args", nargs=argparse.REMAINDER)
    return parser


def main() -> int:
    args = _parser().parse_args()
    mutmut = shutil.which("mutmut")
    if mutmut is None:
        print("prepared mutmut runner failed: mutmut executable is not on PATH")
        return 1

    pythonpath = os.environ.get("PYTHONPATH")
    prefixes = [str(SITECUSTOMIZE), str(PROJECT_ROOT)]
    os.environ["PYTHONPATH"] = os.pathsep.join(
        [*prefixes, *([pythonpath] if pythonpath else [])],
    )
    os.environ["OBLITERATUS_MUTMUT_REUSE_COVERAGE"] = "1"
    os.environ["OBLITERATUS_MUTMUT_REUSE_STATS"] = "1"
    os.environ["OBLITERATUS_MUTMUT_SUBPROCESS_PREFLIGHT"] = "1"

    command = [mutmut, *(args.mutmut_args or ["run"])]
    os.execv(mutmut, command)
    raise AssertionError("os.execv returned unexpectedly")


if __name__ == "__main__":
    raise SystemExit(main())
