File size: 3,189 Bytes
f5ec76c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c1f989
f5ec76c
 
 
 
7c1f989
 
 
 
 
 
f5ec76c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
"""
Cross-platform task runner for RedStack.
Pure stdlib only — no POSIX binaries, no shell assumptions.
Works identically on macOS, Linux, and native Windows (PowerShell/CMD).

Usage:
    python scripts/run.py build
    python scripts/run.py rank
    python scripts/run.py validate
    python scripts/run.py clean
"""

import os
import shutil
import subprocess
import sys
from pathlib import Path

# ---------------------------------------------------------------------------
# Deterministic single-thread execution caps — applied BEFORE any subprocess.
# ---------------------------------------------------------------------------
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["OPENBLAS_NUM_THREADS"] = "1"
os.environ["VECLIB_MAXIMUM_THREADS"] = "1"
os.environ["PYTHONUNBUFFERED"] = "1"
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"

REPO_ROOT = Path(__file__).resolve().parent.parent
SRC_DIR = REPO_ROOT / "src"
PYTHON = sys.executable


def _run(*args: str) -> None:
    # Propagate src/ so bare `python scripts/run.py` works without uv or editable install.
    env = os.environ.copy()
    src = str(SRC_DIR)
    existing_pp = env.get("PYTHONPATH", "")
    env["PYTHONPATH"] = f"{src}{os.pathsep}{existing_pp}" if existing_pp else src
    result = subprocess.run([PYTHON, "-m"] + list(args), cwd=REPO_ROOT, env=env)
    sys.exit(result.returncode)


def cmd_build() -> None:
    artifact_dirs = [
        "artifacts/embeddings",
        "artifacts/models",
        "artifacts/gates",
        "artifacts/weights",
        "artifacts/calibration",
        "artifacts/lexicon",
        "artifacts/archetypes",
    ]
    for d in artifact_dirs:
        Path(REPO_ROOT / d).mkdir(parents=True, exist_ok=True)
    _run(
        "redstack.cli.app",
        "build",
        "--config",
        "configs/runtime/offline.yaml",
        "--no-golden-labels",
    )


def cmd_rank() -> None:
    Path(REPO_ROOT / "artifacts").mkdir(parents=True, exist_ok=True)
    _run(
        "redstack.cli.app",
        "rank",
        "--input",
        "data/raw/candidates.jsonl",
        "--output",
        "artifacts/submission.csv",
    )


def cmd_validate() -> None:
    _run(
        "redstack.cli.app",
        "validate",
        "--submission",
        "artifacts/submission.csv",
    )


def cmd_clean() -> None:
    noise_dirs = [
        ".mypy_cache",
        ".ruff_cache",
        ".pytest_cache",
        ".coverage",
        "htmlcov",
        "dist",
        "build",
    ]
    for name in noise_dirs:
        target = REPO_ROOT / name
        if target.exists():
            shutil.rmtree(target, ignore_errors=True)
            print(f"removed {target}")

    for pycache in REPO_ROOT.rglob("__pycache__"):
        shutil.rmtree(pycache, ignore_errors=True)

    print("clean done.")


COMMANDS = {
    "build": cmd_build,
    "rank": cmd_rank,
    "validate": cmd_validate,
    "clean": cmd_clean,
}

if __name__ == "__main__":
    if len(sys.argv) < 2 or sys.argv[1] not in COMMANDS:
        print(f"Usage: python scripts/run.py [{' | '.join(COMMANDS)}]", file=sys.stderr)
        sys.exit(1)
    COMMANDS[sys.argv[1]]()