|
3 | 3 | # This is a helper script for detecting Python executables in the PATH. It is intended to be used for determining |
4 | 4 | # which Python should be used with idf_tools.py for installing tools and exporting environment variables. |
5 | 5 | # |
6 | | -# 1. The script is looking for python version same or greater than minimal required version on path |
| 6 | +# 1. If environment variable ESP_PYTHON already set, script uses that python binary, otherwise, looks for python version same or greater than minimal required version on path |
7 | 7 | # 2. If required version of python is found it is assigned to environmental variable `ESP_PYTHON` |
8 | 8 | # 3. If required version of python is not found, script will fail |
9 | 9 |
|
10 | 10 | OLDEST_PYTHON_SUPPORTED_MAJOR=3 |
11 | 11 | OLDEST_PYTHON_SUPPORTED_MINOR=10 |
12 | 12 |
|
13 | | -ESP_PYTHON=python |
14 | 13 |
|
15 | | -for p_cmd in python3 python python3.10 python3.11 python3.12 python3.13; do |
16 | | - $p_cmd --version >/dev/null 2>&1 || continue |
| 14 | +if [[ -z "$ESP_PYTHON" ]]; then |
| 15 | + PYTHON_CANDIDATES=(python3 python python3.10 python3.11 python3.12 python3.13) |
| 16 | +else |
| 17 | + echo "Reading ESP_PYTHON from environment: \"$ESP_PYTHON\"" |
| 18 | + PYTHON_CANDIDATES=("$ESP_PYTHON") |
| 19 | +fi |
| 20 | +ESP_PYTHON= # Unset it. Will assign after candidate check |
| 21 | + |
| 22 | +for p_cmd in "${PYTHON_CANDIDATES[@]}"; do |
| 23 | + "$p_cmd" --version >/dev/null 2>&1 || continue |
17 | 24 | echo "Checking \"$p_cmd\" ..." |
18 | 25 |
|
19 | | - $p_cmd -c "import sys; exit(1) if sys.version_info.major < int(\"$OLDEST_PYTHON_SUPPORTED_MAJOR\") else exit(0);" || continue |
20 | | - $p_cmd -c "import sys; exit(1) if sys.version_info.minor < int(\"$OLDEST_PYTHON_SUPPORTED_MINOR\") else exit(0);" || continue |
| 26 | + "$p_cmd" -c "import sys; exit(1) if sys.version_info.major < int(\"$OLDEST_PYTHON_SUPPORTED_MAJOR\") else exit(0);" || continue |
| 27 | + "$p_cmd" -c "import sys; exit(1) if sys.version_info.minor < int(\"$OLDEST_PYTHON_SUPPORTED_MINOR\") else exit(0);" || continue |
21 | 28 |
|
22 | | - ESP_PYTHON=$p_cmd |
| 29 | + export ESP_PYTHON="$p_cmd" |
23 | 30 | break |
24 | 31 | done |
| 32 | +unset PYTHON_CANDIDATES |
25 | 33 |
|
26 | | -$ESP_PYTHON --version 2>/dev/null || { |
27 | | - echo "Python ${OLDEST_PYTHON_SUPPORTED_MAJOR}.${OLDEST_PYTHON_SUPPORTED_MINOR}+ is not installed! Please see the documentation for how to install it." |
| 34 | +[[ -n "$ESP_PYTHON" ]] && "$ESP_PYTHON" --version 2>/dev/null || { |
| 35 | + echo "Python ${OLDEST_PYTHON_SUPPORTED_MAJOR}.${OLDEST_PYTHON_SUPPORTED_MINOR}+ is not installed! Please see the documentation for how to install it." >&2 |
28 | 36 | exit 1 |
29 | 37 | } |
30 | 38 | echo "\"$ESP_PYTHON\" has been detected" |
0 commit comments