Skip to content

Commit 663d2a5

Browse files
authored
Merge pull request #15580 from cxp484/master
FDS Utilities Python: Create a FDS python environment setup script for Firebot and Actions
2 parents 58da6e8 + 183e7d9 commit 663d2a5

File tree

5 files changed

+242
-161
lines changed

5 files changed

+242
-161
lines changed

.github/workflows/linux.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ jobs:
104104
cache: 'pip' # caching pip dependencies
105105
- run: |
106106
echo $GITHUB_WORKSPACE
107-
cd $GITHUB_WORKSPACE/.github
108-
pip install -r requirements.txt
107+
cd $GITHUB_WORKSPACE/Utilities/Python
108+
source setup_python_env.sh --batchmode
109109
110110
- name: Python test
111111
run: |
112112
echo $GITHUB_WORKSPACE
113113
cd $GITHUB_WORKSPACE/Utilities/Python
114+
source $GITHUB_WORKSPACE/.github/fds_python_env/bin/activate
114115
python hello_world.py
115116
: # python FDS_verification_script.py
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
# Configure Spyder IDE for FDS
3+
# Usage (Linux and macOS):
4+
# source ./setup_spyder_env.sh
5+
6+
set -e # Exit immediately if a command exits with a non-zero status
7+
8+
BATCHMODE=false
9+
10+
# Parse command-line arguments
11+
for arg in "$@"; do
12+
case $arg in
13+
--batchmode)
14+
BATCHMODE=true
15+
shift
16+
;;
17+
esac
18+
done
19+
20+
function error_exit {
21+
echo "*** Error: $1"
22+
return 1
23+
}
24+
25+
26+
# Check Python version > 3.7
27+
PYTHON_VERSION=$(python3 -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))')
28+
REQUIRED_VERSION=3.7
29+
if (( $(echo "$PYTHON_VERSION < $REQUIRED_VERSION" | bc -l) )); then
30+
echo "Python 3.7 or higher is required. Found $PYTHON_VERSION."
31+
echo "Stopping python environment setup."
32+
return
33+
else
34+
echo "Python version is OK: $PYTHON_VERSION"
35+
fi
36+
37+
# Save current directory to return later
38+
curdir=$(pwd)
39+
40+
# Go to .github folder
41+
cd "$(dirname "${BASH_SOURCE[0]}")/../../.." || error_exit "Failed to locate repo root"
42+
reporoot=$(pwd)
43+
cd "$reporoot/fds/.github" || error_exit "Directory not found: $reporoot/fds/.github"
44+
45+
VENV_DIR="fds_python_env"
46+
VENV_BIN="$VENV_DIR/bin"
47+
INSTALL_REQUIREMENTS=false
48+
49+
# Check if virtual environment exists
50+
if [ -d "$VENV_DIR" ]; then
51+
if [ "$BATCHMODE" = true ]; then
52+
echo "Batch mode: activating existing virtual environment without prompts."
53+
else
54+
echo "Virtual environment '$VENV_DIR' already exists."
55+
read -p "Do you want to reinstall everything? (y/N): " choice
56+
case "$choice" in
57+
[yY]|[yY][eE][sS])
58+
echo "Removing old environment..."
59+
rm -rf "$VENV_DIR" || error_exit "Failed to remove existing virtual environment"
60+
echo "Creating new virtual environment..."
61+
python3 -m venv "$VENV_DIR" || error_exit "Failed to create virtual environment"
62+
INSTALL_REQUIREMENTS=true
63+
;;
64+
*)
65+
echo "Activating existing environment..."
66+
;;
67+
esac
68+
fi
69+
else
70+
echo "Creating new virtual environment..."
71+
python3 -m venv "$VENV_DIR" || error_exit "Failed to create virtual environment"
72+
INSTALL_REQUIREMENTS=true
73+
fi
74+
source "$VENV_BIN/activate" || error_exit "Failed to activate virtual environment"
75+
76+
# Install requirements only if flagged
77+
if [ "$INSTALL_REQUIREMENTS" = true ]; then
78+
echo "Installing/updating required Python packages..."
79+
pip install --upgrade pip
80+
if [ -f "requirements.txt" ]; then
81+
pip install -r requirements.txt || error_exit "Failed to install requirements"
82+
fi
83+
fi
84+
85+
export PYTHONPATH="$reporoot/fds/Utilities/Python:$PYTHONPATH"
86+
87+
# Run test script to verify environment
88+
cd "$reporoot/fds/Utilities/Python" || error_exit "Failed to find script directory"
89+
python hello_world.py || error_exit "hello_world.py failed"
90+
91+
# Return to original directory
92+
cd "$curdir"
93+
94+
echo "Python environment setup complete."

Utilities/Python/setup_spyder_env.bat

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)