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. set -eu
  7. # Check if a command is available
  8. require() {
  9. command -v "$1" >/dev/null 2>&1
  10. }
  11. query_big_sur() {
  12. if require sw_vers && sw_vers -productVersion | grep -E "11\." > /dev/null; then
  13. return 0
  14. fi
  15. return 1
  16. }
  17. get_shell_startup_script() {
  18. local _startup_script=''
  19. if [[ -n "$SHELL" ]]; then
  20. case "$SHELL" in
  21. */bash)
  22. _startup_script="${HOME}/.bash_profile"
  23. ;;
  24. */zsh)
  25. _startup_script="${HOME}/.zshrc"
  26. ;;
  27. */fish)
  28. _startup_script="${HOME}/.config/fish/config.fish"
  29. ;;
  30. *)
  31. echo "$SHELL is currently not supported."
  32. exit 1
  33. esac
  34. else
  35. echo "The environment variable \$SHELL needs to be defined."
  36. exit 1
  37. fi
  38. echo "$_startup_script"
  39. }
  40. _append_to_startup_script() {
  41. if [[ -n "$SHELL" ]]; then
  42. case "$SHELL" in
  43. */bash)
  44. # shellcheck disable=SC2016
  45. echo -e '\nif command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> "${1}"
  46. ;;
  47. */zsh)
  48. # shellcheck disable=SC2016
  49. echo -e '\nif command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> "${1}"
  50. ;;
  51. */fish)
  52. echo -e '\n\n# pyenv init\nif command -v pyenv 1>/dev/null 2>&1\n pyenv init - | source\nend' >> "$1"
  53. esac
  54. echo "--> Tail of ${1}"
  55. tail -n 3 "${1}"
  56. fi
  57. }
  58. append_to_config() {
  59. if [[ -n "$1" ]]; then
  60. echo "Adding pyenv init (if missing) to ${1}..."
  61. # shellcheck disable=SC2016
  62. if ! grep -qF "pyenv init" "${1}"; then
  63. # pyenv init - is needed to include the pyenv shims in your PATH
  64. # The first \n is very important since on Github workers the output was being appended to
  65. # the last line rather than on a new line. I never figured out why
  66. _append_to_startup_script "${1}"
  67. fi
  68. fi
  69. }
  70. install_pyenv() {
  71. if command -v pyenv &>/dev/null; then
  72. echo "Installing Python (if missing) via pyenv"
  73. local pyenv_version
  74. pyenv_version=$(pyenv -v | awk '{print $2}')
  75. python_version=$(xargs -n1 < .python-version)
  76. if query_big_sur; then
  77. local flag
  78. # NOTE: pyenv 1.2.22 or greater does not require using LDFLAGS
  79. # https://github.com/pyenv/pyenv/pull/1711
  80. if [[ "$pyenv_version" < 1.2.22 ]]; then
  81. flag="-L$(xcrun --show-sdk-path)/usr/lib ${LDFLAGS}"
  82. fi
  83. # cat is used since pyenv would finish to soon when the Python version is already installed
  84. curl -sSL https://github.com/python/cpython/commit/8ea6353.patch | cat | \
  85. LDFLAGS="$flag" pyenv install --skip-existing --patch "$python_version"
  86. else
  87. pyenv install --skip-existing "$python_version"
  88. fi
  89. else
  90. echo "!!! 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. install_pyenv
  97. _startup_script=$(get_shell_startup_script)
  98. append_to_config "$_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. echo "Activating pyenv and validating Python version"
  102. eval "$(pyenv init -)"
  103. python_version=$(python -V | sed s/Python\ //g)
  104. [[ $python_version == $(cat .python-version) ]] || (echo "Wrong Python version: $python_version" && exit 1)
  105. }
  106. setup_pyenv