Files
market/skills/python-runtime/scripts/probe-python.sh
T
yige 3ad8afde37 fix(python-runtime): 修正过时的 Hatch 路径与安装写法(1.0.4)/ correct stale Hatch paths and install commands (#242)
python-runtime 与 dev-environment-setup 还在描述客户端 10.0.105 之前的 Hatch 布局:正常安装时没有 runtime/hatch/hatch,probe-python.sh 的 hatch_path 恒空,Agent 接着就会用系统包管理器。文档的 CLI 写法同样有问题:Hatch 1.16.5 里没有 HATCH_HOME 和 HATCH_PYTHON_MIRROR_URL,不带 --private --dir 的 python install 装到用户数据目录并改写 .zshrc、.zprofile,DesireCore 也看不到那个 Python。

- 二进制改为随包的 static/toolchains/<平台>-<架构>/hatch/hatch;runtime/hatch/hatch 只有手动下载后才存在
- 先用已在 PATH 上的内置 Python;指定版本用 runtime/hatch/local/<版本> 的绝对路径,pip 走探测到的解释器
- 安装命令带 --private --dir,环境变量改为 PYAPP_INSTALL_DIR_HATCH;换下载源用 HATCH_PYTHON_CUSTOM_SOURCE_<NAME>,并写明 URL 形态约束
- probe-python.sh 按新布局查找,新增 hatch_bootstrap,只认与二进制同版本的分发;没有解包好的分发时不执行二进制,避免 --version 触发下载;输出的路径字段做 JSON 转义
- 写明这个 bash 探测脚本在 Windows 上不查找 Hatch,降级到 L3 前先试 L2 的 Windows 路径
- 记录首启导入把本机镜像地址写进 hatch-dist.json,导致 local/ 上的 Hatch 命令报 Unknown distribution source,并给出修复办法;客户端修复见 desirecore/desirecore#5578
- desirecore-runtime.md 的源码路径改为 packages/agent-service/src;decision-tree.md 补上 standalone 的 401
- 版本 python-runtime 1.0.3 → 1.0.4、dev-environment-setup 2.0.5 → 2.0.6,sidecar 与 i18n source_hash 同步;不设客户端版本门槛

Correct the Hatch paths and install commands in python-runtime and dev-environment-setup, which still described the layout from before client 10.0.105 and told Agents to run an install that edits shell startup files and lands where DesireCore cannot see it. probe-python.sh now finds the current layout, reports the unpacked distribution, and no longer triggers a download.

验证:catalog metadata 与 i18n 校验 0 error,translate --check 通过,market-i18n-real-repo 22/22;真实 Hatch 1.16.5 在隔离 HOME 下逐条跑过文档命令;standalone 隔离 HOME + mock 模型下真实 Bash 工具按文档跑通,未调用真实模型;探测脚本覆盖多种布局及含引号、反斜杠的路径。Windows 写法只对照源码。私有敏感词扫描未执行,经用户确认后合并。
2026-11-01 21:50:44 +08:00

139 lines
5.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# python-runtime probe: 输出 Python 环境快照(JSON)
# 协议:见 ../../dev-environment-setup/references/probe-snapshot.md
set +e
# 路径里可能有反斜杠和双引号,写进 JSON 前要转义(调用时仍用原始路径)
json_escape() {
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
}
detect_tool() {
local name="$1"
local path
path=$(command -v "$name" 2>/dev/null)
if [ -z "$path" ]; then
printf '{"path":"","version":""}'
return
fi
local version
version=$("$name" --version 2>&1 | head -n1 | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n1)
printf '{"path":"%s","version":"%s"}' "$(json_escape "$path")" "${version:-}"
}
# ── 平台 ────────────────────────────────
case "$(uname -s)" in
Darwin) PLATFORM="darwin" ;;
Linux) PLATFORM="linux" ;;
*) PLATFORM="other" ;;
esac
ARCH=$(uname -m)
case "$ARCH" in
arm64|aarch64) ARCH="arm64" ;;
x86_64|amd64) ARCH="x64" ;;
esac
# ── DesireCore API ──────────────────────
DESIRECORE_API=""
PORT_FILE="${DESIRECORE_ROOT}/agent-service.port"
if [ -r "$PORT_FILE" ]; then
PORT=$(cat "$PORT_FILE" 2>/dev/null | tr -d '[:space:]')
if [ -n "$PORT" ]; then
if curl -sk --max-time 0.5 "https://127.0.0.1:${PORT}/api/runtime/environment" >/dev/null 2>&1; then
DESIRECORE_API="https://127.0.0.1:${PORT}"
fi
fi
fi
# ── 系统 Python / pip ───────────────────
SYS_PY=$(detect_tool python3)
[ "$(echo "$SYS_PY" | grep -c '"path":""')" = "1" ] && SYS_PY=$(detect_tool python)
SYS_PIP=$(detect_tool pip3)
[ "$(echo "$SYS_PIP" | grep -c '"path":""')" = "1" ] && SYS_PIP=$(detect_tool pip)
# ── DesireCore Hatch ────────────────────
HATCH_DIR="${DESIRECORE_ROOT}/runtime/hatch"
HATCH_TARGET="${PLATFORM}-${ARCH}"
# 二进制与服务同序:手动下载的 runtime/hatch/hatch 优先,其次随包工具链。
# 桌面版的应用目录不在环境变量里,随包路径再看 Volta 入口的链接目标(只读链接,不执行入口;
# Volta 建入口时指向同一工具链目录下的 volta-shim,旧版升级后可能已断)和默认安装位置。
VOLTA_SHIM=$(readlink "${VOLTA_HOME:-${DESIRECORE_ROOT}/runtime/volta-home}/bin/node" 2>/dev/null)
HATCH_PATH=""
for HATCH_BIN in \
"${HATCH_DIR}/hatch" \
"${DESIRECORE_STATIC_DIR}/toolchains/${HATCH_TARGET}/hatch/hatch" \
"${VOLTA_SHIM%/volta/*}/hatch/hatch" \
/Applications/*.app/Contents/static/toolchains/"${HATCH_TARGET}"/hatch/hatch \
/opt/*/static/toolchains/"${HATCH_TARGET}"/hatch/hatch
do
if [ -f "$HATCH_BIN" ] && [ -x "$HATCH_BIN" ]; then
HATCH_PATH="$HATCH_BIN"
break
fi
done
# 随包二进制旁边的 asset.json 写着它的版本;手动下载的二进制没有这个文件
HATCH_VERSION=$(grep -m1 '"version"' "${HATCH_PATH%/*}/asset.json" 2>/dev/null | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n1)
# 客户端解包好的 Hatch 分发(10.0.183 起),调用二进制时经 PYAPP_INSTALL_DIR_HATCH 传入。
# 版本已知时只认同版本的分发(旧版本的分发升级后不会清理),未知时取找到的最后一个
HATCH_BOOTSTRAP=""
for BOOTSTRAP_DIR in "${HATCH_DIR}/bootstrap/"${HATCH_VERSION:-*}"-${HATCH_TARGET}"; do
[ -f "${BOOTSTRAP_DIR}/.ready" ] && HATCH_BOOTSTRAP="$BOOTSTRAP_DIR"
done
# 随包 Hatch 是 PyApp 引导程序:没有解包好的分发时,连 --version 都会下载分发并解压到用户目录,
# 所以只在有分发时才执行二进制
if [ -n "$HATCH_PATH" ] && [ -n "$HATCH_BOOTSTRAP" ]; then
HATCH_VERSION=$(PYAPP_INSTALL_DIR_HATCH="$HATCH_BOOTSTRAP" "$HATCH_PATH" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n1)
fi
# Hatch 已安装的 Python 版本(直接读 local/ 目录,避免依赖 hatch 命令)
HATCH_LOCAL="${HATCH_DIR}/local"
HATCH_VERSIONS="[]"
if [ -d "$HATCH_LOCAL" ]; then
versions=$(ls -1 "$HATCH_LOCAL" 2>/dev/null | sort -V | tr '\n' ',' | sed 's/,$//')
if [ -n "$versions" ]; then
HATCH_VERSIONS="[\"$(echo "$versions" | sed 's/,/","/g')\"]"
fi
fi
# ── 虚拟环境 ────────────────────────────
ACTIVE_VENV="${VIRTUAL_ENV:-}"
# ── PEP 668 ────────────────────────────
PEP668="false"
for marker in /usr/lib/python*/EXTERNALLY-MANAGED /usr/lib/python3/EXTERNALLY-MANAGED /usr/lib/python3*/EXTERNALLY-MANAGED; do
if [ -e "$marker" ]; then
PEP668="true"
break
fi
done
# ── 社区方案 ────────────────────────────
PYENV_PATH=$(command -v pyenv 2>/dev/null)
[ -z "$PYENV_PATH" ] && [ -d "$HOME/.pyenv/bin" ] && PYENV_PATH="$HOME/.pyenv/bin/pyenv"
CONDA_PATH=$(command -v conda 2>/dev/null)
[ -z "$CONDA_PATH" ] && [ -x "$HOME/miniconda3/bin/conda" ] && CONDA_PATH="$HOME/miniconda3/bin/conda"
# ── 输出 JSON ───────────────────────────
cat <<EOF
{
"platform": "${PLATFORM}",
"arch": "${ARCH}",
"desirecore_api": "${DESIRECORE_API}",
"system_python": ${SYS_PY},
"system_pip": ${SYS_PIP},
"hatch_path": "$(json_escape "$HATCH_PATH")",
"hatch_version": "${HATCH_VERSION}",
"hatch_bootstrap": "$(json_escape "$HATCH_BOOTSTRAP")",
"hatch_versions": ${HATCH_VERSIONS},
"active_venv": "$(json_escape "$ACTIVE_VENV")",
"pep668": ${PEP668},
"pyenv_path": "$(json_escape "${PYENV_PATH:-}")",
"conda_path": "$(json_escape "${CONDA_PATH:-}")"
}
EOF