| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #!/usr/bin/env python3
- """Create a private .venv and install the exact local wheel. Offline. Idempotent."""
- from __future__ import annotations
- import os
- import subprocess
- import sys
- import venv
- from pathlib import Path
- def _log(msg: str) -> None:
- try:
- print(msg, flush=True)
- except UnicodeEncodeError:
- sys.stdout.buffer.write((msg + "\n").encode("utf-8", "replace"))
- sys.stdout.buffer.flush()
- def main() -> int:
- root = Path(__file__).resolve().parent
- wheel_txt = root / "WHEEL.txt"
- if not wheel_txt.is_file():
- _log("找不到 WHEEL.txt。这个安装器只能用于官方算法开发包。")
- return 1
- wheel_name = wheel_txt.read_text(encoding="utf-8").strip().splitlines()[0].strip()
- wheel = root / wheel_name
- if not wheel.is_file():
- _log(f"找不到精确 Wheel:{wheel}")
- return 1
- if sys.version_info < (3, 11) or sys.version_info >= (3, 14):
- _log(f"需要 Python 3.11/3.12/3.13,当前是 {sys.version}")
- return 1
- venv_dir = root / ".venv"
- py_win = venv_dir / "Scripts" / "python.exe"
- py_unix = venv_dir / "bin" / "python"
- venv_py = py_win if os.name == "nt" else py_unix
- if not venv_py.is_file():
- _log(f"创建私有环境 {venv_dir}")
- builder = venv.EnvBuilder(with_pip=True, clear=False, upgrade=False)
- builder.create(venv_dir)
- else:
- _log("复用已有 .venv")
- env = os.environ.copy()
- env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
- env.pop("PIP_INDEX_URL", None)
- env.pop("PIP_EXTRA_INDEX_URL", None)
- cmd = [
- str(venv_py),
- "-m",
- "pip",
- "install",
- "--no-index",
- "--no-warn-script-location",
- "--disable-pip-version-check",
- "--find-links",
- str(root / "wheelhouse"),
- str(wheel),
- ]
- if not (root / "wheelhouse").is_dir():
- cmd = [
- str(venv_py),
- "-m",
- "pip",
- "install",
- "--no-index",
- "--no-warn-script-location",
- "--disable-pip-version-check",
- str(wheel),
- ]
- _log("离线安装 " + wheel.name)
- subprocess.check_call(cmd, env=env)
- subprocess.check_call([str(venv_py), "-c", "from hth_sdk import HTH; print('HTH_OK', HTH.demo)"])
- _log("安装完成。")
- if os.name == "nt":
- _log(r"请使用 .venv\Scripts\python.exe 运行 quickstart.py")
- else:
- _log("请使用 ./.venv/bin/python 运行 quickstart.py")
- return 0
- if __name__ == "__main__":
- raise SystemExit(main())
|