This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMakefile
368 lines (294 loc) · 7.94 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
357
358
359
360
361
362
363
364
365
366
367
368
SHELL = /bin/bash
DOCKER_SHELL = /bin/bash
#############################################
### Makefile Definitions
#############################################
### Error codes
ERROR_PYTHON_VERSION = 10
### Main defs
PACKAGE_NAME = hmrb
PACKAGE_VERSION = 1.2.1
srcdir = $(CURDIR)/$(PACKAGE_NAME)
builddir = $(CURDIR)/build
distdir = $(CURDIR)/dist
docdir = $(CURDIR)/docs
autodocdir = $(docdir)/autodoc
### Tools
tools =
ifeq ($(shell uname -s),Darwin)
SED = gsed
else
SED = sed
endif
ifeq ($(shell which ${SED}),)
tools += $(SED)
endif
GREPTOOL = rg
ifeq ($(shell which ${GREPTOOL}),)
GREPTOOL = egrep
endif
AWK = awk
ifeq ($(shell which ${AWK}),)
tools += $(AWK)
endif
### Python
PYTHON = python3
PYTHON_BIN = $(shell which ${PYTHON})
ifeq ($(PYTHON_BIN),)
tools += $(PYTHON)
endif
PIP = $(PYTHON) -m pip
PYTHON3_VER = 3.8.6
PYTHON_VERSION_INFO = $(shell ${PYTHON} -c 'if 1: \
from sys import version_info; \
print("{v.major}.{v.minor}.{v.micro}".format(v=version_info)) \
')
PYTHON_INFO = $(PYTHON_BIN) -> $(PYTHON_VERSION_INFO)
VIRTUALENV = virtualenv
VIRTUALENV_BIN = $(shell which ${VIRTUALENV})
ifeq ($(VIRTUALENV_BIN),)
tools += $(VIRTUALENV)
endif
# use direnv structure: layout pyenv [version]
VENV_DIR = .direnv/python-${PYTHON3_VER}
PYTHON_STD_PKGS = \
pip \
setuptools \
wheel
### Docker
DOCKER = docker
ifeq ($(shell which ${DOCKER}),)
tools += $(DOCKER)
endif
DOCKER_PS_ARGS = -s
DOCKER_BASE_IMAGE = python:$(PYTHON3_VER)-slim-buster
DOCKER_PORT = 8088
#############################################
### All
#############################################
all: help
ifdef tools
$(error Can't find tools:${tools})
endif
@if [[ "$(PYTHON_VERSION_INFO)" == "$(PYTHON3_VER)" ]]; then \
echo "INFO: Found Python interpreter: $(PYTHON_INFO)"; \
else \
echo -n "WARNING: Incompatible Python interpreter version: $(PYTHON_INFO) "; \
echo "(instead of $(PYTHON3_VER))"; \
echo "INFO: Try \`make python-install\`"; \
exit $(ERROR_PYTHON_VERSION); \
fi
#############################################
### Initializing repo
#############################################
.PHONY: init
init:
git init
git add -f *
git add .gitignore
git add .flake8
git commit -m "build: cookiecutter-python"
#############################################
### Updating requirements
#############################################
.PHONY: requirements
# target: requirements - Compile Pip requirements
requirements:
@echo
@if (! docker stats --no-stream > /dev/null); then \
pip install nox; \
nox -rs requirements; \
else \
$(DOCKER) run -it --rm \
-v "$(CURDIR)/requirements.in:/requirements.in" \
-v "$(CURDIR)/requirements.txt:/requirements.txt" \
"$(DOCKER_BASE_IMAGE)" "$(DOCKER_SHELL)" -c \
'pip install -U pip pip-tools && \
CUSTOM_COMPILE_COMMAND="make requirements" \
pip-compile -o requirements.tmp requirements.in && \
cat requirements.tmp > requirements.txt'; \
fi
#############################################
### Installing requirements and package
#############################################
.PHONY: install
# target: install - Install project sources in "development mode"
install: requirements.txt requirements-test.txt
@echo
@if ! [[ -d "$(VENV_DIR)" ]]; then \
$(VIRTUALENV) -p $(PYTHON3_VER) "$(VENV_DIR)"; \
fi
@if [[ -d "$(VENV_DIR)" ]]; then \
\
source "$(VENV_DIR)/bin/activate"; \
\
$(PIP) install -U $(PYTHON_STD_PKGS); \
$(PIP) install -Ur $<; \
$(PIP) install -Ur $(word 2,$^); \
\
echo ""; \
$(PIP) list; \
echo ""; \
\
deactivate; \
fi
@echo
@$(PYTHON) setup.py develop
.PHONY: uninstall
# target: uninstall - Uninstall project sources
uninstall:
@echo
@$(PYTHON) setup.py develop --uninstall
#############################################
### Running
#############################################
run:
@echo
@$(PYTHON) $(PACKAGE_NAME)/__main__.py
#############################################
# Linting
#############################################
black:
@echo
@nox -rs black
lint:
@echo
@nox -rs lint
safety:
@echo
@nox -rs safety
type:
@echo
@nox -rs types
#############################################
### Testing
#############################################
.PHONY: tests
# target: tests - Run tests
tests:
@echo
@nox -rs tests
##
# Building and packaging
.PHONY: changelog
changelog:
@echo
@nox -rs changelog
#############################################
### Publishing
#############################################
.PHONY: sdist
# target: sdist - Create a source distribution
sdist:
@echo
@[[ ! -f "$(distdir)"/*.tar.gz ]] && $(PYTHON) setup.py sdist
.PHONY: dist
# target: dist - Create a binary (wheel) distribution
dist:
@echo
@[[ ! -f "$(distdir)"/*.whl ]] && $(PYTHON) setup.py bdist_wheel
# publish to testpypi
.PHONY: publish
publish:
@echo
@nox -rs publish
# publish to pypi
.PHONY: publish-confirm
publish-confirm:
@echo
@nox -rs publish -- -r pypi
#############################################
### Docker
#############################################
.PHONY: docker-build
# target: docker-build - Build image from scratch
docker-build: dist
@echo
@$(DOCKER) build -f "$(CURDIR)/Dockerfile" -t $(PACKAGE_NAME):$(PACKAGE_VERSION) \
--no-cache .
.PHONY: docker-run
# target: docker-run - Run temporary container in an interactive mode
docker-run:
@echo
@$(DOCKER) run -it --rm -p $(DOCKER_PORT):$(DOCKER_PORT) \
$(PACKAGE_NAME):$(PACKAGE_VERSION)
.PHONY: docker-clean
# target: docker-clean - Remove all unused images, built containers and volumes
docker-clean:
@echo
@$(DOCKER) ps -aq | xargs $(DOCKER) rm -fv
@$(DOCKER) system prune -af --volumes
#############################################
### Documentation
#############################################
SPHINX_BUILDDIR = $(docdir)/_build
AUTODOC_EXCLUDE_MODULES =
SPHINX_OPTS = -d "$(SPHINX_BUILDDIR)/doctrees" "$(docdir)"
.PHONY: docs
docs:
@echo
@nox -rs docs -- sphinx-build -b html $(SPHINX_OPTS) "$(SPHINX_BUILDDIR)/html"
.PHONY: apidoc
# target: apidoc - Create one reST file with automodule directives per package
apidoc: docs
@echo
@nox -rs docs -- sphinx-apidoc --force --private -o "$(autodocdir)" $(PACKAGE_NAME) \
$(foreach module,$(AUTODOC_EXCLUDE_MODULES),$(PACKAGE_NAME)/$(module))
.PHONY: html
# target: html - Render standalone HTML files
html:
@echo
@nox -rs docs -- sphinx-autobuild $(SPHINX_OPTS) "$(SPHINX_BUILDDIR)/html"
#############################################
### Auxiliary targets
#############################################
.PHONY: help
# target: help - Display all callable targets
help:
@echo
@$(GREPTOOL) "^\s*#\s*target\s*:\s*" [Mm]akefile \
| $(SED) -r "s/^\s*#\s*target\s*:\s*//g"
#############################################
### Cleaners
#############################################
.PHONY: clean
# target: clean - Clean the project's directory
clean:
@find "$(CURDIR)" -path "$(CURDIR)/$(VENV_DIR)" -prune -o \
\
-name ".cache" -type d -exec rm -rfv {} + -o \
-name ".mypy_cache" -type d -exec rm -rf {} + -o \
-name ".pytest_cache" -type d -exec rm -rf {} + -o \
-name "__pycache__" -type d -exec rm -rf {} + -o \
\
-name "*.py[cod]" -exec rm -f {} +
@find hmrb -name "*.c" -delete
@find hmrb -name "*.so" -delete
.PHONY: distclean
# target: distclean - Clean the project's build output
distclean:
@find "$(CURDIR)" -path "$(CURDIR)/$(VENV_DIR)" -prune -o \
\
-name ".eggs" -type d -exec rm -rf {} + -o \
-name "*.dist-info" -type d -exec rm -rf {} + -o \
-name "*.egg-info" -type d -exec rm -rf {} +
@rm -rf \
"$(builddir)" \
"$(distdir)"
@find "$(CURDIR)" -name "$(autodocdir)/*.rst" -exec rm -f {} +
@rm -rf \
"$(SPHINX_BUILDDIR)" \
"$(SPHINX_STATIC)" \
"$(SPHINX_TEMPLATES)"
.PHONY: mostlyclean
# target: mostlyclean - Delete almost everything
mostlyclean: clean distclean
@find "$(CURDIR)" -name .DS_Store -exec rm -fv {} +
@rm -rf "$(VENV_DIR)"
build_protoc:
protoc -I=hmrb --python_out=hmrb hmrb/response.proto
install-fast-re:
git clone https://github.com/google/re2
cd re2; make
cd re2; sudo make install
pip3 install fb-re2==1.0.7