117 lines
3.5 KiB
Bash
117 lines
3.5 KiB
Bash
#!/bin/bash
|
|
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
|
|
|
|
source <(curl -f -s --connect-timeout 10 --retry 3 https://dl.acepanel.net/public.sh)
|
|
if [ $? -ne 0 ]; then
|
|
echo "Download public.sh failed, please check the network or try again later."
|
|
exit 1
|
|
fi
|
|
|
|
slug=${1}
|
|
version=${2}
|
|
python_path="${setup_path}/server/python/${slug}"
|
|
in_china=$(curl --retry 2 -m 10 -L -k https://www.qualcomm.cn/cdn-cgi/trace 2>/dev/null | grep -qx 'loc=CN' && echo "true" || echo "false")
|
|
j=$(calculate_j)
|
|
|
|
# 安装依赖
|
|
if [ ${OS} == "rhel" ]; then
|
|
dnf makecache
|
|
dnf groupinstall "Development Tools" -y
|
|
dnf install openssl-devel zlib-devel libffi-devel readline-devel sqlite-devel bzip2-devel xz-devel ncurses-devel gdbm-devel tk-devel libuuid-devel -y
|
|
elif [ ${OS} == "debian" ] || [ ${OS} == "ubuntu" ]; then
|
|
apt-get update
|
|
apt-get install build-essential libssl-dev zlib1g-dev libffi-dev libreadline-dev libsqlite3-dev libbz2-dev liblzma-dev libncurses5-dev libgdbm-dev tk-dev uuid-dev -y
|
|
else
|
|
error "Unsupported operating system"
|
|
fi
|
|
if [ "$?" != "0" ]; then
|
|
error "Failed to install dependencies"
|
|
fi
|
|
|
|
# 准备安装目录
|
|
rm -rf ${python_path}
|
|
mkdir -p ${python_path}
|
|
cd ${python_path}
|
|
|
|
# 下载Python源码
|
|
PYTHON_TARBALL="Python-${version}.tar.xz"
|
|
|
|
if ${in_china}; then
|
|
PYTHON_URL="https://mirrors.aliyun.com/python-release/source/${PYTHON_TARBALL}"
|
|
echo "Downloading ${PYTHON_TARBALL} from Aliyun mirror..."
|
|
else
|
|
PYTHON_URL="https://www.python.org/ftp/python/${version}/${PYTHON_TARBALL}"
|
|
echo "Downloading ${PYTHON_TARBALL} from python.org..."
|
|
fi
|
|
|
|
aria2c -x8 \
|
|
-U "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36" \
|
|
-o ${PYTHON_TARBALL} \
|
|
${PYTHON_URL}
|
|
if [ $? -ne 0 ]; then
|
|
rm -rf ${python_path}
|
|
error "Failed to download Python ${version}"
|
|
fi
|
|
|
|
# 解压
|
|
tar -xJf ${PYTHON_TARBALL}
|
|
if [ $? -ne 0 ]; then
|
|
rm -rf ${python_path}
|
|
error "Failed to extract Python ${version}"
|
|
fi
|
|
|
|
mv Python-${version} src
|
|
rm -f ${PYTHON_TARBALL}
|
|
|
|
# 仅在高配置服务器上启用优化
|
|
[ ${MEM} -ge 15000 ] && [ ${CORES} -ge 8 ] && OPTIMIZE_FLAG="--enable-optimizations --with-lto" || OPTIMIZE_FLAG=""
|
|
|
|
# 编译安装
|
|
cd src
|
|
./configure --prefix=${python_path} ${OPTIMIZE_FLAG} --enable-shared LDFLAGS="-Wl,-rpath,${python_path}/lib"
|
|
if [ $? -ne 0 ]; then
|
|
rm -rf ${python_path}
|
|
error "Configure failed"
|
|
fi
|
|
|
|
make "-j${j}"
|
|
if [ $? -ne 0 ]; then
|
|
rm -rf ${python_path}
|
|
error "Compilation failed"
|
|
fi
|
|
|
|
make install
|
|
if [ ! -f "${python_path}/bin/python3" ]; then
|
|
rm -rf ${python_path}
|
|
error "Python ${version} installation failed"
|
|
fi
|
|
|
|
# 清理源码
|
|
cd ${python_path}
|
|
rm -rf src
|
|
|
|
# 设置软链接
|
|
ln -sf ${python_path}/bin/python3 /usr/local/bin/python${slug}
|
|
ln -sf ${python_path}/bin/pip3 /usr/local/bin/pip${slug}
|
|
[ ! -f /usr/local/bin/python3 ] && ln -sf ${python_path}/bin/python3 /usr/local/bin/python3
|
|
[ ! -f /usr/local/bin/pip3 ] && ln -sf ${python_path}/bin/pip3 /usr/local/bin/pip3
|
|
|
|
# 国内设置pip镜像
|
|
if ${in_china}; then
|
|
${python_path}/bin/pip3 config --global set global.index-url https://mirrors.tencent.com/pypi/simple/
|
|
fi
|
|
|
|
# 创建环境变量配置
|
|
cat >${python_path}/env.sh <<EOF
|
|
export PYTHON_HOME=${python_path}
|
|
export PATH=\$PYTHON_HOME/bin:\$PATH
|
|
export LD_LIBRARY_PATH=\$PYTHON_HOME/lib:\$LD_LIBRARY_PATH
|
|
EOF
|
|
chmod 644 ${python_path}/env.sh
|
|
|
|
echo -e $HR
|
|
echo "Python ${version} installation successful"
|
|
echo "Python path: ${python_path}"
|
|
echo "To use this version, run: source ${python_path}/env.sh"
|
|
echo -e $HR
|