Created
June 4, 2023 16:03
-
-
Save dmoruzzi/7f37160c8ddf112c8d77b28a84872f14 to your computer and use it in GitHub Desktop.
Python 2.7 Installation Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
# Check if running as root | |
if [[ $EUID -eq 0 ]]; then | |
echo "This script should not be run as root; exiting..." | |
exit 1 | |
fi | |
# Set build directory from user input or command line argument | |
if [[ -z $1 ]]; then | |
read -p "Enter Python dist path: " build_dir | |
else | |
build_dir="$1" | |
fi | |
# Ensure build directory exists | |
mkdir -p "$build_dir" | |
# Navigate to build directory | |
cd "$build_dir" || { | |
echo "Unable to navigate to build directory" | |
exit 3 | |
} | |
# Make a request to the API to get all latest versions of Python; store latest 2.7 version in a variable | |
py_versions=$(curl -s https://endoflife.date/api/python.json | grep -oE '"latest":"([^"]+)"' | awk -F'"' '{print $4}') | |
py_latest_2_7=$(echo "$py_versions" | grep -E '^2\.7\.' | sort -V | tail -n1) | |
# Set default value if latest 2.7 version is not found | |
if [[ -z $py_latest_2_7 ]]; then | |
echo "Unable to find latest 2.7 version; setting to 2.7.18" | |
py_latest_2_7="2.7.18" | |
fi | |
# Handle file locations | |
temp_dir=$(mktemp -d) | |
filename="$temp_dir/Python-$py_latest_2_7.tgz" | |
extract_dir="$temp_dir/extracted" | |
mkdir -p "$extract_dir" | |
extracted_dir="$extract_dir/Python-$py_latest_2_7" | |
# Download the latest version of Python | |
curl -o "$filename" "https://www.python.org/ftp/python/$py_latest_2_7/Python-$py_latest_2_7.tgz" | |
# Verify the MD5 checksum of the downloaded file | |
if [[ "$py_latest_2_7" == "2.7.18" ]]; then | |
expected_md5="38c84292658ed4456157195f1c9bcbe1" | |
actual_md5=$(md5sum "$filename" | awk '{print $1}') | |
if [[ "$expected_md5" != "$actual_md5" ]]; then | |
echo "MD5sum of downloaded file does not match expected value" | |
exit 4 | |
fi | |
elif [[ ! -f "$filename" ]]; then | |
echo "Unable to find downloaded file" | |
exit 5 | |
fi | |
# Extract the contents of the .tgz file; set extracted directory variable | |
tar -xf "$filename" -C "$extract_dir" | |
# Configure, build, and install Python | |
"$extracted_dir/configure" --prefix="$build_dir" --enable-optimizations --with-ensurepip=install --enable-shared --with-lto | |
make -j"$(nproc)" | |
make altinstall | |
# Clean up the temporary directory | |
rm -rf "$temp_dir" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment