Skip to content

Commit 1fbdfa5

Browse files
authored
#269: Fix Python version check (#289)
1 parent 1899a6c commit 1fbdfa5

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

mfc.sh

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ unset u_c
44
unset u_cg
55

66
MFC_PYTHON_MIN_MAJOR=3
7-
MFC_PYTHON_MIN_MINOR=9
7+
MFC_PYTHON_MIN_MINOR=8
8+
MFC_PYTHON_MIN_STR="$MFC_PYTHON_MIN_MAJOR.$MFC_PYTHON_MIN_MINOR"
89

910
# Check whether this script was called from MFC's root directory.
1011
if [ ! -f "$(pwd)/toolchain/mfc.py" ]; then
@@ -81,14 +82,14 @@ if [ "$1" == 'load' ]; then
8182
}
8283

8384
COMPUTER="$(__extract "$u_c")"
84-
85+
8586
if [[ -z "$COMPUTER" ]]; then
8687
error "Computer $M$u_cg$CR not recognized."
8788
return
8889
fi
89-
90+
9091
MODULES=($(__extract "$u_c-$cg") $(__combine $(__extract "$u_c-all")))
91-
92+
9293
log "Loading modules for $M$COMPUTER$CR on $M$CG$CR"'s:'
9394

9495
# Reset modules to default system configuration
@@ -124,7 +125,7 @@ if [ "$1" == 'load' ]; then
124125
if [ "$delta" -ge "0" ]; then
125126
printf "%0.s " $(seq 0 $delta)
126127
fi
127-
128+
128129
# Load the module
129130
module load "$module_name" > /dev/null 2>&1
130131

@@ -190,7 +191,7 @@ elif [ "$1" == "docker" ]; then
190191
dockerintopts='--interactive --tty'
191192
fi
192193

193-
__docker_run() {
194+
__docker_run() {
194195
docker run $dockerintopts --rm --workdir /home/me/MFC \
195196
--mount type=bind,source="$(pwd)",target=/home/me/MFC \
196197
sbryngelson/mfc:latest $@
@@ -330,11 +331,27 @@ if [ "$bShouldInstallCMake" = true ]; then
330331
fi
331332

332333

334+
is_python_compatible() {
335+
if ! ${1:-python3} -c "import sys; exit(int(not (sys.version_info[0]==$MFC_PYTHON_MIN_MAJOR and sys.version_info[1] >= $MFC_PYTHON_MIN_MINOR)))"; then
336+
return 1
337+
fi
338+
339+
return 0
340+
}
341+
342+
343+
assert_python_compatible() {
344+
if ! is_python_compatible $1; then
345+
error "$MAGENTA$(${1:-python3} --version)$COLOR_RESET (${1:-python3}) is out of date. Required >= $MAGENTA$MFC_PYTHON_MIN_STR$COLOR_RESET."
346+
347+
exit 1
348+
fi
349+
}
350+
351+
333352
if [ -f "$(pwd)/build/venv/bin/activate" ]; then
334-
# Check Python is still working within the VENV
335-
if ! $(pwd)/build/venv/bin/python3 --version > /dev/null 2>&1; then
336-
# If not, delete it and install it again
337-
warn "$MAGENTA""Python$COLOR_RESET is no longer working inside the Virtualenv."
353+
if ! is_python_compatible "$(pwd)/build/venv/bin/python3"; then
354+
warn "$MAGENTA""Python$COLOR_RESET is outdated inside the Virtualenv."
338355
warn "Deleting the Virtualenv and starting from scratch..."
339356

340357
rm -r "$(pwd)/build/venv"
@@ -349,12 +366,7 @@ if ! command -v pip3 > /dev/null 2>&1 && [ ! -f "$(pwd)/build/venv/bin/activate"
349366
exit 1
350367
fi
351368

352-
# CHeck Python's version for compatibility
353-
if ! python3 -c "import sys; exit(int(not (sys.version_info[0]==$MFC_PYTHON_MIN_MAJOR and sys.version_info[1] >= $MFC_PYTHON_MIN_MINOR)))"; then
354-
error "$(python3 --version) is incompatible.$MAGENTA Python$COLOR_RESET v$MFC_PYTHON_MIN_MAJOR.$MFC_PYTHON_MIN_MINOR or higher is required."
355-
356-
exit 1
357-
fi
369+
assert_python_compatible
358370

359371
get_pip_url="https://bootstrap.pypa.io/pip/get-pip.py"
360372

@@ -381,6 +393,8 @@ fi
381393

382394
# Create a Python virtualenv if it hasn't already been created
383395
if [ ! -f "$(pwd)/build/venv/bin/activate" ]; then
396+
assert_python_compatible
397+
384398
if ! python3 -m venv "$(pwd)/build/venv"; then
385399
error "Failed to create a$MAGENTA Python$COLOR_RESET virtual environment. Delete the build/venv folder and try again."
386400

@@ -420,7 +434,7 @@ fi
420434

421435
# Activate the Python venv
422436
source "$(pwd)/build/venv/bin/activate"
423-
ok "(venv) Entered the$MAGENTA Python$COLOR_RESET virtual environment."
437+
ok "(venv) Entered the $MAGENTA$(python3 --version)$COLOR_RESET virtual environment (>= $MAGENTA$MFC_PYTHON_MIN_STR$COLOR_RESET)."
424438

425439

426440
# Install Python dependencies if, either:
@@ -456,5 +470,5 @@ log "(venv) Exiting the$MAGENTA Python$COLOR_RESET virtual environment."
456470
deactivate
457471

458472

459-
# Exit proper exit code
473+
# Exit with proper exit code
460474
exit $code

0 commit comments

Comments
 (0)