setup_python.sh 2.1 KB

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