-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMakefile
285 lines (254 loc) · 7.56 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
# $Id$
me:= $(firstword $(MAKEFILE_LIST))
# Prints usage message and exits.
perl_usage=\
BEGIN {\
$$/ = "";\
print "Usage: ${MAKE} -f ${me} TARGET";\
print "Maintainer\047s makefile; the following targets are supported:";\
print "";\
}\
/\#\s([^\n]+)\n(\.PHONY:|SC_RULES\+=)\s([\w-]+)\n/ and do {\
my $$tgt = $$3;\
my $$doc = $$1;\
printf (" %-20s %s\n", $$tgt, $$doc);\
};\
END { print ""; }\
${NULL}
.PHONY: usage
usage:
@perl -wnle '${perl_usage}' ${MAKEFILE_LIST}
CHECK_DEPS?= htmlcov-clean
CHECK_FLAKE8=? yes
CHECK_ISORT?= yes
CHECK_MYPY?= yes
COVERAGERC?= .coveragerc
COVERAGERC_EXCLUDE_LINES?=
COVERAGERC_OMIT?=
DOCS_SRC?= docs
DOCS_TGT?= .docs
DOCS_TGT_BRANCH?= gh-pages
FLAKE8RC?= .flake8rc
FLAKE8_OPTIONS?= --color never --config .flake8rc
ISORT_CFG?= .isort.cfg
ISORT_CFG_INCLUDE_TRAILING_COMMA?= True
ISORT_CFG_MULTI_LINE_OUTPUT?= 3
ISORT_CFG_ORDER_BY_TYPE?= False
ISORT_OPTIONS?= --check --diff
MANIFEST_IN?= Manifest.in
MANIFEST_IN_GIT_LS_FILES_PATHSPEC?=
MYPY_OPTIONS?= --show-error-context --show-error-codes
PERL?= perl
PYTEST_INI?= pytest.ini
PYTEST_OPTIONS?= -x
PYTHON?= python
TESTS?= tests
TOX?= tox
TOX_ENVLIST?= mypy, py{39,310,311,312}
TOX_INI?= tox.ini
TOX_OPTIONS?=
TOX_PASSENV?=
TOX_SETENV?=
include Makefile.conf
CHECK_DEPS+= $(if $(filter yes,${CHECK_FLAKE8}),check-flake8)
CHECK_DEPS+= $(if $(filter yes,${CHECK_ISORT}),check-isort)
CHECK_DEPS+= $(if $(filter yes,${CHECK_MYPY}),check-mypy)
PYTEST_OPTIONS+= $(if $(filter yes,${CHECK_MYPY}),--mypy)
split = $(shell printf '$(1)'\
| sed -e 's/\s*<line>/ /g' -e 's,</line>,\\n,g')
# run testsuite
.PHONY: check
check: ${CHECK_DEPS}
pytest ${PYTEST_OPTIONS}
# check sources using flake8
.PHONY: check-flake8
check-flake8:
${PYTHON} -m flake8 ${FLAKE8_OPTIONS} ${PACKAGE} ${TESTS}
# check sources using isort
.PHONY: check-isort
check-isort:
${PYTHON} -m isort ${ISORT_OPTIONS} ${PACKAGE} ${TESTS}
# check sources using mypy
.PHONY: check-mypy
check-mypy:
${PYTHON} -m mypy ${MYPY_OPTIONS} ${PACKAGE} ${TESTS}
# check copyright
.PHONY: check-copyright
check-copyright: check-copyright-python
.PHONY: check-copyright-python
check-copyright-python:
@${CHECK_COPYRIGHT} `git ls-files '${PACKAGE}/*.py' '${TESTS}/*.py'`
CHECK_COPYRIGHT= ${PERL} -s -0777 -wnle '${perl_check_copyright}'
perl_check_copyright:=\
BEGIN {\
$$p = "\# " if !defined $$p;\
$$q = $$p if !defined $$q;\
$$h = "${COPYRIGHT}" if !defined $$h;\
$$l = "SPDX-License-Identifier: ${LICENSE}" if !defined $$l;\
$$regex = qr(\Q$$p\ECopyright \(C\) (20\d\d)(-(20\d\d))? \Q$$h\E\n\Q$$q$$l\E);\
sub guess_year {\
my $$date =\
`git log --diff-filter=$$_[0] --follow --format=%as -1 -- $$ARGV`\
|| `date -I`;\
$$date =~ /^(\d\d\d\d)/s;\
return $$1;\
}\
$$errcnt = 0;\
sub put_error {\
warn("error:$$ARGV: $$_[0]\n");\
$$errcnt++;\
}\
}\
if (/$$regex/m) {\
my $$start = $$1;\
my $$end = $$3 if defined $$3;\
my $$exp_start = guess_year("A");\
if ($$start ne $$exp_start) {\
put_error("bad start year: expected $$exp_start, got $$start");\
} else {\
my $$exp_end = guess_year("M");\
if ($$exp_end eq $$exp_start) {\
if (defined $$end) {\
put_error("bad end year: expected none, got $$end");\
}\
} else {\
if (!defined $$end) {\
put_error("bad end year: expected $$exp_end, got none");\
}\
}\
}\
} else {\
put_error("bad or missing license tag");\
}\
END { exit($$errcnt); }\
${NULL}
# remove generated files
.PHONY: clean
clean: docs-clean htmlcov-clean
-${PYTHON} setup.py clean --all
.PHONY: htmlcov-clean
htmlcov-clean:
-rm -rf ./htmlcov
# build docs
.PHONY: docs
docs:
${MAKE} -C ./${DOCS_SRC} html\
PACKAGE='${PACKAGE}'\
COPYRIGHT='${COPYRIGHT}'\
COPYRIGHT_START_YEAR='${COPYRIGHT_START_YEAR}'
@echo 'Index: file://${PWD}/${DOCS_SRC}/_build/html/index.html'
.PHONY: docs-clean
docs-clean:
${MAKE} -C ./${DOCS_SRC} clean
-rm -rf ./${DOCS_SRC}/generated
# initialize docs branch
.PHONY: docs-init
docs-init:
if ! test -d ${DOCS_TGT}; then\
mkdir -p ${DOCS_TGT};\
cd ${DOCS_TGT};\
git init;\
git checkout --orphan ${DOCS_TGT_BRANCH};\
git remote add origin ${URL_SSH};\
fi
-cd ${DOCS_TGT} && git pull origin ${DOCS_TGT_BRANCH}
# build docs and copy them to ${DOCS_TGT}
.PHONY: docs-publish
docs-publish: docs-clean docs
@if ! test -d ./${DOCS_TGT}; then\
echo 1>&2 "*** ERROR: ${DOCS_TGT} does not exist";\
exit 1;\
fi
touch ./${DOCS_TGT}/.nojekyll
cp -a ./${DOCS_SRC}/_build/html/* ./${DOCS_TGT}
cd ${DOCS_TGT} && git add .
cd ${DOCS_TGT} && git commit -m 'Update docs'
cd ${DOCS_TGT} && git push origin ${DOCS_TGT_BRANCH}
# run all gen-* targets
.PHONY: gen-all
gen-all: gen-coveragerc gen-isort-cfg gen-flake8rc gen-manifest-in\
gen-pytest-ini gen-tox-ini
# generate .coveragerc
.PHONY: gen-coveragerc
gen-coveragerc:
@echo 'generating ${COVERAGERC}'
@echo '[report]' >${COVERAGERC}
@echo 'omit = ${COVERAGERC_OMIT}' >>${COVERAGERC}
@echo 'exclude_lines =' >>${COVERAGERC}
@echo ' pragma: no cover' >>${COVERAGERC}
@echo ' def __repr__' >>${COVERAGERC}
@echo ' def __str__' >>${COVERAGERC}
@echo ' raise AssertionError' >>${COVERAGERC}
@echo ' raise NotImplementedError' >>${COVERAGERC}
@echo ' if 0:' >>${COVERAGERC}
@echo ' if __name__ == .__main__.:' >>${COVERAGERC}
@echo ' class .*\\bProtocol\):' >>${COVERAGERC}
@echo ' @(abc\.)?abstractmethod' >>${COVERAGERC}
@echo ' should_not_get_here' >>${COVERAGERC}
@echo ' ShouldNotGetHere' >>${COVERAGERC}
@echo "$(call split,${COVERAGERC_EXCLUDE_LINES})" >>${COVERAGERC}
# generate .flake8rc
.PHONY: gen-flake8rc
gen-flake8rc:
@echo 'generating ${FLAKE8RC}'
@echo '[flake8]' >${FLAKE8RC}
@echo 'ignore = E741,F403,F405,W503' >>${FLAKE8RC}
# generage .isort.cfg
.PHONY: gen-isort-cfg
gen-isort-cfg:
@echo 'generating ${ISORT_CFG}'
@echo '[settings]' >${ISORT_CFG}
@echo 'include_trailing_comma = ${ISORT_CFG_INCLUDE_TRAILING_COMMA}' >>${ISORT_CFG}
@echo 'multi_line_output = ${ISORT_CFG_MULTI_LINE_OUTPUT}' >>${ISORT_CFG}
@echo 'order_by_type = ${ISORT_CFG_ORDER_BY_TYPE}' >>${ISORT_CFG}
# generate Manifest.in
.PHONY: gen-manifest-in
gen-manifest-in:
@echo 'generating ${MANIFEST_IN}'
@git ls-files ${MANIFEST_IN_GIT_LS_FILES_PATHSPEC} |\
sed 's,\(.*\),include \1,' >${MANIFEST_IN}
# generate pytest.ini
.PHONY: gen-pytest-ini
gen-pytest-ini:
@echo 'generating ${PYTEST_INI}'
@echo '[pytest]' >${PYTEST_INI}
@echo 'addopts = -ra --cov=${PACKAGE} --cov-report=html' >>${PYTEST_INI}
@echo 'testpaths = ${TESTS}' >>${PYTEST_INI}
# generate tox.ini
.PHONY: gen-tox-ini
gen-tox-ini:
@echo 'generating ${TOX_INI}'
@echo '[tox]' >${TOX_INI}
@echo 'envlist = ${TOX_ENVLIST}' >>${TOX_INI}
@echo 'skip_missing_interpreters = true' >>${TOX_INI}
@echo '' >>${TOX_INI}
@echo '[testenv]' >>${TOX_INI}
@echo 'commands = py.test {posargs}' >>${TOX_INI}
@echo 'extras = tests' >>${TOX_INI}
@echo 'passenv =' >>${TOX_INI}
@echo "$(call split,${TOX_PASSENV})" >> ${TOX_INI}
@echo '[testenv:mypy]' >>${TOX_INI}
@echo 'commands = mypy -p ${PACKAGE}' >>${TOX_INI}
# refresh idents
.PHONY: ident
ident:
for f in `grep ident .gitattributes | sed 's/\s*ident$$//'`; do\
touch $$f; git checkout $$f; echo "`head -n1 $$f` $$f";\
done
# install package
.PHONY: install
install:
pip install -e .
# install doc-build and test dependencies
.PHONY: install-deps
install-deps:
pip install -e '.[docs]'
pip install -e '.[tests]'
# run tox
.PHONY: tox
tox:
${TOX_SETENV} ${TOX} ${TOX_OPTIONS}
# uninstall package
.PHONY: uninstall
uninstall:
${PYTHON} setup.py develop -u