.envrc 5.6 KB

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