Skip to content

Commit 9d9f7f6

Browse files
Update scripts (#103)
* Simplify plotter * Refactor Makefile * Fix log disabling * Update README
1 parent 5463330 commit 9d9f7f6

10 files changed

Lines changed: 75 additions & 52 deletions

File tree

Makefile

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ INFRA_INIT_SERVICES ?=
1717
# -----------------------------
1818
# Internal vars / aliases
1919
# -----------------------------
20-
PYTHON_BIN := python
2120
DOCKER_COMPOSE := docker compose -p $(PROJECT_NAME)
22-
DOCKER_COMPOSE_PRUNE := scripts/makefile/docker_prune.sh
21+
PIP_AUDIT := scripts/makefile/pip_audit.sh
22+
SLOTSCHECK := scripts/makefile/slotscheck.sh
23+
DOCKER_ENV := scripts/makefile/docker_env.sh
24+
LOCAL_ENV := scripts/makefile/local_env.sh
25+
DOCKER_PRUNE := scripts/makefile/docker_prune.sh
26+
PYCACHE_DEL := scripts/makefile/pycache_del.sh
27+
DISHKA_PLOT_DATA := scripts/dishka/plot_dependencies_data.py
2328

2429
# Test stack is isolated by project name
2530
TEST_PROJECT ?= $(PROJECT_NAME)-test
@@ -52,25 +57,20 @@ PYTEST_ARGS_COV_DOCKER := \
5257
# Safety
5358
.PHONY: pip-audit
5459
pip-audit:
55-
tmp=$$(mktemp -d); trap 'rm -rf "$$tmp"' EXIT; \
56-
uv -qq export --format pylock.toml -o "$$tmp/pylock.toml"; \
57-
pip-audit --locked "$$tmp" \
58-
|| echo "WARNING: pip-audit found vulnerabilities (non-blocking)" >&2
60+
$(PIP_AUDIT)
5961

6062
# Code quality
6163
.PHONY: slotscheck lint test check
6264
slotscheck:
63-
slotscheck $(SLOTSCHECK_TARGET) 2>&1 | tee /dev/stderr \
64-
| { grep -m1 "Failed to import" || true; } | cut -d"'" -f2 \
65-
| xargs -r -n1 $(PYTHON_BIN) -c 'import importlib,sys; importlib.import_module(sys.argv[1])'
65+
$(SLOTSCHECK) src
6666

6767
lint:
6868
ruff check --fix
6969
ruff format
7070
tombi format
7171
tombi lint
7272
deptry
73-
$(MAKE) slotscheck SLOTSCHECK_TARGET=src
73+
$(MAKE) slotscheck
7474
lint-imports
7575
mypy
7676

@@ -85,33 +85,10 @@ check: lint test
8585
# Docker compose
8686
.PHONY: docker-env local-env upd up upd-local up-local down stop-all
8787
docker-env:
88-
{ \
89-
echo "# This .env file is generated automatically for DOCKER environment by Makefile."; \
90-
echo "# Do not edit it directly; edit env.example / .secrets and Makefile instead."; \
91-
echo; \
92-
cat env.example; \
93-
if [ -f .secrets ]; then \
94-
echo; \
95-
echo "# --- secrets from .secrets (not committed) ---"; \
96-
cat .secrets; \
97-
fi; \
98-
} > .env
88+
$(DOCKER_ENV)
9989

10090
local-env:
101-
{ \
102-
echo "# This .env file is generated automatically for LOCAL environment by Makefile."; \
103-
echo "# Do not edit it directly; edit env.example / .secrets and Makefile instead."; \
104-
echo; \
105-
sed \
106-
-e 's|^EXAMPLE_SERVICE_URL=.*|EXAMPLE_SERVICE_URL=http://127.0.0.1:51999|' \
107-
-e 's|^POSTGRES_HOST=.*|POSTGRES_HOST=127.0.0.1|' \
108-
env.example; \
109-
if [ -f .secrets ]; then \
110-
echo; \
111-
echo "# --- secrets from .secrets (not committed) ---"; \
112-
cat .secrets; \
113-
fi; \
114-
} > .env
91+
$(LOCAL_ENV)
11592

11693
upd: docker-env
11794
$(DOCKER_COMPOSE) up -d --build --force-recreate
@@ -158,18 +135,15 @@ test-docker: docker-env
158135

159136
.PHONY: prune
160137
prune:
161-
$(DOCKER_COMPOSE_PRUNE)
138+
$(DOCKER_PRUNE)
162139

163140
# Project structure visualization
164141
.PHONY: pycache-del tree plot-data
165-
PYCACHE_DEL := scripts/makefile/pycache_del.sh
166-
DISHKA_PLOT_DATA := scripts/dishka/plot_dependencies_data.py
167-
168142
pycache-del:
169-
@$(PYCACHE_DEL)
143+
$(PYCACHE_DEL)
170144

171145
tree: pycache-del
172-
@tree
146+
tree
173147

174148
plot-data:
175-
@$(PYTHON_BIN) $(DISHKA_PLOT_DATA)
149+
APP_LOGGING_LEVEL=CRITICAL python $(DISHKA_PLOT_DATA)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Stay tuned. Refactor in progress, see [`legacy-2025`](https://github.com/ivan-borovets/fastapi-clean-example/tree/legacy-2025) branch for architecture docs
44

55
TODO:
6+
- [x] Write tests
67
- [ ] Explain code and patterns in new README
78
- [ ] Make template project
89

scripts/dishka/plot_dependencies_data.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import asyncio
22

33
import dishka.plotter
4-
from dishka import AsyncContainer
54

65
from app.main.run import make_app
76

87

9-
def generate_d2_dependency_graph(container: AsyncContainer) -> str:
8+
async def main() -> None:
109
"""
1110
Generates dependency graph for container in `d2` format.
1211
See https://d2lang.com for rendering instructions.
1312
"""
14-
return dishka.plotter.render_d2(container)
15-
16-
17-
async def main() -> None:
1813
app = make_app()
1914
container = app.state.dishka_container
2015
try:
21-
print(generate_d2_dependency_graph(container))
16+
print(dishka.plotter.render_d2(container))
2217
finally:
2318
await container.close()
2419

scripts/makefile/docker_env.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
# The script is called from Makefile
3+
set -eu -o pipefail
4+
5+
{
6+
echo "# This .env file is generated automatically for DOCKER environment by Makefile."
7+
echo "# Do not edit it directly; edit env.example / .secrets and Makefile instead."
8+
echo
9+
cat env.example
10+
if [ -f .secrets ]; then
11+
echo
12+
echo "# --- secrets from .secrets (not committed) ---"
13+
cat .secrets
14+
fi
15+
} > .env

scripts/makefile/docker_prune.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22
# The script is called from Makefile
3+
set -eu -o pipefail
4+
35
echo "Warning: This will remove all unused containers, networks, images, and volumes."
46
echo "Are you sure you want to continue? [y/N]"
57
read -r response

scripts/makefile/local_env.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
# The script is called from Makefile
3+
set -eu -o pipefail
4+
5+
{
6+
echo "# This .env file is generated automatically for LOCAL environment by Makefile."
7+
echo "# Do not edit it directly; edit env.example / .secrets and Makefile instead."
8+
echo
9+
sed \
10+
-e 's|^EXAMPLE_SERVICE_URL=.*|EXAMPLE_SERVICE_URL=http://127.0.0.1:51999|' \
11+
-e 's|^POSTGRES_HOST=.*|POSTGRES_HOST=127.0.0.1|' \
12+
env.example
13+
if [ -f .secrets ]; then
14+
echo
15+
echo "# --- secrets from .secrets (not committed) ---"
16+
cat .secrets
17+
fi
18+
} > .env

scripts/makefile/pip_audit.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
# The script is called from Makefile
3+
set -eu -o pipefail
4+
5+
tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT
6+
uv -qq export --format pylock.toml -o "$tmp/pylock.toml"
7+
pip-audit --locked "$tmp" \
8+
|| echo "WARNING: pip-audit found vulnerabilities (non-blocking)" >&2

scripts/makefile/pycache_del.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#!/bin/bash
2-
find . -type d -name '__pycache__' -prune -exec rm -rf {} +; \
3-
find . -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete
2+
# The script is called from Makefile
3+
set -eu -o pipefail
4+
5+
find . -type d -name '__pycache__' -prune -exec rm -rf {} +
6+
find . -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete

scripts/makefile/slotscheck.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
# The script is called from Makefile
3+
set -eu -o pipefail
4+
5+
slotscheck "$1" 2>&1 | tee /dev/stderr \
6+
| { grep -m1 "Failed to import" || true; } | cut -d"'" -f2 \
7+
| xargs -r -n1 python -c 'import importlib,sys; importlib.import_module(sys.argv[1])'

src/app/infrastructure/persistence_sqla/alembic/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Interpret the config file for Python logging.
1919
# This line sets up loggers basically.
2020
if config.config_file_name is not None:
21-
fileConfig(config.config_file_name)
21+
fileConfig(config.config_file_name, disable_existing_loggers=False)
2222

2323
# add your model's MetaData object here
2424
# for 'autogenerate' support

0 commit comments

Comments
 (0)