-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustfile
More file actions
473 lines (387 loc) · 14.9 KB
/
justfile
File metadata and controls
473 lines (387 loc) · 14.9 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# ----------------------------------------------------------------
# NOTE: Setting shell does not work!
# For GitHub-actions we need "bash", but
# for Windows we need "sh".
# The solution is to ensure tasks are written with bash-shebang
# if they involve bash-syntax, e.g. 'if [[ ... ]] then else fi'.
# ----------------------------------------------------------------
# set shell := [ "bash", "-c" ]
_default:
@- just --unsorted --list
menu:
@- just --unsorted --choose
# ----------------------------------------------------------------
# Justfile
# Recipes for various workflows.
# ----------------------------------------------------------------
set dotenv-load := true
# set dotenv-path := [".env", ".env.docker-vars"]
set positional-arguments := true
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# VARIABLES
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PATH_ROOT := justfile_directory()
CURRENT_DIR := invocation_directory()
OS := if os_family() == "windows" { "windows" } else { "linux" }
PYVENV_ON := if os_family() == "windows" { ". .venv/Scripts/activate" } else { ". .venv/bin/activate" }
PYVENV := if os_family() == "windows" { "python" } else { "python3" }
LINTING := "ruff"
GEN_MODELS := "datamodel_code_generator"
GEN_MODELS_DOCUMENTATION := "openapi-generator-cli"
TOOL_TEST_BDD := "behave"
# --------------------------------
# Macros
# --------------------------------
_clean-all-files path pattern:
#!/usr/bin/env bash
find {{path}} -type f -name "{{pattern}}" -exec basename {} \; 2> /dev/null
find {{path}} -type f -name "{{pattern}}" -exec rm {} \; 2> /dev/null
exit 0;
_clean-all-folders path pattern:
#!/usr/bin/env bash
find {{path}} -type d -name "{{pattern}}" -exec basename {} \; 2> /dev/null
find {{path}} -type d -name "{{pattern}}" -exec rm -rf {} \; 2> /dev/null
exit 0;
_generate-documentation path_schema target_path name:
@{{PYVENV_ON}} && {{GEN_MODELS_DOCUMENTATION}} generate \
--skip-validate-spec \
--input-spec {{path_schema}}/schema-{{name}}.yaml \
--generator-name markdown \
--output "{{target_path}}/{{name}}"
_build-documentation-recursively path_schema target_path:
#!/usr/bin/env bash
while read path; do
if [[ "${path}" == "" ]]; then continue; fi
path="${path##*/}";
name="$( echo """${path}""" | sed -E """s/^schema-(.*)\.yaml$/\1/g""")";
echo "- generate documentation for ${name}."
just _generate-documentation "{{path_schema}}" "{{target_path}}" "${name}";
done <<< "$( ls -f {{path_schema}}/schema-*.yaml )";
exit 0;
_generate-models path_schema target_path name:
@ # cf. https://github.com/koxudaxi/datamodel-code-generator?tab=readme-ov-file#all-command-options
@{{PYVENV_ON}} && {{PYVENV}} -m {{GEN_MODELS}} \
--input-file-type openapi \
--output-model-type pydantic_v2.BaseModel \
--encoding "UTF-8" \
--disable-timestamp \
--use-schema-description \
--use-standard-collections \
--use-union-operator \
--use-default-kwarg \
--field-constraints \
--output-datetime-class AwareDatetime \
--capitalise-enum-members \
--enum-field-as-literal one \
--set-default-enum-member \
--use-subclass-enum \
--allow-population-by-field-name \
--strict-nullable \
--use-double-quotes \
--target-python-version 3.11 \
--input "{{path_schema}}/schema-{{name}}.yaml" \
--output "{{target_path}}/{{name}}.py"
_generate-models-recursively path_schema target_path:
#!/usr/bin/env bash
while read path; do
if [[ "${path}" == "" ]]; then continue; fi
path="${path##*/}";
name="$( echo """${path}""" | sed -E """s/^schema-(.*)\.yaml$/\1/g""")";
echo "- generate models for ${name}."
just _generate-models "{{path_schema}}" "{{target_path}}" "${name}";
done <<< "$( ls -f {{path_schema}}/schema-*.yaml )";
exit 0;
_dco project="local" file="docker-compose.yaml" *args:
#!/usr/bin/env bash
# ensure environment is overridden by docker variables
source <(cat .env .env.docker-vars)
# export COMPOSE_BAKE=true
docker compose \
--project-name "{{project}}" \
--file "{{file}}" \
--env-file ".env" \
--env-file ".env.docker-vars" \
{{args}}
_docker-stop service project="${DOCKER_PROJECT}" file="docker-compose.yaml":
@just _dco "{{project}}" "{{file}}" stop "{{service}}"
_docker-build service progress="tty" project="${DOCKER_PROJECT}" file="docker-compose.yaml":
@echo "BUILD DOCKER +LOG {{service}}"
@- just clean-docker "{{service}}" "{{project}}" "{{file}}" 2> /dev/null
@just _dco "{{project}}" "{{file}}" logs --tail=0 {{service}} \
&& just _dco "{{project}}" "{{file}}" --progress={{progress}} build {{service}}
_docker-up service progress="tty" project="${DOCKER_PROJECT}" file="docker-compose.yaml":
@echo "RUN DOCKER +LOG {{service}}"
@- just clean-docker "{{service}}" "{{project}}" "{{file}}" 2> /dev/null
@just _dco "{{project}}" "{{file}}" logs --tail=0 {{service}} \
&& just _dco "{{project}}" "{{file}}" --progress={{progress}} up {{service}}
_docker-run service project="${DOCKER_PROJECT}" file="docker-compose.yaml" cmd="bash --login -i" *args:
@echo "EXEC DOCKER CMD (it) {{service}}"
@- just clean-docker "{{service}}" "{{project}}" "{{file}}" 2> /dev/null
@just _dco "{{project}}" "{{file}}" run --rm --interactive --service-ports {{service}} {{cmd}} {{args}}
# ----------------------------------------------------------------
# TARGETS
# ----------------------------------------------------------------
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TARGETS: build
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setup:
@echo "TASK: SETUP"
@mkdir -p "setup"
@# For general setup
- cp -n "templates/template.env" ".env"
@# For local execution
- cp -n "templates/template-config.yaml" "setup/config.yaml"
- cp -n "templates/template-requests.yaml" "setup/requests.yaml"
@# For docker development
- cp -n "templates/template.env.docker-vars" ".env.docker-vars"
- cp -n "templates/template.env.docker-secrets" ".env.docker-secrets"
build:
@echo "TASK: BUILD"
@- just build-venv
@just check-system
@just build-requirements
@just build-models
build-venv:
@echo "SUBTASK: create venv if not exists"
@${PYTHON_PATH} -m venv .venv
build-requirements:
@echo "SUBTASK: build requirements"
@just build-requirements-basic
@just build-requirements-dependencies
@just build-requirements-export
build-requirements-basic:
@- {{PYVENV_ON}} && {{PYVENV}} -m pip install --upgrade pip 2> /dev/null
@{{PYVENV_ON}} && pip install ruff uv
build-requirements-dependencies:
@{{PYVENV_ON}} && uv pip install \
--exact \
--strict \
--compile-bytecode \
--no-python-downloads \
--requirements pyproject.toml
@{{PYVENV_ON}} && uv sync
build-requirements-export:
@- rm -f requirements.txt 2> /dev/null
@{{PYVENV_ON}} && uv pip freeze \
--exclude-editable \
--no-python-downloads \
--project "." \
--strict \
--verbose \
> requirements.txt
build-models:
@echo "SUBTASK: build data models from schemata."
@rm -rf "src/models/generated" 2> /dev/null
@mkdir -p "src/models/generated"
@touch "src/models/generated/__init__.py"
@just _generate-models-recursively "models" "src/models/generated"
build-docs:
@echo "SUBTASK: build documentation for data models from schemata."
@rm -rf "docs/models" 2> /dev/null
@mkdir -p "docs/models"
@- just _build-documentation-recursively "models" "docs/models"
@- just _clean-all-files "." ".openapi-generator*"
@- just _clean-all-folders "." ".openapi-generator*"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TARGETS: execution
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# run in cli mode
run *args:
@{{PYVENV_ON}} && {{PYVENV}} -m src.cli {{args}}
# run via fast api
start-server env_path=".env" log_path="${PATH_LOGS}":
@{{PYVENV_ON}} && {{PYVENV}} -m src.api \
--env "{{env_path}}" \
--log "{{log_path}}"
# --------------------------------
# TARGETS: docker
# --------------------------------
docker-explore service="build":
@just _docker-run "{{service}}"
docker-clean progress="tty":
@- docker compose rm -sf "base" 2> /dev/null
@- docker compose rm -sf "build" 2> /dev/null
@- docker image prune -a 2> /dev/null
# NOTE: only for debugging purposes - not needed for build
docker-setup progress="tty":
@just _docker-build "base" "{{progress}}"
docker-build progress="tty":
@just create-logs "${PATH_LOGS}"
@just _docker-build "build" "{{progress}}"
docker-qa progress="tty":
@just _docker-up "qa" "{{progress}}"
docker-start-server progress="tty":
@just create-logs "${PATH_LOGS}"
@just _docker-up "server" "{{progress}}"
docker-stop-server:
@just _docker-stop "server"
docker-build-queue progress="tty":
@just _docker-up "queue-build" "{{progress}}"
docker-start-queue progress="tty":
@mkdir -p "${PATH_LOGS_QUEUE}"
@mkdir -p "${PATH_LOGS_QUEUE_STATE}"
@just _docker-up "queue-server" "{{progress}}"
docker-stop-queue progress="tty":
@just _docker-stop "queue-server"
docker-register-users progress="tty":
@mkdir -p "${PATH_LOGS_QUEUE}"
@mkdir -p "${PATH_LOGS_QUEUE_STATE}"
@just _docker-up "queue-admin" "{{progress}}"
# --------------------------------
# TARGETS: development + tools
# --------------------------------
# Recipe only works if local file dev exists
dev *args:
@{{PYVENV_ON}} && {{PYVENV}} -m dev {{args}}
docker-dev:
@just _docker-run "example" "${DOCKER_PROJECT}_sandbox" "docker-compose.sandbox.yaml"
@just _docker-stop "example" "${DOCKER_PROJECT}_sandbox" "docker-compose.sandbox.yaml" 2> /dev/null
ping:
@echo "$(date '+%Y-%m-%d %H:%M:%S') \$dev [DEBUG] ping success"
greet name:
@echo "$(date '+%Y-%m-%d %H:%M:%S') \$dev [DEBUG] Hello, {{name}}!" >> ${PATH_LOGS}/debug.log
check-time-matches-cron cron_expr time="*":
@{{PYVENV_ON}} && {{PYVENV}} -m scripts.cron "{{cron_expr}}" --time "{{time}}"
# Recipe only works if local file scripts/mocks.py exists
create-mocks *args:
@{{PYVENV_ON}} && {{PYVENV}} -m scripts.mocks {{args}}
demo name:
@just run SEARCH-FS --requests "demo/{{name}}/requests.yaml"
demos:
@just demo "example-all"
# --------------------------------
# TARGETS: terminate execution
# --------------------------------
# finds pid based on port
get-pid PORT:
@- lsof -ti ":{{PORT}}" 2> /dev/null
# kills process based on process ID
kill-pid PID:
@echo "Killing {{PID}}"
@kill -9 {{PID}} 2> /dev/null
kill-port PORT:
#!/usr/bin/env bash
PID="$( just get-pid "{{PORT}}" )";
if [[ "${PID}" == "" ]]; then
echo "No process running on PORT {{PORT}}";
else
echo "Process ${PID} running on PORT {{PORT}}";
just kill-pid "${PID}";
fi
exit 0;
# --------------------------------
# TARGETS: tests
# --------------------------------
test-unit path *args:
@{{PYVENV_ON}} && pytest "{{path}}" {{args}}
test-unit-one path method:
@just test-unit "{{path}}" -k "{{method}}"
tests-unit:
@just test-unit "tests/unit" --cov-reset --cov="."
# --------------------------------
# TARGETS: qa
# NOTE: use for development only.
# --------------------------------
qa:
@{{PYVENV_ON}} && coverage report -m
coverage source_path tests_path:
@{{PYVENV_ON}} && pytest {{tests_path}} \
--ignore=tests/integration \
--cov-reset \
--cov={{source_path}} \
--capture=tee-sys \
2> /dev/null
# --------------------------------
# TARGETS: prettify
# --------------------------------
lint path:
@{{PYVENV_ON}} && {{PYVENV}} -m {{LINTING}} check \
--respect-gitignore \
--show-fixes \
--no-unsafe-fixes \
--exit-zero \
--fix \
"{{path}}"
@{{PYVENV_ON}} && {{PYVENV}} -m {{LINTING}} format \
--respect-gitignore \
"{{path}}"
lint-dry path:
@{{PYVENV_ON}} && {{PYVENV}} -m {{LINTING}} check \
--respect-gitignore \
--no-unsafe-fixes \
--exit-zero \
--diff \
"{{path}}"
lint-check path:
@{{PYVENV_ON}} && {{PYVENV}} -m {{LINTING}} check \
--respect-gitignore \
--no-unsafe-fixes \
--exit-zero \
--verbose \
"{{path}}"
prettify:
@just lint "src"
@just lint "tests"
prettify-dry:
@just lint-dry "src"
@just lint-dry "tests"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TARGETS: clean
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clean log_path="${PATH_LOGS}":
@just clean-venv
@just clean-basic "{{log_path}}"
clean-basic log_path="${PATH_LOGS}":
@echo "All system artefacts will be force removed."
@- just _clean-all-files "." ".DS_Store" 2> /dev/null
@echo "All test artefacts will be force removed."
@- rm -rf ".pytest_cache" 2> /dev/null
@- rm -f ".coverage" 2> /dev/null
@echo "All build artefacts will be force removed."
@- rm -rf ".venv" 2> /dev/null
@- rm -rf "build" 2> /dev/null
@- rm -rf "bindings/rust/target" 2> /dev/null
@- just _clean-all-folders "." ".idea" 2> /dev/null
@- just _clean-all-folders "." "__pycache__" 2> /dev/null
clean-venv:
@echo "VENV will be removed."
@- just _delete-if-folder-exists ".venv" 2> /dev/null
# removes containers
clean-docker service project="${DOCKER_PROJECT}" file="docker-compose.yaml":
@just _dco "{{project}}" "{{file}}" rm -f "{{service}}"
# removes containers and images
clean-docker-full service project="${DOCKER_PROJECT}" file="docker-compose.yaml":
@just _dco "{{project}}" "{{file}}" rm -f "{{service}}"
@just _dco "{{project}}" "{{file}}" down --remove-orphans
@just _dco "{{project}}" "{{file}}" down --rmi local
@docker volume prune -f
# --------------------------------
# TARGETS: logging, session
# --------------------------------
clear-logs log_path="${PATH_LOGS}":
@rm -rf "{{log_path}}" 2> /dev/null
create-logs log_path="${PATH_LOGS}":
@just _create-logs-part "debug" "{{log_path}}"
@just _create-logs-part "out" "{{log_path}}"
@just _create-logs-part "err" "{{log_path}}"
_create-logs-part part log_path="${PATH_LOGS}":
@mkdir -p "{{log_path}}"
@touch "{{log_path}}/{{part}}.log"
watch-logs n="10":
@tail -f -n {{n}} ${PATH_LOGS}/out.log
watch-logs-err n="10":
@tail -f -n {{n}} ${PATH_LOGS}/err.log
watch-logs-debug n="10":
@tail -f -n {{n}} ${PATH_LOGS}/debug.log
watch-logs-all n="10":
@just watch-logs {{n}} &
@just watch-logs-err {{n}} &
@just watch-logs-debug {{n}} &
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TARGETS: requirements
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check-system:
@echo "Operating System detected: {{os_family()}}"
@echo "Python command used: ${PYTHON_PATH}"
@echo "Python command for venv: {{PYVENV}}"
@echo "Python path for venv: $( {{PYVENV_ON}} && which {{PYVENV}} )"