forked from jenstroeger/python-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
134 lines (122 loc) · 4.43 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
#! /usr/bin/env make -f
# This variable contains the first goal that matches any of the listed goals
# here, else it contains an empty string. The net effect is to filter out
# whether this current run of `make` requires a Python virtual environment.
NEED_VENV := $(or \
$(findstring setup,$(MAKECMDGOALS)), \
$(findstring upgrade,$(MAKECMDGOALS)), \
$(findstring requirements,$(MAKECMDGOALS)), \
$(findstring all,$(MAKECMDGOALS)), \
$(findstring quick-check,$(MAKECMDGOALS)), \
$(findstring check,$(MAKECMDGOALS)), \
$(findstring test,$(MAKECMDGOALS)), \
$(findstring dist,$(MAKECMDGOALS)), \
$(findstring bdist-wheel,$(MAKECMDGOALS)), \
$(findstring sdist,$(MAKECMDGOALS)), \
$(findstring docs,$(MAKECMDGOALS)) \
)
ifeq ($(NEED_VENV),)
# None of the current goals requires a virtual environment.
else
ifeq ($(origin VIRTUAL_ENV),undefined)
$(warning No Python virtual environment found, proceeding anyway)
else
ifeq ($(wildcard .upgraded),)
$(warning Python virtual environment not yet set up, proceeding anyway)
endif
endif
endif
# Create a virtual environment, either for Python3.10 (default) or using
# the Python interpreter specified in the PYTHON environment variable.
.PHONY: venv
venv:
if [ ! -z "${VIRTUAL_ENV}" ]; then \
echo "Found an activated Python virtual environment, exiting" && exit 1; \
fi
if [ -z "${PYTHON}" ]; then \
echo "Creating virtual envirnoment in .venv/ for python3.10"; \
python3.10 -m venv --upgrade-deps .venv; \
else \
echo "Creating virtual envirnoment in .venv/ for ${PYTHON}"; \
${PYTHON} -m venv --upgrade-deps .venv; \
fi
# Set up a newly created virtual environment. Note: pre-commit uses the
# venv's Python interpreter, so if you've created multiple venvs then
# pre-commit's git hooks run against the most recently set up venv.
.PHONY: setup
setup: force-upgrade
pre-commit install
pre-commit install --hook-type commit-msg
pre-commit install --hook-type pre-push
# Install or upgrade an existing virtual environment based on the
# package dependencies declared in setup.py.
.PHONY: upgrade
upgrade: .upgraded
.upgraded: setup.py
pip install --upgrade pip
pip install --upgrade --upgrade-strategy eager --editable .[hooks,dev,test,docs]
echo "Automatically generated by Python Package Makefile." > .upgraded
force-upgrade:
rm -f .upgraded
$(MAKE) upgrade
# Generate a requirements.txt file containing version and integrity
# hashes for all packages currently installed in the virtual environment.
.PHONY: requirements
requirements: requirements.txt
requirements.txt: setup.py
echo "" > requirements.txt
# See also: https://github.com/peterbe/hashin/issues/139
for pkg in `pip list --format freeze`; do hashin --verbose $$pkg; done
# Check, test, and build artifacts for this package.
.PHONY: all
all: check test dist docs
# Run some or all checks over the package code base.
.PHONY: quick-check check
quick-check:
pre-commit run pylint --all-files
pre-commit run mypy --all-files
check:
pre-commit run --all-files
# Run all unit tests.
.PHONY: test
test:
pre-commit run pytest --hook-stage push
# Build a source distribution package and a binary wheel distribution artifact.
.PHONY: dist bdist-wheel sdist
ifeq ($(wildcard .upgraded),)
PACKAGE_VERSION=unknown
else
PACKAGE_VERSION=$(shell python -c 'import package; print(package.__version__)')
endif
dist: bdist-wheel sdist
bdist-wheel: dist/package-$(PACKAGE_VERSION)-py3-none-any.whl
dist/package-$(PACKAGE_VERSION)-py3-none-any.whl: check test
python setup.py bdist_wheel --dist-dir dist/ --bdist-dir build/
sdist: dist/package-$(PACKAGE_VERSION).tar.gz
dist/package-$(PACKAGE_VERSION).tar.gz: check test
python setup.py sdist --dist-dir dist/
# Build the HTML documentation from the package's source.
.PHONY: docs
docs: docs/_build/html/index.html
docs/_build/html/index.html: check test
$(MAKE) -C docs/ html
# Clean test caches and remove build artifacts.
.PHONY: dist-clean clean
dist-clean:
rm -fr build/ dist/*
clean: dist-clean
rm -fr .coverage .hypothesis/ .mypy_cache/ .pytest_cache/
rm -fr docs/_build/
# Remove code caches, or the entire virtual environment.
.PHONY: nuke-caches nuke
nuke-caches: clean
find src/ -name __pycache__ -exec rm -fr {} +
find tests/ -name __pycache__ -exec rm -fr {} +
nuke: nuke-caches
if [ ! -z "${VIRTUAL_ENV}" ]; then \
echo "Deactivating and nuking virtual environment!"; \
deactivate; \
rm -fr $(VIRTUAL_ENV); \
fi
rm -fr src/package.egg-info/
rm -f requirements.txt .upgraded