on 484dc99 help sign in
searx/utils/makefile.python
252 lines 9.5 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
90# ------------------------------------------------------------------------------
91# commands
92# ------------------------------------------------------------------------------
94# $2 path to folder with setup.py, this uses pip from the OS
95quiet_cmd_pyinstall = INSTALL $2
96 cmd_pyinstall = $(PIP) $(PIP_VERBOSE) install $(PIP_INST) -e $2$(PY_SETUP_EXTRAS)
98# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
99quiet_cmd_pyenvinstall = PYENV install $2
100 cmd_pyenvinstall = \
101 if ! cat $(PY_ENV)/requirements.sha256 2>/dev/null | sha256sum --check --status 2>/dev/null; then \
102 $(PY_ENV_BIN)/python -m pip $(PIP_VERBOSE) install -e $2$(PY_SETUP_EXTRAS) ;\
103 sha256sum requirements*.txt > $(PY_ENV)/requirements.sha256 ;\
104 else \
105 echo "PYENV $2 already installed"; \
106 fi
108# Uninstall the package. Since pip does not uninstall the no longer needed
109# depencies (something like autoremove) the depencies remain.
111# $2 package name to uninstall, this uses pip from the OS.
112quiet_cmd_pyuninstall = UNINSTALL $2
113 cmd_pyuninstall = $(PIP) $(PIP_VERBOSE) uninstall --yes $2
115# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
116quiet_cmd_pyenvuninstall = PYENV uninstall $2
117 cmd_pyenvuninstall = $(PY_ENV_BIN)/python -m pip $(PIP_VERBOSE) uninstall --yes $2
119# $2 path to folder where virtualenv take place
120quiet_cmd_virtualenv = PYENV usage: $ source ./$@/bin/activate
121 cmd_virtualenv = \
122 if [ ! -d "./$(PY_ENV)" ];then \
123 $(PYTHON) -m venv $(VTENV_OPTS) $2; \
124 $(PY_ENV_BIN)/python -m pip install $(PIP_VERBOSE) -U pip wheel setuptools; \
125 $(PY_ENV_BIN)/python -m pip install $(PIP_VERBOSE) -r requirements.txt; \
126 else \
127 echo "PYENV using virtualenv from $2"; \
128 fi
130# $2 path to lint
131quiet_cmd_pylint = LINT $@
132 cmd_pylint = $(PY_ENV_BIN)/python -m pylint -j 0 --rcfile $(PYLINT_RC) $2
134quiet_cmd_pytest = TEST $@
135 cmd_pytest = $(PY_ENV_BIN)/python -m tox -vv
137# setuptools, pip, easy_install its a mess full of cracks, a documentation hell
138# and broken by design ... all sucks, I really, really hate all this ... aaargh!
140# About python packaging see `Python Packaging Authority`_. Most of the names
141# here are mapped to ``setup(<name1>=..., <name2>=...)`` arguments in
142# ``setup.py``. See `Packaging and distributing projects`_ about ``setup(...)``
143# arguments. If this is all new for you, start with `PyPI Quick and Dirty`_.
145# Further read:
147# - pythonwheels_
148# - setuptools_
149# - packaging_
150# - sdist_
151# - installing_
153# .. _`Python Packaging Authority`: https://www.pypa.io
154# .. _`Packaging and distributing projects`: https://packaging.python.org/guides/distributing-packages-using-setuptools/
155# .. _`PyPI Quick and Dirty`: https://hynek.me/articles/sharing-your-labor-of-love-pypi-quick-and-dirty/
156# .. _pythonwheels: https://pythonwheels.com/
157# .. _setuptools: https://setuptools.readthedocs.io/en/latest/setuptools.html
158# .. _packaging: https://packaging.python.org/guides/distributing-packages-using-setuptools/#packaging-and-distributing-projects
159# .. _sdist: https://packaging.python.org/guides/distributing-packages-using-setuptools/#source-distributions
160# .. _bdist_wheel: https://packaging.python.org/guides/distributing-packages-using-setuptools/#pure-python-wheels
161# .. _installing: https://packaging.python.org/tutorials/installing-packages/
163quiet_cmd_pybuild = BUILD $@
164 cmd_pybuild = $(PY_ENV_BIN)/python setup.py \
165 sdist -d $(PYDIST) \
166 bdist_wheel --bdist-dir $(PYBUILD) -d $(PYDIST)
168quiet_cmd_pyclean = CLEAN $@
169# remove 'build' folder since bdist_wheel does not care the --bdist-dir
170 cmd_pyclean = \
171 rm -rf $(PYDIST) $(PYBUILD) $(PY_ENV) ./.tox *.egg-info ;\
172 find . -name '*.pyc' -exec rm -f {} + ;\
173 find . -name '*.pyo' -exec rm -f {} + ;\
174 find . -name __pycache__ -exec rm -rf {} +
176# ------------------------------------------------------------------------------
177# targets
178# ------------------------------------------------------------------------------
180# for installation use the pip from the OS!
181PHONY += pyinstall
182pyinstall: pip-exe
183 $(call cmd,pyinstall,.)
185PHONY += pyuninstall
186pyuninstall: pip-exe
187 $(call cmd,pyuninstall,$(PYOBJECTS))
189# for installation use the pip from PY_ENV (not the OS)!
190PHONY += pyenvinstall
191pyenvinstall: $(PY_ENV)
192 $(call cmd,pyenvinstall,.)
194PHONY += pyenvuninstall
195pyenvuninstall: $(PY_ENV)
196 $(call cmd,pyenvuninstall,$(PYOBJECTS))
198PHONY += pyclean
199pyclean:
200 $(call cmd,pyclean)
202# to build *local* environment, python from the OS is needed!
203pyenv: $(PY_ENV)
204$(PY_ENV): python-exe
205 $(call cmd,virtualenv,$(PY_ENV))
207PHONY += pylint-exe
208pylint-exe: $(PY_ENV)
209 @$(PY_ENV_BIN)/python -m pip $(PIP_VERBOSE) install pylint
211PHONY += pylint
212pylint: pylint-exe
213 $(call cmd,pylint,$(PYOBJECTS))
215PHONY += pybuild
216pybuild: $(PY_ENV)
217 $(call cmd,pybuild)
219PHONY += pytest
220pytest: $(PY_ENV)
221 $(call cmd,pytest)
223PHONY += pydebug
224# set breakpoint with:
225# DEBUG()
226# e.g. to run tests in debug mode in emacs use:
227# 'M-x pdb' ... 'make pydebug'
228pydebug: $(PY_ENV)
229 DEBUG=$(DEBUG) $(PY_ENV_BIN)/pytest $(DEBUG) -v $(TEST_FOLDER)/$(TEST)
231# runs python interpreter from ./local/py<N>/bin/python
232pyenv-python: pyenvinstall
233 $(PY_ENV_BIN)/python -i
235# With 'dependency_links=' setuptools supports dependencies on packages hosted
236# on other reposetories then PyPi, see "Packages Not On PyPI" [1]. The big
237# drawback is, due to security reasons (I don't know where the security gate on
238# PyPi is), this feature is not supported by pip [2]. Thats why an upload to
239# PyPi is required and since uploads via setuptools is not recommended, we have
240# to imstall / use twine ... its really a mess.
242# [1] https://python-packaging.readthedocs.io/en/latest/dependencies.html#packages-not-on-pypi
243# [2] https://github.com/pypa/pip/pull/1519
245# https://github.com/pypa/twine
246PHONY += upload-pypi upload-pypi-test
247upload-pypi: pyclean pyenvinstall pybuild
248 @$(PY_ENV_BIN)/twine upload $(PYDIST)/*
250upload-pypi-test: pyclean pyenvinstall pybuild
251 @$(PY_ENV_BIN)/twine upload -r testpypi $(PYDIST)/*
252.PHONY: $(PHONY)