-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
356 lines (309 loc) · 12.5 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
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
ifneq (,)
.error This Makefile requires GNU Make.
endif
# -------------------------------------------------------------------------------------------------
# Can be changed
# -------------------------------------------------------------------------------------------------
# This can be adjusted
PYTHON_VERSION = 3.12
PYLINT_VERSION = latest-0.8
MYPY_VERSION = latest-py3.10
# -------------------------------------------------------------------------------------------------
# Default configuration
# -------------------------------------------------------------------------------------------------
.PHONY: help lint code test autoformat build clean
SHELL := /bin/bash
NAME = $(shell grep -E '^[[:space:]]*name' setup.py | awk -F'"' '{print $$2}' | sed 's/-/_/g' )
VENV = venv
BIN = pagey
SRC = pagey
IMAGE = flaconi/slackbot-pagey
TAG = latest
FL_VERSION = latest-0.8
FL_IGNORES = .git/,.github/,$(NAME).egg-info,.mypy_cache/,$(ENV)
# -------------------------------------------------------------------------------------------------
# Default Target
# -------------------------------------------------------------------------------------------------
help:
@echo "lint Lint repository files"
@echo "code Run code linters: black, mypy, pylint, pydocstyle, pycodestyle"
@echo "test Run integration tests"
@echo "autoformat Autoformat code according to Python Black standard"
@echo "build Build Python package"
@echo "clean Clean current Python package build"
# -------------------------------------------------------------------------------------------------
# Lint Targets
# -------------------------------------------------------------------------------------------------
lint: _lint-files
lint: _lint-version
lint: _lint-bin-name
lint: _lint-pkg-name
lint: _lint-description
.PHONY: _lint-files
_lint-files:
@echo "# --------------------------------------------------------------------"
@echo "# Lint files"
@echo "# -------------------------------------------------------------------- #"
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-cr --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-crlf --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-single-newline --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-space --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8 --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8-bom --text --ignore '$(FL_IGNORES)' --path .
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) git-conflicts --text --ignore '$(FL_IGNORES)' --path .
.PHONY: _lint-version
_lint-version:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check version"
@echo "# -------------------------------------------------------------------- #"
@VERSION_CODE=$$( cat $(SRC)/defaults.py | grep ^DEF_VERSION | awk -F'"' '{print $$2}' ); \
VERSION_SETUP=$$( grep version= setup.py | awk -F'"' '{print $$2}' || true ); \
if [ "$${VERSION_CODE}" != "$${VERSION_SETUP}" ]; then \
echo "[ERROR] Version mismatch"; \
echo "$(SRC)/defaults.py $${VERSION_CODE}"; \
echo "setup.py: $${VERSION_SETUP}"; \
exit 1; \
else \
echo "[OK] Version match"; \
echo "$(SRC)/defaults.py $${VERSION_CODE}"; \
echo "setup.py: $${VERSION_SETUP}"; \
exit 0; \
fi \
.PHONY: _lint-bin-name
_lint-bin-name:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check binary name"
@echo "# -------------------------------------------------------------------- #"
@NAME_CODE=$$( cat $(SRC)/defaults.py | grep ^DEF_BIN | awk -F'"' '{print $$2}' ); \
NAME_SETUP=$$( grep ':main' setup.py | awk -F'"' '{print $$2}' | awk -F'=' '{print $$1}' ); \
if [ "$${NAME_CODE}" != "$${NAME_SETUP}" ]; then \
echo "[ERROR] Name mismatch"; \
echo "$(SRC)/defaults.py $${NAME_CODE}"; \
echo "setup.py: $${NAME_SETUP}"; \
exit 1; \
else \
echo "[OK] Name match"; \
echo "$(SRC)/defaults.py $${NAME_CODE}"; \
echo "setup.py: $${NAME_SETUP}"; \
exit 0; \
fi \
.PHONY: _lint-pkg-name
_lint-pkg-name:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check package name"
@echo "# -------------------------------------------------------------------- #"
@NAME_CODE=$$( cat $(SRC)/defaults.py | grep ^DEF_NAME | awk -F'"' '{print $$2}' ); \
NAME_SETUP=$$( grep 'name=' setup.py | awk -F'"' '{print $$2}' | awk -F'=' '{print $$1}' ); \
if [ "$${NAME_CODE}" != "$${NAME_SETUP}" ]; then \
echo "[ERROR] Name mismatch"; \
echo "$(SRC)/defaults.py $${NAME_CODE}"; \
echo "setup.py: $${NAME_SETUP}"; \
exit 1; \
else \
echo "[OK] Name match"; \
echo "$(SRC)/defaults.py $${NAME_CODE}"; \
echo "setup.py: $${NAME_SETUP}"; \
exit 0; \
fi \
.PHONY: _lint-description
_lint-description:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check description"
@echo "# -------------------------------------------------------------------- #"
@DESC_CODE=$$( \
grep -A1 -E '^DEF_DESC\s*=\s*("|\()' $(SRC)/defaults.py \
| grep -E '".+"' \
| head -1 \
| awk -F'"' '{print $$2}' \
); \
DESC_SETUP=$$( grep description= setup.py | awk -F'"' '{print $$2}' || true ); \
if [ "$${DESC_CODE}" != "$${DESC_SETUP}" ]; then \
echo "[ERROR] Desc mismatch"; \
echo "$(SRC)/defaults.py $${DESC_CODE}"; \
echo "setup.py: $${DESC_SETUP}"; \
exit 1; \
else \
echo "[OK] Desc match"; \
echo "$(SRC)/defaults.py $${DESC_CODE}"; \
echo "setup.py: $${DESC_SETUP}"; \
exit 0; \
fi \
# -------------------------------------------------------------------------------------------------
# Code Style Targets
# -------------------------------------------------------------------------------------------------
code: _code-pycodestyle
code: _code-pydocstyle
code: _code-pylint
code: _code-black
code: _code-mypy
code: _code-bandit
.PHONY: _code-pycodestyle
_code-pycodestyle:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check pycodestyle"
@echo "# -------------------------------------------------------------------- #"
docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/pycodestyle --config=setup.cfg $(SRC)/
.PHONY: _code-pydocstyle
_code-pydocstyle:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check pydocstyle"
@echo "# -------------------------------------------------------------------- #"
docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/pydocstyle --explain --config=setup.cfg $(SRC)/
.PHONY: _code-pylint
_code-pylint:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check pylint"
@echo "# -------------------------------------------------------------------- #"
docker run --rm $$(tty -s && echo "-it" || echo) \
--network=host \
-v $(PWD):/data \
--entrypoint=sh \
cytopia/pylint:$(PYLINT_VERSION) -c '\
apk add py3-pip \
&& pip3 install -r requirements.txt \
&& pylint --rcfile=setup.cfg $(SRC)/'
.PHONY: _code-black
_code-black:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check Python Black"
@echo "# -------------------------------------------------------------------- #"
docker run --rm $$(tty -s && echo "-it" || echo) -v ${PWD}:/data cytopia/black -l 100 --check --diff $(SRC)/
.PHONY: _code-bandit
_code-bandit:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check Python Security with Bandit"
@echo "# -------------------------------------------------------------------- #"
docker run --rm $$(tty -s && echo "-it" || echo) -v ${PWD}:/data cytopia/bandit -r $(SRC)
.PHONY: _code-mypy
_code-mypy:
@echo "# -------------------------------------------------------------------- #"
@echo "# Check mypy"
@echo "# -------------------------------------------------------------------- #"
docker run --rm $$(tty -s && echo "-it" || echo) \
--network=host \
-v ${PWD}:/data \
--entrypoint=sh \
cytopia/mypy:$(MYPY_VERSION) -c ' \
python -m pip install -r requirements.txt \
&& python -m pip install types-python-dateutil types-requests \
&& mypy --config-file setup.cfg $(SRC)/ \
'
# -------------------------------------------------------------------------------------------------
# Test Targets
# -------------------------------------------------------------------------------------------------
.PHONY: test
test:
@echo "Check Python package"
docker run \
--rm \
--network=host \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
python:$(PYTHON_VERSION)-alpine sh -c "\
pip install -r requirements.txt \
&& ./bin/$(BIN) --help \
"
# -------------------------------------------------------------------------------------------------
# Build Targets
# -------------------------------------------------------------------------------------------------
build: clean
build: _lint-version
build: _build-source_dist
build: _build-binary_dist
build: _build-python_package
build: _build-check_python_package
.PHONY: _build_source_dist
_build-source_dist:
@echo "Create source distribution"
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
-u $$(id -u):$$(id -g) \
python:$(PYTHON_VERSION)-alpine \
python setup.py sdist
.PHONY: _build_binary_dist
_build-binary_dist:
@echo "Create binary distribution"
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
-u $$(id -u):$$(id -g) \
python:$(PYTHON_VERSION)-alpine \
python setup.py bdist_wheel --universal
.PHONY: _build_python_package
_build-python_package:
@echo "Build Python package"
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
-u $$(id -u):$$(id -g) \
python:$(PYTHON_VERSION)-alpine \
python setup.py build
.PHONY: _build_check_python_package
_build-check_python_package:
@echo "Check Python package"
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
python:$(PYTHON_VERSION)-slim \
sh -c "pip install twine \
&& twine check dist/*"
clean:
rm -rf build/
rm -rf dist/
rm -rf $(NAME).egg-info
find . -type f -name '*.pyc' -exec rm -f {} \;
find . -type d -name '__pycache__' -prune -exec rmdir {} \;
.PHONY: venv
venv:
python3 -m venv $(VENV)
@echo source $(VENV)/bin/activate
@echo python3 setup.py install
# -------------------------------------------------------------------------------------------------
# Publish Targets
# -------------------------------------------------------------------------------------------------
deploy: _build-check_python_package
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
python:$(PYTHON_VERSION)-slim \
sh -c "pip install twine \
&& twine upload dist/*"
# -------------------------------------------------------------------------------------------------
# Docker Targets
# -------------------------------------------------------------------------------------------------
docker-login:
@yes | docker login --username $(DOCKERHUB_USERNAME) --password $(DOCKERHUB_PASSWORD)
docker-build:
docker build $(NO_CACHE) \
--label "org.opencontainers.image.created"="$$(date --rfc-3339=s)" \
--label "org.opencontainers.image.revision"="$$(git rev-parse HEAD)" \
--label "org.opencontainers.image.version"="${VERSION}" \
-t $(IMAGE):$(TAG) .
docker-push:
docker push $(IMAGE):$(TAG)
docker-pull-base-image:
@grep -E '^\s*FROM' Dockerfile \
| sed -e 's/^FROM//g' -e 's/[[:space:]]*as[[:space:]]*.*$$//g' \
| xargs -n1 docker pull;
# -------------------------------------------------------------------------------------------------
# Misc Targets
# -------------------------------------------------------------------------------------------------
autoformat:
docker run \
--rm \
$$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
-w /data \
cytopia/black -l 100 $(SRC)/