|
| 1 | +@echo off |
| 2 | +REM Configure Spyder IDE for FDS (WINDOWS) |
| 3 | +REM Usage (WINDOWS): |
| 4 | +REM 1. Install Python 3.11 or below and add its "python.exe" folder to the PATH |
| 5 | +REM 2. Open normal Windows Command Prompt |
| 6 | +REM 3. call ./setup_python_env.bat [--batchmode] |
| 7 | + |
| 8 | +setlocal enabledelayedexpansion |
| 9 | + |
| 10 | +REM === Parse command-line arguments === |
| 11 | +set BATCHMODE=false |
| 12 | +for %%a in (%*) do ( |
| 13 | + if "%%a"=="--batchmode" set BATCHMODE=true |
| 14 | +) |
| 15 | + |
| 16 | +REM === Check for python === |
| 17 | +where python >nul 2>nul |
| 18 | +if errorlevel 1 ( |
| 19 | + set ERROR_MSG=python is not installed or not in PATH |
| 20 | + call :ERROR_EXIT |
| 21 | +) |
| 22 | + |
| 23 | +REM === Ensure Python version >= 3.7 === |
| 24 | +for /f "delims=" %%v in ('python -c "import sys; print(sys.version_info[0])"') do set PY_MAJOR=%%v |
| 25 | +for /f "delims=" %%v in ('python -c "import sys; print(sys.version_info[1])"') do set PY_MINOR=%%v |
| 26 | + |
| 27 | +if %PY_MAJOR% LSS 3 ( |
| 28 | + set ERROR_MSG=Python 3.7 or higher is required. Found %PY_MAJOR%.%PY_MINOR%. |
| 29 | + call :ERROR_EXIT |
| 30 | +) |
| 31 | +if %PY_MAJOR%==3 if %PY_MINOR% LSS 7 ( |
| 32 | + set ERROR_MSG=Python 3.7 or higher is required. Found %PY_MAJOR%.%PY_MINOR%. |
| 33 | + call :ERROR_EXIT |
| 34 | +) |
| 35 | + |
| 36 | +echo Python version is OK: %PY_MAJOR%.%PY_MINOR% |
| 37 | + |
| 38 | +REM === Save current directory and navigate to .github folder === |
| 39 | +set CURDIR=%cd% |
| 40 | +cd /d "%~dp0..\..\.." |
| 41 | +if errorlevel 1 ( |
| 42 | + set ERROR_MSG=Failed to navigate to repo root |
| 43 | + call :ERROR_EXIT |
| 44 | +) |
| 45 | +set REPOROOT=%cd% |
| 46 | +cd /d "%REPOROOT%\fds\.github" |
| 47 | +if errorlevel 1 ( |
| 48 | + set ERROR_MSG=Directory not found: %REPOROOT%\fds\.github |
| 49 | + call :ERROR_EXIT |
| 50 | +) |
| 51 | + |
| 52 | +REM === Setup virtual environment === |
| 53 | +set VENV_DIR=fds_python_env |
| 54 | +set INSTALL_REQUIREMENTS=false |
| 55 | + |
| 56 | +if exist "%VENV_DIR%" ( |
| 57 | + if "%BATCHMODE%"=="true" ( |
| 58 | + echo Batch mode: activating existing virtual environment without prompts or deletion. |
| 59 | + ) else ( |
| 60 | + echo ⚠ Virtual environment "%VENV_DIR%" already exists. |
| 61 | + set /p choice="Do you want to reinstall everything? (y/N): " |
| 62 | + if /i "!choice!"=="y" ( |
| 63 | + REM Deactivate if currently active (only works if environment was manually activated) |
| 64 | + if defined VIRTUAL_ENV ( |
| 65 | + call deactivate |
| 66 | + ) |
| 67 | + echo Removing old environment... |
| 68 | + rmdir /s /q "%VENV_DIR%" || ( |
| 69 | + set ERROR_MSG=Failed to remove existing virtual environment |
| 70 | + call :ERROR_EXIT |
| 71 | + ) |
| 72 | + echo Creating new virtual environment... |
| 73 | + python -m venv "%VENV_DIR%" || ( |
| 74 | + set ERROR_MSG=Failed to create virtual environment |
| 75 | + call :ERROR_EXIT |
| 76 | + ) |
| 77 | + set INSTALL_REQUIREMENTS=true |
| 78 | + ) else ( |
| 79 | + echo Activating existing environment... |
| 80 | + ) |
| 81 | + ) |
| 82 | +) else ( |
| 83 | + echo Creating new virtual environment... |
| 84 | + python -m venv "%VENV_DIR%" || ( |
| 85 | + set ERROR_MSG=Failed to create virtual environment |
| 86 | + call :ERROR_EXIT |
| 87 | + ) |
| 88 | + set INSTALL_REQUIREMENTS=true |
| 89 | +) |
| 90 | + |
| 91 | +REM === Activate environment === |
| 92 | +if not exist "%VENV_DIR%\Scripts\activate.bat" ( |
| 93 | + set ERROR_MSG=Virtual environment activation script not found |
| 94 | + call :ERROR_EXIT |
| 95 | +) |
| 96 | +call "%VENV_DIR%\Scripts\activate.bat" || ( |
| 97 | + set ERROR_MSG=Failed to activate virtual environment |
| 98 | + call :ERROR_EXIT |
| 99 | +) |
| 100 | + |
| 101 | +REM === Upgrade pip and install requirements if flagged === |
| 102 | +if "%INSTALL_REQUIREMENTS%"=="true" ( |
| 103 | + echo Installing/updating required Python packages... |
| 104 | + python -m pip install --upgrade pip || ( |
| 105 | + set ERROR_MSG=Failed to upgrade pip |
| 106 | + call :ERROR_EXIT |
| 107 | + ) |
| 108 | + if exist requirements.txt ( |
| 109 | + python -m pip install -r requirements.txt || ( |
| 110 | + set ERROR_MSG=Failed to install requirements |
| 111 | + call :ERROR_EXIT |
| 112 | + ) |
| 113 | + ) |
| 114 | +) |
| 115 | + |
| 116 | +REM === Set PYTHONPATH === |
| 117 | +set PYTHONPATH=%REPOROOT%\fds\Utilities\Python;%PYTHONPATH% |
| 118 | + |
| 119 | +REM === Run test script === |
| 120 | +cd /d "%REPOROOT%\fds\Utilities\Python" |
| 121 | +if not exist hello_world.py ( |
| 122 | + set ERROR_MSG=hello_world.py not found |
| 123 | + call :ERROR_EXIT |
| 124 | +) |
| 125 | +python hello_world.py || ( |
| 126 | + set ERROR_MSG=hello_world.py failed |
| 127 | + call :ERROR_EXIT |
| 128 | +) |
| 129 | + |
| 130 | +REM === Return to original directory === |
| 131 | +cd /d "%CURDIR%" |
| 132 | + |
| 133 | +echo. |
| 134 | +echo Python environment setup complete. |
| 135 | + |
| 136 | +REM === Error handler === |
| 137 | +:ERROR_EXIT |
| 138 | +if "%ERROR_MSG%"=="" ( |
| 139 | + echo. |
| 140 | +) else ( |
| 141 | + echo *** Error: %ERROR_MSG% |
| 142 | +) |
| 143 | +exit /b 1 |
| 144 | + |
| 145 | +endlocal |
0 commit comments