#! /bin/bash # DESCRIPTION: # Building Python from sources. # # DEPENDENCIES: # # PARAMETERS: # 1: "qn" - execution without pauses # 2: version of Python. Defaults to 3.9.5 # 3: path to log # # FUNCTIONS: # ####################################### # Print message and add to log. # Globals: # logs # Arguments: # 1: message to print and logging ####################################### addtologs() { echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}" } ####################################### # Waiting for press [ENTER]. # Globals: # None # Arguments: # None ####################################### execpause() { read -r -p "Press [ENTER] to continue... " } ####################################### # Exit procedure. # Globals: # show # Arguments: # None ####################################### execquite() { addtologs "execution time is $(($(date +%s)-time)) seconds, exit" if [ "${show}" != "qn" ]; then execpause fi exit } ####################################### # Error exit procedure with Telegram notification. # Globals: # telegramapiurl # telegramchatid # Arguments: # 1: message to print and logging ####################################### execerror() { addtologs "error: $1" execquite } ####################################### # Checking user rights. # Globals: # None # Arguments: # None # return: # 0 - if privileged rights, 1 - if not privileged rights ####################################### checkroot() { if [ "${EUID}" -ne 0 ]; then return 1 # false else return 0 # true fi } ####################################### # Installing required packages. # Globals: # None # Arguments: # None ####################################### installutils(){ if grep '^NAME=' /etc/os-release | grep -q 'Debian' || \ grep '^NAME=' /etc/os-release | grep -q 'Ubuntu'; then apt-get update apt-get install -y \ wget \ tar \ build-essential \ tk-dev \ libncurses5-dev \ libncursesw5-dev \ libreadline6-dev \ libdb5.3-dev \ libgdbm-dev \ libsqlite3-dev \ libssl-dev \ libbz2-dev \ libexpat1-dev \ liblzma-dev \ zlib1g-dev \ libffi-dev fi if grep '^NAME=' /etc/os-release | grep -q 'CentOS'; then yum install -y \ wget \ tar \ gcc \ openssl-devel \ bzip2-devel \ libffi-devel \ zlib-devel \ xz-devel fi } ####################################### # Downloading and unpacking sources. # Globals: # None # Arguments: # 1: version of Python ####################################### # shellcheck disable=SC2317 downloadcode(){ subvers=$1 version=$(echo "${subvers}" | cut -d. -f1,2) wget "https://www.python.org/ftp/python/${subvers}/Python-${subvers}.tgz" tar zxf "Python-${subvers}.tgz" } ####################################### # Downloading and unpacking sources. # Globals: # version # Arguments: # None ####################################### bulidingcode(){ cd "Python-${subvers}" || execerror "cd" ./configure --enable-optimizations --prefix=/usr/local/opt/python-"${version}" make } ####################################### # Installing and cleaning after. # Globals: # subvers # Arguments: # None ####################################### installclean(){ make altinstall cd .. rm -r "Python-${subvers}"* if grep '^NAME=' /etc/os-release | grep -q 'Debian' || \ grep '^NAME=' /etc/os-release | grep -q 'Ubuntu'; then apt-get autoremove -y fi if grep '^NAME=' /etc/os-release | grep -q 'CentOS'; then yum autoremove -y fi } # # VARIABLES: # show=$1 vers=$2 logs=$3 time=$(date +%s) cd "$(dirname "$(realpath "$0")")" || cd "${HOME}" || execerror "cd" if [ -z "${logs}" ]; then logs=/dev/null elif [ ! -e "${logs}" ]; then touch "${logs}" fi if [ -z "${vers}" ]; then vers=3.9.5 fi # # MAIN: # if checkroot; then addtologs "Installing dependencies..." if installutils >> "${logs}" 2>&1; then addtologs "Downolading sources..." if downloadcode "${vers}" >> "${logs}" 2>&1; then addtologs "Making package..." if bulidingcode >> "${logs}" 2>&1; then addtologs "Installing package and cleaning..." if installclean >> "${logs}" 2>&1; then addtologs "Installed $(/usr/local/opt/python-"${version}"/bin/python"${version}" -V)" execquite else execerror "Installing package and cleaning..." fi else execerror "Making package..." fi else execerror "Downolading sources..." fi else execerror "Installing dependencies..." fi else execerror "Restart this as root!" fi