setup_python.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env bash
  2. # Regexp for matching job names which are incompatible with Python 3
  3. # - assign_test, nvs_compatible_test, IT - auto_test_script causes the incompatibility
  4. # - UT_009_ - multi-device tests are not compatible
  5. # - UT_014_ - multi-device tests are not compatible
  6. # - UT_017_ - multi-device tests are not compatible
  7. py3_incomp='assign_test|nvs_compatible_test|IT|UT_009_|UT_013_|UT_014_|UT_017_'
  8. if [ -z ${PYTHON_VER+x} ] || [[ $CI_JOB_NAME =~ $py3_incomp ]]; then
  9. # Use this version of the Python interpreter if it was not defined before or
  10. # the given job is not compatible with Python 3
  11. PYTHON_VER=2.7.15
  12. fi
  13. if [ -f /opt/pyenv/activate ];
  14. then
  15. source /opt/pyenv/activate
  16. pyenv global $PYTHON_VER || {
  17. echo 'Python' $PYTHON_VER 'is not installed.'
  18. INSTALLED_PY_VERS=$(pyenv versions --bare)
  19. while [ ${#PYTHON_VER} -gt 0 ]
  20. do
  21. echo 'Tring to locate a match for' $PYTHON_VER
  22. for ver in ${INSTALLED_PY_VERS[@]}
  23. do
  24. if [[ $ver == $PYTHON_VER* ]];
  25. then
  26. pyenv global $ver
  27. break 2
  28. fi
  29. done
  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. }
  40. elif command -v python -V 1>/dev/null 2>&1;
  41. then
  42. python --version
  43. echo 'No /opt/pyenv/activate exists and Python from path is used.'
  44. else
  45. echo 'No /opt/pyenv/activate exists and no Python interpreter is found!'
  46. exit 1
  47. fi
  48. # add esp-idf local package path to PYTHONPATH so it can be imported directly
  49. export PYTHONPATH="$IDF_PATH/tools:$IDF_PATH/tools/ci/python_packages:$PYTHONPATH"