From 9227219893d0a11058bd2b0fb0f41b1b28d095c4 Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Wed, 8 May 2024 15:29:02 +0900 Subject: [PATCH] Enhance error handling for init.sh --- scripts/init/init.sh | 25 ++++++++++++++++--------- scripts/init/requirements.txt | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/scripts/init/init.sh b/scripts/init/init.sh index e3f70472..2d0dcb23 100755 --- a/scripts/init/init.sh +++ b/scripts/init/init.sh @@ -25,23 +25,30 @@ else exit 1 fi -# Check if venv module is available and python3-venv is installed -if ! python3 -c "import venv ensurepip" &> /dev/null; then - if ! dpkg -s python${PYTHON_MAJOR}.${PYTHON_MINOR}-venv &> /dev/null; then - echo "python3-venv package for Python ${PYTHON_MAJOR}.${PYTHON_MINOR} is not installed. Installing..." +# Try creating the virtual environment first +echo "Attempting to create a virtual environment..." +if python3 -m venv "$SCRIPT_DIR/initPyEnv"; then + echo "Virtual environment created successfully." +else + echo "Failed to create the virtual environment. Checking for ensurepip..." + # Check if venv module and ensurepip are available + if ! python3 -c "import venv, ensurepip" &> /dev/null; then + echo "venv or ensurepip module is not available. Installing python3-venv..." sudo apt update sudo apt install -y python${PYTHON_MAJOR}.${PYTHON_MINOR}-venv if [[ $? -ne 0 ]]; then - echo "Failed to install python${PYTHON_MAJOR}.${PYTHON_MINOR}-venv. Please check the package availability or use DeadSnakes PPA." + echo "Failed to install python${PYTHON_MAJOR}.${PYTHON_MINOR}-venv. Please check the package availability." + exit 1 + fi + # Retry creating the virtual environment + if ! python3 -m venv "$SCRIPT_DIR/initPyEnv"; then + echo "Failed to create the virtual environment after installing python3-venv." exit 1 fi fi -else - echo "venv module is available." fi -echo "Creating and activating the virtual environment..." -python3 -m venv "$SCRIPT_DIR/initPyEnv" +# Activate the virtual environment source "$SCRIPT_DIR/initPyEnv/bin/activate" echo diff --git a/scripts/init/requirements.txt b/scripts/init/requirements.txt index ddb38b97..1915f724 100644 --- a/scripts/init/requirements.txt +++ b/scripts/init/requirements.txt @@ -2,7 +2,7 @@ requests==2.31.0 # Used for parsing and creating YAML files -PyYAML==6.0 +PyYAML==6.0.1 # Used for displaying progress bars tqdm==4.66.3