Rebuild broken .venv when activate is missing after ensurepip failure.

Half-created venvs often keep bin/python3 without activate; directory existence is not enough.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-29 21:37:04 +08:00
parent 467203b61d
commit c425a08a8f
2 changed files with 24 additions and 6 deletions
+16 -6
View File
@@ -44,13 +44,18 @@ 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
# 缺 ensurepip 时会留下半截 .venv:常有 bin/python3 却没有 activate,不能只看目录是否存在
project_venv_ok() {
[[ -f "$ROOT/.venv/bin/activate" ]] \
&& [[ -x "$ROOT/.venv/bin/python3" ]] \
&& "$ROOT/.venv/bin/python3" -c "import sys" >/dev/null 2>&1
}
if [[ ! -d "$ROOT/.venv" ]]; then
if ! project_venv_ok; then
if [[ -e "$ROOT/.venv" ]]; then
echo "[比特骆驼] .venv 不可用(半截/损坏),删除后重建"
rm -rf "$ROOT/.venv"
fi
echo "[比特骆驼] create 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")"
@@ -59,6 +64,11 @@ if [[ ! -d "$ROOT/.venv" ]]; then
rm -rf "$ROOT/.venv"
exit 1
fi
if ! project_venv_ok; then
echo "ERROR: venv 创建后仍不可用(缺 activate 或 python)。" >&2
rm -rf "$ROOT/.venv"
exit 1
fi
fi
# shellcheck disable=SC1091
source "$ROOT/.venv/bin/activate"
+8
View File
@@ -5,6 +5,14 @@
---
## 2026-07-29 — 部署:半截 .venv 必须重建(缺 activate
### 变更
1. 判定 venv 可用改为同时检查 `bin/activate` + `bin/python3`(仅有目录或仅有 python 仍会删掉重建)。
---
## 2026-07-29 — 部署:自动安装 python3-venvensurepip
### 变更