Skip to content

Commit cae48b8

Browse files
FozzTexxmarkjfisher
authored andcommitted
Be smarter about finding python3
1 parent 6a5c65a commit cae48b8

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

build.sh

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@ CMAKE_GENERATOR=""
2828
INI_FILE="${SCRIPT_DIR}/platformio-generated.ini"
2929
LOCAL_INI_VALUES_FILE="${SCRIPT_DIR}/platformio.local.ini"
3030

31+
# Function to check if the specified Python version is 3
32+
check_python_version() {
33+
local python_bin=$1
34+
35+
if ! command -v "${python_bin}" &> /dev/null; then
36+
return 1
37+
fi
38+
39+
# Extract the major version number
40+
local major_version="$(${python_bin} --version 2>&1 | cut -d' ' -f2 | cut -d'.' -f1)"
41+
42+
# Verify if it's Python 3
43+
if [ "${major_version}" -eq 3 ]; then
44+
return 0
45+
else
46+
return 1
47+
fi
48+
}
49+
50+
# Check if "python" exists first since that's what PlatformIO names it
51+
PYTHON=python
52+
if ! check_python_version "${PYTHON}" ; then
53+
PYTHON=python3
54+
if ! check_python_version "${PYTHON}" ; then
55+
echo "Python 3 is not installed"
56+
exit 1
57+
fi
58+
fi
59+
3160
function display_board_names {
3261
while IFS= read -r piofile; do
3362
BOARD_NAME=$(echo $(basename $piofile) | sed 's#^platformio-##;s#.ini$##')
@@ -186,7 +215,7 @@ if [ ! -z "$PC_TARGET" ] ; then
186215
# python_modules.txt contains pairs of module name and installable package names, separated by pipe symbol
187216
MOD_LIST=$(sed '/^#/d' < "${SCRIPT_DIR}/python_modules.txt" | cut -d\| -f1 | tr '\n' ' ' | sed 's# *$##;s# \{1,\}# #g')
188217
echo "Checking python modules installed: $MOD_LIST"
189-
python -c "import importlib.util, sys; sys.exit(0 if all(importlib.util.find_spec(mod.strip()) for mod in '''$MOD_LIST'''.split()) else 1)"
218+
${PYTHON} -c "import importlib.util, sys; sys.exit(0 if all(importlib.util.find_spec(mod.strip()) for mod in '''$MOD_LIST'''.split()) else 1)"
190219
if [ $? -eq 1 ] ; then
191220
echo "At least one of the required python modules is missing"
192221
bash ${SCRIPT_DIR}/install_python_modules.sh
@@ -230,14 +259,14 @@ if [ -z "$SETUP_NEW_BOARD" ] ; then
230259
fi
231260

232261
if [ ${ZIP_MODE} -eq 1 ] ; then
233-
python create-platformio-ini.py -o $INI_FILE -l $LOCAL_INI_VALUES_FILE -f platformio-ini-files/platformio.zip-options.ini
262+
${PYTHON} create-platformio-ini.py -o $INI_FILE -l $LOCAL_INI_VALUES_FILE -f platformio-ini-files/platformio.zip-options.ini
234263
else
235-
python create-platformio-ini.py -o $INI_FILE -l $LOCAL_INI_VALUES_FILE
264+
${PYTHON} create-platformio-ini.py -o $INI_FILE -l $LOCAL_INI_VALUES_FILE
236265
fi
237266
create_result=$?
238267
else
239268
# this will create a clean platformio INI file, but honours the command line args
240-
if [ $ANSWER_YES -eq 0 ] ; then
269+
if [ -e ${LOCAL_INI_VALUES_FILE} -a $ANSWER_YES -eq 0 ] ; then
241270
echo "WARNING! This will potentially overwrite any local changes in $LOCAL_INI_VALUES_FILE"
242271
echo -n "Do you want to proceed? (y|N) "
243272
read answer
@@ -248,9 +277,9 @@ else
248277
fi
249278
fi
250279
if [ ${ZIP_MODE} -eq 1 ] ; then
251-
python create-platformio-ini.py -n $SETUP_NEW_BOARD -o $INI_FILE -l $LOCAL_INI_VALUES_FILE -f platformio-ini-files/platformio.zip-options.ini
280+
${PYTHON} create-platformio-ini.py -n $SETUP_NEW_BOARD -o $INI_FILE -l $LOCAL_INI_VALUES_FILE -f platformio-ini-files/platformio.zip-options.ini
252281
else
253-
python create-platformio-ini.py -n $SETUP_NEW_BOARD -o $INI_FILE -l $LOCAL_INI_VALUES_FILE
282+
${PYTHON} create-platformio-ini.py -n $SETUP_NEW_BOARD -o $INI_FILE -l $LOCAL_INI_VALUES_FILE
254283
fi
255284

256285
create_result=$?

0 commit comments

Comments
 (0)