| 1 |
# release.mk version 2.2.3 (2024-10-10) |
| 2 |
# |
| 3 |
# Helpful Makefile rules for releasing Python packages. |
| 4 |
# https://github.com/mgedmin/python-project-skel |
| 5 |
|
| 6 |
# You might want to change these |
| 7 |
FILE_WITH_VERSION ?= setup.py |
| 8 |
FILE_WITH_CHANGELOG ?= CHANGES.rst |
| 9 |
CHANGELOG_DATE_FORMAT ?= %Y-%m-%d |
| 10 |
CHANGELOG_FORMAT ?= $(changelog_ver) ($(changelog_date)) |
| 11 |
DISTCHECK_DIFF_OPTS ?= $(DISTCHECK_DIFF_DEFAULT_OPTS) |
| 12 |
|
| 13 |
# These should be fine |
| 14 |
PYTHON ?= python3 |
| 15 |
PYPI_PUBLISH ?= rm -rf dist && $(PYTHON) -m build && twine check dist/* && twine upload dist/* |
| 16 |
LATEST_RELEASE_MK_URL = https://raw.githubusercontent.com/mgedmin/python-project-skel/master/release.mk |
| 17 |
DISTCHECK_DIFF_DEFAULT_OPTS = -x PKG-INFO -x setup.cfg -x '*.egg-info' -x .github -I'^\#' |
| 18 |
|
| 19 |
# These should be fine, as long as you use Git |
| 20 |
VCS_GET_LATEST ?= git pull |
| 21 |
VCS_STATUS ?= git status --porcelain |
| 22 |
VCS_EXPORT ?= git archive --format=tar --prefix=tmp/tree/ HEAD | tar -xf - |
| 23 |
VCS_TAG ?= git tag -s $(changelog_ver) -m \"Release $(changelog_ver)\" |
| 24 |
VCS_COMMIT_AND_PUSH ?= git commit -av -m "Post-release version bump" && git push && git push --tags |
| 25 |
|
| 26 |
# These are internal implementation details |
| 27 |
changelog_ver = `$(PYTHON) setup.py --version` |
| 28 |
changelog_date = `LC_ALL=C date +'$(CHANGELOG_DATE_FORMAT)'` |
| 29 |
|
| 30 |
# Tweaking the look of 'make help'; most of these are awk literals and need the quotes |
| 31 |
HELP_INDENT = "" |
| 32 |
HELP_PREFIX = "make " |
| 33 |
HELP_WIDTH = 24 |
| 34 |
HELP_SEPARATOR = " \# " |
| 35 |
HELP_SECTION_SEP = "\n" |
| 36 |
|
| 37 |
.PHONY: help |
| 38 |
help: |
| 39 |
@grep -Eh -e '^[a-zA-Z0-9_ -]+:.*?##: .*$$' -e '^##:' $(MAKEFILE_LIST) \ |
| 40 |
| awk 'BEGIN {FS = "(^|:[^#]*)##: "; section=""}; \ |
| 41 |
/^##:/ {printf "%s%s\n%s", section, $$2, $(HELP_SECTION_SEP); section=$(HELP_SECTION_SEP)} \ |
| 42 |
/^[^#]/ {printf "%s\033[36m%-$(HELP_WIDTH)s\033[0m%s%s\n", \ |
| 43 |
$(HELP_INDENT), $(HELP_PREFIX) $$1, $(HELP_SEPARATOR), $$2}' |
| 44 |
|
| 45 |
.PHONY: dist |
| 46 |
dist: |
| 47 |
$(PYTHON) -m build |
| 48 |
|
| 49 |
# Provide a default 'make check' to be the same as 'make test', since that's |
| 50 |
# what 80% of my projects use, but make it possible to override. Now |
| 51 |
# overriding Make rules is painful, so instead of a regular rule definition |
| 52 |
# you'll have to override the check_recipe macro. |
| 53 |
.PHONY: check |
| 54 |
check: |
| 55 |
$(check_recipe) |
| 56 |
|
| 57 |
ifndef check_recipe |
| 58 |
define check_recipe = |
| 59 |
@$(MAKE) test |
| 60 |
endef |
| 61 |
endif |
| 62 |
|
| 63 |
.PHONY: distcheck |
| 64 |
distcheck: distcheck-vcs distcheck-sdist |
| 65 |
|
| 66 |
.PHONY: distcheck-vcs |
| 67 |
distcheck-vcs: |
| 68 |
ifndef FORCE |
| 69 |
# Bit of a chicken-and-egg here, but if the tree is unclean, make |
| 70 |
# distcheck-sdist will fail. |
| 71 |
@test -z "`$(VCS_STATUS) 2>&1`" || { echo; echo "Your working tree is not clean:" 1>&2; $(VCS_STATUS) 1>&2; exit 1; } |
| 72 |
endif |
| 73 |
|
| 74 |
# NB: do not use $(MAKE) in rules with multiple shell commands joined by && |
| 75 |
# because then make -n distcheck will actually run those instead of just |
| 76 |
# printing what it does |
| 77 |
|
| 78 |
# TBH this could (and probably should) be replaced by check-manifest |
| 79 |
|
| 80 |
.PHONY: distcheck-sdist |
| 81 |
distcheck-sdist: dist |
| 82 |
pkg_and_version=`$(PYTHON) setup.py --name|tr A-Z.- a-z__`-`$(PYTHON) setup.py --version` && \ |
| 83 |
rm -rf tmp && \ |
| 84 |
mkdir tmp && \ |
| 85 |
$(VCS_EXPORT) && \ |
| 86 |
cd tmp && \ |
| 87 |
tar -xzf ../dist/$$pkg_and_version.tar.gz && \ |
| 88 |
diff -ur $$pkg_and_version tree $(DISTCHECK_DIFF_OPTS) && \ |
| 89 |
cd $$pkg_and_version && \ |
| 90 |
make dist check && \ |
| 91 |
cd .. && \ |
| 92 |
mkdir one two && \ |
| 93 |
cd one && \ |
| 94 |
tar -xzf ../../dist/$$pkg_and_version.tar.gz && \ |
| 95 |
cd ../two/ && \ |
| 96 |
tar -xzf ../$$pkg_and_version/dist/$$pkg_and_version.tar.gz && \ |
| 97 |
cd .. && \ |
| 98 |
diff -ur one two -x SOURCES.txt -I'^#:' && \ |
| 99 |
cd .. && \ |
| 100 |
rm -rf tmp && \ |
| 101 |
echo "sdist seems to be ok" |
| 102 |
|
| 103 |
.PHONY: check-latest-rules |
| 104 |
check-latest-rules: |
| 105 |
ifndef FORCE |
| 106 |
@curl -s $(LATEST_RELEASE_MK_URL) | cmp -s release.mk || { printf "\nYour release.mk does not match the latest version at\n$(LATEST_RELEASE_MK_URL)\n\n" 1>&2; exit 1; } |
| 107 |
endif |
| 108 |
|
| 109 |
.PHONY: check-latest-version |
| 110 |
check-latest-version: |
| 111 |
$(VCS_GET_LATEST) |
| 112 |
|
| 113 |
.PHONY: check-version-number |
| 114 |
check-version-number: |
| 115 |
@$(PYTHON) setup.py --version | grep -qv dev || { \ |
| 116 |
echo "Please remove the 'dev' suffix from the version number in $(FILE_WITH_VERSION)"; exit 1; } |
| 117 |
|
| 118 |
.PHONY: check-long-description |
| 119 |
check-long-description: |
| 120 |
@$(PYTHON) setup.py --long-description | rst2html --exit-status=2 > /dev/null |
| 121 |
|
| 122 |
.PHONY: check-changelog |
| 123 |
check-changelog: |
| 124 |
@ver_and_date="$(CHANGELOG_FORMAT)" && \ |
| 125 |
grep -q "^$$ver_and_date$$" $(FILE_WITH_CHANGELOG) || { \ |
| 126 |
echo "$(FILE_WITH_CHANGELOG) has no entry for $$ver_and_date"; exit 1; } |
| 127 |
|
| 128 |
|
| 129 |
# NB: the Makefile that includes release.mk may want to add additional |
| 130 |
# dependencies to the releasechecklist target, but I want 'make distcheck' to |
| 131 |
# happen last, so that's why I put it into the recipe and not at the end of the |
| 132 |
# list of dependencies. |
| 133 |
|
| 134 |
.PHONY: releasechecklist |
| 135 |
releasechecklist: check-latest-rules check-latest-version check-version-number check-long-description check-changelog |
| 136 |
$(MAKE) distcheck |
| 137 |
|
| 138 |
.PHONY: release |
| 139 |
release: releasechecklist do-release ##: prepare a new PyPI release |
| 140 |
|
| 141 |
.PHONY: do-release |
| 142 |
do-release: |
| 143 |
$(release_recipe) |
| 144 |
|
| 145 |
define default_release_recipe_publish_and_tag = |
| 146 |
# I'm chicken so I won't actually do these things yet |
| 147 |
@echo "Please run" |
| 148 |
@echo |
| 149 |
@echo " $(PYPI_PUBLISH)" |
| 150 |
@echo " $(VCS_TAG)" |
| 151 |
@echo |
| 152 |
endef |
| 153 |
define default_release_recipe_increment_and_push = |
| 154 |
@echo "Please increment the version number in $(FILE_WITH_VERSION)" |
| 155 |
@echo "and add a new empty entry at the top of the changelog in $(FILE_WITH_CHANGELOG), then" |
| 156 |
@echo |
| 157 |
@echo ' $(VCS_COMMIT_AND_PUSH)' |
| 158 |
@echo |
| 159 |
endef |
| 160 |
ifndef release_recipe |
| 161 |
define release_recipe = |
| 162 |
$(default_release_recipe_publish_and_tag) |
| 163 |
$(default_release_recipe_increment_and_push) |
| 164 |
endef |
| 165 |
endif |