on b329058 help sign in
searx/utils/makefile.python
263 lines 9.6 kB history raw
1# -*- coding: utf-8; mode: makefile-gmake -*-
3# list of python packages (folders) or modules (files) of this build
4PYOBJECTS ?=
6SITE_PYTHON ?=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))site-python
7export PYTHONPATH := $(SITE_PYTHON):$$PYTHONPATH
8export PY_ENV PYDIST PYBUILD
10# folder where the python distribution takes place
11PYDIST = ./$(LXC_ENV_FOLDER)dist
12# folder where the python intermediate build files take place
13PYBUILD = ./$(LXC_ENV_FOLDER)build
14# python version to use
15PY ?=3
16# $(PYTHON) points to the python interpreter from the OS! The python from the
17# OS is needed e.g. to create a virtualenv. For tasks inside the virtualenv the
18# interpeter from '$(PY_ENV_BIN)/python' is used.
19PYTHON ?= python$(PY)
20PIP ?= pip$(PY)
21PIP_INST ?= --user
23# https://www.python.org/dev/peps/pep-0508/#extras
24#PY_SETUP_EXTRAS ?= \[develop,test\]
25PY_SETUP_EXTRAS ?=
27PYDEBUG ?= --pdb
28PYLINT_RC ?= .pylintrc
30TEST_FOLDER ?= ./tests
31TEST ?= .
33PY_ENV = ./$(LXC_ENV_FOLDER)local/py$(PY)
34PY_ENV_BIN = $(PY_ENV)/bin
35PY_ENV_ACT = . $(PY_ENV_BIN)/activate
37ifeq ($(OS),Windows_NT)
38 PYTHON = python
39 PY_ENV_BIN = $(PY_ENV)/Scripts
40 PY_ENV_ACT = $(PY_ENV_BIN)/activate
41endif
43VTENV_OPTS ?=
44ifeq ($(PYTHON),python)
45 VIRTUALENV = virtualenv
46else
47 VIRTUALENV = virtualenv --python=$(PYTHON)
48endif
50ifeq ($(KBUILD_VERBOSE),1)
51 PIP_VERBOSE =
52 VIRTUALENV_VERBOSE =
53else
54 PIP_VERBOSE = "-q"
55 VIRTUALENV_VERBOSE = "-q"
56endif
58python-help::
59 @echo 'makefile.python:'
60 @echo ' pyenv | pyenv[un]install'
61 @echo ' build $(PY_ENV) & [un]install python objects'
62 @echo ' targts using pyenv $(PY_ENV):'
63 @echo ' pylint - run pylint *linting*'
64 @echo ' pytest - run *tox* test on python objects'
65 @echo ' pydebug - run tests within a PDB debug session'
66 @echo ' pybuild - build python packages ($(PYDIST) $(PYBUILD))'
67 @echo ' pyclean - clean intermediate python objects'
68 @echo ' targets using system users environment:'
69 @echo ' py[un]install - [un]install python objects in editable mode'
70 @echo ' upload-pypi - upload $(PYDIST)/* files to PyPi'
71 @echo 'options:'
72 @echo ' make PY=2 [targets] => to eval targets with python 2 ($(PY))'
73 @echo ' make PIP_INST= => to set/unset pip install options ($(PIP_INST))'
74 @echo ' make TEST=. => choose test from $(TEST_FOLDER) (default "." runs all)'
75 @echo ' make DEBUG= => target "debug": do not invoke PDB on errors'
76 @echo ' make PY_SETUP_EXTRAS => also install extras_require from setup.py \[develop,test\]'
77 @echo ' when using target "pydebug", set breakpoints within py-source by adding::'
78 @echo ' DEBUG()'
80# ------------------------------------------------------------------------------
81# OS requirements
82# ------------------------------------------------------------------------------
84PHONY += msg-python-exe python-exe
85msg-python-exe:
86 @echo "\n $(PYTHON) is required\n\n\
87 Make sure you have $(PYTHON) installed, grab it from\n\
88 https://www.python.org or install it from your package\n\
89 manager. On debian based OS these requirements are\n\
90 installed by::\n\n\
91 sudo -H apt-get install $(PYTHON)\n" | $(FMT)
93ifeq ($(shell which $(PYTHON) >/dev/null 2>&1; echo $$?), 1)
94python-exe: msg-python-exe
95 $(error The '$(PYTHON)' command was not found)
96else
97python-exe:
98 @:
99endif
101# ------------------------------------------------------------------------------
102# commands
103# ------------------------------------------------------------------------------
105# $2 path to folder with setup.py, this uses pip from the OS
106quiet_cmd_pyinstall = INSTALL $2
107 cmd_pyinstall = $(PIP) $(PIP_VERBOSE) install $(PIP_INST) -e $2$(PY_SETUP_EXTRAS)
109# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
110quiet_cmd_pyenvinstall = PYENV install $2
111 cmd_pyenvinstall = $(PY_ENV_BIN)/python -m pip $(PIP_VERBOSE) install -e $2$(PY_SETUP_EXTRAS)
113# Uninstall the package. Since pip does not uninstall the no longer needed
114# depencies (something like autoremove) the depencies remain.
116# $2 package name to uninstall, this uses pip from the OS.
117quiet_cmd_pyuninstall = UNINSTALL $2
118 cmd_pyuninstall = $(PIP) $(PIP_VERBOSE) uninstall --yes $2
120# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
121quiet_cmd_pyenvuninstall = PYENV uninstall $2
122 cmd_pyenvuninstall = $(PY_ENV_BIN)/python -m pip $(PIP_VERBOSE) uninstall --yes $2
124# $2 path to folder where virtualenv take place
125quiet_cmd_virtualenv = PYENV usage: $ source ./$@/bin/activate
126 cmd_virtualenv = \
127 if [ ! -d "./$(PY_ENV)" ];then \
128 $(VIRTUALENV) $(VIRTUALENV_VERBOSE) $(VTENV_OPTS) $2; \
129 else \
130 echo "PYENV using virtualenv from $2"; \
131 fi
133# $2 path to lint
134quiet_cmd_pylint = LINT $@
135 cmd_pylint = $(PY_ENV_BIN)/python -m pylint --rcfile $(PYLINT_RC) $2
137quiet_cmd_pytest = TEST $@
138 cmd_pytest = $(PY_ENV_BIN)/python -m 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!
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`_.
148# Further read:
150# - pythonwheels_
151# - setuptools_
152# - packaging_
153# - sdist_
154# - installing_
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/
166quiet_cmd_pybuild = BUILD $@
167 cmd_pybuild = $(PY_ENV_BIN)/python setup.py \
168 sdist -d $(PYDIST) \
169 bdist_wheel --bdist-dir $(PYBUILD) -d $(PYDIST)
171quiet_cmd_pyclean = CLEAN $@
172# remove 'build' folder since bdist_wheel does not care the --bdist-dir
173 cmd_pyclean = \
174 rm -rf $(PYDIST) $(PYBUILD) $(PY_ENV) ./.tox *.egg-info ;\
175 find . -name '*.pyc' -exec rm -f {} + ;\
176 find . -name '*.pyo' -exec rm -f {} + ;\
177 find . -name __pycache__ -exec rm -rf {} +
179# ------------------------------------------------------------------------------
180# targets
181# ------------------------------------------------------------------------------
183# for installation use the pip from the OS!
184PHONY += pyinstall
185pyinstall: pip-exe
186 $(call cmd,pyinstall,.)
188PHONY += pyuninstall
189pyuninstall: pip-exe
190 $(call cmd,pyuninstall,$(PYOBJECTS))
192# for installation use the pip from PY_ENV (not the OS)!
193PHONY += pyenvinstall
194pyenvinstall: $(PY_ENV)
195 $(call cmd,pyenvinstall,.)
197PHONY += pyenvuninstall
198pyenvuninstall: $(PY_ENV)
199 $(call cmd,pyenvuninstall,$(PYOBJECTS))
201PHONY += pyclean
202pyclean:
203 $(call cmd,pyclean)
205# to build *local* environment, python from the OS is needed!
206pyenv: $(PY_ENV)
207$(PY_ENV): python-exe
208 $(call cmd,virtualenv,$(PY_ENV))
209 $(Q)$(PY_ENV_BIN)/python -m pip install $(PIP_VERBOSE) -U pip wheel pip setuptools
210 $(Q)$(PY_ENV_BIN)/python -m pip install $(PIP_VERBOSE) -r requirements.txt
212PHONY += pylint-exe
213pylint-exe: $(PY_ENV)
214 @$(PY_ENV_BIN)/python -m pip $(PIP_VERBOSE) install pylint
216PHONY += pylint
217pylint: pylint-exe
218 $(call cmd,pylint,$(PYOBJECTS))
220PHONY += pybuild
221pybuild: $(PY_ENV)
222 $(call cmd,pybuild)
224PHONY += pytest
225pytest: $(PY_ENV)
226 $(call cmd,pytest)
228PHONY += pydebug
229# set breakpoint with:
230# DEBUG()
231# e.g. to run tests in debug mode in emacs use:
232# 'M-x pdb' ... 'make pydebug'
233pydebug: $(PY_ENV)
234 DEBUG=$(DEBUG) $(PY_ENV_BIN)/pytest $(DEBUG) -v $(TEST_FOLDER)/$(TEST)
236# install / uninstall python objects into virtualenv (PYENV)
237pyenv-install: $(PY_ENV)
238 @$(PY_ENV_BIN)/python -m pip $(PIP_VERBOSE) install -e .
239 @echo "ACTIVATE $(call normpath,$(PY_ENV_ACT)) "
241pyenv-uninstall: $(PY_ENV)
242 @$(PY_ENV_BIN)/python -m pip $(PIP_VERBOSE) uninstall --yes .
244# runs python interpreter from ./local/py<N>/bin/python
245pyenv-python: pyenv-install
246 $(PY_ENV_BIN)/python -i
248# With 'dependency_links=' setuptools supports dependencies on packages hosted
249# on other reposetories then PyPi, see "Packages Not On PyPI" [1]. The big
250# drawback is, due to security reasons (I don't know where the security gate on
251# PyPi is), this feature is not supported by pip [2]. Thats why an upload to
252# PyPi is required and since uploads via setuptools is not recommended, we have
253# to imstall / use twine ... its really a mess.
255# [1] http://python-packaging.readthedocs.io/en/latest/dependencies.html#packages-not-on-pypi
256# [2] https://github.com/pypa/pip/pull/1519
258# https://github.com/pypa/twine
259PHONY += upload-pypi
260upload-pypi: pyclean pyenvinstall pybuild
261 @$(PY_ENV_BIN)/twine upload $(PYDIST)/*
263.PHONY: $(PHONY)