本地工具路径DTU_*环境变量改造落地:10处调用点去硬编码+变量优先解析,本机全链路验证通过,文档同步收口,requirements补pre-commit

This commit is contained in:
2026-10-02 22:14:49 +08:00
parent b96d649e62
commit 33a4aa4945
30 changed files with 4074 additions and 2917 deletions
+3 -2
View File
@@ -32,8 +32,9 @@
## 使用方式
```bash
# 全库跑门禁
& 'C:\ProgramData\anaconda3\python.exe' -m pre_commit run --all-files
# 全库跑门禁(用仓库 .venv 的 python,其 pre-commit 随 requirements.txt 安装;
# 其他装有 pre-commit 的解释器可用 DTU_PRECOMMIT_PYTHON 指定)
& '.venv\Scripts\python.exe' -m pre_commit run --all-files
# 本地跑与 CI 等价的门禁(L0 + L0.5)
& '.venv\Scripts\python.exe' scripts/ci_local.py
+34 -9
View File
@@ -38,6 +38,7 @@
import argparse
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
@@ -46,18 +47,40 @@ REPO_ROOT = Path(__file__).resolve().parent.parent
def _resolve_python():
"""挑一个真正装了 pre-commit 的解释器。
r"""挑一个真正装了 pre-commit 的解释器。
本机可能有多个 python(.venv / anaconda3 / 系统),而 **pre-commit 只装在其中一个**。
按优先级探测:当前解释器 → `.venv` → anaconda3 → PATH 上的 `pre-commit` 所在环境。
本机可能有多个 python(.venv / 发行版 / 系统),而 **pre-commit 只装在其中一个**。
按优先级探测(见 docs/环境与部署/本地工具路径环境变量方案.md §3.1):
1. DTU_PRECOMMIT_PYTHON(显式配置:文件缺失或无 pre-commit 模块 → 立即报
配置错误,**不静默回退**到别的解释器)
2. 当前解释器(sys.executable)
3. 仓库 `.venv\Scripts\python.exe`
4. PATH 上的 `python`
找到即返回 (python_path, note);都找不到则返回 (当前解释器, None) 交由调用方报错。
"""
candidates = [sys.executable]
venv = REPO_ROOT / ".venv" / "Scripts" / "python.exe"
if venv.is_file():
candidates.append(str(venv))
candidates.append(r"C:\ProgramData\anaconda3\python.exe")
explicit = os.environ.get("DTU_PRECOMMIT_PYTHON", "").strip()
if explicit:
if not os.path.isfile(explicit):
print("[ERROR] DTU_PRECOMMIT_PYTHON 已设置但文件不存在: {}".format(explicit))
print(" 请修正该变量(python.exe 完整路径),或清除该变量改用自动探测。")
sys.exit(2)
try:
cp = subprocess.run([explicit, "-m", "pre_commit", "--version"],
capture_output=True, timeout=60)
except (OSError, subprocess.TimeoutExpired) as e:
print("[ERROR] DTU_PRECOMMIT_PYTHON 指定的解释器无法执行: {}({})".format(explicit, e))
sys.exit(2)
if cp.returncode != 0:
print("[ERROR] DTU_PRECOMMIT_PYTHON 指向的解释器没有 pre-commit 模块: {}".format(explicit))
print(" 请修正该变量(指向装有 pre-commit 的 python),或清除该变量改用自动探测。")
sys.exit(2)
return explicit, "(显式配置 DTU_PRECOMMIT_PYTHON)"
candidates = [
sys.executable,
str(REPO_ROOT / ".venv" / "Scripts" / "python.exe"),
shutil.which("python"),
]
seen = set()
for py in candidates:
if not py or py in seen or not os.path.isfile(py):
@@ -230,7 +253,9 @@ def main(argv=None):
if probe.returncode != 0:
print("[ERROR] 该解释器没有 pre-commit 模块。请任选其一:")
print(" - 用装了 pre-commit 的解释器跑本脚本(如 "
r"& 'C:\ProgramData\anaconda3\python.exe' scripts/ci_local.py)")
"& '.venv\\Scripts\\python.exe' scripts/ci_local.py)")
print(" - 或设置 DTU_PRECOMMIT_PYTHON 指向该解释器")
print(" (见 docs/环境与部署/本地工具路径环境变量方案.md)")
print(" - 或在当前环境安装:pip install pre-commit")
return 2