@@ -0,0 +1,290 @@
1+# -*- coding: utf-8; mode: makefile-gmake -*-
2+
3+# list of python packages (folders) or modules (files) of this build
4+PYOBJECTS ?=
5+
6+SITE_PYTHON ?=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))site-python
7+export PYTHONPATH := $(SITE_PYTHON):$$PYTHONPATH
8+
9+# folder where the python distribution takes place
10+PYDIST ?= ./py_dist
11+# folder where the python intermediate build files take place
12+PYBUILD ?= ./py_build
13+# python version to use
14+PY ?=3
15+PYTHON ?= python$(PY)
16+PIP ?= pip$(PY)
17+PIP_INST ?= --user
18+
19+# https://www.python.org/dev/peps/pep-0508/#extras
20+#PY_SETUP_EXTRAS ?= \[develop,test\]
21+PY_SETUP_EXTRAS ?=
22+
23+PYDEBUG ?= --pdb
24+PYLINT_RC ?= .pylintrc
25+
26+TEST_FOLDER ?= ./tests
27+TEST ?= .
28+
29+VTENV_OPTS = "--no-site-packages"
30+PY_ENV = ./local/py$(PY)
31+PY_ENV_BIN = $(PY_ENV)/bin
32+PY_ENV_ACT = . $(PY_ENV_BIN)/activate
33+
34+ifeq ($(OS),Windows_NT)
35+ PYTHON = python
36+ PY_ENV_BIN = $(PY_ENV)/Scripts
37+ PY_ENV_ACT = $(PY_ENV_BIN)/activate
38+endif
39+
40+ifeq ($(PYTHON),python)
41+ VIRTUALENV = virtualenv
42+else
43+ VIRTUALENV = virtualenv --python=$(PYTHON)
44+endif
45+
46+ifeq ($(KBUILD_VERBOSE),1)
47+ PIP_VERBOSE =
48+ VIRTUALENV_VERBOSE =
49+else
50+ PIP_VERBOSE = "-q"
51+ VIRTUALENV_VERBOSE = "-q"
52+endif
53+
54+python-help::
55+ @echo 'makefile.python:'
56+ @echo ' pyenv | pyenv[un]install'
57+ @echo ' build $(PY_ENV) & [un]install python objects'
58+ @echo ' targts using pyenv $(PY_ENV):'
59+ @echo ' pylint - run pylint *linting*'
60+ @echo ' pytest - run *tox* test on python objects'
61+ @echo ' pydebug - run tests within a PDB debug session'
62+ @echo ' pybuild - build python packages'
63+ @echo ' pyclean - clean intermediate python objects'
64+ @echo ' targets using system users environment:'
65+ @echo ' py[un]install - [un]install python objects in editable mode'
66+ @echo ' upload-pypi - upload $(PYDIST)/* files to PyPi'
67+ @echo 'options:'
68+ @echo ' make PY=2 [targets] => to eval targets with python 2 ($(PY))'
69+ @echo ' make PIP_INST= => to set/unset pip install options ($(PIP_INST))'
70+ @echo ' make TEST=. => choose test from $(TEST_FOLDER) (default "." runs all)'
71+ @echo ' make DEBUG= => target "debug": do not invoke PDB on errors'
72+ @echo ' make PY_SETUP_EXTRAS => also install extras_require from setup.py \[develop,test\]'
73+ @echo ' when using target "pydebug", set breakpoints within py-source by adding::'
74+ @echo ' DEBUG()'
75+
76+# ------------------------------------------------------------------------------
77+# OS requirements
78+# ------------------------------------------------------------------------------
79+
80+PHONY += msg-python-exe python-exe
81+msg-python-exe:
82+ @echo "\n $(PYTHON) is required\n\n\
83+ Make sure you have $(PYTHON) installed, grab it from\n\
84+ https://www.python.org or install it from your package\n\
85+ manager. On debian based OS these requirements are\n\
86+ installed by::\n\n\
87+ sudo -H apt-get install $(PYTHON)\n" | $(FMT)
88+
89+ifeq ($(shell which $(PYTHON) >/dev/null 2>&1; echo $$?), 1)
90+python-exe: msg-python-exe
91+ $(error The '$(PYTHON)' command was not found)
92+else
93+python-exe:
94+ @:
95+endif
96+
97+msg-pip-exe:
98+ @echo "\n $(PIP) is required\n\n\
99+ Make sure you have updated pip installed, grab it from\n\
100+ https://pip.pypa.io or install it from your package\n\
101+ manager. On debian based OS these requirements are\n\
102+ installed by::\n\n\
103+ sudo -H apt-get install python$(PY)-pip\n" | $(FMT)
104+
105+ifeq ($(shell which $(PIP) >/dev/null 2>&1; echo $$?), 1)
106+pip-exe: msg-pip-exe
107+ $(error The '$(PIP)' command was not found)
108+else
109+pip-exe:
110+ @:
111+endif
112+
113+PHONY += msg-virtualenv-exe virtualenv-exe
114+msg-virtualenv-exe:
115+ @echo "\n virtualenv is required\n\n\
116+ Make sure you have an updated virtualenv installed, grab it from\n\
117+ https://virtualenv.pypa.io/en/stable/installation/ or install it\n\
118+ via pip by::\n\n\
119+ pip install --user https://github.com/pypa/virtualenv/tarball/master\n" | $(FMT)
120+
121+ifeq ($(shell which virtualenv >/dev/null 2>&1; echo $$?), 1)
122+virtualenv-exe: msg-virtualenv-exe
123+ $(error The 'virtualenv' command was not found)
124+else
125+virtualenv-exe:
126+ @:
127+endif
128+
129+# ------------------------------------------------------------------------------
130+# commands
131+# ------------------------------------------------------------------------------
132+
133+# $2 path to folder with setup.py, this uses pip from the OS
134+quiet_cmd_pyinstall = INSTALL $2
135+ cmd_pyinstall = $(PIP) $(PIP_VERBOSE) install $(PIP_INST) -e $2$(PY_SETUP_EXTRAS)
136+
137+# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
138+quiet_cmd_pyenvinstall = PYENV install $2
139+ cmd_pyenvinstall = $(PY_ENV_BIN)/pip $(PIP_VERBOSE) install -e $2$(PY_SETUP_EXTRAS)
140+
141+# Uninstall the package. Since pip does not uninstall the no longer needed
142+# depencies (something like autoremove) the depencies remain.
143+
144+# $2 package name to uninstall, this uses pip from the OS.
145+quiet_cmd_pyuninstall = UNINSTALL $2
146+ cmd_pyuninstall = $(PIP) $(PIP_VERBOSE) uninstall --yes $2
147+
148+# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
149+quiet_cmd_pyenvuninstall = PYENV uninstall $2
150+ cmd_pyenvuninstall = $(PY_ENV_BIN)/pip $(PIP_VERBOSE) uninstall --yes $2
151+
152+# $2 path to folder where virtualenv take place
153+quiet_cmd_virtualenv = PYENV usage: $ source ./$@/bin/activate
154+ cmd_virtualenv = \
155+ if [ ! -d "./$(PY_ENV)" ];then \
156+ $(VIRTUALENV) $(VIRTUALENV_VERBOSE) $(VTENV_OPTS) $2; \
157+ else \
158+ echo " PYENV using virtualenv from $2"; \
159+ fi
160+
161+# $2 path to lint
162+quiet_cmd_pylint = LINT $@
163+ cmd_pylint = $(PY_ENV_BIN)/pylint --rcfile $(PYLINT_RC) $2
164+
165+quiet_cmd_pytest = TEST $@
166+ cmd_pytest = $(PY_ENV_BIN)/tox -vv
167+
168+# setuptools, pip, easy_install its a mess full of cracks, a documentation hell
169+# and broken by design ... all sucks, I really, really hate all this ... aaargh!
170+#
171+# About python packaging see `Python Packaging Authority`_. Most of the names
172+# here are mapped to ``setup(<name1>=..., <name2>=...)`` arguments in
173+# ``setup.py``. See `Packaging and distributing projects`_ about ``setup(...)``
174+# arguments. If this is all new for you, start with `PyPI Quick and Dirty`_.
175+#
176+# Further read:
177+#
178+# - pythonwheels_
179+# - setuptools_
180+# - packaging_
181+# - sdist_
182+# - installing_
183+#
184+# .. _`Python Packaging Authority`: https://www.pypa.io
185+# .. _`Packaging and distributing projects`: https://packaging.python.org/guides/distributing-packages-using-setuptools/
186+# .. _`PyPI Quick and Dirty`: https://hynek.me/articles/sharing-your-labor-of-love-pypi-quick-and-dirty/
187+# .. _pythonwheels: https://pythonwheels.com/
188+# .. _setuptools: https://setuptools.readthedocs.io/en/latest/setuptools.html
189+# .. _packaging: https://packaging.python.org/guides/distributing-packages-using-setuptools/#packaging-and-distributing-projects
190+# .. _sdist: https://packaging.python.org/guides/distributing-packages-using-setuptools/#source-distributions
191+# .. _bdist_wheel: https://packaging.python.org/guides/distributing-packages-using-setuptools/#pure-python-wheels
192+# .. _installing: https://packaging.python.org/tutorials/installing-packages/
193+#
194+quiet_cmd_pybuild = BUILD $@
195+ cmd_pybuild = $(PY_ENV_BIN)/$(PYTHON) setup.py \
196+ sdist -d $(PYDIST) \
197+ bdist_wheel --bdist-dir $(PYBUILD) -d $(PYDIST)
198+
199+quiet_cmd_pyclean = CLEAN $@
200+# remove 'build' folder since bdist_wheel does not care the --bdist-dir
201+ cmd_pyclean = \
202+ rm -rf $(PYDIST) $(PYBUILD) ./local ./.tox *.egg-info ;\
203+ find . -name '*.pyc' -exec rm -f {} + ;\
204+ find . -name '*.pyo' -exec rm -f {} + ;\
205+ find . -name __pycache__ -exec rm -rf {} +
206+
207+# ------------------------------------------------------------------------------
208+# targets
209+# ------------------------------------------------------------------------------
210+
211+# for installation use the pip from the OS!
212+PHONY += pyinstall
213+pyinstall: pip-exe
214+ $(call cmd,pyinstall,.)
215+
216+PHONY += pyuninstall
217+pyuninstall: pip-exe
218+ $(call cmd,pyuninstall,$(PYOBJECTS))
219+
220+# for installation use the pip from PY_ENV (not the OS)!
221+PHONY += pyenvinstall
222+pyenvinstall: $(PY_ENV)
223+ $(call cmd,pyenvinstall,.)
224+
225+PHONY += pyenvuninstall
226+pyenvuninstall: $(PY_ENV)
227+ $(call cmd,pyenvuninstall,$(PYOBJECTS))
228+
229+PHONY += pyclean
230+pyclean:
231+ $(call cmd,pyclean)
232+
233+# to build *local* environment, python and virtualenv from the OS is needed!
234+pyenv: $(PY_ENV)
235+$(PY_ENV): virtualenv-exe python-exe
236+ $(call cmd,virtualenv,$(PY_ENV))
237+ @$(PY_ENV_BIN)/pip install $(PIP_VERBOSE) -r requirements.txt
238+
239+PHONY += pylint-exe
240+pylint-exe: $(PY_ENV)
241+ @$(PY_ENV_BIN)/pip $(PIP_VERBOSE) install pylint
242+
243+PHONY += pylint
244+pylint: pylint-exe
245+ $(call cmd,pylint,$(PYOBJECTS))
246+
247+PHONY += pybuild
248+pybuild: $(PY_ENV)
249+ $(call cmd,pybuild)
250+
251+PHONY += pytest
252+pytest: $(PY_ENV)
253+ $(call cmd,pytest)
254+
255+PHONY += pydebug
256+# set breakpoint with:
257+# DEBUG()
258+# e.g. to run tests in debug mode in emacs use:
259+# 'M-x pdb' ... 'make pydebug'
260+pydebug: $(PY_ENV)
261+ DEBUG=$(DEBUG) $(PY_ENV_BIN)/pytest $(DEBUG) -v $(TEST_FOLDER)/$(TEST)
262+
263+# install / uninstall python objects into virtualenv (PYENV)
264+pyenv-install: $(PY_ENV)
265+ @$(PY_ENV_BIN)/pip $(PIP_VERBOSE) install -e .
266+ @echo " ACTIVATE $(call normpath,$(PY_ENV_ACT)) "
267+
268+pyenv-uninstall: $(PY_ENV)
269+ @$(PY_ENV_BIN)/pip $(PIP_VERBOSE) uninstall --yes .
270+
271+# runs python interpreter from ./local/py<N>/bin/python
272+pyenv-python: pyenv-install
273+ cd ./local; ../$(PY_ENV_BIN)/python -i
274+
275+# With 'dependency_links=' setuptools supports dependencies on packages hosted
276+# on other reposetories then PyPi, see "Packages Not On PyPI" [1]. The big
277+# drawback is, due to security reasons (I don't know where the security gate on
278+# PyPi is), this feature is not supported by pip [2]. Thats why an upload to
279+# PyPi is required and since uploads via setuptools is not recommended, we have
280+# to imstall / use twine ... its really a mess.
281+#
282+# [1] http://python-packaging.readthedocs.io/en/latest/dependencies.html#packages-not-on-pypi
283+# [2] https://github.com/pypa/pip/pull/1519
284+
285+# https://github.com/pypa/twine
286+PHONY += upload-pypi
287+upload-pypi: pyclean pybuild
288+ @$(PY_ENV_BIN)/twine upload $(PYDIST)/*
289+
290+.PHONY: $(PHONY)