Files
hoywu 5b5b3e2f99 feat(setup): auto-detect available PyPI mirror for Linux install scripts
Linux one-click-start.sh and setup_backend_linux.sh previously installed
dependencies from the default PyPI source, which is often slow or blocked
in mainland China. Mirror the Windows pipeline's PyPI mirror behaviour by
probing well-known mirrors and falling back to the official source.

- New scripts/pypi_mirror.sh: ordered list of Tsinghua / Aliyun / USTC /
  Tencent / pypi.org; pypi_mirror_probe checks each with curl or wget
  (3s timeout); pypi_mirror_select returns the first reachable URL;
  ensure_pypi_mirror_pip_args sets PIP_ARGS=("-i" <url>) only when the
  caller has not already supplied --pip-arg. Reusable via 'source'.
- one-click-start.sh sources pypi_mirror.sh and calls
  ensure_pypi_mirror_pip_args before invoking the backend installer, so a
  bare one-click-start.sh on a CN host picks the fastest mirror.
- scripts/setup_backend_linux.sh does the same for direct invocations
  (e.g. ./scripts/setup_backend_linux.sh), with the same skip-if-pip-arg-
  set semantics. Header comment updated to document the behaviour.

Users who pass --pip-arg keep full control; everyone else gets a working
mirror automatically and the install no longer wedges on pypi.org.
2026-06-24 20:53:24 +08:00

57 lines
1.6 KiB
Bash
Raw Permalink 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.
#!/bin/bash
# PyPI 镜像探测(国内优先,官方兜底)。供 Linux 安装脚本 source 使用。
#
# 用法(在已定义 PIP_ARGS 数组的脚本中):
# source "$SCRIPT_DIR/scripts/pypi_mirror.sh"
# ensure_pypi_mirror_pip_args # 仅当 PIP_ARGS 为空时写入 -i <mirror>
PYPI_MIRRORS=(
"https://pypi.tuna.tsinghua.edu.cn/simple"
"https://mirrors.aliyun.com/pypi/simple"
"https://pypi.mirrors.ustc.edu.cn/simple"
"https://mirrors.cloud.tencent.com/pypi/simple"
"https://pypi.org/simple"
)
pypi_mirror_probe() {
local url="$1"
local code="000"
if command -v curl >/dev/null 2>&1; then
code="$(curl -fsSIL --max-time 3 -o /dev/null -w "%{http_code}" "$url" 2>/dev/null || true)"
[ -n "$code" ] || code="000"
elif command -v wget >/dev/null 2>&1; then
if wget --spider --timeout=3 --tries=1 "$url" >/dev/null 2>&1; then
code="200"
fi
else
return 1
fi
[[ "$code" =~ ^[2-4][0-9][0-9]$ ]]
}
pypi_mirror_select() {
local url
for url in "${PYPI_MIRRORS[@]}"; do
if pypi_mirror_probe "$url"; then
echo "PyPI 镜像可用: $url" >&2
printf '%s\n' "$url"
return 0
fi
echo "PyPI 镜像不可用: $url" >&2
done
echo "所有镜像探测失败,回退官方源 pypi.org" >&2
printf '%s\n' "https://pypi.org/simple"
}
# 用户未传 --pip-arg 时,自动探测可用镜像并写入 PIP_ARGS(-i <url>)。
ensure_pypi_mirror_pip_args() {
if [ "${#PIP_ARGS[@]}" -gt 0 ]; then
return 0
fi
local mirror
mirror="$(pypi_mirror_select)"
PIP_ARGS=("-i" "$mirror")
}