manage 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. # shellcheck disable=SC2031
  5. # shellcheck source=utils/lib.sh
  6. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib.sh"
  7. # shellcheck source=utils/brand.env
  8. source "${REPO_ROOT}/utils/brand.env"
  9. source_dot_config
  10. # shellcheck source=utils/lib_static.sh
  11. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_static.sh"
  12. # config
  13. PYOBJECTS="searx"
  14. PY_SETUP_EXTRAS='[test]'
  15. GECKODRIVER_VERSION="v0.28.0"
  16. # SPHINXOPTS=
  17. pylint.FILES() {
  18. # List files tagged by comment:
  19. #
  20. # # lint: pylint
  21. #
  22. # These py files are linted by test.pylint(), all other files are linted by
  23. # test.pep8()
  24. grep -l -r --include \*.py '^#[[:blank:]]*lint:[[:blank:]]*pylint' searx searx_extra tests
  25. }
  26. YAMLLINT_FILES=()
  27. while IFS= read -r line; do
  28. YAMLLINT_FILES+=("$line")
  29. done <<< "$(git ls-files './tests/*.yml' './searx/*.yml' './utils/templates/etc/searx/*.yml')"
  30. PYLINT_SEARX_DISABLE_OPTION="\
  31. I,C,R,\
  32. W0105,W0212,W0511,W0603,W0613,W0621,W0702,W0703,W1401,\
  33. E1136"
  34. PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES="supported_languages,language_aliases"
  35. PYLINT_OPTIONS="-m pylint -j 0 --rcfile .pylintrc"
  36. help() {
  37. cat <<EOF
  38. buildenv:
  39. rebuild ./utils/brand.env
  40. babel.compile:
  41. pybabel compile ./searx/translations
  42. data.:
  43. all : update searx/languages.py and ./data/*
  44. languages : update searx/data/engines_languages.json & searx/languages.py
  45. useragents: update searx/data/useragents.json with the most recent versions of Firefox.
  46. docs.:
  47. html : build HTML documentation
  48. live : autobuild HTML documentation while editing
  49. gh-pages : deploy on gh-pages branch
  50. prebuild : build reST include files (./${DOCS_BUILD}/includes)
  51. clean : clean documentation build
  52. docker.:
  53. build : build docker image
  54. push : build and push docker image
  55. gecko.driver:
  56. download & install geckodriver if not already installed (required for
  57. robot_tests)
  58. node.:
  59. env : download & install npm dependencies locally
  60. clean : drop npm installations
  61. py.:
  62. build : Build python packages at ./${PYDIST}
  63. clean : delete virtualenv and intermediate py files
  64. pyenv.:
  65. install : developer install of searx into virtualenv
  66. uninstall : uninstall developer installation
  67. cmd ... : run command ... in virtualenv
  68. OK : test if virtualenv is OK
  69. pypi.upload:
  70. Upload python packages to PyPi (to test use pypi.upload.test)
  71. test.:
  72. pylint : lint PYLINT_FILES, searx/engines, searx & tests
  73. pep8 : pycodestyle (pep8) for all files except PYLINT_FILES
  74. unit : run unit tests
  75. coverage : run unit tests with coverage
  76. robot : run robot test
  77. clean : clean intermediate test stuff
  78. themes.:
  79. all : build all themes
  80. oscar : build oscar theme
  81. simple : build simple theme
  82. pygments.:
  83. less : build LESS files for pygments
  84. EOF
  85. static_help
  86. }
  87. if [ "$VERBOSE" = "1" ]; then
  88. SPHINX_VERBOSE="-v"
  89. PYLINT_VERBOSE="-v"
  90. fi
  91. # needed by sphinx-docs
  92. export DOCS_BUILD
  93. buildenv() {
  94. # settings file from repository's working tree are used by default
  95. SEARX_SETTINGS_PATH="${REPO_ROOT}/searx/settings.yml"
  96. if [ -r '/etc/searx/settings.yml' ]; then
  97. if ask_yn "should settings read from: /etc/searx/settings.yml"; then
  98. SEARX_SETTINGS_PATH='/etc/searx/settings.yml'
  99. fi
  100. fi
  101. export SEARX_SETTINGS_PATH
  102. SEARX_DEBUG=1 pyenv.cmd python utils/build_env.py 2>&1 \
  103. | prefix_stdout "${_Blue}BUILDENV${_creset} "
  104. return "${PIPESTATUS[0]}"
  105. }
  106. buildenv.unset_env(){
  107. # Some defaults in the settings.yml are taken from the environment,
  108. # e.g. SEARX_BIND_ADDRESS (:py:obj:`searx.settings_defaults.SHEMA`). In
  109. # some tasks (e.g. test.robot) we do not want these envorionment applied.
  110. unset GIT_URL
  111. unset GIT_BRANCH
  112. unset SEARX_URL
  113. unset SEARX_PORT
  114. unset SEARX_BIND_ADDRESS
  115. }
  116. babel.compile() {
  117. build_msg BABEL compile
  118. pyenv.cmd pybabel compile -d "${REPO_ROOT}/searx/translations"
  119. dump_return $?
  120. }
  121. data.all() {
  122. data.languages
  123. data.useragents
  124. data.osm_keys_tags
  125. build_msg DATA "update searx/data/ahmia_blacklist.txt"
  126. pyenv.cmd python searx_extra/update/update_ahmia_blacklist.py
  127. build_msg DATA "update searx/data/wikidata_units.json"
  128. pyenv.cmd python searx_extra/update/update_wikidata_units.py
  129. build_msg DATA "update searx/data/currencies.json"
  130. pyenv.cmd python searx_extra/update/update_currencies.py
  131. }
  132. data.languages() {
  133. ( set -e
  134. build_msg ENGINES "fetch languages .."
  135. pyenv.cmd python searx_extra/update/update_languages.py
  136. build_msg ENGINES "update update searx/languages.py"
  137. build_msg DATA "update searx/data/engines_languages.json"
  138. )
  139. dump_return $?
  140. }
  141. data.useragents() {
  142. build_msg DATA "update searx/data/useragents.json"
  143. pyenv.cmd python searx_extra/update/update_firefox_version.py
  144. dump_return $?
  145. }
  146. data.osm_keys_tags() {
  147. build_msg DATA "update searx/data/osm_keys_tags.json"
  148. pyenv.cmd python searx_extra/update/update_osm_keys_tags.py
  149. dump_return $?
  150. }
  151. docs.prebuild() {
  152. build_msg DOCS "build ${DOCS_BUILD}/includes"
  153. (
  154. set -e
  155. [ "$VERBOSE" = "1" ] && set -x
  156. mkdir -p "${DOCS_BUILD}/includes"
  157. ./utils/searx.sh doc | cat > "${DOCS_BUILD}/includes/searx.rst"
  158. ./utils/filtron.sh doc | cat > "${DOCS_BUILD}/includes/filtron.rst"
  159. ./utils/morty.sh doc | cat > "${DOCS_BUILD}/includes/morty.rst"
  160. )
  161. dump_return $?
  162. }
  163. docker.push() {
  164. docker.build push
  165. }
  166. docker.buildx() {
  167. docker.build buildx
  168. }
  169. # shellcheck disable=SC2119
  170. docker.build() {
  171. pyenv.install
  172. local SEARX_GIT_VERSION
  173. local VERSION_GITCOMMIT
  174. local SEARX_PYTHON_VERSION
  175. local GITHUB_USER
  176. local SEARX_IMAGE_NAME
  177. local BUILD
  178. build_msg DOCKER build
  179. # run installation in a subprocess and activate pyenv
  180. # See https://www.shellcheck.net/wiki/SC1001 and others ..
  181. # shellcheck disable=SC2031,SC2230,SC2002,SC2236,SC2143,SC1001
  182. ( set -e
  183. # shellcheck source=/dev/null
  184. source "${PY_ENV_BIN}/activate"
  185. # Check if it is a git repository
  186. if [ ! -d .git ]; then
  187. die 1 "This is not Git repository"
  188. fi
  189. if [ ! -x "$(which git)" ]; then
  190. die 1 "git is not installed"
  191. fi
  192. if ! git remote get-url origin 2> /dev/null; then
  193. die 1 "there is no remote origin"
  194. fi
  195. # This is a git repository
  196. # "git describe" to get the Docker version (for example : v0.15.0-89-g0585788e)
  197. # awk to remove the "v" and the "g"
  198. 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}')
  199. # add the suffix "-dirty" if the repository has uncommited change
  200. # /!\ HACK for searx/searx: ignore utils/brand.env
  201. git update-index -q --refresh
  202. if [ ! -z "$(git diff-index --name-only HEAD -- | grep -v 'utils/brand.env')" ]; then
  203. SEARX_GIT_VERSION="${SEARX_GIT_VERSION}-dirty"
  204. fi
  205. # Get the last git commit id, will be added to the Searx version (see Dockerfile)
  206. VERSION_GITCOMMIT=$(echo "$SEARX_GIT_VERSION" | cut -d- -f2-4)
  207. build_msg DOCKER "Last commit : $VERSION_GITCOMMIT"
  208. # Check consistency between the git tag and the searx/version.py file
  209. # /! HACK : parse Python file with bash /!
  210. # otherwise it is not possible build the docker image without all Python
  211. # dependencies ( version.py loads __init__.py )
  212. # SEARX_PYTHON_VERSION=$(python3 -c "import six; import searx.version; six.print_(searx.version.VERSION_STRING)")
  213. SEARX_PYTHON_VERSION=$(cat searx/version.py | grep "\(VERSION_MAJOR\|VERSION_MINOR\|VERSION_BUILD\) =" | cut -d\= -f2 | sed -e 's/^[[:space:]]*//' | paste -sd "." -)
  214. if [ "$(echo "$SEARX_GIT_VERSION" | cut -d- -f1)" != "$SEARX_PYTHON_VERSION" ]; then
  215. err_msg "git tag: $SEARX_GIT_VERSION"
  216. err_msg "searx/version.py: $SEARX_PYTHON_VERSION"
  217. die 1 "Inconsistency between the last git tag and the searx/version.py file"
  218. fi
  219. # define the docker image name
  220. GITHUB_USER=$(echo "${GIT_URL}" | sed 's/.*github\.com\/\([^\/]*\).*/\1/')
  221. SEARX_IMAGE_NAME="${SEARX_IMAGE_NAME:-${GITHUB_USER:-searxng}/searxng}"
  222. BUILD="build"
  223. if [ "$1" = "buildx" ]; then
  224. # buildx includes the push option
  225. CACHE_TAG="${SEARX_IMAGE_NAME}:latest-build-cache"
  226. BUILD="buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 --push --cache-from=type=registry,ref=$CACHE_TAG --cache-to=type=registry,ref=$CACHE_TAG,mode=max"
  227. shift
  228. fi
  229. build_msg DOCKER "Build command: ${BUILD}"
  230. # build Docker image
  231. build_msg DOCKER "Building image ${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}"
  232. # shellcheck disable=SC2086
  233. docker $BUILD \
  234. --build-arg BASE_IMAGE="${DEPENDENCIES_IMAGE_NAME}" \
  235. --build-arg GIT_URL="${GIT_URL}" \
  236. --build-arg SEARX_GIT_VERSION="${SEARX_GIT_VERSION}" \
  237. --build-arg VERSION_GITCOMMIT="${VERSION_GITCOMMIT}" \
  238. --build-arg LABEL_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
  239. --build-arg LABEL_VCS_REF="$(git rev-parse HEAD)" \
  240. --build-arg LABEL_VCS_URL="${GIT_URL}" \
  241. --build-arg TIMESTAMP_SETTINGS="$(git log -1 --format="%cd" --date=unix -- searx/settings.yml)" \
  242. --build-arg TIMESTAMP_UWSGI="$(git log -1 --format="%cd" --date=unix -- dockerfiles/uwsgi.ini)" \
  243. -t "${SEARX_IMAGE_NAME}:latest" -t "${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}" .
  244. if [ "$1" = "push" ]; then
  245. docker push "${SEARX_IMAGE_NAME}:latest"
  246. docker push "${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}"
  247. fi
  248. )
  249. dump_return $?
  250. }
  251. # shellcheck disable=SC2119
  252. gecko.driver() {
  253. pyenv.install
  254. build_msg INSTALL "gecko.driver"
  255. # run installation in a subprocess and activate pyenv
  256. ( set -e
  257. # shellcheck source=/dev/null
  258. source "${PY_ENV_BIN}/activate"
  259. # TODO : check the current geckodriver version
  260. geckodriver -V > /dev/null 2>&1 || NOTFOUND=1
  261. set +e
  262. if [ -z "$NOTFOUND" ]; then
  263. build_msg INSTALL "geckodriver already installed"
  264. return
  265. fi
  266. PLATFORM="$(python3 -c 'import platform; print(platform.system().lower(), platform.architecture()[0])')"
  267. case "$PLATFORM" in
  268. "linux 32bit" | "linux2 32bit") ARCH="linux32";;
  269. "linux 64bit" | "linux2 64bit") ARCH="linux64";;
  270. "windows 32 bit") ARCH="win32";;
  271. "windows 64 bit") ARCH="win64";;
  272. "mac 64bit") ARCH="macos";;
  273. esac
  274. GECKODRIVER_URL="https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-$ARCH.tar.gz";
  275. build_msg GECKO "Installing ${PY_ENV_BIN}/geckodriver from $GECKODRIVER_URL"
  276. FILE="$(mktemp)"
  277. wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "${PY_ENV_BIN}" -f "$FILE" geckodriver
  278. rm -- "$FILE"
  279. chmod 755 -- "${PY_ENV_BIN}/geckodriver"
  280. )
  281. dump_return $?
  282. }
  283. node.env() {
  284. if ! required_commands npm; then
  285. info_msg "to install build tools use::"
  286. info_msg " sudo -H ./utils/searx.sh install buildhost"
  287. die 1 "install needed build tools first"
  288. fi
  289. ( set -e
  290. build_msg INSTALL "searx/static/themes/oscar/package.json"
  291. npm --prefix searx/static/themes/oscar install
  292. build_msg INSTALL "searx/static/themes/simple/package.json"
  293. npm --prefix searx/static/themes/simple install
  294. )
  295. dump_return $?
  296. }
  297. node.clean() {
  298. if ! required_commands npm 2>/dev/null; then
  299. build_msg CLEAN "npm is not installed / ignore npm dependencies"
  300. return 0
  301. fi
  302. build_msg CLEAN "locally installed npm dependencies"
  303. ( set -e
  304. npm --prefix searx/static/themes/oscar run clean
  305. npm --prefix searx/static/themes/simple run clean
  306. )
  307. dump_return $?
  308. }
  309. pygments.less() {
  310. build_msg PYGMENTS "searx_extra/update/update_pygments.py"
  311. if ! pyenv.cmd python searx_extra/update/update_pygments.py; then
  312. build_msg PYGMENTS "building LESS files for pygments failed"
  313. return 1
  314. fi
  315. return 0
  316. }
  317. py.build() {
  318. build_msg BUILD "python package ${PYDIST}"
  319. pyenv.cmd python setup.py \
  320. sdist -d "${PYDIST}" \
  321. bdist_wheel --bdist-dir "${PYBUILD}" -d "${PYDIST}"
  322. }
  323. py.clean() {
  324. build_msg CLEAN pyenv
  325. ( set -e
  326. pyenv.drop
  327. [ "$VERBOSE" = "1" ] && set -x
  328. rm -rf "${PYDIST}" "${PYBUILD}" "${PY_ENV}" ./.tox ./*.egg-info
  329. find . -name '*.pyc' -exec rm -f {} +
  330. find . -name '*.pyo' -exec rm -f {} +
  331. find . -name __pycache__ -exec rm -rf {} +
  332. )
  333. }
  334. pyenv.check() {
  335. cat <<EOF
  336. import yaml
  337. print('import yaml --> OK')
  338. EOF
  339. }
  340. pyenv.install() {
  341. if ! pyenv.OK; then
  342. py.clean > /dev/null
  343. fi
  344. if pyenv.install.OK > /dev/null; then
  345. return 0
  346. fi
  347. ( set -e
  348. pyenv
  349. build_msg PYENV "[install] pip install -e 'searx${PY_SETUP_EXTRAS}'"
  350. "${PY_ENV_BIN}/python" -m pip install -e ".${PY_SETUP_EXTRAS}"
  351. buildenv
  352. )
  353. local exit_val=$?
  354. if [ ! $exit_val -eq 0 ]; then
  355. die 42 "error while pip install (${PY_ENV_BIN})"
  356. fi
  357. }
  358. pyenv.uninstall() {
  359. build_msg PYENV "[pyenv.uninstall] uninstall packages: ${PYOBJECTS}"
  360. pyenv.cmd python setup.py develop --uninstall 2>&1 \
  361. | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
  362. }
  363. pypi.upload() {
  364. py.clean
  365. py.build
  366. # https://github.com/pypa/twine
  367. pyenv.cmd twine upload "${PYDIST}"/*
  368. }
  369. pypi.upload.test() {
  370. py.clean
  371. py.build
  372. pyenv.cmd twine upload -r testpypi "${PYDIST}"/*
  373. }
  374. test.yamllint() {
  375. build_msg TEST "[yamllint] \$YAMLLINT_FILES"
  376. pyenv.cmd yamllint --format parsable "${YAMLLINT_FILES[@]}"
  377. }
  378. test.pylint() {
  379. # shellcheck disable=SC2086
  380. ( set -e
  381. build_msg TEST "[pylint] \$PYLINT_FILES"
  382. pyenv.cmd python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  383. "${PYLINT_FILES[@]}"
  384. build_msg TEST "[pylint] searx/engines"
  385. pyenv.cmd python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  386. --disable="${PYLINT_SEARX_DISABLE_OPTION}" \
  387. --additional-builtins="${PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES}" \
  388. searx/engines
  389. build_msg TEST "[pylint] searx tests"
  390. pyenv.cmd python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  391. --disable="${PYLINT_SEARX_DISABLE_OPTION}" \
  392. --ignore=searx/engines \
  393. searx tests
  394. )
  395. dump_return $?
  396. }
  397. test.pep8() {
  398. build_msg TEST 'pycodestyle (formerly pep8)'
  399. local _exclude=""
  400. printf -v _exclude '%s, ' "${PYLINT_FILES[@]}"
  401. pyenv.cmd pycodestyle \
  402. --exclude="searx/static, searx/languages.py, $_exclude " \
  403. --max-line-length=120 \
  404. --ignore "E117,E252,E402,E722,E741,W503,W504,W605" \
  405. searx tests
  406. dump_return $?
  407. }
  408. test.unit() {
  409. build_msg TEST 'tests/unit'
  410. pyenv.cmd python -m nose2 -s tests/unit
  411. dump_return $?
  412. }
  413. test.coverage() {
  414. build_msg TEST 'unit test coverage'
  415. ( set -e
  416. pyenv.cmd python -m nose2 -C --log-capture --with-coverage --coverage searx -s tests/unit
  417. pyenv.cmd coverage report
  418. pyenv.cmd coverage html
  419. )
  420. dump_return $?
  421. }
  422. test.robot() {
  423. build_msg TEST 'robot'
  424. buildenv.unset_env
  425. gecko.driver
  426. PYTHONPATH=. pyenv.cmd python searx/testing.py robot
  427. dump_return $?
  428. }
  429. test.clean() {
  430. build_msg CLEAN "test stuff"
  431. rm -rf geckodriver.log .coverage coverage/
  432. dump_return $?
  433. }
  434. themes.all() {
  435. ( set -e
  436. pygments.less
  437. node.env
  438. themes.oscar
  439. themes.simple
  440. )
  441. dump_return $?
  442. }
  443. themes.oscar() {
  444. build_msg GRUNT "theme: oscar"
  445. npm --prefix searx/static/themes/oscar run build
  446. dump_return $?
  447. }
  448. themes.simple() {
  449. build_msg GRUNT "theme: simple"
  450. npm --prefix searx/static/themes/simple run build
  451. dump_return $?
  452. }
  453. PYLINT_FILES=()
  454. while IFS= read -r line; do
  455. PYLINT_FILES+=("$line")
  456. done <<< "$(pylint.FILES)"
  457. # shellcheck disable=SC2119
  458. main() {
  459. local _type
  460. local cmd="$1"; shift
  461. if [ "$cmd" == "" ]; then
  462. help
  463. err_msg "missing command"
  464. return 42
  465. fi
  466. case "$cmd" in
  467. --getenv) var="$1"; echo "${!var}";;
  468. --help) help;;
  469. --*)
  470. help
  471. err_msg "unknown option $cmd"
  472. return 42
  473. ;;
  474. *)
  475. _type="$(type -t "$cmd")"
  476. if [ "$_type" != 'function' ]; then
  477. err_msg "unknown command: $cmd / use --help"
  478. return 42
  479. else
  480. "$cmd" "$@"
  481. fi
  482. ;;
  483. esac
  484. }
  485. main "$@"