#!/usr/bin/env bash # 测速:为 CPython 源码包挑选可用的下载源。 # # p4a 的 python3/hostpython3 recipe 指向 GitHub 归档 # https://github.com/python/cpython/archive/refs/tags/v{version}.tar.gz # GitHub 直连经常几百 KB 就挂住(p4a 用 urllib 下载且没有超时), # 所以这里测一批镜像,把结果写进 /tmp 供后续脚本取用。 set -uo pipefail VERSION="${PYTHON_VERSION:-3.11.15}" TARBALL="v${VERSION}.tar.gz" OUT_DIR="${OUT_DIR:-/tmp/lnxgit-src-probe}" mkdir -p "$OUT_DIR" GH="github.com/python/cpython/archive/refs/tags/$TARBALL" CANDIDATES=( "github|https://$GH" "codeload|https://codeload.github.com/python/cpython/tar.gz/refs/tags/$TARBALL" "ghproxy.net|https://ghproxy.net/https://$GH" "gh-proxy.com|https://gh-proxy.com/https://$GH" "ghfast.top|https://ghfast.top/https://$GH" "moeyy|https://github.moeyy.xyz/https://$GH" "python.org|https://www.python.org/ftp/python/$VERSION/Python-$VERSION.tgz" "huaweicloud|https://mirrors.huaweicloud.com/python/$VERSION/Python-$VERSION.tgz" ) echo "=== 下载源测速(各取前 4 MB,限时 20 秒) ===" results=() for entry in "${CANDIDATES[@]}"; do name="${entry%%|*}" url="${entry#*|}" probe="$OUT_DIR/$name.part" rm -f "$probe" start=$(date +%s.%N) if curl -fsSL --http1.1 --connect-timeout 8 --max-time 20 \ -r 0-4194303 -o "$probe" "$url" 2>/dev/null; then bytes=$(stat -c %s "$probe" 2>/dev/null || echo 0) else bytes=$(stat -c %s "$probe" 2>/dev/null || echo 0) fi end=$(date +%s.%N) secs=$(awk -v a="$start" -v b="$end" 'BEGIN{printf "%.1f", b-a}') if [ "$bytes" -gt 100000 ]; then kbps=$(awk -v b="$bytes" -v s="$secs" 'BEGIN{printf "%.0f", b/1024/s}') echo " [可用] $name ${bytes} 字节 / ${secs}s ≈ ${kbps} KB/s" results+=("$kbps|$name|$url") else echo " [失败] $name (${bytes} 字节 / ${secs}s)" fi done echo if [ "${#results[@]}" -eq 0 ]; then echo "==> 没有任何可用源" exit 1 fi best=$(printf '%s\n' "${results[@]}" | sort -t'|' -k1 -nr | head -1) echo "==> 最快:$best" printf '%s\n' "$best" | cut -d'|' -f3 > "$OUT_DIR/best_url.txt" echo "==> 已写入 $OUT_DIR/best_url.txt"