-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
164 lines (138 loc) · 5.34 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
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
.PHONY: all jupyter pause address containers list-containers stop-containers \
restart-containers lint tests pytest isort black flake8 mypy shell clear-nb \
clean
# Usage:
# make # just alias to containers command
# make jupyter # startup Docker container running Jupyter server
# make pause # pause PSECS (to pause between commands)
# make address # get Docker container address/port
# make containers # launch all Docker containers
# make list-containers # list all running containers
# make stop-containers # simply stops all running Docker containers
# make restart-containers # restart all containers
# make lint # run linters
# make tests # 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
# make clear-nb # simply clears Jupyter notebook output
# make clean # combines all clearing commands into one
################################################################################
# GLOBALS #
################################################################################
# make cli args
DCTNR := $(notdir $(PWD))
INTDR := notebooks
PSECS := 5
# notebook-related variables
CURRENTDIR := $(PWD)
NOTEBOOKS := $(shell find ${INTDR} -name "*.ipynb" -not -path "*/.ipynb_*/*")
# docker-related variables
JPTCTNR = jupyter.${DCTNR}
SRCPATH = /usr/local/src/frame-sampling
DCKRIMG = ghcr.io/diogenesanalytics/frame-sampling:master
JPTRSRV = docker run --rm -v ${CURRENTDIR}:/home/jovyan -it ${DCKRIMG}
DCKRTST = docker run --rm -v ${CURRENTDIR}:${SRCPATH} -it ${DCKRIMG}
# jupyter nbconvert vars
NBCLER = jupyter nbconvert --clear-output --inplace
################################################################################
# COMMANDS #
################################################################################
# launch jupyter
all: containers
# launch jupyter notebook development Docker image
jupyter:
@ echo "Launching Jupyter in Docker container -> ${JPTCTNR} ..."
@ if ! docker ps --format={{.Names}} | grep -q "${JPTCTNR}"; then \
docker run -d \
--rm \
--name ${JPTCTNR} \
-e JUPYTER_ENABLE_LAB=yes \
-p 8888 \
-v "${CURRENTDIR}":/usr/local/src/frame-sampling \
${DCKROPT} \
${DCKRIMG} && \
if ! grep -sq "${JPTCTNR}" "${CURRENTDIR}/.running_containers"; then \
echo "${JPTCTNR}" >> .running_containers; \
fi \
else \
echo "Container already running. Try setting DCTNR manually."; \
fi
# simply wait for a certain amount of time
pause:
@ echo "Sleeping ${PSECS} seconds ..."
@ sleep ${PSECS}
# get containerized server address
address:
@ if [ -f "${CURRENTDIR}/.running_containers" ]; then \
while read container; do \
if echo "$${container}" | grep -q "${JPTCTNR}" ; then \
echo "Server address: $$(docker logs $${container} 2>&1 | \
grep http://127.0.0.1 | tail -n 1 | \
sed s/:8888/:$$(docker port $${container} | \
grep '0.0.0.0:' | awk '{print $$3}' | sed 's/0.0.0.0://g')/g | \
tr -d '[:blank:]')"; \
else \
echo "Could not find running container: ${JPTCTNR}." \
"Try running: make list-containers"; \
fi \
done < "${CURRENTDIR}/.running_containers"; \
else \
echo ".running_containers file not found. Is a Docker container running?"; \
fi
# launch all docker containers
containers: jupyter pause address
# list all running containers
list-containers:
@ if [ -f "${CURRENTDIR}/.running_containers" ]; then \
echo "Currently running containers:"; \
while read container; do \
echo "--> $${container}"; \
done < "${CURRENTDIR}/.running_containers"; \
else \
echo ".running_containers file not found. Is a Docker container running?"; \
fi
# stop all containers
stop-containers:
@ 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 all containers
restart-containers: stop-containers containers
# run linters
lint: isort black flake8 mypy
# run full testing suite
tests: pytest lint
# run pytest in docker container
pytest:
@ ${DCKRTST} pytest
# run isort in docker container
isort:
@ ${DCKRTST} isort src/ tests/
# run black in docker container
black:
@ ${DCKRTST} black src/ tests/
# run flake8 in docker container
flake8:
@ ${DCKRTST} flake8
# run mypy in docker container
mypy:
@ ${DCKRTST} mypy src/ tests/
# create interactive shell in docker container
shell:
@ ${DCKRTST} bash || true
# remove output from executed notebooks
clear-nb:
@ echo "Removing all output from Jupyter notebooks."
@ ${JPTRSRV} ${NBCLER} ${NOTEBOOKS}
# cleanup everything
clean: clear-nb