Files
ai-shiliu/backend_main.py

47 lines
1.3 KiB
Python
Raw Normal View History

import os
import sys
from pathlib import Path
from app.application.services import AppConfig
from app.infrastructure.backend import start_backend
PROJECT_ROOT = Path(__file__).resolve().parent
VENV_ROOT = PROJECT_ROOT / ".venv"
VENV_DIR = VENV_ROOT / "Scripts"
VENV_PYTHON = VENV_DIR / "python.exe"
def _resolve(path):
try:
return Path(path).resolve()
except Exception:
return None
def _running_in_project_venv():
current_prefix = _resolve(sys.prefix)
if current_prefix == _resolve(VENV_ROOT):
return True
current_executable = _resolve(sys.executable)
return current_executable == _resolve(VENV_PYTHON)
def ensure_project_venv():
if _running_in_project_venv():
return
if os.environ.get("OPENCLAW_PROJECT_VENV_REEXEC") == "1":
raise RuntimeError("重启后仍未进入项目 .venv请检查 .venv 是否损坏")
if not VENV_PYTHON.exists():
raise RuntimeError(f"未找到项目 .venv 解释器: {VENV_PYTHON}")
os.environ["OPENCLAW_PROJECT_VENV_REEXEC"] = "1"
os.chdir(str(PROJECT_ROOT))
os.execv(str(VENV_PYTHON), [str(VENV_PYTHON), str(PROJECT_ROOT / "backend_main.py")])
ensure_project_venv()
if __name__ == "__main__":
config = AppConfig()
start_backend(host=config.host, port=config.port)