.envrc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # This is the .envrc for sentry, for use with direnv.
  2. # It's responsible for enforcing a standard dev environment by checking as much state as possible, and either performing
  3. # initialization (e.g. activating the venv) or giving recommendations on how to reach the desired state.
  4. # It also sets useful environment variables.
  5. # If you'd like to override or set any custom environment variables, this .envrc will read a .env file at the end.
  6. set -e
  7. bold="$(tput bold)"
  8. red="$(tput setaf 1)"
  9. green="$(tput setaf 2)"
  10. yellow="$(tput setaf 3)"
  11. reset="$(tput sgr0)"
  12. # XXX: we can't trap bash EXIT, because it'll override direnv's finalizing routines.
  13. # consequently, using "exit" anywhere will skip this notice from showing.
  14. # so need to use set -e, and return 1.
  15. trap notice ERR
  16. notice() {
  17. [ $? -eq 0 ] && return
  18. cat <<EOF
  19. ${red}${bold}direnv wasn't able to complete execution.
  20. You may have been given some recommendations in the error message.
  21. Follow them, and then you'll need to redo direnv by running "direnv allow".${reset}
  22. direnv tooling is in an ALPHA state!
  23. If you're having trouble, or have questions, please ask in #discuss-dev-tooling
  24. and/or reach out to @josh.
  25. EOF
  26. }
  27. require() {
  28. command -v "$1" 2>&1 >/dev/null
  29. }
  30. warn() {
  31. cat <<EOF
  32. ${yellow}${bold}direnv: ${@}${reset}
  33. EOF
  34. }
  35. info() {
  36. cat <<EOF
  37. ${bold}direnv: ${@}${reset}
  38. EOF
  39. }
  40. die() {
  41. cat >&2 <<EOF
  42. ${red}${bold}direnv FATAL: ${@}
  43. ${reset}
  44. EOF
  45. return 1
  46. }
  47. advice_init_venv() {
  48. python_version="$1"
  49. venv_name="$2"
  50. deactivate 2>/dev/null || true
  51. info "To create a virtualenv, please type: ${python_version} -m virtualenv ${venv_name}"
  52. require ${python_version} ||
  53. die "You'll need to install ${python_version}, or make it available on your PATH.
  54. It's recommended to use pyenv - please refer to https://docs.sentry.io/development/contribute/environment"
  55. return 1
  56. }
  57. advice_install_sentry() {
  58. info "To install sentry, please type: make install-py-dev"
  59. return 1
  60. }
  61. advice_install_pre_commit() {
  62. info "To install pre-commit, please type: make setup-git"
  63. return 1
  64. }
  65. advice_install_yarn_pkgs() {
  66. info "To install yarn packages, please type: make install-js-dev"
  67. return 1
  68. }
  69. ### Environment ###
  70. # don't write *.pyc files; using stale python code occasionally causes subtle problems
  71. export PYTHONDONTWRITEBYTECODE=1
  72. # make sure we don't have any conflicting PYTHONPATH
  73. unset PYTHONPATH
  74. # don't check pypi for a potential new pip version; low-hanging fruit to save a bit of time
  75. export PIP_DISABLE_PIP_VERSION_CHECK=on
  76. # increase node's memory limit, required for our webpacking
  77. export NODE_OPTIONS=--max-old-space-size=4096
  78. ### System ###
  79. for pkg in \
  80. make \
  81. docker \
  82. chromedriver \
  83. pkg-config \
  84. openssl; do
  85. if ! require "$pkg"; then
  86. die "You seem to be missing the system dependency: ${pkg}
  87. Please install homebrew, and run brew bundle."
  88. fi
  89. done
  90. ### Git ###
  91. info "Configuring git..."
  92. make setup-git-config
  93. ### Python ###
  94. # we're enforcing that virtualenv be in .venv{,3}, since future tooling e.g.
  95. # venv-update will rely on this.
  96. venv_name=".venv"
  97. python_version="python2.7"
  98. if [ "$SENTRY_PYTHON3" = "1" ]; then
  99. venv_name=".venv3"
  100. python_version="python3.6"
  101. warn "Running in EXPERIMENTAL Python 3 mode... Good luck"
  102. fi
  103. info "Activating virtualenv..."
  104. if [ ! -f "${venv_name}/bin/activate" ]; then
  105. info "You don't seem to have a virtualenv."
  106. advice_init_venv "$python_version" "$venv_name"
  107. fi
  108. # The user might be cd'ing into sentry with another non-direnv managed
  109. # (in that it would be automatically deactivated) virtualenv active.
  110. deactivate 2>/dev/null || true
  111. source ${venv_name}/bin/activate
  112. # XXX: ideally, direnv is able to export PS1 as modified by sourcing venvs
  113. # but we'd have to patch direnv, and ".venv" isn't descriptive anyways
  114. unset PS1
  115. # We're explicitly disallowing symlinked venvs (python would resolve to the canonical location)
  116. if [ "$(command -v python)" != "${PWD}/${venv_name}/bin/python" ]; then
  117. info "Failed to activate virtualenv. Your virtualenv's probably symlinked." \
  118. "We want everyone to be on the same page here, so you'll have to recreate your virtualenv."
  119. advice_init_venv "$python_version" "$venv_name"
  120. fi
  121. if [ "$SENTRY_PYTHON3" = "1" ]; then
  122. python -c "import sys; sys.exit(sys.version_info[:2] != (3, 6))" ||
  123. die "For some reason, the virtualenv isn't Python 3.6."
  124. else
  125. python -c "import sys; sys.exit(sys.version_info[:2] != (2, 7))" ||
  126. die "For some reason, the virtualenv isn't Python 2.7."
  127. fi
  128. if [ "$(command -v sentry)" != "${PWD}/${venv_name}/bin/sentry" ]; then
  129. info "Your virtual env is activated, but sentry doesn't seem to be installed."
  130. # XXX: if direnv fails, the venv won't be activated outside of direnv execution...
  131. # So, it is critical that make install-py-dev is guarded by scripts/ensure-venv.
  132. advice_install_sentry
  133. fi
  134. ### pre-commit ###
  135. info "Checking pre-commit..."
  136. if ! require pre-commit; then
  137. info "Looks like you don't have pre-commit installed."
  138. advice_install_pre_commit
  139. fi
  140. ### Node ###
  141. info "Checking node..."
  142. if ! require node; then
  143. die "You don't seem to have node installed. Install volta (a node version manager): https://develop.sentry.dev/environment/#javascript"
  144. fi
  145. make node-version-check
  146. if [ ! -x "node_modules/.bin/webpack" ]; then
  147. info "You don't seem to have yarn packages installed."
  148. advice_install_yarn_pkgs
  149. fi
  150. PATH_add node_modules/.bin
  151. ### Overrides ###
  152. if [ -f '.env' ]; then
  153. info ".env found. Reading it..."
  154. dotenv .env
  155. fi
  156. cat <<EOF
  157. ${green}${bold}direnv: SUCCESS!
  158. ${reset}
  159. EOF
  160. if [ ! -n "${SENTRY_SILENCE_DIRENV_NOTICE:-}" ]; then
  161. cat <<EOF
  162. direnv tooling is in an ALPHA state!
  163. If you're having trouble, or have questions, please ask in #discuss-dev-tooling
  164. and/or reach out to @josh.
  165. EOF
  166. fi