-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathMakefile
More file actions
146 lines (119 loc) 路 4.21 KB
/
Copy pathMakefile
File metadata and controls
146 lines (119 loc) 路 4.21 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
export UID=$(shell id -u)
default: build start create_db populate test stop clean
# Build containers
.PHONY: build
build: create_empty_secret create_default_env
docker compose build
.PHONY: build_with_version
build_with_version: create_empty_secret create_default_env
docker compose build --build-arg MAKE_PYTHON_VERSION=$(PYTHON_VERSION)
.PHONY: test_with_version
test_with_version: build_with_version
touch OpenOversight/tests/coverage.xml
docker compose run --rm web-test pytest --cov-report xml:OpenOversight/tests/coverage.xml -n 4 --dist=loadfile -v OpenOversight/tests/
# Run containers
.PHONY: start
start: build
docker compose up -d
.PHONY: create_db
create_db: start
@until docker compose exec postgres psql -h localhost -U openoversight -c '\l' postgres &>/dev/null; do \
echo "Postgres is unavailable - sleeping..."; \
sleep 1; \
done
@echo "Postgres is up"
## Creating database
docker compose run --rm web python ./create_db.py
.PHONY: db_diagram
db_diagram:
# Create new dot file showing current version of schema
eralchemy2 -i postgresql://openoversight:terriblepassword@postgres/openoversight-dev -o database/schema.new.md
# Remove hyperlink in file
sed -i '/^!\[\](/d' database/schema.new.md
# Sort new version of schema file
LC_ALL=C sort database/schema.new.md -o schema.new.md.sorted
# Create old schema file if it does not exist and then sort it
touch database/schema.md
LC_ALL=C sort database/schema.md -o schema.md.sorted
# Create a new diagram if there are changes, otherwise clean up files
@if diff schema.md.sorted schema.new.md.sorted > /dev/null 2>&1; then \
echo 'No schema changes detected!'; \
rm database/schema.new.md; \
else \
echo 'Detected schema changes, making new DB relationship diagram!'; \
mv database/schema.new.md database/schema.md; \
eralchemy2 -i postgresql://openoversight:terriblepassword@postgres/openoversight-dev -o database/schema.dot; \
dot -Tpng -o /usr/src/app/database/database_relationships.png -Grankdir=TB -Kdot database/schema.dot; \
rm database/schema.dot; \
fi
# Remove all sorted files
rm -f schema.md.sorted schema.new.md.sorted
.PHONY: create_db_diagram
create_db_diagram: build start
docker compose run --rm web-test make db_diagram
.PHONY: dev
dev: create_empty_secret create_default_env build start create_db populate
# Build and run containers
.PHONY: populate
populate: create_db
@until docker compose exec postgres psql -h localhost -U openoversight -c '\l' postgres &>/dev/null; do \
echo "Postgres is unavailable - sleeping..."; \
sleep 1; \
done
@echo "Postgres is up"
## Populate database with test data
docker compose run --rm web python ./test_data.py -p
# Run tests
.PHONY: test
test: start
if [ -z "$(name)" ]; then \
docker compose run --rm web-test pytest -n auto --dist=loadfile -v OpenOversight/tests/; \
else \
docker compose run --rm web-test pytest -v OpenOversight/tests/ -k $(name); \
fi
.PHONY: lint
lint:
uv run pre-commit run --all-files
.PHONY: env
env:
uv venv
.PHONY: install
install:
uv pip install -r requirements.txt -r dev-requirements.txt
# Stop containers
.PHONY: stop
stop:
docker compose stop
# Remove containers
.PHONY: clean
clean: stop
docker compose rm -f
# Wipe database
.PHONY: clean_all
clean_all: clean stop
docker compose down -v
# Build project documentation in live reload for editing
.PHONY: docs
docs:
make -C docs/ clean && sphinx-autobuild docs/ docs/_build/html
# Print this message and exit
.PHONY: help
help:
@printf "OpenOversight: Makefile for development, documentation and testing.\n"
@printf "Subcommands:\n\n"
@awk 'BEGIN {FS = ":.*?## "} /^[0-9a-zA-Z_-]+:.*?## / {printf "\033[36m%s\033[0m : %s\n", $$1, $$2}' $(MAKEFILE_LIST) \
| sort \
| column -s ':' -t
.PHONY: attach
attach:
docker compose exec postgres psql -h localhost -U openoversight openoversight-dev
# This is needed to make sure docker doesn't create an empty directory, or delete that directory first
.PHONY: create_empty_secret
create_empty_secret:
touch service_account_key.json || \
(echo "Need to delete that empty directory first"; \
sudo rm -d service_account_key.json/; \
touch service_account_key.json)
.PHONY: create_default_env
create_default_env:
if [ ! -f .env ]; then cp .env.example .env; fi