Files
deerflow-code/deploy/linux-docker-offline-deerflow-only/release-template/scripts/install_wheelhouse.py
T

98 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Install operator-selected offline wheels into the persistent vendor path."""
from __future__ import annotations
import hashlib
import inspect
import json
import subprocess
import sys
from pathlib import Path
def digest_inputs(wheelhouse: Path, requirements: Path) -> str:
digest = hashlib.sha256()
inputs = [requirements, *sorted(wheelhouse.glob("*.whl"), key=lambda path: path.name.lower())]
for path in inputs:
digest.update(path.name.encode("utf-8"))
digest.update(b"\0")
with path.open("rb") as stream:
for block in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(block)
return digest.hexdigest()
def has_requirements(path: Path) -> bool:
return any(line.strip() and not line.lstrip().startswith("#") for line in path.read_text(encoding="utf-8-sig").splitlines())
def verify_aiomysql_bundle(vendor: Path, requirements: Path) -> None:
if "aiomysql" not in requirements.read_text(encoding="utf-8-sig").lower():
return
sys.path.insert(0, str(vendor))
import aiomysql # noqa: F401
import pymysql # noqa: F401
from sqlalchemy.dialects.mysql.aiomysql import AsyncAdapt_aiomysql_connection
reconnect = inspect.signature(AsyncAdapt_aiomysql_connection.ping).parameters.get("reconnect")
if reconnect is None or reconnect.default is not False:
raise RuntimeError("SQLAlchemy aiomysql 适配器不兼容:ping(reconnect=False) 校验失败")
def main() -> int:
wheelhouse = Path(sys.argv[1] if len(sys.argv) > 1 else "/app/wheelhouse").resolve()
vendor = Path(
sys.argv[2] if len(sys.argv) > 2 else "/app/backend/.deer-flow/python-vendor"
).resolve()
requirements = wheelhouse / "requirements.txt"
if not wheelhouse.is_dir():
raise SystemExit(f"wheelhouse 目录不存在: {wheelhouse}")
if not requirements.is_file():
raise SystemExit(f"wheelhouse 缺少 requirements.txt: {requirements}")
if not has_requirements(requirements):
print("wheelhouse/requirements.txt 没有启用的依赖,跳过安装。")
return 0
fingerprint = digest_inputs(wheelhouse, requirements)
marker = vendor / ".wheelhouse-state.json"
vendor.mkdir(parents=True, exist_ok=True)
try:
state = json.loads(marker.read_text(encoding="utf-8"))
except (FileNotFoundError, json.JSONDecodeError):
state = {}
if state.get("fingerprint") == fingerprint and any(vendor.iterdir()):
verify_aiomysql_bundle(vendor, requirements)
print("wheelhouse 内容未变化,持久依赖已就绪。")
return 0
command = [
sys.executable,
"-m",
"pip",
"install",
"--disable-pip-version-check",
"--no-index",
"--find-links",
str(wheelhouse),
"--target",
str(vendor),
"--upgrade",
"-r",
str(requirements),
]
print("检测到 wheelhouse 变化,正在从本地 wheel 离线安装……")
subprocess.run(command, check=True)
verify_aiomysql_bundle(vendor, requirements)
marker.write_text(
json.dumps({"format_version": 1, "fingerprint": fingerprint}, ensure_ascii=False, indent=2)
+ "\n",
encoding="utf-8",
)
print("wheelhouse 依赖安装完成。")
return 0
if __name__ == "__main__":
raise SystemExit(main())