1#!/bin/bash 2# Script used only in CD pipeline 3set -uex -o pipefail 4 5PYTHON_DOWNLOAD_URL=https://www.python.org/ftp/python 6PYTHON_DOWNLOAD_GITHUB_BRANCH=https://github.com/python/cpython/archive/refs/heads 7GET_PIP_URL=https://bootstrap.pypa.io/get-pip.py 8 9# Python versions to be installed in /opt/$VERSION_NO 10CPYTHON_VERSIONS=${CPYTHON_VERSIONS:-"3.8.1 3.9.0 3.10.1 3.11.0 3.12.0 3.13.0"} 11 12function check_var { 13 if [ -z "$1" ]; then 14 echo "required variable not defined" 15 exit 1 16 fi 17} 18 19function do_cpython_build { 20 local py_ver=$1 21 local py_folder=$2 22 check_var $py_ver 23 check_var $py_folder 24 tar -xzf Python-$py_ver.tgz 25 pushd $py_folder 26 27 local prefix="/opt/_internal/cpython-${py_ver}" 28 mkdir -p ${prefix}/lib 29 if [[ -n $(which patchelf) ]]; then 30 local shared_flags="--enable-shared" 31 else 32 local shared_flags="--disable-shared" 33 fi 34 if [[ -z "${WITH_OPENSSL+x}" ]]; then 35 local openssl_flags="" 36 else 37 local openssl_flags="--with-openssl=${WITH_OPENSSL} --with-openssl-rpath=auto" 38 fi 39 40 # -Wformat added for https://bugs.python.org/issue17547 on Python 2.6 41 CFLAGS="-Wformat" ./configure --prefix=${prefix} ${openssl_flags} ${shared_flags} > /dev/null 42 43 make -j40 > /dev/null 44 make install > /dev/null 45 46 if [[ "${shared_flags}" == "--enable-shared" ]]; then 47 patchelf --set-rpath '$ORIGIN/../lib' ${prefix}/bin/python3 48 fi 49 50 popd 51 rm -rf $py_folder 52 # Some python's install as bin/python3. Make them available as 53 # bin/python. 54 if [ -e ${prefix}/bin/python3 ]; then 55 ln -s python3 ${prefix}/bin/python 56 fi 57 ${prefix}/bin/python get-pip.py 58 if [ -e ${prefix}/bin/pip3 ] && [ ! -e ${prefix}/bin/pip ]; then 59 ln -s pip3 ${prefix}/bin/pip 60 fi 61 # install setuptools since python 3.12 is required to use distutils 62 ${prefix}/bin/pip install wheel==0.34.2 setuptools==68.2.2 63 local abi_tag=$(${prefix}/bin/python -c "from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag; print('{0}{1}-{2}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag()))") 64 ln -s ${prefix} /opt/python/${abi_tag} 65} 66 67function build_cpython { 68 local py_ver=$1 69 check_var $py_ver 70 check_var $PYTHON_DOWNLOAD_URL 71 local py_ver_folder=$py_ver 72 if [ "$py_ver" = "3.13.0" ]; then 73 PY_VER_SHORT="3.13" 74 check_var $PYTHON_DOWNLOAD_GITHUB_BRANCH 75 wget $PYTHON_DOWNLOAD_GITHUB_BRANCH/$PY_VER_SHORT.tar.gz -O Python-$py_ver.tgz 76 do_cpython_build $py_ver cpython-$PY_VER_SHORT 77 else 78 wget -q $PYTHON_DOWNLOAD_URL/$py_ver_folder/Python-$py_ver.tgz 79 do_cpython_build $py_ver Python-$py_ver 80 fi 81 82 rm -f Python-$py_ver.tgz 83} 84 85function build_cpythons { 86 check_var $GET_PIP_URL 87 curl -sLO $GET_PIP_URL 88 for py_ver in $@; do 89 build_cpython $py_ver 90 done 91 rm -f get-pip.py 92} 93 94mkdir -p /opt/python 95mkdir -p /opt/_internal 96build_cpythons $CPYTHON_VERSIONS 97