pyenv_setup.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. # NOTE: We're dropping support for older pyenv versions
  79. if [[ "$(pyenv -v | awk '{print $2}')" < 2.0.0 ]]; then
  80. echo >&2 "!!! We've dropped support for pyenv v1." \
  81. "Run the following (this is slow) and try again."
  82. # brew upgrade does not quite do the right thing
  83. # > ~/.pyenv/shims/python: line 8: /usr/local/Cellar/pyenv/1.2.26/libexec/pyenv: No such file or directory
  84. echo >&2 "brew update && brew uninstall pyenv && brew install pyenv"
  85. exit 1
  86. fi
  87. pyenv install --skip-existing
  88. else
  89. echo >&2 "!!! pyenv not found, try running bootstrap script again or run \`brew bundle\` in the sentry repo"
  90. exit 1
  91. fi
  92. }
  93. # Setup pyenv of path
  94. setup_pyenv() {
  95. configure-sentry-cli
  96. install_pyenv
  97. append_to_config "$(get_shell_startup_script)"
  98. # If the script is called with the "dot space right" approach (. ./scripts/pyenv_setup.sh),
  99. # the effects of this will be persistent outside of this script
  100. # Sets up PATH for pyenv
  101. eval "$(pyenv init --path)"
  102. python_version=$(python -V | sed s/Python\ //g)
  103. [[ $python_version == "$(cat .python-version)" ]] ||
  104. (echo "Wrong Python version: $python_version. Please report in #discuss-dev-tooling" && exit 1)
  105. }
  106. setup_pyenv