setup_python.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env bash
  2. # py2_incomp='assign_test|nvs_compatible_test|IT'
  3. # - auto_test_script do not support python2 anymore
  4. # but there are still some jobs incompatible with Python 3 (eg: example_test)
  5. # keep the default python interpreter as 2.7.15 until all the jobs support python3
  6. # Regexp for matching job names which are incompatible with Python 3
  7. # - UT_009_ - multi-device tests are not compatible
  8. # - UT_014_ - multi-device tests are not compatible
  9. # - UT_017_ - multi-device tests are not compatible
  10. py3_incomp='UT_009_|UT_013_|UT_014_|UT_017_'
  11. if [ -z ${PYTHON_VER+x} ] || [[ $CI_JOB_NAME =~ $py3_incomp ]]; then
  12. # Use this version of the Python interpreter if it was not defined before or
  13. # the given job is not compatible with Python 3
  14. PYTHON_VER=2.7.15
  15. fi
  16. if [ -f /opt/pyenv/activate ];
  17. then
  18. source /opt/pyenv/activate
  19. pyenv global $PYTHON_VER || {
  20. echo 'Python' $PYTHON_VER 'is not installed.'
  21. INSTALLED_PY_VERS=$(pyenv versions --bare)
  22. while [ ${#PYTHON_VER} -gt 0 ]
  23. do
  24. echo 'Tring to locate a match for' $PYTHON_VER
  25. for ver in ${INSTALLED_PY_VERS[@]}
  26. do
  27. if [[ $ver == $PYTHON_VER* ]];
  28. then
  29. pyenv global $ver
  30. break 2
  31. fi
  32. done
  33. # Removing last character and trying to find some match.
  34. # For example, if 3.4.8 was selected but isn't installed then it will try to
  35. # find some other installed 3.4.X version, and then some 3.X.X version.
  36. PYTHON_VER=${PYTHON_VER: : -1}
  37. done
  38. }
  39. python --version || {
  40. echo 'No matching Python interpreter is found!'
  41. exit 1
  42. }
  43. elif command -v python -V 1>/dev/null 2>&1;
  44. then
  45. python --version
  46. echo 'No /opt/pyenv/activate exists and Python from path is used.'
  47. else
  48. echo 'No /opt/pyenv/activate exists and no Python interpreter is found!'
  49. exit 1
  50. fi
  51. # add esp-idf local package path to PYTHONPATH so it can be imported directly
  52. export PYTHONPATH="$IDF_PATH/tools:$IDF_PATH/tools/ci/python_packages:$PYTHONPATH"