setup_python.sh 2.2 KB

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