manage 16 KB

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