Repository navigation
Expand file tree
/
Copy pathMakefile.pyproject
More file actions
268 lines (225 loc) · 9.05 KB
/
Copy pathMakefile.pyproject
File metadata and controls
268 lines (225 loc) · 9.05 KB
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
# DO NOT EDIT — managed by https://github.com/mnot/pyproject-tmpl, overwritten by `make update-pyproject`.
#
# Common Makefile for Python projects
#
#
# Insert `include Makefile.pyproject` at the bottom of your Makefile to enable these
# rules. Requires Makefile.venv to also be present.
#
# Requires $PROJECT to be set to the name of the project, and for that to be the source folder.
#
# This Makefile provides the following targets:
#
# clean_py
# Clean Python-related temporary files
# tidy_py
# Run isort and black on Python files in $PROJECT
# lint_py
# Run pylint on $PROJECT, and validate pyproject.toml
# typecheck_py
# Run mypy on $PROJECT
# bump-calver
# Bump the __version__ in $PROJECT/__init__.py using calver (https://calver.org/)
# bump-semver[-major,-minor,-micro]
# Bump the __version__ in $PROJECT/__init__.py using semver (https://semver.org/)
# version <<< COMMITS TO GIT
# Bump the version according to $VERSIONING (default: semver-micro).
# version-calver, version-semver-{major,minor,micro} <<< COMMITS TO GIT
# Explicit variants of `version` that pick the bump flavor directly
# (so `make version-<TAB>` offers them in shell completion).
# build
# Build the project
# pypi-pub
# Publish the project to pypi
# release <<< TAGS AND PUSHES TO GITHUB
# Release the project on Pypi and Github releases (requires /.github/workflows/release.yml)
# update-pyproject
# Refresh template-managed files in place from the pyproject-tmpl repo.
# update-python-targets
# Refresh requires-python in pyproject.toml and the python-version
# matrix in .github/workflows/test.yml from endoflife.date.
#
# See also Makefile.venv.
PY?=python
VERSIONING?=semver-micro
YEAR=`date +%Y`
MONTH=`date +%m`
# Python-specific targets
.PHONY: clean_py
clean_py: clean-venv
find . -d -type d -name __pycache__ -exec rm -rf {} \;
rm -rf build/ dist/ MANIFEST $(PROJECT).egg-info/ .mypy_cache/ *.log changelog.md
.PHONY: tidy_py
tidy_py: venv
$(VENV)/isort $(PROJECT)
$(VENV)/black $(PROJECT)
.PHONY: lint_py
lint_py: venv
PYTHONPATH=$(VENV) $(VENV)/pylint --output-format=colorized $(PROJECT)
$(VENV)/validate-pyproject pyproject.toml
.PHONY: typecheck_py
typecheck_py: venv
PYTHONPATH=$(VENV) $(VENV)/python -m mypy $(PROJECT)
## Release
.PHONY: py_version
py_version: venv
$(eval VERSION=$(shell $(VENV)/python -c "import $(PROJECT); print($(PROJECT).__version__)"))
$(eval VER_MAJOR=$(shell echo $(VERSION) | cut -d. -f1))
$(eval VER_MINOR=$(shell echo $(VERSION) | cut -d. -f2))
$(eval VER_MICRO=$(shell echo $(VERSION) | cut -d. -f3))
## for calendar-based versioning
$(eval NEXT_CALMICRO=$(shell \
if [[ $(YEAR) != $(VER_MAJOR) || $(MONTH) != $(VER_MINOR) ]] ; then \
echo "1"; \
else \
echo $$(( $(VER_MICRO) + 1 )); \
fi; \
))
## for semantic versioning
$(eval NEXT_SEMMAJOR=$(shell \
echo $$(( $(VER_MAJOR) + 1 )); \
))
$(eval NEXT_SEMMINOR=$(shell \
echo $$(( $(VER_MINOR) + 1 )); \
))
$(eval NEXT_SEMMICRO=$(shell \
echo $$(( $(VER_MICRO) + 1 )); \
))
.PHONY: bump-calver
bump-calver: py_version
sed -i "" -e "s/$(VERSION)/$(YEAR).$(MONTH).$(NEXT_CALMICRO)/" $(PROJECT)/__init__.py
.PHONY: bump-semver-micro
bump-semver-micro: py_version
sed -i "" -e "s/$(VERSION)/$(VER_MAJOR).$(VER_MINOR).$(NEXT_SEMMICRO)/" $(PROJECT)/__init__.py
.PHONY: bump-semver-minor
bump-semver-minor: py_version
sed -i "" -e "s/$(VERSION)/$(VER_MAJOR).$(NEXT_SEMMINOR).0/" $(PROJECT)/__init__.py
.PHONY: bump-semver-major
bump-semver-major: py_version
sed -i "" -e "s/$(VERSION)/$(NEXT_SEMMAJOR).0.0/" $(PROJECT)/__init__.py
# `version` dispatches to `version-$(VERSIONING)`; the static pattern rule
# below does the actual work. Listing the variants explicitly (rather than
# using a `version-%` implicit rule) is required for two reasons: GNU make
# won't apply implicit pattern rules to .PHONY targets, and shell
# completion needs literal names to offer after `make version-<TAB>`.
VERSION_TARGETS := version-calver version-semver-major version-semver-minor version-semver-micro
.PHONY: version $(VERSION_TARGETS)
version: version-$(VERSIONING)
$(VERSION_TARGETS): version-%: typecheck lint test bump-%
git add $(PROJECT)/__init__.py
git commit -m 'bump version'
.PHONY: build
build: venv
$(VENV)/python -m build
.PHONY: changelog.md
changelog.md:
$(eval PREV_RELEASE=$(shell git tag --sort=-taggerdate -l 'v*' | head -1))
@if [ -n "$(PREV_RELEASE)" ]; then RANGE="$(PREV_RELEASE)..HEAD"; else RANGE="HEAD"; fi; \
git --no-pager log --no-merges --pretty="- %s" $$RANGE --grep "^Added:" --grep "^Changed:" --grep "^Fixed:" --grep "^Removed:" > $@.tmp; \
rm -f $@; \
for p in Added Changed Fixed Removed; do grep "^- $$p:" $@.tmp >> $@ || true; done; \
rm -f $@.tmp
@if [ ! -s $@ ]; then echo "- Maintenance release (no changelog entries)" > $@; fi
sed -i "" -e "1s/^/\n\n/" $@
# Manual pypi publication; no GitHub release
.PHONY: pypi-pub
pypi-pub: venv build
$(VENV)/python -m twine upload dist/*
# Github release with Pypi publication; requires /.github/workflows/release.yml
.PHONY: release
release: py_version changelog.md
git tag -a "v$(VERSION)" -F changelog.md
rm -f changelog.md
git push
git push --tags origin # github action will push to pypi and create a release
## Template self-update
TEMPLATE_REPO?=mnot/pyproject-tmpl
TEMPLATE_BRANCH?=main
TEMPLATE_RAW=https://raw.githubusercontent.com/$(TEMPLATE_REPO)/$(TEMPLATE_BRANCH)
# Makefiles refreshed in stage 1 (so the recursive $(MAKE) call below picks
# up the new recipes for the rest of the update).
UPDATE_MAKEFILES = Makefile.pyproject Makefile.venv
# Other files fully overwritten in stage 2.
UPDATE_FILES = \
mypy.ini \
.pylintrc \
.isort.cfg \
.github/workflows/publish.yml \
tools/partial_update.py \
tools/update_python_targets.py
# Two-stage so that updates to Makefile.pyproject/Makefile.venv take effect
# in the same run: stage 1 refreshes the makefiles, then a recursive $(MAKE)
# re-reads them and executes the rest of the update.
.PHONY: update-pyproject
update-pyproject:
@for f in $(UPDATE_MAKEFILES); do \
echo " updating $$f"; \
curl -fsSL "$(TEMPLATE_RAW)/$$f" -o "$$f.tmp" || { echo " failed: $$f"; rm -f "$$f.tmp"; exit 1; }; \
mv "$$f.tmp" "$$f"; \
done
@$(MAKE) --no-print-directory update-pyproject-rest
.PHONY: update-pyproject-rest
update-pyproject-rest: update-files update-gitignore update-editorconfig update-test-yml update-strip
@echo "Template files updated from $(TEMPLATE_REPO)@$(TEMPLATE_BRANCH)."
.PHONY: update-files
update-files:
@for f in $(UPDATE_FILES); do \
echo " updating $$f"; \
mkdir -p "$$(dirname $$f)"; \
curl -fsSL "$(TEMPLATE_RAW)/$$f" -o "$$f.tmp" || { echo " failed: $$f"; rm -f "$$f.tmp"; exit 1; }; \
mv "$$f.tmp" "$$f"; \
done
# Refresh a template-managed file that contains named project regions.
# Each file is mostly template-controlled; explicit start/end markers wrap
# regions whose content the project owns. On update, the file is rewritten
# from the template, but the local content of each named region is spliced
# back in. Local regions without a matching slot in the new template are
# warned about and dropped. See tools/partial_update.py.
define partial_update
@echo " updating $(1)"
@mkdir -p "$$(dirname $(1))"
@curl -fsSL "$(TEMPLATE_RAW)/$(1)" -o "$(1).tmpl"
@python3 tools/partial_update.py "$(1)" "$(1).tmpl"
@rm -f "$(1).tmpl"
endef
# These shell out to tools/partial_update.py, which update-files fetches;
# order-only so `make -jN` can't run them before it lands on disk.
update-gitignore update-editorconfig update-test-yml: | update-files
.PHONY: update-gitignore
update-gitignore:
$(call partial_update,.gitignore)
.PHONY: update-editorconfig
update-editorconfig:
$(call partial_update,.editorconfig)
.PHONY: update-test-yml
update-test-yml:
$(call partial_update,.github/workflows/test.yml)
.PHONY: update-python-targets
update-python-targets: venv
@$(VENV)/python tools/update_python_targets.py
.PHONY: update-strip
update-strip:
@if grep -qE '^\[tool\.(mypy|pylint|isort)' pyproject.toml; then \
echo " stripping [tool.mypy|pylint|isort] sections from pyproject.toml (moved to dotfiles)"; \
awk 'BEGIN{skip=0} /^\[/{skip=($$0 ~ /^\[tool\.(mypy|pylint|isort)/)} !skip' pyproject.toml > pyproject.toml.new; \
mv pyproject.toml.new pyproject.toml; \
fi
.PHONY: bootstrap
bootstrap:
@if [ ! -d SHORTNAME ]; then echo "Already bootstrapped."; exit 1; fi
@read -p "Project Name (e.g., myproject): " NAME; \
read -p "Description: " DESC; \
read -p "Project URL: " URL; \
PKG=$$(echo "$$NAME" | tr '-' '_'); \
find . -type f -not -path './.git/*' -not -name 'Makefile.pyproject' -exec sed -i "" -e "s,SHORTNAME,$$PKG,g" -e "s,DESCRIPTION,$$DESC,g" -e "s,PROJECT_URL,$$URL,g" {} +; \
sed -i "" "s,^name = \"$$PKG\"\$$,name = \"$$NAME\"," pyproject.toml; \
mv SHORTNAME $$PKG; \
echo "Project initialized as $$NAME (package: $$PKG)."
include Makefile.venv
## Patch venv to install from pyproject.toml
venv: | $(VENV)/$(MARKER)-dev
$(VENV)/$(MARKER)-dev: $(VENV)/$(MARKER)
$(VENV)/pip install -e .[dev]
touch $@
## Optional per-checkout overrides (not managed by the template, git-ignored)
-include Makefile.local