From 81de2d24df210f0ee88bc8cab0f5e3f55c9cb825 Mon Sep 17 00:00:00 2001 From: "pavel.muhortov" Date: Sat, 25 Mar 2023 18:17:23 +0300 Subject: [PATCH] add build-python.sh --- README.md | 101 +++++++++++++++------- build-python.sh | 220 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 291 insertions(+), 30 deletions(-) create mode 100644 build-python.sh diff --git a/README.md b/README.md index 2f2ea04..0a0baf7 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ Small tools needed to solve immediate tasks independently or as part of a project +* [`build-python`.sh](https://git.hmp.today/pavel.muhortov/utils#build-python-sh) * [`cronutil`](https://git.hmp.today/pavel.muhortov/utils#cronutil) * [`confutil`.py](https://git.hmp.today/pavel.muhortov/utils#confutil-py) * [`sendmail`.py](https://git.hmp.today/pavel.muhortov/utils#sendmail-py) @@ -9,28 +10,56 @@ Small tools needed to solve immediate tasks independently or as part of a projec ____ +## `build-python`.sh + +**Description:** +> Building Python from sources. + +**Dependencies:** +> +> * [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) + +| POSITION | PARAMETERS | DESCRIPTION | DEFAULT | +|-----------|--------------|------------------------|---------------| +| 1 |**[qn]**|execution without pauses|| +| 2 |**[version]**|version of Python|`3.9.5`| +| 3 |**[path/to/log]**|path to log|`/dev/null`| + +Example usage in terminal with make the script executable: + +```bash +wget https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/build-python.sh +chmod u+x ./build-python.sh +sudo ./build-python.sh qn 3.9.5 ./build-python.log +``` + +____ + ## `cronutil` -**Description:** Control wrapper for the [schedule](https://github.com/dbader/schedule) package -**Dependencies:** Python 3 (tested version 3.9.5) +**Description:** +> Control wrapper for the [schedule](https://github.com/dbader/schedule) package. -Scheduler works only on a weekly scale. This is due to the use of the [schedule](https://github.com/dbader/schedule) package . +**Dependencies:** +> +> * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) + +Scheduler works only on a weekly scale. This is due to the use of the [schedule](https://github.com/dbader/schedule) package. Target format: wD:HH:MM:SS, where: +> +> wD - day of week unit: `'1'`, `'2'`, ... , `'6'`, `'7'`, `'*'` +> HH - hour unit in 24-hours format: `'00'`, `'01'`, ... , `'22'`, `'23'`, `'**'` +> MM - minute unit: `'00'`, `'01'`, ... , `'58'`, `'59'`, `'**'` +> SS - second unit: `'00'`, `'01'`, ... , `'58'`, `'59'`, `'**'` -wD - day of week unit: `'1'`, `'2'`, ... , `'6'`, `'7'`, `'*'` -HH - hour unit in 24-hours format: `'00'`, `'01'`, ... , `'22'`, `'23'`, `'**'` -MM - minute unit: `'00'`, `'01'`, ... , `'58'`, `'59'`, `'**'` -SS - second unit: `'00'`, `'01'`, ... , `'58'`, `'59'`, `'**'` - -Units can be listed separated by commas. -Examples: -`'*:**:**:**'` - every second of every minute of every hour of every day -`'*:**:**:*5'` - every 05,15,25,35,45,55 seconds of every minute of every hour of every day -`'*:2*:**:**'` - every second of every minute of every 20,21,22,23 hours of every day -`'*:2*:**:*5'` - every 05,15,25,35,45,55 seconds of every minute of every 20,21,22,23 hours of every day -`'3,5:2*:**:*5'` - every 05,15,25,35,45,55 seconds every minute every 20,21,22,23 hours of Wednesday, Friday -`'1,7:12:00:**'` - every second 00 minutes 12 hours of Monday, Sunday -`'1:07:00:00'` - every 00 seconds 00 minutes 07 hours Monday +Units can be listed separated by commas. Examples: +> `'*:**:**:**'` - every second of every minute of every hour of every day +> `'*:**:**:*5'` - every 05,15,25,35,45,55 seconds of every minute of every hour of every day +> `'*:2*:**:**'` - every second of every minute of every 20,21,22,23 hours of every day +> `'*:2*:**:*5'` - every 05,15,25,35,45,55 seconds of every minute of every 20,21,22,23 hours of every day +> `'3,5:2*:**:*5'` - every 05,15,25,35,45,55 seconds every minute every 20,21,22,23 hours of Wednesday, Friday +> `'1,7:12:00:**'` - every second 00 minutes 12 hours of Monday, Sunday +> `'1:07:00:00'` - every 00 seconds 00 minutes 07 hours Monday Example usage in Python: @@ -53,8 +82,12 @@ ____ ## `confutil`.py -**Description:** Parser of configs, arguments, parameters -**Dependencies:** Python 3 (tested version 3.9.5) +**Description:** +> Parser of configs, arguments, parameters. + +**Dependencies:** +> +> * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) Example config to parse: @@ -89,8 +122,12 @@ ____ ## `sendmail`.py -**Description:** Sending email from Python -**Dependencies:** Python 3 (tested version 3.9.5) +**Description:** +> Sending email from Python. + +**Dependencies:** +> +> * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) | PARAMETERS | DESCRIPTION | DEFAULT| |-------------|-------------|--------| @@ -110,13 +147,13 @@ ____ Example usage in terminal with Python: -```shell +```bash python3 ./sendmail.py -u user@gmail.com -p pass -d addr1@gmail.com,addr2@gmail.com ``` Example usage in terminal with make the script executable: -```shell +```bash chmod u+x ./sendmail.py ./sendmail.py -u user@gmail.com -p pass -d addr1@gmail.com,addr2@gmail.com ``` @@ -135,11 +172,15 @@ ____ ## `simplewc`.py -**Description:** Update Let's Encrypt wildcard certificate with DNS-01 challenge -**Dependencies:** Python 3 (tested version 3.9.5), -installed or downloaded [acme.sh](https://github.com/Neilpang/acme.sh), -installed [dnspython](https://github.com/rthalley/dnspython) package, -dns is supported to [dynamic update](https://en.wikipedia.org/wiki/Dynamic_DNS) +**Description:** +> Update Let's Encrypt wildcard certificate with DNS-01 challenge. + +**Dependencies:** +> +> * [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) +> * installed or downloaded [acme.sh](https://github.com/Neilpang/acme.sh) +> * installed [dnspython](https://github.com/rthalley/dnspython) package +> * dns is supported to [dynamic update](https://en.wikipedia.org/wiki/Dynamic_DNS) | PARAMETERS | DESCRIPTION | DEFAULT| |-------------|-------------|--------| @@ -154,7 +195,7 @@ dns is supported to [dynamic update](https://en.wikipedia.org/wiki/Dynamic_DNS) Example usage in cron with Python: -```shell +```bash # at 00:00 on Monday 0 0 * * 1 /usr/bin/python3 ~/simplewc.py --domain EXAMPLE.COM --server 8.8.8.8 --keyname KEY --keydata YOU_KEY_CONTENT > /dev/null # 00:00 on day-of-month 1 and 15 @@ -164,7 +205,7 @@ Example usage in cron with Python: Example usage in terminal with make the script executable: -```shell +```bash chmod u+x ./simplewc.py ./simplewc.py --domain EXAMPLE.COM --server 8.8.8.8 --keyname KEY --keydata YOU_KEY_CONTENT --test --force ./simplewc.py --domain EXAMPLE.COM --server dyn.dns.he.net --keyname - --keydata YOU_DDNSKEY --test --force diff --git a/build-python.sh b/build-python.sh new file mode 100644 index 0000000..178c638 --- /dev/null +++ b/build-python.sh @@ -0,0 +1,220 @@ +#! /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")")" || 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