install_kit.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. """Create a private .venv and install the exact local wheel. Offline. Idempotent."""
  3. from __future__ import annotations
  4. import os
  5. import subprocess
  6. import sys
  7. import venv
  8. from pathlib import Path
  9. def _log(msg: str) -> None:
  10. try:
  11. print(msg, flush=True)
  12. except UnicodeEncodeError:
  13. sys.stdout.buffer.write((msg + "\n").encode("utf-8", "replace"))
  14. sys.stdout.buffer.flush()
  15. def main() -> int:
  16. root = Path(__file__).resolve().parent
  17. wheel_txt = root / "WHEEL.txt"
  18. if not wheel_txt.is_file():
  19. _log("找不到 WHEEL.txt。这个安装器只能用于官方算法开发包。")
  20. return 1
  21. wheel_name = wheel_txt.read_text(encoding="utf-8").strip().splitlines()[0].strip()
  22. wheel = root / wheel_name
  23. if not wheel.is_file():
  24. _log(f"找不到精确 Wheel:{wheel}")
  25. return 1
  26. if sys.version_info < (3, 11) or sys.version_info >= (3, 14):
  27. _log(f"需要 Python 3.11/3.12/3.13,当前是 {sys.version}")
  28. return 1
  29. venv_dir = root / ".venv"
  30. py_win = venv_dir / "Scripts" / "python.exe"
  31. py_unix = venv_dir / "bin" / "python"
  32. venv_py = py_win if os.name == "nt" else py_unix
  33. if not venv_py.is_file():
  34. _log(f"创建私有环境 {venv_dir}")
  35. builder = venv.EnvBuilder(with_pip=True, clear=False, upgrade=False)
  36. builder.create(venv_dir)
  37. else:
  38. _log("复用已有 .venv")
  39. env = os.environ.copy()
  40. env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
  41. env.pop("PIP_INDEX_URL", None)
  42. env.pop("PIP_EXTRA_INDEX_URL", None)
  43. cmd = [
  44. str(venv_py),
  45. "-m",
  46. "pip",
  47. "install",
  48. "--no-index",
  49. "--no-warn-script-location",
  50. "--disable-pip-version-check",
  51. "--find-links",
  52. str(root / "wheelhouse"),
  53. str(wheel),
  54. ]
  55. if not (root / "wheelhouse").is_dir():
  56. cmd = [
  57. str(venv_py),
  58. "-m",
  59. "pip",
  60. "install",
  61. "--no-index",
  62. "--no-warn-script-location",
  63. "--disable-pip-version-check",
  64. str(wheel),
  65. ]
  66. _log("离线安装 " + wheel.name)
  67. subprocess.check_call(cmd, env=env)
  68. subprocess.check_call([str(venv_py), "-c", "from hth_sdk import HTH; print('HTH_OK', HTH.demo)"])
  69. _log("安装完成。")
  70. if os.name == "nt":
  71. _log(r"请使用 .venv\Scripts\python.exe 运行 quickstart.py")
  72. else:
  73. _log("请使用 ./.venv/bin/python 运行 quickstart.py")
  74. return 0
  75. if __name__ == "__main__":
  76. raise SystemExit(main())