on master help sign in
boilerplate: add inital Makefile with run & install targets

Add *Makefile* boilerplate useful for python projects. All python tasks are using a virtualenv from ./local/py3 $ make help run - run developer instance install - developer install (./local) uninstall - uninstall (./local) Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>

commitb55a8004ed4c3d11d0d820480eca4769665ef38b authorMarkus Heiser <markus.heiser@darmarit.de> dateThu 28 Nov 2019, 19:54 (7 years ago) parenta56c56e
4 files changed+463
.gitignore+3 Makefile+42 utils/makefile.include+128 utils/makefile.python+290
.gitignore+3
@@ -18,3 +18,6 @@ setup.cfg
1818node_modules/
1919
2020.tx/
21+
22+local/
23+searx.egg-info/
Makefilenew file+42
@@ -0,0 +1,42 @@
1+# -*- coding: utf-8; mode: makefile-gmake -*-
2+
3+PYOBJECTS = searx
4+PY_SETUP_EXTRAS ?= \[test\]
5+
6+include utils/makefile.include
7+include utils/makefile.python
8+
9+all: clean install
10+
11+PHONY += help
12+help:
13+ @echo ' run - run developer instance'
14+ @echo ' install - developer install (./local)'
15+ @echo ' uninstall - uninstall (./local)'
16+ @echo ''
17+ @$(MAKE) -s -f utils/makefile.include make-help
18+ @echo ''
19+ @$(MAKE) -s -f utils/makefile.python python-help
20+
21+PHONY += install
22+install: pyenvinstall
23+
24+PHONY += uninstall
25+uninstall: pyenvuninstall
26+
27+PHONY += clean
28+clean: pyclean
29+ $(call cmd,common_clean)
30+
31+PHONY += run
32+run: pyenvinstall
33+ $(Q) ( \
34+ sed -i -e "s/debug : False/debug : True/g" ./searx/settings.yml ; \
35+ sleep 2 ; \
36+ xdg-open http://127.0.0.1:8888/ ; \
37+ sleep 3 ; \
38+ sed -i -e "s/debug : True/debug : False/g" ./searx/settings.yml ; \
39+ ) &
40+ $(PY_ENV)/bin/python ./searx/webapp.py
41+
42+.PHONY: $(PHONY)
utils/makefile.includenew file+128
@@ -0,0 +1,128 @@
1+# -*- coding: utf-8; mode: makefile-gmake -*-
2+
3+make-help:
4+ @echo ' make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build'
5+ @echo ' make V=2 [targets] 2 => give reason for rebuild of target'
6+
7+quiet_cmd_common_clean = CLEAN $@
8+ cmd_common_clean = \
9+ rm -rf tests/build ;\
10+ find . -name '*.orig' -exec rm -f {} + ;\
11+ find . -name '*.rej' -exec rm -f {} + ;\
12+ find . -name '*~' -exec rm -f {} + ;\
13+ find . -name '*.bak' -exec rm -f {} + ;\
14+
15+FMT = cat
16+ifeq ($(shell which fmt >/dev/null 2>&1; echo $$?), 0)
17+FMT = fmt
18+endif
19+
20+# MS-Windows
21+#
22+# For a minimal *make-environment*, I'am using the gnu-tools from:
23+#
24+# - GNU MCU Eclipse Windows Build Tools, which brings 'make', 'rm' etc.
25+# https://github.com/gnu-mcu-eclipse/windows-build-tools/releases
26+#
27+# - git for Windows, which brings 'find', 'grep' etc.
28+# https://git-scm.com/download/win
29+
30+
31+# normpath
32+#
33+# System-dependent normalization of the path name
34+#
35+# usage: $(call normpath,/path/to/file)
36+
37+normpath = $1
38+ifeq ($(OS),Windows_NT)
39+ normpath = $(subst /,\,$1)
40+endif
41+
42+
43+# stolen from linux/Makefile
44+#
45+
46+ifeq ("$(origin V)", "command line")
47+ KBUILD_VERBOSE = $(V)
48+endif
49+ifndef KBUILD_VERBOSE
50+ KBUILD_VERBOSE = 0
51+endif
52+
53+ifeq ($(KBUILD_VERBOSE),1)
54+ quiet =
55+ Q =
56+else
57+ quiet=quiet_
58+ Q = @
59+endif
60+
61+# stolen from linux/scripts/Kbuild.include
62+#
63+
64+# Convenient variables
65+comma := ,
66+quote := "
67+#" this comment is only for emacs highlighting
68+squote := '
69+#' this comment is only for emacs highlighting
70+empty :=
71+space := $(empty) $(empty)
72+space_escape := _-_SPACE_-_
73+
74+# Find any prerequisites that is newer than target or that does not exist.
75+# PHONY targets skipped in both cases.
76+any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
77+#
78+###
79+# why - tell why a a target got build
80+# enabled by make V=2
81+# Output (listed in the order they are checked):
82+# (1) - due to target is PHONY
83+# (2) - due to target missing
84+# (3) - due to: file1.h file2.h
85+# (4) - due to command line change
86+# (5) - due to missing .cmd file
87+# (6) - due to target not in $(targets)
88+# (1) PHONY targets are always build
89+# (2) No target, so we better build it
90+# (3) Prerequisite is newer than target
91+# (4) The command line stored in the file named dir/.target.cmd
92+# differed from actual command line. This happens when compiler
93+# options changes
94+# (5) No dir/.target.cmd file (used to store command line)
95+# (6) No dir/.target.cmd file and target not listed in $(targets)
96+# This is a good hint that there is a bug in the kbuild file
97+ifeq ($(KBUILD_VERBOSE),2)
98+why = \
99+ $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
100+ $(if $(wildcard $@), \
101+ $(if $(strip $(any-prereq)),- due to: $(any-prereq), \
102+ $(if $(arg-check), \
103+ $(if $(cmd_$@),- due to command line change, \
104+ $(if $(filter $@, $(targets)), \
105+ - due to missing .cmd file, \
106+ - due to $(notdir $@) not in $$(targets) \
107+ ) \
108+ ) \
109+ ) \
110+ ), \
111+ - due to target missing \
112+ ) \
113+ )
114+
115+echo-why = $(call escsq, $(strip $(why)))
116+endif
117+#
118+###
119+# Escape single quote for use in echo statements
120+escsq = $(subst $(squote),'\$(squote)',$1)
121+#
122+# echo command.
123+# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
124+echo-cmd = $(if $($(quiet)cmd_$(1)),echo '$(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
125+#
126+# printing commands
127+cmd = @$(echo-cmd) $(cmd_$(1))
128+
utils/makefile.pythonnew file+290
@@ -0,0 +1,290 @@
1+# -*- coding: utf-8; mode: makefile-gmake -*-
2+
3+# list of python packages (folders) or modules (files) of this build
4+PYOBJECTS ?=
5+
6+SITE_PYTHON ?=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))site-python
7+export PYTHONPATH := $(SITE_PYTHON):$$PYTHONPATH
8+
9+# folder where the python distribution takes place
10+PYDIST ?= ./py_dist
11+# folder where the python intermediate build files take place
12+PYBUILD ?= ./py_build
13+# python version to use
14+PY ?=3
15+PYTHON ?= python$(PY)
16+PIP ?= pip$(PY)
17+PIP_INST ?= --user
18+
19+# https://www.python.org/dev/peps/pep-0508/#extras
20+#PY_SETUP_EXTRAS ?= \[develop,test\]
21+PY_SETUP_EXTRAS ?=
22+
23+PYDEBUG ?= --pdb
24+PYLINT_RC ?= .pylintrc
25+
26+TEST_FOLDER ?= ./tests
27+TEST ?= .
28+
29+VTENV_OPTS = "--no-site-packages"
30+PY_ENV = ./local/py$(PY)
31+PY_ENV_BIN = $(PY_ENV)/bin
32+PY_ENV_ACT = . $(PY_ENV_BIN)/activate
33+
34+ifeq ($(OS),Windows_NT)
35+ PYTHON = python
36+ PY_ENV_BIN = $(PY_ENV)/Scripts
37+ PY_ENV_ACT = $(PY_ENV_BIN)/activate
38+endif
39+
40+ifeq ($(PYTHON),python)
41+ VIRTUALENV = virtualenv
42+else
43+ VIRTUALENV = virtualenv --python=$(PYTHON)
44+endif
45+
46+ifeq ($(KBUILD_VERBOSE),1)
47+ PIP_VERBOSE =
48+ VIRTUALENV_VERBOSE =
49+else
50+ PIP_VERBOSE = "-q"
51+ VIRTUALENV_VERBOSE = "-q"
52+endif
53+
54+python-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()'
75+
76+# ------------------------------------------------------------------------------
77+# OS requirements
78+# ------------------------------------------------------------------------------
79+
80+PHONY += msg-python-exe python-exe
81+msg-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)
88+
89+ifeq ($(shell which $(PYTHON) >/dev/null 2>&1; echo $$?), 1)
90+python-exe: msg-python-exe
91+ $(error The '$(PYTHON)' command was not found)
92+else
93+python-exe:
94+ @:
95+endif
96+
97+msg-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)
104+
105+ifeq ($(shell which $(PIP) >/dev/null 2>&1; echo $$?), 1)
106+pip-exe: msg-pip-exe
107+ $(error The '$(PIP)' command was not found)
108+else
109+pip-exe:
110+ @:
111+endif
112+
113+PHONY += msg-virtualenv-exe virtualenv-exe
114+msg-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)
120+
121+ifeq ($(shell which virtualenv >/dev/null 2>&1; echo $$?), 1)
122+virtualenv-exe: msg-virtualenv-exe
123+ $(error The 'virtualenv' command was not found)
124+else
125+virtualenv-exe:
126+ @:
127+endif
128+
129+# ------------------------------------------------------------------------------
130+# commands
131+# ------------------------------------------------------------------------------
132+
133+# $2 path to folder with setup.py, this uses pip from the OS
134+quiet_cmd_pyinstall = INSTALL $2
135+ cmd_pyinstall = $(PIP) $(PIP_VERBOSE) install $(PIP_INST) -e $2$(PY_SETUP_EXTRAS)
136+
137+# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
138+quiet_cmd_pyenvinstall = PYENV install $2
139+ cmd_pyenvinstall = $(PY_ENV_BIN)/pip $(PIP_VERBOSE) install -e $2$(PY_SETUP_EXTRAS)
140+
141+# Uninstall the package. Since pip does not uninstall the no longer needed
142+# depencies (something like autoremove) the depencies remain.
143+
144+# $2 package name to uninstall, this uses pip from the OS.
145+quiet_cmd_pyuninstall = UNINSTALL $2
146+ cmd_pyuninstall = $(PIP) $(PIP_VERBOSE) uninstall --yes $2
147+
148+# $2 path to folder with setup.py, this uses pip from pyenv (not OS!)
149+quiet_cmd_pyenvuninstall = PYENV uninstall $2
150+ cmd_pyenvuninstall = $(PY_ENV_BIN)/pip $(PIP_VERBOSE) uninstall --yes $2
151+
152+# $2 path to folder where virtualenv take place
153+quiet_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
160+
161+# $2 path to lint
162+quiet_cmd_pylint = LINT $@
163+ cmd_pylint = $(PY_ENV_BIN)/pylint --rcfile $(PYLINT_RC) $2
164+
165+quiet_cmd_pytest = TEST $@
166+ cmd_pytest = $(PY_ENV_BIN)/tox -vv
167+
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!
170+#
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`_.
175+#
176+# Further read:
177+#
178+# - pythonwheels_
179+# - setuptools_
180+# - packaging_
181+# - sdist_
182+# - installing_
183+#
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/
193+#
194+quiet_cmd_pybuild = BUILD $@
195+ cmd_pybuild = $(PY_ENV_BIN)/$(PYTHON) setup.py \
196+ sdist -d $(PYDIST) \
197+ bdist_wheel --bdist-dir $(PYBUILD) -d $(PYDIST)
198+
199+quiet_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 {} +
206+
207+# ------------------------------------------------------------------------------
208+# targets
209+# ------------------------------------------------------------------------------
210+
211+# for installation use the pip from the OS!
212+PHONY += pyinstall
213+pyinstall: pip-exe
214+ $(call cmd,pyinstall,.)
215+
216+PHONY += pyuninstall
217+pyuninstall: pip-exe
218+ $(call cmd,pyuninstall,$(PYOBJECTS))
219+
220+# for installation use the pip from PY_ENV (not the OS)!
221+PHONY += pyenvinstall
222+pyenvinstall: $(PY_ENV)
223+ $(call cmd,pyenvinstall,.)
224+
225+PHONY += pyenvuninstall
226+pyenvuninstall: $(PY_ENV)
227+ $(call cmd,pyenvuninstall,$(PYOBJECTS))
228+
229+PHONY += pyclean
230+pyclean:
231+ $(call cmd,pyclean)
232+
233+# to build *local* environment, python and virtualenv from the OS is needed!
234+pyenv: $(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
238+
239+PHONY += pylint-exe
240+pylint-exe: $(PY_ENV)
241+ @$(PY_ENV_BIN)/pip $(PIP_VERBOSE) install pylint
242+
243+PHONY += pylint
244+pylint: pylint-exe
245+ $(call cmd,pylint,$(PYOBJECTS))
246+
247+PHONY += pybuild
248+pybuild: $(PY_ENV)
249+ $(call cmd,pybuild)
250+
251+PHONY += pytest
252+pytest: $(PY_ENV)
253+ $(call cmd,pytest)
254+
255+PHONY += 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'
260+pydebug: $(PY_ENV)
261+ DEBUG=$(DEBUG) $(PY_ENV_BIN)/pytest $(DEBUG) -v $(TEST_FOLDER)/$(TEST)
262+
263+# install / uninstall python objects into virtualenv (PYENV)
264+pyenv-install: $(PY_ENV)
265+ @$(PY_ENV_BIN)/pip $(PIP_VERBOSE) install -e .
266+ @echo " ACTIVATE $(call normpath,$(PY_ENV_ACT)) "
267+
268+pyenv-uninstall: $(PY_ENV)
269+ @$(PY_ENV_BIN)/pip $(PIP_VERBOSE) uninstall --yes .
270+
271+# runs python interpreter from ./local/py<N>/bin/python
272+pyenv-python: pyenv-install
273+ cd ./local; ../$(PY_ENV_BIN)/python -i
274+
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.
281+#
282+# [1] http://python-packaging.readthedocs.io/en/latest/dependencies.html#packages-not-on-pypi
283+# [2] https://github.com/pypa/pip/pull/1519
284+
285+# https://github.com/pypa/twine
286+PHONY += upload-pypi
287+upload-pypi: pyclean pybuild
288+ @$(PY_ENV_BIN)/twine upload $(PYDIST)/*
289+
290+.PHONY: $(PHONY)