pyenv_setup.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/bin/bash
  2. # This script correctly sets up pyenv
  3. #
  4. # Assumptions:
  5. # - This script assumes you're calling from the top directory of the repository
  6. #
  7. # shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values.
  8. set -eu
  9. HERE="$(
  10. cd "$(dirname "${BASH_SOURCE[0]}")"
  11. pwd -P
  12. )"
  13. source "${HERE}/lib.sh"
  14. get_shell_startup_script() {
  15. local _startup_script=''
  16. if [[ -n "$SHELL" ]]; then
  17. case "$SHELL" in
  18. */bash)
  19. _startup_script="${HOME}/.bash_profile"
  20. ;;
  21. */zsh)
  22. _startup_script="${HOME}/.zprofile"
  23. ;;
  24. */fish)
  25. _startup_script="${HOME}/.config/fish/config.fish"
  26. ;;
  27. *)
  28. echo "$SHELL is currently not supported."
  29. exit 1
  30. ;;
  31. esac
  32. else
  33. echo "The environment variable \$SHELL needs to be defined."
  34. exit 1
  35. fi
  36. echo "$_startup_script"
  37. }
  38. # The first \n is important on Github workers since it was being appended to
  39. # the last line rather than on a new line. I never figured out why
  40. _append_to_startup_script() {
  41. if [[ -n "$SHELL" ]]; then
  42. case "$SHELL" in
  43. */bash)
  44. # shellcheck disable=SC2016
  45. echo "Visit https://github.com/pyenv/pyenv#installation on how to fully set up your Bash shell."
  46. ;;
  47. */zsh)
  48. # shellcheck disable=SC2016
  49. echo -e '# It is assumed that pyenv is installed via Brew, so this is all we need to do.' \
  50. '\neval "$(pyenv init --path)"' >>"${1}"
  51. ;;
  52. */fish)
  53. # shellcheck disable=SC2016
  54. echo -e '\n# pyenv init\nstatus is-login; and pyenv init --path | source' >>"${1}"
  55. ;;
  56. esac
  57. echo "--> Tail of ${1}"
  58. tail -n 3 "${1}"
  59. fi
  60. }
  61. append_to_config() {
  62. if [[ -n "$1" ]]; then
  63. [ ! -f "$1" ] && touch "$1"
  64. if grep -qF "(pyenv init -)" "${1}"; then
  65. echo >&2 "!!! Please remove the old-style pyenv initialization and try again:"
  66. echo "sed -i.bak 's/(pyenv init -)/(pyenv init --path)/' ${1}"
  67. exit 1
  68. fi
  69. if ! grep -qF "pyenv init --path" "${1}"; then
  70. echo "Adding pyenv init --path to ${1}..."
  71. # pyenv init --path is needed to include the pyenv shims in your PATH
  72. _append_to_startup_script "${1}"
  73. fi
  74. fi
  75. }
  76. install_pyenv() {
  77. if require pyenv; then
  78. pyenv_version=$(pyenv -v | awk '{print $2}')
  79. # NOTE: We're dropping support for older pyenv versions
  80. if [[ "${pyenv_version}" < 2.2.5 ]]; then
  81. echo >&2 "!!! We've dropped support for pyenv version ${pyenv_version}." \
  82. "Run the following steps (this is slow) and try again."
  83. # brew upgrade does not quite do the right thing
  84. # > ~/.pyenv/shims/python: line 8: /usr/local/Cellar/pyenv/1.2.26/libexec/pyenv: No such file or directory
  85. echo >&2 "brew update && brew uninstall pyenv && brew install pyenv"
  86. exit 1
  87. fi
  88. pyenv install --skip-existing
  89. else
  90. echo >&2 "!!! pyenv not found, try running bootstrap script again or run \`brew bundle\` in the sentry repo"
  91. exit 1
  92. fi
  93. }
  94. # Setup pyenv of path
  95. setup_pyenv() {
  96. configure-sentry-cli
  97. install_pyenv
  98. append_to_config "$(get_shell_startup_script)"
  99. # If the script is called with the "dot space right" approach (. ./scripts/pyenv_setup.sh),
  100. # the effects of this will be persistent outside of this script
  101. # Sets up PATH for pyenv
  102. eval "$(pyenv init --path)"
  103. python_version=$(python -V | sed s/Python\ //g)
  104. [[ $python_version == "$(cat .python-version)" ]] ||
  105. (echo "Wrong Python version: $python_version. Please report in #discuss-dev-infra" && exit 1)
  106. }
  107. setup_pyenv