setup_python.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #! /bin/bash
  2. # Regexp for matching job names which are incompatible with Python 3
  3. # - UT_009_ - multi-device tests are not compatible
  4. # - UT_014_ - multi-device tests are not compatible
  5. # - UT_017_ - multi-device tests are not compatible
  6. py3_incomp='UT_009_|UT_013_|UT_014_|UT_017_'
  7. if [ -z ${PYTHON_VER+x} ] || [[ $CI_JOB_NAME =~ $py3_incomp ]]; then
  8. # Use this version of the Python interpreter if it was not defined before or
  9. # the given job is not compatible with Python 3
  10. PYTHON_VER=2.7.15
  11. fi
  12. if [ -f /opt/pyenv/activate ];
  13. then
  14. source /opt/pyenv/activate
  15. pyenv global $PYTHON_VER || {
  16. echo 'Python' $PYTHON_VER 'is not installed.'
  17. INSTALLED_PY_VERS=$(pyenv versions --bare)
  18. while [ ${#PYTHON_VER} -gt 0 ]
  19. do
  20. echo 'Tring to locate a match for' $PYTHON_VER
  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. # Removing last character and trying to find some match.
  30. # For example, if 3.4.8 was selected but isn't installed then it will try to
  31. # find some other installed 3.4.X version, and then some 3.X.X version.
  32. PYTHON_VER=${PYTHON_VER: : -1}
  33. done
  34. }
  35. python --version || {
  36. echo 'No matching Python interpreter is found!'
  37. exit 1
  38. }
  39. elif command -v python -V 1>/dev/null 2>&1;
  40. then
  41. python --version
  42. echo 'No /opt/pyenv/activate exists and Python from path is used.'
  43. else
  44. echo 'No /opt/pyenv/activate exists and no Python interpreter is found!'
  45. exit 1
  46. fi
  47. # add esp-idf local package path to PYTHONPATH so it can be imported directly
  48. export PYTHONPATH="$IDF_PATH/tools:$IDF_PATH/tools/ci/python_packages:$PYTHONPATH"