generated from opensafely-core/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
justfile
179 lines (117 loc) · 4.77 KB
/
justfile
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
# Load .env files by default
set dotenv-load := true
export VIRTUAL_ENV := env_var_or_default("VIRTUAL_ENV", ".venv")
export BIN := VIRTUAL_ENV + if os_family() == "unix" { "/bin" } else { "/Scripts" }
export PIP := BIN + if os_family() == "unix" { "/python -m pip" } else { "/python.exe -m pip" }
export DEFAULT_PYTHON := if os_family() == "unix" { "python3.12" } else { "python" }
export DEV_USERID := `id -u`
export DEV_GROUPID := `id -g`
# list available commands
default:
@"{{ just_executable() }}" --list
# clean up temporary files
clean:
rm -rf .venv
_env:
#!/usr/bin/env bash
set -euo pipefail
test -f .env || cp dotenv-sample .env
# ensure valid virtualenv
virtualenv: _env
#!/usr/bin/env bash
set -euo pipefail
# allow users to specify python version in .env
PYTHON_VERSION=${PYTHON_VERSION:-$DEFAULT_PYTHON}
# create venv and upgrade pip
test -d $VIRTUAL_ENV || { $PYTHON_VERSION -m venv $VIRTUAL_ENV && $PIP install --upgrade pip; }
# ensure we have pip-tools so we can run pip-compile
test -e $BIN/pip-compile || $PIP install pip-tools
_compile src dst *args: virtualenv
#!/usr/bin/env bash
set -euo pipefail
# exit if src file is older than dst file (-nt = 'newer than', but we negate with || to avoid error exit code)
test "${FORCE:-}" = "true" -o {{ src }} -nt {{ dst }} || exit 0
$BIN/pip-compile --allow-unsafe --generate-hashes --output-file={{ dst }} {{ src }} {{ args }}
# update requirements.prod.txt if requirements.prod.in has changed
requirements-prod *args:
"{{ just_executable() }}" _compile requirements.prod.in requirements.prod.txt {{ args }}
# update requirements.dev.txt if requirements.dev.in has changed
requirements-dev *args: requirements-prod
"{{ just_executable() }}" _compile requirements.dev.in requirements.dev.txt {{ args }}
# ensure prod requirements installed and up to date
prodenv: requirements-prod
#!/usr/bin/env bash
set -euo pipefail
# exit if .txt file has not changed since we installed them (-nt == "newer than', but we negate with || to avoid error exit code)
test requirements.prod.txt -nt $VIRTUAL_ENV/.prod || exit 0
$PIP install -r requirements.prod.txt
touch $VIRTUAL_ENV/.prod
# && dependencies are run after the recipe has run. Needs just>=0.9.9. This is
# a killer feature over Makefiles.
#
# ensure dev requirements installed and up to date
devenv: prodenv requirements-dev && install-precommit
#!/usr/bin/env bash
set -euo pipefail
# exit if .txt file has not changed since we installed them (-nt == "newer than', but we negate with || to avoid error exit code)
test requirements.dev.txt -nt $VIRTUAL_ENV/.dev || exit 0
$PIP install -r requirements.dev.txt
touch $VIRTUAL_ENV/.dev
# ensure precommit is installed
install-precommit:
#!/usr/bin/env bash
set -euo pipefail
BASE_DIR=$(git rev-parse --show-toplevel)
test -f $BASE_DIR/.git/hooks/pre-commit || $BIN/pre-commit install
# upgrade dev or prod dependencies (specify package to upgrade single package, all by default)
upgrade env package="": virtualenv
#!/usr/bin/env bash
set -euo pipefail
opts="--upgrade"
test -z "{{ package }}" || opts="--upgrade-package {{ package }}"
FORCE=true "{{ just_executable() }}" requirements-{{ env }} $opts
update-dependencies: virtualenv
just upgrade prod
just upgrade dev
# *args is variadic, 0 or more. This allows us to do `just test -k match`, for example.
# Run the tests
test *args: devenv
$BIN/pytest {{ args }}
coverage: devenv
$BIN/coverage run --module pytest
$BIN/coverage report || $BIN/coverage html
black *args=".": devenv
$BIN/black --check {{ args }}
ruff *args=".": devenv
$BIN/ruff check {{ args }}
# run the various dev checks but does not change any files
check: black ruff
# fix formatting and import sort ordering
fix: devenv
$BIN/black .
$BIN/ruff check --fix .
# Run the grafana stack
grafana:
docker compose up grafana
# Run a metrics task (defaults to running all tasks)
metrics *args: devenv
#!/usr/bin/env bash
set -euo pipefail
MODULE="metrics.tasks{{ if args == "" { "" } else { "." + args } }}"
$BIN/python -m $MODULE
docker-build env="dev": _env
#!/usr/bin/env bash
set -euo pipefail
test -z "${SKIP_BUILD:-}" || { echo "SKIP_BUILD set"; exit 0; }
# set build args for prod builds
export BUILD_DATE=$(date -u +'%y-%m-%dT%H:%M:%SZ')
export GITREF=$(git rev-parse --short HEAD)
# build the thing
docker compose build --pull metrics-{{ env }}
# run command in dev|prod container
docker-run env="dev" *args="": _env
{{ just_executable() }} docker-build {{ env }}
docker compose run --rm metrics-{{ env }} {{ args }}
# See DEVELOPERS.md
clean-cache:
rm -f github-cache.sqlite