From d5d9077713522cf75ea6bef11f44494cbe29328c Mon Sep 17 00:00:00 2001 From: Falck Date: Sat, 18 Apr 2026 00:10:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BE=9D=E8=B5=96=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E4=B8=8E=E5=AE=89=E8=A3=85=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- start.sh | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/start.sh b/start.sh index c548e38..15fdce6 100755 --- a/start.sh +++ b/start.sh @@ -119,7 +119,12 @@ CURRENT_STEP=0 # ═══════════════════════════════════════════════════════════ step "安装系统依赖" -DEPENDENCIES=("git" "curl" "wget" "php" "php-cli" "php-mbstring" "php-xml" "php-zip" "python3" "python3-pip" "python3-venv") +# 系统命令依赖和PHP扩展分开处理 +CMD_DEPENDENCIES=("git" "curl" "wget" "php" "python3") +PHP_EXTENSIONS=("php-cli" "php-mbstring" "php-xml" "php-zip") +PYTHON_PKGS=("pip" "venv") + +DEPENDENCIES=(${CMD_DEPENDENCIES[@]} ${PHP_EXTENSIONS[@]} "python3-pip" "python3-venv") DEP_TOTAL=${#DEPENDENCIES[@]} DEP_INSTALLED=0 @@ -127,6 +132,15 @@ for dep in "${DEPENDENCIES[@]}"; do DEP_INSTALLED=$((DEP_INSTALLED + 1)) progress_bar $DEP_INSTALLED $DEP_TOTAL "检测 $dep" + # PHP扩展包不通过command检测,跳过 + if [[ "$dep" == php-* ]]; then + continue + fi + # python3-venv也不通过command检测 + if [[ "$dep" == "python3-venv" || "$dep" == "python3-pip" ]]; then + continue + fi + if command -v "$dep" &>/dev/null; then continue fi @@ -191,11 +205,68 @@ fi source "$VENV_DIR/bin/activate" PIP_CMD="$VENV_DIR/bin/pip" +# ═══════════════════════════════════════════════════════════ +# 检测虚拟环境依赖完整性 +# ═══════════════════════════════════════════════════════════ +info "检测虚拟环境依赖完整性..." + +VENV_INCOMPLETE=false +MISSING_VENV_DEPS=() + +for pkg in "${CORE_DEPS[@]}"; do + import_name="${PKG_IMPORT_MAP[$pkg]}" + if ! $PYTHON_CMD -c "import $import_name" 2>/dev/null; then + VENV_INCOMPLETE=true + MISSING_VENV_DEPS+=("$pkg") + fi +done + +# 同时检测 requirements.txt 中的依赖 +if [[ -f "requirements.txt" ]]; then + while IFS= read -r line || [[ -n "$line" ]]; do + # 跳过空行和注释 + [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue + # 提取包名(去除版本号) + pkg_name=$(echo "$line" | sed -E 's/([a-zA-Z0-9_-]+).*/\1/') + [[ -z "$pkg_name" ]] && continue + # 尝试导入(将连字符替换为下划线) + import_name="${pkg_name//-/_}" + if ! $PYTHON_CMD -c "import $import_name" 2>/dev/null; then + # 不在核心依赖列表中的才添加 + if [[ ! " ${CORE_DEPS[@]} " =~ " ${pkg_name} " ]]; then + VENV_INCOMPLETE=true + MISSING_VENV_DEPS+=("$pkg_name") + fi + fi + done < "requirements.txt" +fi + +if $VENV_INCOMPLETE; then + warn "虚拟环境依赖不完整,缺失: ${MISSING_VENV_DEPS[*]}" + info "正在安装缺失的依赖..." + for pkg in "${MISSING_VENV_DEPS[@]}"; do + $PIP_CMD install "$pkg" -q 2>/dev/null || \ + $PIP_CMD install "$pkg" --break-system-packages -q 2>/dev/null || \ + warn "无法安装 $pkg" + done +else + ok "虚拟环境依赖完整" +fi + # ═══════════════════════════════════════════════════════════ # 4. 安装 Python 依赖 # ═══════════════════════════════════════════════════════════ step "安装 Python 依赖" +# 包名到导入名的映射 +declare -A PKG_IMPORT_MAP=( + ["click"]="click" + ["pyyaml"]="yaml" + ["websockets"]="websockets" + ["psutil"]="psutil" + ["cryptography"]="cryptography" +) + CORE_DEPS=("click" "pyyaml" "websockets" "psutil" "cryptography") DEP_COUNT=${#CORE_DEPS[@]} DEP_CURRENT=0 @@ -204,7 +275,9 @@ for pkg in "${CORE_DEPS[@]}"; do DEP_CURRENT=$((DEP_CURRENT + 1)) progress_bar $DEP_CURRENT $DEP_COUNT "安装 $pkg" - $PYTHON_CMD -c "import $pkg" 2>/dev/null && continue + # 使用正确的导入名检测 + import_name="${PKG_IMPORT_MAP[$pkg]}" + $PYTHON_CMD -c "import $import_name" 2>/dev/null && continue $PIP_CMD install "$pkg" -q 2>/dev/null || \ $PIP_CMD install "$pkg" --break-system-packages -q 2>/dev/null || true