diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index f60bb58e946..2c947b621b8 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -104,12 +104,13 @@ jobs: cache: 'pip' # caching pip dependencies - run: | echo $GITHUB_WORKSPACE - cd $GITHUB_WORKSPACE/.github - pip install -r requirements.txt + cd $GITHUB_WORKSPACE/Utilities/Python + source setup_python_env.sh --batchmode - name: Python test run: | echo $GITHUB_WORKSPACE cd $GITHUB_WORKSPACE/Utilities/Python + source $GITHUB_WORKSPACE/.github/fds_python_env/bin/activate python hello_world.py : # python FDS_verification_script.py diff --git a/Utilities/Python/setup_python_env.bat b/Utilities/Python/setup_python_env.bat new file mode 100644 index 00000000000..ada12ebe44f --- /dev/null +++ b/Utilities/Python/setup_python_env.bat @@ -0,0 +1,145 @@ +@echo off +REM Configure Spyder IDE for FDS (WINDOWS) +REM Usage (WINDOWS): +REM 1. Install Python 3.11 or below and add its "python.exe" folder to the PATH +REM 2. Open normal Windows Command Prompt +REM 3. call ./setup_python_env.bat [--batchmode] + +setlocal enabledelayedexpansion + +REM === Parse command-line arguments === +set BATCHMODE=false +for %%a in (%*) do ( + if "%%a"=="--batchmode" set BATCHMODE=true +) + +REM === Check for python === +where python >nul 2>nul +if errorlevel 1 ( + set ERROR_MSG=python is not installed or not in PATH + call :ERROR_EXIT +) + +REM === Ensure Python version >= 3.7 === +for /f "delims=" %%v in ('python -c "import sys; print(sys.version_info[0])"') do set PY_MAJOR=%%v +for /f "delims=" %%v in ('python -c "import sys; print(sys.version_info[1])"') do set PY_MINOR=%%v + +if %PY_MAJOR% LSS 3 ( + set ERROR_MSG=Python 3.7 or higher is required. Found %PY_MAJOR%.%PY_MINOR%. + call :ERROR_EXIT +) +if %PY_MAJOR%==3 if %PY_MINOR% LSS 7 ( + set ERROR_MSG=Python 3.7 or higher is required. Found %PY_MAJOR%.%PY_MINOR%. + call :ERROR_EXIT +) + +echo Python version is OK: %PY_MAJOR%.%PY_MINOR% + +REM === Save current directory and navigate to .github folder === +set CURDIR=%cd% +cd /d "%~dp0..\..\.." +if errorlevel 1 ( + set ERROR_MSG=Failed to navigate to repo root + call :ERROR_EXIT +) +set REPOROOT=%cd% +cd /d "%REPOROOT%\fds\.github" +if errorlevel 1 ( + set ERROR_MSG=Directory not found: %REPOROOT%\fds\.github + call :ERROR_EXIT +) + +REM === Setup virtual environment === +set VENV_DIR=fds_python_env +set INSTALL_REQUIREMENTS=false + +if exist "%VENV_DIR%" ( + if "%BATCHMODE%"=="true" ( + echo Batch mode: activating existing virtual environment without prompts or deletion. + ) else ( + echo ⚠ Virtual environment "%VENV_DIR%" already exists. + set /p choice="Do you want to reinstall everything? (y/N): " + if /i "!choice!"=="y" ( + REM Deactivate if currently active (only works if environment was manually activated) + if defined VIRTUAL_ENV ( + call deactivate + ) + echo Removing old environment... + rmdir /s /q "%VENV_DIR%" || ( + set ERROR_MSG=Failed to remove existing virtual environment + call :ERROR_EXIT + ) + echo Creating new virtual environment... + python -m venv "%VENV_DIR%" || ( + set ERROR_MSG=Failed to create virtual environment + call :ERROR_EXIT + ) + set INSTALL_REQUIREMENTS=true + ) else ( + echo Activating existing environment... + ) + ) +) else ( + echo Creating new virtual environment... + python -m venv "%VENV_DIR%" || ( + set ERROR_MSG=Failed to create virtual environment + call :ERROR_EXIT + ) + set INSTALL_REQUIREMENTS=true +) + +REM === Activate environment === +if not exist "%VENV_DIR%\Scripts\activate.bat" ( + set ERROR_MSG=Virtual environment activation script not found + call :ERROR_EXIT +) +call "%VENV_DIR%\Scripts\activate.bat" || ( + set ERROR_MSG=Failed to activate virtual environment + call :ERROR_EXIT +) + +REM === Upgrade pip and install requirements if flagged === +if "%INSTALL_REQUIREMENTS%"=="true" ( + echo Installing/updating required Python packages... + python -m pip install --upgrade pip || ( + set ERROR_MSG=Failed to upgrade pip + call :ERROR_EXIT + ) + if exist requirements.txt ( + python -m pip install -r requirements.txt || ( + set ERROR_MSG=Failed to install requirements + call :ERROR_EXIT + ) + ) +) + +REM === Set PYTHONPATH === +set PYTHONPATH=%REPOROOT%\fds\Utilities\Python;%PYTHONPATH% + +REM === Run test script === +cd /d "%REPOROOT%\fds\Utilities\Python" +if not exist hello_world.py ( + set ERROR_MSG=hello_world.py not found + call :ERROR_EXIT +) +python hello_world.py || ( + set ERROR_MSG=hello_world.py failed + call :ERROR_EXIT +) + +REM === Return to original directory === +cd /d "%CURDIR%" + +echo. +echo Python environment setup complete. + +REM === Error handler === +:ERROR_EXIT +if "%ERROR_MSG%"=="" ( + echo. +) else ( + echo *** Error: %ERROR_MSG% +) +exit /b 1 + +endlocal diff --git a/Utilities/Python/setup_python_env.sh b/Utilities/Python/setup_python_env.sh new file mode 100755 index 00000000000..76654cd604c --- /dev/null +++ b/Utilities/Python/setup_python_env.sh @@ -0,0 +1,94 @@ +#!/bin/bash +# Configure Spyder IDE for FDS +# Usage (Linux and macOS): +# source ./setup_spyder_env.sh + +set -e # Exit immediately if a command exits with a non-zero status + +BATCHMODE=false + +# Parse command-line arguments +for arg in "$@"; do + case $arg in + --batchmode) + BATCHMODE=true + shift + ;; + esac +done + +function error_exit { + echo "*** Error: $1" + return 1 +} + + +# Check Python version > 3.7 +PYTHON_VERSION=$(python3 -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))') +REQUIRED_VERSION=3.7 +if (( $(echo "$PYTHON_VERSION < $REQUIRED_VERSION" | bc -l) )); then + echo "Python 3.7 or higher is required. Found $PYTHON_VERSION." + echo "Stopping python environment setup." + return +else + echo "Python version is OK: $PYTHON_VERSION" +fi + +# Save current directory to return later +curdir=$(pwd) + +# Go to .github folder +cd "$(dirname "${BASH_SOURCE[0]}")/../../.." || error_exit "Failed to locate repo root" +reporoot=$(pwd) +cd "$reporoot/fds/.github" || error_exit "Directory not found: $reporoot/fds/.github" + +VENV_DIR="fds_python_env" +VENV_BIN="$VENV_DIR/bin" +INSTALL_REQUIREMENTS=false + +# Check if virtual environment exists +if [ -d "$VENV_DIR" ]; then + if [ "$BATCHMODE" = true ]; then + echo "Batch mode: activating existing virtual environment without prompts." + else + echo "Virtual environment '$VENV_DIR' already exists." + read -p "Do you want to reinstall everything? (y/N): " choice + case "$choice" in + [yY]|[yY][eE][sS]) + echo "Removing old environment..." + rm -rf "$VENV_DIR" || error_exit "Failed to remove existing virtual environment" + echo "Creating new virtual environment..." + python3 -m venv "$VENV_DIR" || error_exit "Failed to create virtual environment" + INSTALL_REQUIREMENTS=true + ;; + *) + echo "Activating existing environment..." + ;; + esac + fi +else + echo "Creating new virtual environment..." + python3 -m venv "$VENV_DIR" || error_exit "Failed to create virtual environment" + INSTALL_REQUIREMENTS=true +fi +source "$VENV_BIN/activate" || error_exit "Failed to activate virtual environment" + +# Install requirements only if flagged +if [ "$INSTALL_REQUIREMENTS" = true ]; then + echo "Installing/updating required Python packages..." + pip install --upgrade pip + if [ -f "requirements.txt" ]; then + pip install -r requirements.txt || error_exit "Failed to install requirements" + fi +fi + +export PYTHONPATH="$reporoot/fds/Utilities/Python:$PYTHONPATH" + +# Run test script to verify environment +cd "$reporoot/fds/Utilities/Python" || error_exit "Failed to find script directory" +python hello_world.py || error_exit "hello_world.py failed" + +# Return to original directory +cd "$curdir" + +echo "Python environment setup complete." diff --git a/Utilities/Python/setup_spyder_env.bat b/Utilities/Python/setup_spyder_env.bat deleted file mode 100644 index a95aa483a08..00000000000 --- a/Utilities/Python/setup_spyder_env.bat +++ /dev/null @@ -1,113 +0,0 @@ -@echo off -REM Configure spyder IDE for FDS (WINDOWS) -REM Usage (WINDOWS): -REM 1. Install Python 3.11 or below and add its "python.exe" folder to the PATH -REM 2. Open normal Windows Command prompt -REM 3. call ./setup_spyder_env.bat -REM 4. spyder - - - -setlocal enabledelayedexpansion - -REM === Check for python === -where python >nul 2>nul -if errorlevel 1 ( - set ERROR_MSG=python is not installed or not in PATH - call :ERROR_EXIT -) - -REM === Ensure it's Python 3 === -for /f "delims=" %%v in ('python -c "import sys; print(sys.version_info[0])"') do ( - if not %%v==3 ( - set ERROR_MSG=Python 3 is required but not found - call :ERROR_EXIT - ) -) - -REM === Save current directory and move to .github folder=== -set CURDIR=%cd% -cd /d "%~dp0..\..\.." -if errorlevel 1 ( - set ERROR_MSG=Failed to navigate to repo root - call :ERROR_EXIT -) -set REPOROOT=%cd% -cd /d "%REPOROOT%\fds\.github" -if errorlevel 1 ( - set ERROR_MSG=Directory not found: %REPOROOT%\fds\.github - call :ERROR_EXIT -) - -REM === Create venv if not exists and activate it== -set VENV_DIR=fds_python_env -if not exist "%VENV_DIR%" ( - python -m venv "%VENV_DIR%" - if errorlevel 1 ( - set ERROR_MSG=Failed to create virtual environment - call :ERROR_EXIT - ) -) -if not exist "%VENV_DIR%\Scripts\activate.bat" ( - set ERROR_MSG=Virtual environment activation script not found - call :ERROR_EXIT -) -call "%VENV_DIR%\Scripts\activate.bat" -if errorlevel 1 ( - set ERROR_MSG=Failed to activate virtual environment - call :ERROR_EXIT -) - -REM === Upgrade pip and install requirements === -python -m pip install --upgrade pip -if errorlevel 1 ( - set ERROR_MSG=Failed to upgrade pip - call :ERROR_EXIT -) - -if exist requirements.txt ( - python -m pip install -r requirements.txt - if errorlevel 1 ( - set ERROR_MSG=Failed to install requirements - call :ERROR_EXIT - ) -) - -REM === Install Spyder IDE === -python -m pip install spyder -if errorlevel 1 ( - set ERROR_MSG=Failed to install Spyder - call :ERROR_EXIT -) - -set PYTHONPATH=%REPOROOT%\fds\Utilities\Python;%PYTHONPATH% - -REM === Run test script === -cd /d "%REPOROOT%\fds\Utilities\Python" -if not exist hello_world.py ( - set ERROR_MSG=hello_world.py not found - call :ERROR_EXIT -) - -python hello_world.py -if errorlevel 1 ( - set ERROR_MSG=hello_world.py failed - call :ERROR_EXIT -) - -cd /d "%CURDIR%" - -echo. -echo Python environment setup complete. - -REM === Function-like error handler using variable === -:ERROR_EXIT -if "%ERROR_MSG%"=="" ( - echo -) else ( - echo ***Error: %ERROR_MSG% -) -exit /b 1 - - -endlocal diff --git a/Utilities/Python/setup_spyder_env.sh b/Utilities/Python/setup_spyder_env.sh deleted file mode 100755 index 174e0ca2067..00000000000 --- a/Utilities/Python/setup_spyder_env.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash -# Configure spyder IDE for FDS -# Usage (Linux and MacOS): -# 1. source ./setup_spyder_env.sh -# 2. spyder - -function error_exit { - echo "***Error: $1" - return 1 -} - -# Check for python3 -if ! command -v python3 >/dev/null 2>&1; then - error_exit "python3 is not installed" -fi - -curdir=$(pwd) -cd "$(dirname "${BASH_SOURCE[0]}")/../../.." || error_exit "Failed to locate repo root" -reporoot=$(pwd) - -# Create and activate virtual environment -cd "$reporoot/fds/.github" || error_exit "Directory not found: $reporoot/fds/.github" -VENV_DIR="fds_python_env" -VENV_BIN="$VENV_DIR/bin" -if [ ! -d "$VENV_DIR" ]; then - python3 -m venv "$VENV_DIR" || error_exit "Failed to create virtual environment" -fi -source "$VENV_BIN/activate" - -# Install requirements -pip install --upgrade pip -pip install -r requirements.txt || error_exit "Failed to install requirements" -pip install spyder - -# Set PYTHONPATH -export PYTHONPATH="$reporoot/fds/Utilities/Python:$PYTHONPATH" - -# Run test script -cd "$reporoot/fds/Utilities/Python" || error_exit "Failed to find script directory" -python hello_world.py || error_exit "hello_world.py failed" - -# Return to original directory -cd "$curdir" - -echo "Python environment setup complete." -