chore: 开发脚本自动进入虚拟环境

This commit is contained in:
Nixevol
2026-07-13 16:28:08 +08:00
parent 182f0e5612
commit 3c74f03e7e
4 changed files with 57 additions and 10 deletions
+44
View File
@@ -26,12 +26,56 @@
"""
from __future__ import annotations
import os
import re
import subprocess
import sys
import threading
import time
from pathlib import Path
ROOT_DIR = Path(__file__).resolve().parent.parent
def in_virtualenv() -> bool:
return (
bool(os.environ.get("VIRTUAL_ENV"))
or sys.prefix != getattr(sys, "base_prefix", sys.prefix)
or hasattr(sys, "real_prefix")
)
def venv_python(venv_dir: Path) -> Path:
return venv_dir / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
def ensure_virtualenv() -> None:
"""Re-run dev.py inside the project virtualenv before importing script dependencies."""
if in_virtualenv() or os.environ.get("ARTVERIS_SKIP_VENV") == "1":
return
existing = next(
(path for path in (ROOT_DIR / ".venv", ROOT_DIR / "venv") if venv_python(path).exists()),
None,
)
venv_dir = existing or (ROOT_DIR / ".venv")
created = existing is None
if existing is None:
print(f"[artveris] 未检测到虚拟环境,正在创建 {venv_dir} ...")
subprocess.run([sys.executable, "-m", "venv", str(venv_dir)], cwd=str(ROOT_DIR), check=True)
python = venv_python(venv_dir)
requirements = ROOT_DIR / "scripts" / "requirements.txt"
if created and requirements.exists():
print(f"[artveris] 使用虚拟环境安装脚本依赖:{requirements}")
subprocess.run([str(python), "-m", "pip", "install", "-r", str(requirements)], cwd=str(ROOT_DIR), check=True)
print(f"[artveris] 切换到虚拟环境 Python:{python}")
os.execv(str(python), [str(python), str(Path(__file__).resolve()), *sys.argv[1:]])
ensure_virtualenv()
from _lib import (
ROOT,
create_kill_job,