From f03b346ba39575f4ecc33c5345e62a7e30ff5edd Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Wed, 25 Dec 2024 15:26:04 +0800 Subject: [PATCH] feat: enhance Python installation script with requirements handling and dependency updates - Added jq to the dependencies in the Docker base image installation script. - Updated the Python installation script to support handling of custom requirements via command-line arguments. - Implemented a new function to manage requirements installation, falling back to a default requirements file if none is provided. - Improved version check logic to prevent redundant installations of Python versions, enhancing efficiency in Docker environments. --- docker/base-image/install/deps/deps.sh | 1 + docker/base-image/install/python/python.sh | 44 ++++++++++++++-------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/docker/base-image/install/deps/deps.sh b/docker/base-image/install/deps/deps.sh index 11a33dfc..944ba83e 100644 --- a/docker/base-image/install/deps/deps.sh +++ b/docker/base-image/install/deps/deps.sh @@ -24,6 +24,7 @@ apt-get install -y \ nginx \ unzip \ zip \ + jq \ gnupg2 \ libc6 diff --git a/docker/base-image/install/python/python.sh b/docker/base-image/install/python/python.sh index cc129c32..5427711d 100644 --- a/docker/base-image/install/python/python.sh +++ b/docker/base-image/install/python/python.sh @@ -5,7 +5,7 @@ set -e # Function to print usage print_usage() { - echo "Usage: $0 [version]" + echo "Usage: $0 [version] [requirements]" echo "Commands:" echo " install - Install Python version (default: 3.12)" echo " uninstall - Uninstall Python version" @@ -86,18 +86,41 @@ cleanup() { apt-get autoremove -y } +# Function to handle requirements +handle_requirements() { + local requirements_content="$1" + if [ -n "$requirements_content" ]; then + REQUIREMENTS_FILE="/tmp/requirements_$(date +%s)_$RANDOM.txt" + echo "$requirements_content" > "$REQUIREMENTS_FILE" + pip install -r "$REQUIREMENTS_FILE" + rm "$REQUIREMENTS_FILE" + else + # Fallback to default requirements file if it exists + if [ -f "/app/install/python/requirements.txt" ]; then + pip install -r /app/install/python/requirements.txt + fi + fi +} + # Main logic command="${1:-install}" version="${2:-3.12}" +requirements="${3:-}" case $command in "install") - install_dependencies setup_pyenv - pyenv install $version - pyenv global $version + # Check if version is already installed + if pyenv versions | grep -q $version; then + echo "Python $version is already installed. Switching to it..." + pyenv global $version + else + install_dependencies + pyenv install $version + pyenv global $version + fi verify_python $version - pip install -r /app/install/python/requirements.txt + handle_requirements "$requirements" create_symlinks cleanup ;; @@ -111,17 +134,6 @@ case $command in pyenv uninstall -f $version ;; - "switch") - if [ -z "$version" ]; then - echo "Please specify a version to switch to" - exit 1 - fi - setup_pyenv - pyenv global $version - verify_python $version - create_symlinks - ;; - "list") setup_pyenv pyenv versions