From 1b4799528c55b863c4945c7d25a645ee1510552e Mon Sep 17 00:00:00 2001 From: Young Joon Lee Date: Sat, 16 Mar 2024 15:05:07 +0900 Subject: [PATCH] feat(chezmoi): add script to install Python and manage Python versions. --- .../bin/executable_install-python.tmpl | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 chezmoi/dot_local/bin/executable_install-python.tmpl diff --git a/chezmoi/dot_local/bin/executable_install-python.tmpl b/chezmoi/dot_local/bin/executable_install-python.tmpl new file mode 100644 index 0000000..b6d82df --- /dev/null +++ b/chezmoi/dot_local/bin/executable_install-python.tmpl @@ -0,0 +1,54 @@ +#!/bin/bash + +# Function to install Python dependencies +install_dependencies() { + sudo apt update + sudo apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev \ + libssl-dev libreadline-dev libffi-dev wget +} + +# Function to download and install Python +install_python() { + local python_version=$1 + local python_url="https://www.python.org/ftp/python/${python_version}/Python-${python_version}.tgz" + + wget "$python_url" + tar -xf "Python-${python_version}.tgz" + cd "Python-${python_version}" || exit + + ./configure --enable-optimizations + make -j "$(nproc)" + sudo make altinstall + + cd .. + rm -rf "Python-${python_version}" "Python-${python_version}.tgz" +} + +# Function to update alternatives for Python +update_alternatives() { + local python_version=$1 + local python_priority=$2 + + sudo update-alternatives --install /usr/bin/python python "/usr/local/bin/python${python_version%.*}" "$python_priority" + sudo update-alternatives --install /usr/bin/pip pip "/usr/local/bin/pip${python_version%.*}" "$python_priority" +} + +# Main script +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root or with sudo." + exit 1 +fi + +read -rp "Enter the Python version to install (e.g., 3.9.7): " python_version +read -rp "Enter the priority for the Python version (e.g., 1): " python_priority + +echo "Installing Python dependencies..." +install_dependencies + +echo "Installing Python $python_version..." +install_python "$python_version" + +echo "Updating alternatives for Python..." +update_alternatives "$python_version" "$python_priority" + +echo "Python $python_version installation completed."