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