Detect ensurepip and auto-install python3-venv in one-click deploy.

Avoid false OK on bare python3; repair broken .venv; re-check deps after git pull.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-29 21:33:18 +08:00
parent 9ec0cacf45
commit 467203b61d
3 changed files with 42 additions and 6 deletions
+21 -2
View File
@@ -8,7 +8,7 @@ set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
# 有 root 时:检测 Python / Node / PM2,缺则装、有则跳过
# 有 root 时:检测 Python / Node / PM2,缺则装、有则跳过pull 前用当前脚本)
if [[ -f "$ROOT/deploy/lib/common.sh" ]] && [[ "$(id -u)" -eq 0 ]]; then
# shellcheck source=lib/common.sh
source "$ROOT/deploy/lib/common.sh"
@@ -21,6 +21,13 @@ if [[ -d "$ROOT/.git" ]]; then
git pull --ff-only
fi
# pull 后再检测一次,确保用上仓库里最新的 python3-venv / ensurepip 逻辑
if [[ -f "$ROOT/deploy/lib/common.sh" ]] && [[ "$(id -u)" -eq 0 ]]; then
# shellcheck source=lib/common.sh
source "$ROOT/deploy/lib/common.sh"
ensure_system_deps
fi
if [[ ! -f "$ROOT/.env" ]]; then
echo "[比特骆驼] missing .env — copy from example"
cp "$ROOT/.env.example" "$ROOT/.env"
@@ -37,9 +44,21 @@ if ! command -v python3 >/dev/null 2>&1; then
exit 1
fi
# 半截失败的 .venv(缺 ensurepip 时常见)会留下坏目录,必须重建
if [[ -d "$ROOT/.venv" ]] && [[ ! -x "$ROOT/.venv/bin/python3" ]]; then
echo "[比特骆驼] .venv 不完整,删除后重建"
rm -rf "$ROOT/.venv"
fi
if [[ ! -d "$ROOT/.venv" ]]; then
echo "[比特骆驼] create venv"
python3 -m venv "$ROOT/.venv"
if ! python3 -m venv "$ROOT/.venv"; then
pyver="$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])' 2>/dev/null || echo "3")"
echo "ERROR: 创建 venv 失败(多为缺 python3-venv / ensurepip)。" >&2
echo " 可执行: apt install -y python3-venv python${pyver}-venv" >&2
rm -rf "$ROOT/.venv"
exit 1
fi
fi
# shellcheck disable=SC1091
source "$ROOT/.venv/bin/activate"