File size: 1,340 Bytes
315e4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Refuse to write train/eval/compress products into the Python venv."""

from __future__ import annotations

import os
import sys
from pathlib import Path


def venv_roots() -> list[Path]:
    roots: list[Path] = []
    venv = os.environ.get("VIRTUAL_ENV")
    if venv:
        roots.append(Path(venv))
    evie = os.environ.get("EVIE_ROOT")
    if evie:
        for name in ("env", "venv", ".venv", "train-env"):
            roots.append(Path(evie) / name)
    prefix = Path(sys.prefix)
    if (prefix / "pyvenv.cfg").is_file():
        roots.append(prefix)
    out: list[Path] = []
    seen: set[Path] = set()
    for raw in roots:
        try:
            resolved = raw.resolve()
        except OSError:
            continue
        if resolved in seen:
            continue
        seen.add(resolved)
        out.append(resolved)
    return out


def forbid_venv_path(path: str | Path, label: str) -> Path:
    path = Path(path).expanduser().resolve()
    for root in venv_roots():
        try:
            path.relative_to(root)
        except ValueError:
            continue
        raise ValueError(
            f"{label} must not sit inside the Python env ({root}): {path}. "
            "Checkpoints -> $RUNS_DIR ($EVIE_ROOT/runs); "
            "HF caches -> $EVIE_ROOT/.cache; never $EVIE_ROOT/env."
        )
    return path