makefile.python 9.7 KB

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