-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev
More file actions
executable file
·343 lines (306 loc) · 10.6 KB
/
dev
File metadata and controls
executable file
·343 lines (306 loc) · 10.6 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
#
# SPDX-FileCopyrightText: 2026 German Aerospace Center (DLR)
# SPDX-License-Identifier: Apache-2.0
#
###############################################################################
# Helper commands #
###################
# This script contains a few little helper commands to make development easier.
###############################################################################
set -e
set -o pipefail
# imports
source "$(dirname "${BASH_SOURCE[0]}")/scripts/utils.sh"
# global variables
PROJECT_ROOT="."
PROJECT_ROOT_ABSOLUTE="$(realpath ${PROJECT_ROOT})"
BUILD_ROOT="${PROJECT_ROOT}/build"
DOC_ROOT="${PROJECT_ROOT}/docs"
SOURCE_DJANGO="${PROJECT_ROOT}/fl_server"
SOURCE_AI="${PROJECT_ROOT}/fl_server_ai"
SOURCE_API="${PROJECT_ROOT}/fl_server_api"
SOURCE_CORE="${PROJECT_ROOT}/fl_server_core"
SCRIPTS_ROOT="${PROJECT_ROOT}/scripts"
# default values of arguments
HTTP_SERVE=true
# parse arguments
action=${1,,}; shift
[ "$action" == "--help" ] || [ "$action" == "-h" ] && action=help
while [[ $# -gt 0 ]]; do
case $1 in
--no-http-serve) HTTP_SERVE=false; shift ;;
--help|-h) action=help; shift ;;
--version) action=version; shift ;;
--) shift; break ;;
*)
# warn "Unknown argument: '$1'; Skip further parsing.";
break ;;
esac
done
no_actions="$(compgen -A function)"
###############################################################################
# action functions
function help() {
actions="$(actions="$(printf '%s,' "${action_map[@]}")"; echo "{${actions%,}}")"
[ ${#actions} -lt 22 ] && actions=$(printf '%-22s' "$actions") || actions="$actions\n$(printf %24s "")"
info2 "usage: $0 <action> [options]"
info2 ""
info2 "positional arguments:"
info2 -e " ${actions}Available sub commands"
info2 " help Show this help message and exit"
info2 " start Run the application"
info2 " celery Run celery worker"
info2 " migrate Run database migrations"
info2 " makemigrations Create new database migrations"
info2 " manage Run django manage.py"
info2 " superuser Create superuser"
info2 " collectstatic Collect static files"
info2 " db-reset Reset database"
info2 " docker-build Build docker images for local development"
info2 " test Run all tests"
info2 " lint Run all linter"
info2 " lint-code Run code linter"
info2 " lint-doc Run documentation linter"
info2 " lint-scripts Run bash script linter"
info2 " mypy Run type checker"
info2 " coverage Run unit tests"
info2 " coverage-report Generate test coverage report"
info2 " doc Start documentation server"
info2 " doc-build Build documentation"
info2 " licenses Check licenses"
info2 " safety-check Check dependencies for known security vulnerabilities"
info2 " install Install package"
info2 " clean Clean up local files"
info2 " version Show package version"
info2 " versions Show versions"
info2 ""
info2 "options:"
info2 " --no-http-serve Do not serve the action result via HTTP"
}
function version() {
awk -F "=" '/version/ {print $2}' "${PROJECT_ROOT}/pyproject.toml" | awk -F'"' '{print $2}' | awk NF | head -n 1
}
function versions() {
info "versions"
if ! command -v docker > /dev/null 2>&1; then
warn "uv not found, skipping uv version"
else
info "uv version: $(uv --version)"
fi
if ! command -v docker > /dev/null 2>&1; then
warn "docker not found, skipping docker version"
else
info "$(docker --version)"
info "$(docker compose version)"
fi
info -n "package version: "
version
}
function install() {
versions
if ! command -v uv &> /dev/null; then
if ! command -v curl &> /dev/null; then
error "uv and curl are not installed. Please install one of them."
exit 1
fi
info "install uv"
curl -LsSf https://astral.sh/uv/install.sh | sh
fi
info "install package"
uv sync --all-extras
}
function clean() {
info "remove __pycache__|.pyc|.pyo"
find "${PROJECT_ROOT}" | grep -E "(__pycache__|\.pyc$$|\.pyo$$)" | xargs rm -rf
info "remove builds"
rm -rf "${BUILD_ROOT}"
rm -rf "${PROJECT_ROOT}/site"
info "remove egg-info"
rm -rf "${PROJECT_ROOT}/*.egg-info"
info "remove tox"
rm -rf "${PROJECT_ROOT}/.tox"
info "remove pytest cache"
rm -rf "${PROJECT_ROOT}/.pytest_cache"
info "remove mypy cache"
rm -rf "${PROJECT_ROOT}/.mypy_cache"
}
function start() {
docker compose up -d db redis
migrate
uv run python manage.py runserver
}
function celery() {
docker compose up -d redis
DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE:-"fl_server.settings.development"}" \
uv run celery --workdir "${PROJECT_ROOT_ABSOLUTE}" --app fl_server_ai worker --loglevel=INFO
}
function migrate() {
uv run python manage.py migrate
uv run python manage.py check
}
function makemigrations() {
uv run python manage.py makemigrations fl_server_core
}
function manage() {
uv run python manage.py "$@"
}
function superuser() {
DJANGO_SUPERUSER_PASSWORD="${DJANGO_SUPERUSER_PASSWORD:-"password"}" \
uv run python manage.py createsuperuser --username admin --email "admin@example.com" --no-input
}
function collectstatic() {
versions
info "$(tar --version | head -n 1)"
info "collect static files"
rm -rf static
rm -rf "${BUILD_ROOT}/static"
FL_DJANGO_SECRET_KEY="${FL_DJANGO_SECRET_KEY:-}" FL_POSTGRES_PASSWD="${FL_POSTGRES_PASSWD:-}" \
uv run python manage.py collectstatic --settings "fl_server.settings.production"
info "compressing static files"
mkdir -p "${BUILD_ROOT}/static"
info " | create admin.tar.gz"
tar -czf "${BUILD_ROOT}/static/admin.tar.gz" -C static admin
info " | create rest_framework.tar.gz"
tar -czf "${BUILD_ROOT}/static/rest_framework.tar.gz" -C static rest_framework
info "clean up"
rm -rf static
}
function db-reset() {
if docker ps --format "table {{.Names}}" | grep -q -e "db"; then
info "Destroy db container including its volumes and restart it"
docker compose rm -fsv db
docker compose up -d db
info "Clear redis cache"
docker exec -it redis redis-cli FLUSHALL
else
warn "Remove all unused anonymous volumes"
docker volume prune -f
fi
}
function docker-build() {
versions
info "build docker images for local development"
project_name="$(awk -F "=" '/name/ {print $2}' "${PROJECT_ROOT}/pyproject.toml" | awk -F'"' '{print $2}' | awk NF | head -n 1)"
celery_image_name="local/${project_name}-celery:latest"
django_image_name="local/${project_name}-django:latest"
info "build docker image: ${celery_image_name}"
docker build -f ./docker/celery/Dockerfile -t "${celery_image_name}" . "$@"
info "build docker image: ${django_image_name}"
docker build -f ./docker/django/Dockerfile -t "${django_image_name}" . "$@"
}
function test() {
versions
info "run all tests"
lint
mypy
coverage
coverage-report
if [ "${HTTP_SERVE}" = "true" ]; then
if [ -d "${BUILD_ROOT}/htmlcov" ]; then
uv run -m http.server --directory "${BUILD_ROOT}/htmlcov" 8080
else
error "no coverage report found"
exit 1
fi
fi
}
function lint() {
versions
info "linting"
lint-code
lint-doc
lint-scripts
}
function lint-code() {
info "lint code"
info "flake8 version: $(uv run flake8 --version | xargs)"
uv run flake8 "${SOURCE_CORE}"
uv run flake8 "${SOURCE_API}"
uv run flake8 "${SOURCE_AI}"
uv run flake8 "${SOURCE_DJANGO}"
uv run flake8 "${SCRIPTS_ROOT}"
}
function lint-doc() {
info "lint documentation"
# use markdownlint from David Anson (based on nodejs)
# https://github.com/DavidAnson/markdownlint
npm exec "markdownlint-cli2@0.22.0" -- "${DOC_ROOT}/**/*.md" "${PROJECT_ROOT}/README.md"
}
function lint-scripts() {
info "lint bash scripts"
info "shellcheck $(uv run shellcheck --version | head -n 2 | tail -n 1)"
uv run shellcheck --external-sources --shell bash --source-path "${PROJECT_ROOT}" "${PROJECT_ROOT}/dev"
uv run shellcheck --external-sources --shell bash --source-path "${SCRIPTS_ROOT}" "${SCRIPTS_ROOT}/"*.sh
}
function mypy() {
info "type checking"
uv run mypy "${SOURCE_CORE}"
uv run mypy "${SOURCE_API}"
uv run mypy "${SOURCE_AI}"
uv run mypy "${SOURCE_DJANGO}"
if find "${SCRIPTS_ROOT}" -type f -name "*.py" | grep -q .; then
uv run mypy "${SCRIPTS_ROOT}"
fi
}
function coverage() {
info "run python tests with coverage"
# TODO should we start the DB here (docker compose up db -d) or better not?
uv run coverage run manage.py test
uv run coverage html --directory "${BUILD_ROOT}/htmlcov"
}
function coverage-report() {
info "print test coverage report"
uv run coverage report
}
function doc() {
versions
info
mkdocs_version="$(uv run mkdocs --version)"
info "${mkdocs_version#"uv run "}"
# check if the user has passed the --dirtyreload flag
dirty_flag=false
for value in "$@"; do
[[ "--dirty" = "$value" ]] && dirty_flag=true
done
if [[ "$dirty_flag" == "false" ]]; then
warn "consider using --dirty to reload only file changes instead of the"
warn "whole project. This can lead to a significant speed up during the"
warn "documentation development."
fi
# create and serve documentation
DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE:-"fl_server.settings.production"}" \
FL_DJANGO_SECRET_KEY="${FL_DJANGO_SECRET_KEY:-}" FL_POSTGRES_PASSWD="${FL_POSTGRES_PASSWD:-}" \
uv run mkdocs serve "$@"
}
function doc-build() {
versions
mkdocs_version="$(uv run mkdocs --version)"
info "${mkdocs_version#"uv run "}"
info "build documentation"
DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE:-"fl_server.settings.production"}" \
FL_DJANGO_SECRET_KEY="${FL_DJANGO_SECRET_KEY:-}" FL_POSTGRES_PASSWD="${FL_POSTGRES_PASSWD:-}" \
uv run mkdocs build "$@"
}
function licenses() {
info "search for license conflicts"
uv run licensecheck
info "search for non-compliant files with REUSE"
uv run reuse lint
}
function safety-check() {
info "check dependencies for known security vulnerabilities"
# main only no dev dependencies etc.
uv run tox --recreate -e safety
}
# create array with all action functions (above)
readarray -t action_map <<< "$(comm -3 <(compgen -A function) <(echo "$no_actions"))"
###############################################################################
# run action
if ! printf '%s\n' "${action_map[@]}" | grep -x -q -- "$action"; then
echo "Invalid action : $action"
echo "Allowed actions: ${action_map[*]}"
echo "Use --help for more information"
exit 1
fi
$action "$@"