1#!/usr/bin/env bash
2
3if [ -z ${PYTHON_VER+x} ]; then
4    # Use this version of the Python interpreter if it was not defined before.
5    # 3.6.13 is the default python3 interpreter in esp32-ci-env
6    # Jobs which doesn't support this version should define PYTHON_VER themselves
7    PYTHON_VER=3.6.13
8fi
9
10if [ -f /opt/pyenv/activate ];
11then
12    source /opt/pyenv/activate
13    pyenv global $PYTHON_VER || {
14                                    echo 'Python' $PYTHON_VER 'is not installed.'
15                                    INSTALLED_PY_VERS=$(pyenv versions --bare)
16
17                                    while [ ${#PYTHON_VER} -gt 0 ]
18                                    do
19                                        echo 'Tring to locate a match for' $PYTHON_VER
20
21                                        for ver in ${INSTALLED_PY_VERS[@]}
22                                        do
23                                            if [[ $ver == $PYTHON_VER* ]];
24                                            then
25                                                pyenv global $ver
26                                                break 2
27                                            fi
28                                        done
29
30                                        # Removing last character and trying to find some match.
31                                        # For example, if 3.4.8 was selected but isn't installed then it will try to
32                                        # find some other installed 3.4.X version, and then some 3.X.X version.
33                                        PYTHON_VER=${PYTHON_VER: : -1}
34                                    done
35                                }
36    python --version || {
37                            echo 'No matching Python interpreter is found!'
38                            exit 1
39                        }
40elif command -v python -V 1>/dev/null 2>&1;
41then
42    python --version
43    echo 'No /opt/pyenv/activate exists and Python from path is used.'
44else
45    echo 'No /opt/pyenv/activate exists and no Python interpreter is found!'
46    exit 1
47fi
48
49# add esp-idf local package path to PYTHONPATH so it can be imported directly
50export PYTHONPATH="$IDF_PATH/tools:$IDF_PATH/tools/ci/python_packages:$PYTHONPATH"
51