@@ -0,0 +1,501 @@
2+# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
3+# SPDX-License-Identifier: AGPL-3.0-or-later
4+# shellcheck disable=SC2031
6+# shellcheck source=utils/lib.sh
7+source "$(dirname "${BASH_SOURCE[0]}")/utils/lib.sh"
8+# shellcheck source=utils/brand.env
9+source "${REPO_ROOT}/utils/brand.env"
14+PY_SETUP_EXTRAS='[test]'
15+NPM_PACKAGES="less@2.7 less-plugin-clean-css grunt-cli"
16+GECKODRIVER_VERSION="v0.28.0"
19+# These py files are linted by test.pylint(), all other files are linted by
24+ searx/engines/gigablast.py
25+ searx/engines/deviantart.py
26+ searx/engines/digg.py
27+ searx/engines/google.py
28+ searx/engines/google_news.py
29+ searx/engines/google_videos.py
30+ searx/engines/google_images.py
31+ searx/engines/mediathekviewweb.py
32+ searx/engines/meilisearch.py
33+ searx/engines/solidtorrents.py
34+ searx/engines/solr.py
35+ searx/engines/google_scholar.py
36+ searx/engines/yahoo_news.py
37+ searx/engines/apkmirror.py
38+ searx_extra/update/update_external_bangs.py
41+PYLINT_SEARX_DISABLE_OPTION="\
43+W0105,W0212,W0511,W0603,W0613,W0621,W0702,W0703,W1401,\
45+PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES="supported_languages,language_aliases"
46+PYLINT_OPTIONS="-m pylint -j 0 --rcfile .pylintrc"
51+ rebuild ./utils/brand.env
53+ pybabel compile ./searx/translations
55+ all : update searx/languages.py and ./data/*
56+ languages : update searx/data/engines_languages.json & searx/languages.py
57+ useragents: update searx/data/useragents.json with the most recent versions of Firefox.
59+ html : build HTML documentation
60+ live : autobuild HTML documentation while editing
61+ gh-pages : deploy on gh-pages branch
62+ prebuild : build reST include files (./${DOCS_BUILD}/includes)
63+ clean : clean documentation build
65+ build : build docker image
66+ push : build and push docker image
68+ download & install geckodriver if not already installed (required for
71+ env : download & install npm dependencies locally
72+ clean : drop npm installations
74+ build : Build python packages at ./${PYDIST}
75+ clean : delete virtualenv and intermediate py files
77+ install : developer install of searx into virtualenv
78+ uninstall : uninstall developer installation
79+ cmd ... : run command ... in virtualenv
80+ OK : test if virtualenv is OK
82+ Upload python packages to PyPi (to test use pypi.upload.test)
84+ pylint : lint PYLINT_FILES, searx/engines, searx & tests
85+ pep8 : pycodestyle (pep8) for all files except PYLINT_FILES
86+ unit : run unit tests
87+ coverage : run unit tests with coverage
88+ robot : run robot test
89+ clean : clean intermediate test stuff
91+ all : build all themes
92+ oscar : build oscar theme
93+ simple : build simple theme
98+if [ "$VERBOSE" = "1" ]; then
103+# needed by sphinx-docs
107+ SEARX_DEBUG=1 pyenv.cmd python utils/build_env.py 2>&1 \
108+ | prefix_stdout "${_Blue}BUILDENV${_creset} "
109+ return "${PIPESTATUS[0]}"
113+ build_msg BABEL compile
114+ pyenv.cmd pybabel compile -d "${REPO_ROOT}/searx/translations"
122+ build_msg DATA "update searx/data/ahmia_blacklist.txt"
123+ pyenv.cmd python searx_extra/update/update_ahmia_blacklist.py
124+ build_msg DATA "update searx/data/wikidata_units.json"
125+ pyenv.cmd python searx_extra/update/update_wikidata_units.py
126+ build_msg DATA "update searx/data/currencies.json"
127+ pyenv.cmd python searx_extra/update/update_currencies.py
133+ build_msg ENGINES "fetch languages .."
134+ pyenv.cmd python searx_extra/update/update_languages.py
135+ build_msg ENGINES "update update searx/languages.py"
136+ build_msg DATA "update searx/data/engines_languages.json"
142+ build_msg DATA "update searx/data/useragents.json"
143+ pyenv.cmd python searx_extra/update/update_firefox_version.py
148+ build_msg DOCS "build ${DOCS_BUILD}/includes"
151+ [ "$VERBOSE" = "1" ] && set -x
152+ mkdir -p "${DOCS_BUILD}/includes"
153+ ./utils/searx.sh doc | cat > "${DOCS_BUILD}/includes/searx.rst"
154+ ./utils/filtron.sh doc | cat > "${DOCS_BUILD}/includes/filtron.rst"
155+ ./utils/morty.sh doc | cat > "${DOCS_BUILD}/includes/morty.rst"
164+# shellcheck disable=SC2119
168+ build_msg DOCKER build
169+ # run installation in a subprocess and activate pyenv
171+ # See https://www.shellcheck.net/wiki/SC1001 and others ..
172+ # shellcheck disable=SC2031,SC2230,SC2002,SC2236,SC2143,SC1001
174+ # shellcheck source=/dev/null
175+ source "${PY_ENV_BIN}/activate"
177+ # Check if it is a git repository
178+ if [ ! -d .git ]; then
179+ die 1 "This is not Git repository"
181+ if [ ! -x "$(which git)" ]; then
182+ die 1 "git is not installed"
185+ if ! git remote get-url origin 2> /dev/null; then
186+ die 1 "there is no remote origin"
189+ # This is a git repository
191+ # "git describe" to get the Docker version (for example : v0.15.0-89-g0585788e)
192+ # awk to remove the "v" and the "g"
193+ SEARX_GIT_VERSION=$(git describe --match "v[0-9]*\.[0-9]*\.[0-9]*" HEAD 2>/dev/null | awk -F'-' '{OFS="-"; $1=substr($1, 2); if ($3) { $3=substr($3, 2); } print}')
195+ # add the suffix "-dirty" if the repository has uncommited change
196+ # /!\ HACK for searx/searx: ignore utils/brand.env
197+ git update-index -q --refresh
198+ if [ ! -z "$(git diff-index --name-only HEAD -- | grep -v 'utils/brand.env')" ]; then
199+ SEARX_GIT_VERSION="${SEARX_GIT_VERSION}-dirty"
202+ # Get the last git commit id, will be added to the Searx version (see Dockerfile)
203+ VERSION_GITCOMMIT=$(echo "$SEARX_GIT_VERSION" | cut -d- -f2-4)
204+ build_msg DOCKER "Last commit : $VERSION_GITCOMMIT"
206+ # Check consistency between the git tag and the searx/version.py file
207+ # /! HACK : parse Python file with bash /!
208+ # otherwise it is not possible build the docker image without all Python
209+ # dependencies ( version.py loads __init__.py )
210+ # SEARX_PYTHON_VERSION=$(python3 -c "import six; import searx.version; six.print_(searx.version.VERSION_STRING)")
211+ SEARX_PYTHON_VERSION=$(cat searx/version.py | grep "\(VERSION_MAJOR\|VERSION_MINOR\|VERSION_BUILD\) =" | cut -d\= -f2 | sed -e 's/^[[:space:]]*//' | paste -sd "." -)
212+ if [ "$(echo "$SEARX_GIT_VERSION" | cut -d- -f1)" != "$SEARX_PYTHON_VERSION" ]; then
213+ err_msg "git tag: $SEARX_GIT_VERSION"
214+ err_msg "searx/version.py: $SEARX_PYTHON_VERSION"
215+ die 1 "Inconsistency between the last git tag and the searx/version.py file"
218+ # define the docker image name
219+ GITHUB_USER=$(echo "${GIT_URL}" | sed 's/.*github\.com\/\([^\/]*\).*/\1/')
220+ SEARX_IMAGE_NAME="${SEARX_IMAGE_NAME:-${GITHUB_USER:-searx}/searx}"
222+ # build Docker image
223+ build_msg DOCKER "Building image ${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}"
225+ --build-arg GIT_URL="${GIT_URL}" \
226+ --build-arg SEARX_GIT_VERSION="${SEARX_GIT_VERSION}" \
227+ --build-arg VERSION_GITCOMMIT="${VERSION_GITCOMMIT}" \
228+ --build-arg LABEL_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
229+ --build-arg LABEL_VCS_REF="$(git rev-parse HEAD)" \
230+ --build-arg LABEL_VCS_URL="${GIT_URL}" \
231+ --build-arg TIMESTAMP_SETTINGS="$(git log -1 --format="%cd" --date=unix -- searx/settings.yml)" \
232+ --build-arg TIMESTAMP_UWSGI="$(git log -1 --format="%cd" --date=unix -- dockerfiles/uwsgi.ini)" \
233+ -t "${SEARX_IMAGE_NAME}:latest" -t "${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}" .
235+ if [ "$1" = "push" ]; then
236+ sudo docker push "${SEARX_IMAGE_NAME}:latest"
237+ sudo docker push "${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}"
243+# shellcheck disable=SC2119
247+ build_msg INSTALL "gecko.driver"
248+ # run installation in a subprocess and activate pyenv
250+ # shellcheck source=/dev/null
251+ source "${PY_ENV_BIN}/activate"
253+ # TODO : check the current geckodriver version
254+ geckodriver -V > /dev/null 2>&1 || NOTFOUND=1
256+ if [ -z "$NOTFOUND" ]; then
257+ build_msg INSTALL "geckodriver already installed"
260+ PLATFORM="$(python3 -c 'import platform; print(platform.system().lower(), platform.architecture()[0])')"
262+ "linux 32bit" | "linux2 32bit") ARCH="linux32";;
263+ "linux 64bit" | "linux2 64bit") ARCH="linux64";;
264+ "windows 32 bit") ARCH="win32";;
265+ "windows 64 bit") ARCH="win64";;
266+ "mac 64bit") ARCH="macos";;
268+ GECKODRIVER_URL="https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-$ARCH.tar.gz";
270+ build_msg GECKO "Installing ${PY_ENV_BIN}/geckodriver from $GECKODRIVER_URL"
273+ wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "${PY_ENV_BIN}" -f "$FILE" geckodriver
275+ chmod 755 -- "${PY_ENV_BIN}/geckodriver"
282+ pushd "${REPO_ROOT}" &> /dev/null
283+ # shellcheck disable=SC2230
284+ which npm &> /dev/null || die 1 'node.env - npm is not found!'
287+ # shellcheck disable=SC2030
288+ PATH="$(npm bin):$PATH"
291+ build_msg INSTALL "npm install $NPM_PACKAGES"
292+ # shellcheck disable=SC2086
293+ npm install $NPM_PACKAGES
295+ cd "${REPO_ROOT}/searx/static/themes/oscar"
296+ build_msg INSTALL "($(pwd)) npm install"
299+ build_msg INSTALL "($(pwd)) npm install"
300+ cd "${REPO_ROOT}/searx/static/themes/simple"
310+ build_msg CLEAN "locally installed npm dependencies"
313+ ./package-lock.json \
314+ ./searx/static/themes/oscar/package-lock.json \
315+ ./searx/static/themes/oscar/node_modules \
316+ ./searx/static/themes/simple/package-lock.json \
317+ ./searx/static/themes/simple/node_modules
322+ build_msg BUILD "[pylint] python package ${PYDIST}"
323+ pyenv.cmd python setup.py \
324+ sdist -d "${PYDIST}" \
325+ bdist_wheel --bdist-dir "${PYBUILD}" -d "${PYDIST}"
329+ build_msg CLEAN pyenv
332+ [ "$VERBOSE" = "1" ] && set -x
333+ rm -rf "${PYDIST}" "${PYBUILD}" "${PY_ENV}" ./.tox ./*.egg-info
334+ find . -name '*.pyc' -exec rm -f {} +
335+ find . -name '*.pyo' -exec rm -f {} +
336+ find . -name __pycache__ -exec rm -rf {} +
343+print('import yaml --> OK')
350+ py.clean > /dev/null
352+ if pyenv.install.OK > /dev/null; then
357+ build_msg PYENV "[install] pip install -e 'searx${PY_SETUP_EXTRAS}'"
358+ "${PY_ENV_BIN}/python" -m pip install -e ".${PY_SETUP_EXTRAS}"
360+ ) || die 42 "error while build & install pyenv (${PY_ENV_BIN})"
364+ build_msg PYENV "[pyenv.uninstall] uninstall packages: ${PYOBJECTS}"
365+ pyenv.cmd python setup.py develop --uninstall 2>&1 \
366+ | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
373+ # https://github.com/pypa/twine
374+ pyenv.cmd twine upload "${PYDIST}"/*
380+ pyenv.cmd twine upload -r testpypi "${PYDIST}"/*
384+ # shellcheck disable=SC2086
386+ build_msg TEST "[pylint] \$PYLINT_FILES"
387+ pyenv.cmd python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
388+ "${PYLINT_FILES[@]}"
390+ build_msg TEST "[pylint] searx/engines"
391+ pyenv.cmd python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
392+ --disable="${PYLINT_SEARX_DISABLE_OPTION}" \
393+ --additional-builtins="${PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES}" \
396+ build_msg TEST "[pylint] searx tests"
397+ pyenv.cmd python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
398+ --disable="${PYLINT_SEARX_DISABLE_OPTION}" \
399+ --ignore=searx/engines \
406+ build_msg TEST 'pycodestyle (formerly pep8)'
408+ printf -v _exclude '%s, ' "${PYLINT_FILES[@]}"
409+ pyenv.cmd pycodestyle \
410+ --exclude="searx/static, searx/languages.py, $_exclude " \
411+ --max-line-length=120 \
412+ --ignore "E117,E252,E402,E722,E741,W503,W504,W605" \
418+ build_msg TEST 'tests/unit'
419+ pyenv.cmd python -m nose2 -s tests/unit
424+ build_msg TEST 'unit test coverage'
426+ pyenv.cmd python -m nose2 -C --log-capture --with-coverage --coverage searx -s tests/unit
427+ pyenv.cmd coverage report
428+ pyenv.cmd coverage html
434+ build_msg TEST 'robot'
436+ PYTHONPATH=. pyenv.cmd python searx/testing.py robot
441+ build_msg CLEAN "test stuff"
442+ rm -rf geckodriver.log .coverage coverage/
456+ local gruntfile=searx/static/themes/oscar/gruntfile.js
457+ build_msg GRUNT "${gruntfile}"
458+ PATH="$(npm bin):$PATH" grunt --gruntfile "${gruntfile}"
463+ local gruntfile=searx/static/themes/simple/gruntfile.js
464+ build_msg GRUNT "${gruntfile}"
465+ PATH="$(npm bin):$PATH" grunt --gruntfile "${gruntfile}"
469+# shellcheck disable=SC2119
473+ local cmd="$1"; shift
475+ if [ "$cmd" == "" ]; then
477+ err_msg "missing command"
482+ --getenv) var="$1"; echo "${!var}";;
486+ err_msg "unknown option $cmd"
490+ _type="$(type -t "$cmd")"
491+ if [ "$_type" != 'function' ]; then
492+ err_msg "unknown command $1 / use --help"