makefile.python 9.3 KB

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