-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathlib.sh
executable file
·279 lines (243 loc) · 8.2 KB
/
lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
# Module containing code shared across various shell scripts
# Execute functions from this module via the script do.sh
# shellcheck disable=SC2034 # Unused variables
# shellcheck disable=SC2001 # https://github.com/koalaman/shellcheck/wiki/SC2001
# This block is a safe-guard since in CI calling tput will fail and abort scripts
if [ -z "${CI+x}" ]; then
bold="$(tput bold)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
reset="$(tput sgr0)"
fi
venv_name=".venv"
# NOTE: This file is sourced in CI across different repos (e.g. snuba),
# so renaming this file or any functions can break CI!
# Check if a command is available
require() {
command -v "$1" >/dev/null 2>&1
}
configure-sentry-cli() {
if [ -n "${SENTRY_DSN+x}" ] && [ -z "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
if ! require sentry-cli; then
curl -sL https://sentry.io/get-cli/ | bash
fi
eval "$(sentry-cli bash-hook)"
fi
}
query-mac() {
[[ $(uname -s) = 'Darwin' ]]
}
query-big-sur() {
if require sw_vers && sw_vers -productVersion | grep -E "11\." >/dev/null; then
return 0
fi
return 1
}
query-apple-m1() {
query-mac && [[ $(uname -m) = 'arm64' ]]
}
get-pyenv-version() {
if [[ -n "${SENTRY_PYTHON_VERSION:-}" ]]; then
echo "${SENTRY_PYTHON_VERSION}"
return 0
fi
local PYENV_VERSION
PYENV_VERSION=3.6.13
if query-apple-m1; then
PYENV_VERSION=3.8.12
fi
echo "${PYENV_VERSION}"
}
query-valid-python-version() {
if [[ -n "${SENTRY_PYTHON_VERSION:-}" ]]; then
python_version=$(python3 -V 2>&1 | awk '{print $2}')
if [ "$python_version" != "$SENTRY_PYTHON_VERSION" ]; then
cat <<EOF
${red}${bold}
ERROR: You have explicitly set a non-recommended Python version (${SENTRY_PYTHON_VERSION}),
but it doesn't match the value of python's version: ${python_version}
You should create a new ${SENTRY_PYTHON_VERSION} virtualenv by running "rm -rf ${venv_name} && direnv allow".
${reset}
EOF
return 1
fi
cat <<EOF
${yellow}${bold}
You have explicitly set a non-recommended Python version (${SENTRY_PYTHON_VERSION}). You're on your own.
${reset}
EOF
return 0
fi
python_version=$(python3 -V 2>&1 | awk '{print $2}')
minor=$(echo "${python_version}" | sed 's/[0-9]*\.\([0-9]*\)\.\([0-9]*\)/\1/')
patch=$(echo "${python_version}" | sed 's/[0-9]*\.\([0-9]*\)\.\([0-9]*\)/\2/')
# For Apple M1, we only allow 3.8 and at least patch version 10
if query-apple-m1; then
if [ "$minor" -ne 8 ] || [ "$patch" -lt 10 ]; then
cat <<EOF
${red}${bold}
ERROR: You're running a virtualenv with Python ${python_version}.
On Apple M1 machines, we only support >= 3.8.10 < 3.9.
Either run "rm -rf ${venv_name} && direnv allow" to
OR set SENTRY_PYTHON_VERSION=${python_version} to an .env file to bypass this check."
EOF
return 1
fi
elif [ "$minor" -ne 6 ] && [ "$minor" -ne 8 ]; then
cat <<EOF
${red}${bold}
ERROR: You're running a virtualenv with Python ${python_version}.
We only support 3.6 or 3.8.
Either run "rm -rf ${venv_name} && direnv allow" to
OR set SENTRY_PYTHON_VERSION=${python_version} to an .env file to bypass this check."
EOF
return 1
fi
}
sudo-askpass() {
if [ -z "${sudo-askpass-x}" ]; then
sudo --askpass "$@"
else
sudo "$@"
fi
}
upgrade-pip() {
pip install --upgrade "pip==21.1.2" "wheel==0.36.2"
}
install-py-dev() {
upgrade-pip
# It places us within top src dir to be at the same path as setup.py
# This helps when getsentry calls into this script
cd "${HERE}/.." || exit
echo "--> Installing Sentry (for development)"
if query-apple-m1; then
# This installs pyscopg-binary2 since there's no arm64 wheel
# This saves having to install postgresql on the Developer's machine + using flags
# https://github.com/psycopg/psycopg2/issues/1286
pip install https://storage.googleapis.com/python-arm64-wheels/psycopg2_binary-2.8.6-cp38-cp38-macosx_11_0_arm64.whl
fi
# SENTRY_LIGHT_BUILD=1 disables webpacking during setup.py.
# Webpacked assets are only necessary for devserver (which does it lazily anyways)
# and acceptance tests, which webpack automatically if run.
SENTRY_LIGHT_BUILD=1 pip install -e '.[dev]'
}
setup-git-config() {
git config --local branch.autosetuprebase always
git config --local core.ignorecase false
git config --local blame.ignoreRevsFile .git-blame-ignore-revs
}
setup-git() {
setup-git-config
echo "--> Installing git hooks"
mkdir -p .git/hooks && cd .git/hooks && ln -sf ../../config/hooks/* ./ && cd - || exit
# shellcheck disable=SC2016
python3 -c '' || (
echo 'Please run `make setup-pyenv` to install the required Python 3 version.'
exit 1
)
pip install -r requirements-pre-commit.txt
pre-commit install --install-hooks
echo ""
}
node-version-check() {
# Checks to see if node's version matches the one specified in package.json for Volta.
node -pe "process.exit(Number(!(process.version == 'v' + require('./package.json').volta.node )))" ||
(
echo 'Unexpected node version. Recommended to use https://github.com/volta-cli/volta'
exit 1
)
}
install-js-dev() {
node-version-check
echo "--> Installing Yarn packages (for development)"
# Use NODE_ENV=development so that yarn installs both dependencies + devDependencies
NODE_ENV=development yarn install --frozen-lockfile
# A common problem is with node packages not existing in `node_modules` even though `yarn install`
# says everything is up to date. Even though `yarn install` is run already, it doesn't take into
# account the state of the current filesystem (it only checks .yarn-integrity).
# Add an additional check against `node_modules`
yarn check --verify-tree || yarn install --check-files
}
develop() {
setup-git
install-js-dev
install-py-dev
}
init-config() {
sentry init --dev
}
run-dependent-services() {
sentry devservices up
}
create-db() {
# shellcheck disable=SC2155
local CREATEDB=$(command -v createdb 2>/dev/null)
if [[ -z "$CREATEDB" ]]; then
CREATEDB="docker exec sentry_postgres createdb"
fi
echo "--> Creating 'sentry' database"
${CREATEDB} -h 127.0.0.1 -U postgres -E utf-8 sentry || true
}
apply-migrations() {
echo "--> Applying migrations"
sentry upgrade --noinput
}
create-user() {
if [[ -n "${GITHUB_ACTIONS+x}" ]]; then
sentry createuser --superuser --email foo@tbd.com --no-password
else
sentry createuser --superuser
fi
}
build-platform-assets() {
echo "--> Building platform assets"
echo "from sentry.utils.integrationdocs import sync_docs; sync_docs(quiet=True)" | sentry exec
}
bootstrap() {
develop
init-config
run-dependent-services
create-db
apply-migrations
create-user
build-platform-assets
}
clean() {
echo "--> Cleaning static cache"
rm -rf dist/* src/sentry/static/sentry/dist/*
echo "--> Cleaning integration docs cache"
rm -rf src/sentry/integration-docs
echo "--> Cleaning pyc files"
find . -name "*.pyc" -delete
echo "--> Cleaning python build artifacts"
rm -rf build/ dist/ src/sentry/assets.json
echo ""
}
drop-db() {
# shellcheck disable=SC2155
local DROPDB=$(command -v dropdb 2>/dev/null)
if [[ -z "$DROPDB" ]]; then
DROPDB="docker exec sentry_postgres dropdb"
fi
echo "--> Dropping existing 'sentry' database"
${DROPDB} -h 127.0.0.1 -U postgres sentry || true
}
reset-db() {
drop-db
create-db
apply-migrations
}
prerequisites() {
brew update -q && brew bundle -q
}
direnv-help() {
cat >&2 <<EOF
If you're a Sentry employee and you're stuck or have questions, ask in #discuss-dev-tooling.
If you're not, please file an issue under https://github.com/getsentry/sentry/issues/new/choose and mention @getsentry/owners-sentry-dev
You can configure the behaviour of direnv by adding the following variables to a .env file:
- SENTRY_DIRENV_DEBUG=1: This will allow printing debug messages
- SENTRY_DEVENV_NO_REPORT=1: Do not report development environment errors to Sentry.io
EOF
}