python - install python version to custom prefix or altinstall

This commit is contained in:
2023-02-03 10:25:05 +01:00
parent 655beece68
commit 9ba4335bcc
2 changed files with 128 additions and 0 deletions

5
seqs/python.cfg.example Normal file
View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bash
readonly sc_pyVersion=3.10
# If empty, configure (for altinstall) uses default value (/usr/local)
readonly sc_pyPrefix= #todo

123
seqs/python.sh Executable file
View File

@@ -0,0 +1,123 @@
#!/usr/bin/env bash
readonly toolName=python
readonly buildDeps=(build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev)
readonly buildHome="/tmp/pythonbuild"
readonly buildTgz="${buildHome}/py.tgz"
# Already defined by sequencer.sh, but may be overwritten
#readonly seq_configName="${sq_scriptName:?}.cfg"
#readonly seq_configTemplate="${seq_origin:?}/${sq_configName:?}.example"
sq_aptOpt=
sq_config=0
req_pyVersion=
sq_pyVersion=
seq_config() {
## or to use sequencer api with global config file:
if ! initSeqConfig "${seq_configName:?}" "${seq_configTemplate:?}" ; then
# End if no configuration file exists
dry || return 1
fi
## Apt cmdline option to suppress user interaction
interactive || sq_aptOpt="-y"
## Disable error checks if external scripts are used
## e.g. error on unbound variables
#disableErrorCheck
## Return of non zero value will abort the sequence
return 0
}
step_1_info() { echo 'Status'; }
step_1_alias() { echo 'status'; }
step_1() {
local pyPath=
if pyPath=$(which "python${sc_pyVersion}") ; then
info "$(python${sc_pyVersion} --version) installed"
info -a "at ($pyPath)"
else
info "${toolName} version ${sc_pyVersion} not installed"
fi
}
step_10_info() { echo 'Setup build environment'; }
step_10_alias() { echo 'buildsetup'; }
step_10() {
exe apt update
exe apt install "${buildDeps[@]}" ${sq_aptOpt}
}
step_11_info() { echo "(Alt)install given version"; }
step_11_options() { echo "[VERSION]"; }
step_11_alias() { echo "altinstall"; }
step_11() {
shift
local downUrl=
local version="${sc_pyVersion:?}"
[ -f "${buildTgz}" ] && return 0
[ -n "${1:-}" ] && version="${1}"
if ! downUrl="$(getSourceUrl "${version}")" ; then
fatal "Could not determine download link for version ${1:-}"
fi
info "Downloading $sq_pyVersion from $downUrl"
exe mkdir -p "${buildHome}"
exe wget -O "${buildTgz}" "${downUrl}"
}
step_12() {
local lPrefix=
[ -z "${sq_pyVersion}" ] && getSourceUrl "${sc_pyVersion}" >>/dev/null
exe cd "${buildHome}"
exe tar xf "${buildTgz}"
exe cd "Python-${sq_pyVersion}"
if [ -n "${sc_pyPrefix}" ] ; then
exe mkdir -p "${sc_pyPrefix}"
endReturn "Couldn't create prefix directory ${sc_pyPrefix}"
lPrefix="--prefix=${sc_pyPrefix}"
fi
exe ./configure --enable-optimizations "${lPrefix}"
}
step_13() {
[ -z "${sq_pyVersion}" ] && getSourceUrl "${sc_pyVersion}" >>/dev/null
exe cd "${buildHome}/Python-${sq_pyVersion}"
# Leave one processor for other tasks
exe make -j 3
}
step_14() {
local lInstall="altinstall"
[ -z "${sq_pyVersion}" ] && getSourceUrl "${sc_pyVersion}" >>/dev/null
exe cd "${buildHome}/Python-${sq_pyVersion}"
[ -n "${sc_pyPrefix}" ] && lInstall="install"
exe make "${lInstall}"
}
# getSourceUrl <VERSION>
# <VERSION> : can be only the major version or more specific
#
# Get the download URL for the latest .tgz file for the given version.
#
# Examples (all valid):
# getSourceUrl 3
# getSourceUrl 3.10
# getSourceUrl 3.10.8
getSourceUrl() {
[[ ${1:-} =~ ^[0-9\.]+$ ]] || return 1
local downUrl=
downUrl="$(curl --silent -L "https://www.python.org/downloads/source/" | \
grep --max-count 1 -Po "href=\"\K.*Python-${1//./\\.}.*?(?=\")")"
[[ -z "${downUrl}" ]] && return 1
sq_pyVersion="$(grep -Po "python/\K.*?(?=/)" <<<"${downUrl}")"
echo "${downUrl}"
}
# shellcheck disable=SC2034 # Appears unused
readonly sqr_minVersion=16
# shellcheck disable=SC1091 # Don't follow this source
. /usr/local/bin/sequencer.sh