Skip to content

Commit d1ce4f8

Browse files
committed
refactor: add Makefile variables for both source and binary distributions, and simplify rules a bit using these variables
1 parent f3757ab commit d1ce4f8

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

Makefile

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ SHELL := bash
77
PACKAGE_NAME := package
88
PACKAGE_VERSION := $(shell python -c $$'try: import $(PACKAGE_NAME); print($(PACKAGE_NAME).__version__);\nexcept: print("unknown");')
99

10+
# Put together the file names for both the source distribution package (sdist)
11+
# and the binary distribution package (wheel). For more details on these two,
12+
# see: https://packaging.python.org/en/latest/discussions/package-formats/
13+
#
14+
# For the wheel, users may require additional code to generate the Python tag
15+
# (py3), the ABI tag (none), and the platform tag (any) for the final wheel
16+
# file name.
17+
PACKAGE_BASE_NAME := $(PACKAGE_NAME)-$(PACKAGE_VERSION)
18+
PACKAGE_SDIST_NAME := $(PACKAGE_BASE_NAME).tar.gz
19+
PACKAGE_WHEEL_NAME := $(PACKAGE_BASE_NAME)-py3-none-any.whl
20+
1021
# If a PYTHON environment variable exists then use that, else define it here.
1122
PYTHON ?= python3.13
1223

@@ -107,7 +118,7 @@ upgrade-quiet:
107118
# Generate a Software Bill of Materials (SBOM).
108119
.PHONY: sbom
109120
sbom: requirements
110-
python -m cyclonedx_py requirements --output-reproducible --output-format json --output-file dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-sbom.json
121+
python -m cyclonedx_py requirements --output-reproducible --output-format json --output-file dist/$(PACKAGE_BASE_NAME)-sbom.json
111122

112123
# Generate a requirements.txt file containing version and integrity hashes for all
113124
# packages currently installed in the virtual environment. There's no easy way to
@@ -129,14 +140,14 @@ requirements.txt: pyproject.toml
129140
[[ $$pkg =~ (.*)==(.*) ]] && curl -s https://pypi.org/pypi/$${BASH_REMATCH[1]}/$${BASH_REMATCH[2]}/json | python -c "import json, sys; print(''.join(f''' \\\\\n --hash=sha256:{pkg['digests']['sha256']}''' for pkg in json.load(sys.stdin)['urls']));" >> requirements.txt; \
130141
done
131142
echo -e -n "$(PACKAGE_NAME)==$(PACKAGE_VERSION)" >> requirements.txt
132-
if [ -f dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION).tar.gz ]; then \
133-
echo -e -n " \\\\\n $$(python -m pip hash --algorithm sha256 dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION).tar.gz | grep '^\-\-hash')" >> requirements.txt; \
143+
if [ -f dist/$(PACKAGE_SDIST_NAME) ]; then \
144+
echo -e -n " \\\\\n $$(python -m pip hash --algorithm sha256 dist/$(PACKAGE_SDIST_NAME) | grep '^\-\-hash')" >> requirements.txt; \
134145
fi
135-
if [ -f dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any.whl ]; then \
136-
echo -e -n " \\\\\n $$(python -m pip hash --algorithm sha256 dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any.whl | grep '^\-\-hash')" >> requirements.txt; \
146+
if [ -f dist/$(PACKAGE_WHEEL_NAME) ]; then \
147+
echo -e -n " \\\\\n $$(python -m pip hash --algorithm sha256 dist/$(PACKAGE_WHEEL_NAME) | grep '^\-\-hash')" >> requirements.txt; \
137148
fi
138149
echo "" >> requirements.txt
139-
cp requirements.txt dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-requirements.txt
150+
cp requirements.txt dist/$(PACKAGE_BASE_NAME)-requirements.txt
140151

141152
# Audit the currently installed packages. Skip packages that are installed in
142153
# editable mode (like the one in development here) because they may not have
@@ -182,30 +193,30 @@ test-all: test-unit test-integration test-performance
182193
# When building these artifacts, we need the environment variable SOURCE_DATE_EPOCH
183194
# set to the build date/epoch. For more details, see: https://flit.pypa.io/en/latest/reproducible.html
184195
.PHONY: dist
185-
dist: dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any.whl dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION).tar.gz dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-docs-html.zip dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-docs-md.zip
186-
dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any.whl: check test-all
196+
dist: dist/$(PACKAGE_WHEEL_NAME) dist/$(PACKAGE_SDIST_NAME) dist/$(PACKAGE_BASE_NAME)-docs-html.zip dist/$(PACKAGE_BASE_NAME)-docs-md.zip
197+
dist/$(PACKAGE_WHEEL_NAME): check test-all
187198
SOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH) python -m flit build --setup-py --format wheel
188-
echo $(SOURCE_DATE_EPOCH) > dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any-build-epoch.txt
189-
dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION).tar.gz: check test-all
199+
echo $(SOURCE_DATE_EPOCH) > dist/$(PACKAGE_BASE_NAME)-py3-none-any-build-epoch.txt
200+
dist/$(PACKAGE_SDIST_NAME): check test-all
190201
SOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH) python -m flit build --no-setup-py --format sdist
191-
dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-docs-html.zip: docs-html
192-
python -m zipfile -c dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-docs-html.zip docs/_build/html/
193-
dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-docs-md.zip: docs-md
194-
python -m zipfile -c dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-docs-md.zip docs/_build/markdown/
202+
dist/$(PACKAGE_BASE_NAME)-docs-html.zip: docs-html
203+
python -m zipfile -c dist/$(PACKAGE_BASE_NAME)-docs-html.zip docs/_build/html/
204+
dist/$(PACKAGE_BASE_NAME)-docs-md.zip: docs-md
205+
python -m zipfile -c dist/$(PACKAGE_BASE_NAME)-docs-md.zip docs/_build/markdown/
195206

196207
# Build a PEP-503 compatible Simple Repository directory inside of dist/. For details on
197208
# the layout of that directory and the normalized project name, see: https://peps.python.org/pep-0503/
198209
# The directory can then be used to install (hashed) artifacts by using `pip` and
199210
# its `--extra-index-url` argument: https://pip.pypa.io/en/stable/cli/pip_install/#cmdoption-extra-index-url
200211
PROJECT_NAME := $(shell python -c $$'import re; print(re.sub(r"[-_.]+", "-", "$(PACKAGE_NAME)").lower());')
201212
.PHONY: simple-index
202-
simple-index: dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any.whl dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION).tar.gz
213+
simple-index: dist/$(PACKAGE_WHEEL_NAME) dist/$(PACKAGE_SDIST_NAME)
203214
mkdir -p dist/simple-index/$(PROJECT_NAME)
204215
echo -e "<!-- https://peps.python.org/pep-0503/ -->\n<!DOCTYPE html><html lang='en'><head><meta name='pypi:repository-version' content='1.3'><title>Simple Index</title></head><body><a href='/$(PACKAGE_NAME)/'>$(PACKAGE_NAME)</a></body></html>" > dist/simple-index/index.html
205-
echo -e "<!-- https://peps.python.org/pep-0503/ -->\n<!DOCTYPE html><html lang='en'><head><meta name='pypi:repository-version' content='1.3'><title>Simple Index: $(PROJECT_NAME)</title></head><body><a href='$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any.whl#sha256="$$(python -c "with open('dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any.whl', 'rb') as f: import hashlib; print(hashlib.sha256(f.read()).hexdigest());")"'>$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any.whl</a><a href='$(PACKAGE_NAME)-$(PACKAGE_VERSION).tar.gz#sha256="$$(python -c "with open('dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION).tar.gz', 'rb') as f: import hashlib; print(hashlib.sha256(f.read()).hexdigest());")"'>$(PACKAGE_NAME)-$(PACKAGE_VERSION).tar.gz</a></body></html>" > dist/simple-index/$(PROJECT_NAME)/index.html
206-
cp -f dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-py3-none-any.whl dist/simple-index/$(PROJECT_NAME)/
207-
cp -f dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION).tar.gz dist/simple-index/$(PROJECT_NAME)/
208-
python -m tarfile --create dist/$(PACKAGE_NAME)-$(PACKAGE_VERSION)-pep503-simple-index.tar dist/simple-index/
216+
echo -e "<!-- https://peps.python.org/pep-0503/ -->\n<!DOCTYPE html><html lang='en'><head><meta name='pypi:repository-version' content='1.3'><title>Simple Index: $(PROJECT_NAME)</title></head><body><a href='$(PACKAGE_WHEEL_NAME)#sha256="$$(python -c "with open('dist/$(PACKAGE_WHEEL_NAME)', 'rb') as f: import hashlib; print(hashlib.sha256(f.read()).hexdigest());")"'>$(PACKAGE_WHEEL_NAME)</a><a href='$(PACKAGE_SDIST_NAME)#sha256="$$(python -c "with open('dist/$(PACKAGE_SDIST_NAME)', 'rb') as f: import hashlib; print(hashlib.sha256(f.read()).hexdigest());")"'>$(PACKAGE_SDIST_NAME)</a></body></html>" > dist/simple-index/$(PROJECT_NAME)/index.html
217+
cp -f dist/$(PACKAGE_WHEEL_NAME) dist/simple-index/$(PROJECT_NAME)/
218+
cp -f dist/$(PACKAGE_SDIST_NAME) dist/simple-index/$(PROJECT_NAME)/
219+
python -m tarfile --create dist/$(PACKAGE_BASE_NAME)-pep503-simple-index.tar dist/simple-index/
209220

210221
# Build the HTML and Markdown documentation from the package's source.
211222
DOCS_SOURCE := $(shell git ls-files docs/source)

0 commit comments

Comments
 (0)