setup_python.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #! /bin/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_ - RS485 multi-device test is not started properly
  5. py3_incomp='assign_test|nvs_compatible_test|IT|UT_009_'
  6. if [ -z ${PYTHON_VER+x} ] || [[ $CI_JOB_NAME =~ $py3_incomp ]]; then
  7. # Use this version of the Python interpreter if it was not defined before or
  8. # the given job is not compatible with Python 3
  9. PYTHON_VER=2.7.15
  10. fi
  11. if [ -f /opt/pyenv/activate ];
  12. then
  13. source /opt/pyenv/activate
  14. pyenv global $PYTHON_VER || {
  15. echo 'Python' $PYTHON_VER 'is not installed.'
  16. INSTALLED_PY_VERS=$(pyenv versions --bare)
  17. while [ ${#PYTHON_VER} -gt 0 ]
  18. do
  19. echo 'Tring to locate a match for' $PYTHON_VER
  20. for ver in ${INSTALLED_PY_VERS[@]}
  21. do
  22. if [[ $ver == $PYTHON_VER* ]];
  23. then
  24. pyenv global $ver
  25. break 2
  26. fi
  27. done
  28. # Removing last character and trying to find some match.
  29. # For example, if 3.4.8 was selected but isn't installed then it will try to
  30. # find some other installed 3.4.X version, and then some 3.X.X version.
  31. PYTHON_VER=${PYTHON_VER: : -1}
  32. done
  33. }
  34. python --version || {
  35. echo 'No matching Python interpreter is found!'
  36. exit 1
  37. }
  38. elif command -v python -V 1>/dev/null 2>&1;
  39. then
  40. python --version
  41. echo 'No /opt/pyenv/activate exists and Python from path is used.'
  42. else
  43. echo 'No /opt/pyenv/activate exists and no Python interpreter is found!'
  44. exit 1
  45. fi