makefile.python 9.9 KB

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