-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
130 lines (110 loc) · 4.09 KB
/
Makefile
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
.PHONY: build serve server-container pause address stop-server restart-server lint \
test pytest isort black flake8 mypy shell
# Usage:
# make build # build the docker image
# make serve # serve the website
# make server-container # build server container
# make pause # pause 1 second (to pause between commands)
# make address # get Docker container address/port
# make stop-server # stop the running web server
# make restart-server # restart the running web server
# make lint # run linters
# make test # run full testing suite
# make pytest # run pytest in docker container
# make isort # run isort in docker container
# make black # run black in docker container
# make flake8 # run flake8 in docker container
# make mypy # run mypy in docker container
# make shell # create interactive shell in docker container
################################################################################
# GLOBALS #
################################################################################
# path related variables
CURRENTDIR := $(PWD)
# docker-related variables
DCTNR := webserver.$(notdir $(PWD))
SRCPATH = /usr/local/src/parley
DCKRIMG = ghcr.io/diogenesanalytics/parley:master
DCKRBLD = docker build -t ${DCKRIMG} . --load
DCKRUSR = --user 1000:1000
DCKRTST = docker run --rm ${DCKRUSR} -v ${CURRENTDIR}:${SRCPATH} -it ${DCKRIMG}
################################################################################
# COMMANDS #
################################################################################
# build docker image
build:
${DCKRBLD}
# serve the website
serve: server-container pause address
# build server container
server-container:
@ echo "Launching web server in Docker container -> ${DCTNR} ..."
@ if ! docker ps --format="{{.Names}}" | grep -q "${DCTNR}"; then \
docker run -d \
--rm \
--name ${DCTNR} \
-p 8000 \
-v "${CURRENTDIR}":/usr/local/src/parley \
${DCKROPT} \
${DCKRIMG} \
python3 -m http.server 8000 && \
if ! grep -sq "${DCTNR}" "${CURRENTDIR}/.running_containers"; then \
echo "${DCTNR}" >> .running_containers; \
fi; \
else \
echo "Container already running. Try setting DCTNR manually."; \
fi
# simply wait for a certain amount of time
pause:
@ echo "Sleeping 1 seconds ..."
@ sleep 1
# get containerized server address
address:
@ if [ -f "${CURRENTDIR}/.running_containers" ]; then \
while read container; do \
if echo "$${container}" | grep -q "${DCTNR}" ; then \
echo "Server address: http://$$(docker port ${DCTNR}| grep 0.0.0.0: | \
awk '{print $$3}')"; \
else \
echo "Could not find running container: ${DCTNR}." \
"Try running: make list-containers"; \
fi \
done < "${CURRENTDIR}/.running_containers"; \
else \
echo ".running_containers file not found. Is a Docker container running?"; \
fi
# stop all containers
stop-server:
@ if [ -f "${CURRENTDIR}/.running_containers" ]; then \
echo "Stopping Docker containers ..."; \
while read container; do \
echo "Container $$(docker stop $$container) stopped."; \
done < "${CURRENTDIR}/.running_containers"; \
rm -f "${CURRENTDIR}/.running_containers"; \
else \
echo "${CURRENTDIR}/.running_containers file not found."; \
fi
# restart server
restart-server: stop-server serve
# run linters
lint: isort black flake8 mypy
# run full testing suite
test: pytest lint
# run pytest in docker container
pytest:
@ ${DCKRTST} pytest
# run isort in docker container
isort:
@ ${DCKRTST} isort tests/
# run black in docker container
black:
@ ${DCKRTST} black tests/
# run flake8 in docker container
flake8:
@ ${DCKRTST} flake8 --config=tests/.flake8
# run mypy in docker container
mypy:
@ ${DCKRTST} mypy --ignore-missing-imports tests/
# create interactive shell in docker container
shell:
@ ${DCKRTST} bash || true