Skip to content

Commit

Permalink
Merge pull request #40 from quaxalber/development
Browse files Browse the repository at this point in the history
Add error messages on abort
  • Loading branch information
quaxalber authored Oct 29, 2023
2 parents 723226c + 12658ef commit 8dd3037
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions install_python_3.11.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ colored_output() {
fi
}

abort_install() {
local message="$1"
colored_output ${RED} "Aborting installation. ${message}"
exit 1
}

# Check if the script is running as root and elevate privileges if not
if [[ $EUID -ne 0 ]]; then
colored_output ${RED} "This script must be run as root. Attempting to elevate privileges..."
Expand All @@ -36,51 +42,62 @@ colored_output ${GREEN} "Installing Python $PY_VER_MINOR from source as Debian p

# Update package list and install prerequisites
colored_output ${GREEN} "Updating package list and installing prerequisites..."
apt-get update -y || exit 1
apt-get install -y build-essential zlib1g-dev libc6-dev libncursesw5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget checkinstall || exit 1

apt-get update -y || abort_install "Failed updating packages."

apt-get install -y build-essential zlib1g-dev libc6-dev libncursesw5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget checkinstall || abort_install "Failed installing required packages."

# Download and extract Python source code
colored_output ${GREEN} "Downloading Python $PY_VER_MINOR source code..."

PY_SRC_DIR="Python-$PY_VER_PATCH"
PY_TARBALL="$PY_SRC_DIR.tgz"
wget "https://www.python.org/ftp/python/$PY_VER_PATCH/$PY_TARBALL" || exit 1
tar -zxvf "$PY_TARBALL" || exit 1

wget "https://www.python.org/ftp/python/$PY_VER_PATCH/$PY_TARBALL" || abort_install "Failed downloading Python source."

tar -zxvf "$PY_TARBALL" || abort_install "Failed extracting Python source."

rm "$PY_TARBALL"

colored_output ${GREEN} "Configuring and compiling Python $PY_VER_MINOR..."

cd "$PY_SRC_DIR"

# Configure Python with optimizations
./configure --enable-optimizations
./configure --enable-optimizations || abort_install "Failed configuring Python."

# Compile Python utilizing all available cores
make -j$(nproc) || exit 1
make -j$(nproc) || abort_install "Failed compiling Python."

# Install Python as Debian package using checkinstall
colored_output ${GREEN} "Installing Python $PY_VER_MINOR as a Debian package..."
sudo checkinstall -y || exit 1

sudo checkinstall -y || abort_install "Failed installing Python as Debian package."

# Cleanup
colored_output ${GREEN} "Performing cleanup..."

cd ..

if [[ -z "$PY_SRC_DIR" ]]; then
colored_output ${RED} "PY_SRC_DIR is empty. Exiting to avoid potential data loss."
exit 1
fi

colored_output ${GREEN} "Going to remove no longer needed files and directories under: $PY_SRC_DIR "

# Check for automation flag to bypass prompt
if [ "$AUTO" == "true" ]; then
REPLY="y"
else
colored_output ${YELLOW} "Continue removal? (y/n): " -n
# Read single character input
read -n 1 -r
fi
colored_output ${GREEN} "Going to remove no longer needed files and directories under: $PY_SRC_DIR "

# Check for automation flag to bypass prompt
if [ "$AUTO" == "true" ]; then
REPLY="y"
else
colored_output ${YELLOW} "Continue removal? (y/n): " -n
# Read single character input
read -n 1 -r
fi

echo
echo

if [[ $REPLY =~ ^[Yy]$ ]]; then
rm -rf "$PY_SRC_DIR"
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm -rf "$PY_SRC_DIR"
fi
fi

# Verify installation
Expand Down

0 comments on commit 8dd3037

Please sign in to comment.