on aea4667 help sign in
searx/utils/makefile.python
290 lines 10 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
9# folder where the python distribution takes place
10PYDIST ?= ./py_dist
11# folder where the python intermediate build files take place
12PYBUILD ?= ./py_build
13# python version to use
14PY ?=3
15PYTHON ?= python$(PY)
16PIP ?= pip$(PY)
17PIP_INST ?= --user
19# https://www.python.org/dev/peps/pep-0508/#extras
20#PY_SETUP_EXTRAS ?= \[develop,test\]
21PY_SETUP_EXTRAS ?=
23PYDEBUG ?= --pdb
24PYLINT_RC ?= .pylintrc
26TEST_FOLDER ?= ./tests
27TEST ?= .
29VTENV_OPTS = "--no-site-packages"
30PY_ENV = ./local/py$(PY)
31PY_ENV_BIN = $(PY_ENV)/bin
32PY_ENV_ACT = . $(PY_ENV_BIN)/activate
34ifeq ($(OS),Windows_NT)
35 PYTHON = python
36 PY_ENV_BIN = $(PY_ENV)/Scripts
37 PY_ENV_ACT = $(PY_ENV_BIN)/activate
38endif
40ifeq ($(PYTHON),python)
41 VIRTUALENV = virtualenv
42else
43 VIRTUALENV = virtualenv --python=$(PYTHON)
44endif
46ifeq ($(KBUILD_VERBOSE),1)
47 PIP_VERBOSE =
48 VIRTUALENV_VERBOSE =
49else
50 PIP_VERBOSE = "-q"
51 VIRTUALENV_VERBOSE = "-q"
52endif
54python-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()'
76# ------------------------------------------------------------------------------
77# OS requirements
78# ------------------------------------------------------------------------------
80PHONY += msg-python-exe python-exe
81msg-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)
89ifeq ($(shell which $(PYTHON) >/dev/null 2>&1; echo $$?), 1)
90python-exe: msg-python-exe
91 $(error The '$(PYTHON)' command was not found)
92else
93python-exe:
94 @:
95endif
97msg-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)
105ifeq ($(shell which $(PIP) >/dev/null 2>&1; echo $$?), 1)
106pip-exe: msg-pip-exe
107 $(error The '$(PIP)' command was not found)
108else
109pip-exe:
110 @:
111endif
113PHONY += msg-virtualenv-exe virtualenv-exe
114msg-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)
121ifeq ($(shell which virtualenv >/dev/null 2>&1; echo $$?), 1)
122virtualenv-exe: msg-virtualenv-exe
123 $(error The 'virtualenv' command was not found)
124else
125virtualenv-exe:
126 @:
127endif
129# ------------------------------------------------------------------------------
130# commands
131# ------------------------------------------------------------------------------
133# $2 path to folder with setup.py, this uses pip from the OS
134quiet_cmd_pyinstall = INSTALL $2
135 cmd_pyinstall = $(PIP) $(PIP_VERBOSE) install $(PIP_INST) -e $2$(PY_SETUP_EXTRAS)
137# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
138quiet_cmd_pyenvinstall = PYENV install $2
139 cmd_pyenvinstall = $(PY_ENV_BIN)/pip $(PIP_VERBOSE) install -e $2$(PY_SETUP_EXTRAS)
141# Uninstall the package. Since pip does not uninstall the no longer needed
142# depencies (something like autoremove) the depencies remain.
144# $2 package name to uninstall, this uses pip from the OS.
145quiet_cmd_pyuninstall = UNINSTALL $2
146 cmd_pyuninstall = $(PIP) $(PIP_VERBOSE) uninstall --yes $2
148# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
149quiet_cmd_pyenvuninstall = PYENV uninstall $2
150 cmd_pyenvuninstall = $(PY_ENV_BIN)/pip $(PIP_VERBOSE) uninstall --yes $2
152# $2 path to folder where virtualenv take place
153quiet_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
161# $2 path to lint
162quiet_cmd_pylint = LINT $@
163 cmd_pylint = $(PY_ENV_BIN)/pylint --rcfile $(PYLINT_RC) $2
165quiet_cmd_pytest = TEST $@
166 cmd_pytest = $(PY_ENV_BIN)/tox -vv
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!
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`_.
176# Further read:
178# - pythonwheels_
179# - setuptools_
180# - packaging_
181# - sdist_
182# - installing_
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/
194quiet_cmd_pybuild = BUILD $@
195 cmd_pybuild = $(PY_ENV_BIN)/$(PYTHON) setup.py \
196 sdist -d $(PYDIST) \
197 bdist_wheel --bdist-dir $(PYBUILD) -d $(PYDIST)
199quiet_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 {} +
207# ------------------------------------------------------------------------------
208# targets
209# ------------------------------------------------------------------------------
211# for installation use the pip from the OS!
212PHONY += pyinstall
213pyinstall: pip-exe
214 $(call cmd,pyinstall,.)
216PHONY += pyuninstall
217pyuninstall: pip-exe
218 $(call cmd,pyuninstall,$(PYOBJECTS))
220# for installation use the pip from PY_ENV (not the OS)!
221PHONY += pyenvinstall
222pyenvinstall: $(PY_ENV)
223 $(call cmd,pyenvinstall,.)
225PHONY += pyenvuninstall
226pyenvuninstall: $(PY_ENV)
227 $(call cmd,pyenvuninstall,$(PYOBJECTS))
229PHONY += pyclean
230pyclean:
231 $(call cmd,pyclean)
233# to build *local* environment, python and virtualenv from the OS is needed!
234pyenv: $(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
239PHONY += pylint-exe
240pylint-exe: $(PY_ENV)
241 @$(PY_ENV_BIN)/pip $(PIP_VERBOSE) install pylint
243PHONY += pylint
244pylint: pylint-exe
245 $(call cmd,pylint,$(PYOBJECTS))
247PHONY += pybuild
248pybuild: $(PY_ENV)
249 $(call cmd,pybuild)
251PHONY += pytest
252pytest: $(PY_ENV)
253 $(call cmd,pytest)
255PHONY += 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'
260pydebug: $(PY_ENV)
261 DEBUG=$(DEBUG) $(PY_ENV_BIN)/pytest $(DEBUG) -v $(TEST_FOLDER)/$(TEST)
263# install / uninstall python objects into virtualenv (PYENV)
264pyenv-install: $(PY_ENV)
265 @$(PY_ENV_BIN)/pip $(PIP_VERBOSE) install -e .
266 @echo "ACTIVATE $(call normpath,$(PY_ENV_ACT)) "
268pyenv-uninstall: $(PY_ENV)
269 @$(PY_ENV_BIN)/pip $(PIP_VERBOSE) uninstall --yes .
271# runs python interpreter from ./local/py<N>/bin/python
272pyenv-python: pyenv-install
273 cd ./local; ../$(PY_ENV_BIN)/python -i
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.
282# [1] http://python-packaging.readthedocs.io/en/latest/dependencies.html#packages-not-on-pypi
283# [2] https://github.com/pypa/pip/pull/1519
285# https://github.com/pypa/twine
286PHONY += upload-pypi
287upload-pypi: pyclean pybuild
288 @$(PY_ENV_BIN)/twine upload $(PYDIST)/*
290.PHONY: $(PHONY)