manage 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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=SC2034
  5. main_cmd="$(basename "$0")"
  6. # shellcheck source=utils/lib.sh
  7. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib.sh"
  8. # shellcheck source=utils/lib.sh
  9. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_nvm.sh"
  10. # shellcheck source=utils/lib_static.sh
  11. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_static.sh"
  12. # shellcheck source=utils/lib_go.sh
  13. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_go.sh"
  14. # shellcheck source=utils/lib_redis.sh
  15. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_redis.sh"
  16. PATH="${REPO_ROOT}/node_modules/.bin:${PATH}"
  17. # config
  18. PYOBJECTS="searx"
  19. PY_SETUP_EXTRAS='[test]'
  20. GECKODRIVER_VERSION="v0.30.0"
  21. export NODE_MINIMUM_VERSION="16.13.0"
  22. # SPHINXOPTS=
  23. BLACK_OPTIONS=("--target-version" "py37" "--line-length" "120" "--skip-string-normalization")
  24. BLACK_TARGETS=("--exclude" "searx/static,searx/languages.py" "--include" 'searxng.msg|\.pyi?$' "searx" "searxng_extra" "tests")
  25. _dev_redis_sock="/usr/local/searxng-redis/run/redis.sock"
  26. # set SEARXNG_REDIS_URL if it is not defined and "{_dev_redis_sock}" exists.
  27. if [ -S "${_dev_redis_sock}" ] && [ -z "${SEARXNG_REDIS_URL}" ]; then
  28. export SEARXNG_REDIS_URL="unix://${_dev_redis_sock}?db=0"
  29. fi
  30. pylint.FILES() {
  31. # List files tagged by comment:
  32. #
  33. # # lint: pylint
  34. #
  35. # These py files are linted by test.pylint()
  36. grep -l -r --include \*.py '^#[[:blank:]]*lint:[[:blank:]]*pylint' searx searxng_extra tests
  37. find . -name searxng.msg
  38. }
  39. YAMLLINT_FILES=()
  40. while IFS= read -r line; do
  41. YAMLLINT_FILES+=("$line")
  42. done <<< "$(git ls-files './tests/*.yml' './searx/*.yml' './utils/templates/etc/searxng/*.yml')"
  43. RST_FILES=(
  44. 'README.rst'
  45. )
  46. PYLINT_SEARXNG_DISABLE_OPTION="\
  47. I,C,R,\
  48. W0105,W0212,W0511,W0603,W0613,W0621,W0702,W0703,W1401,\
  49. E1136"
  50. PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES="supported_languages,language_aliases,logger,categories"
  51. PYLINT_OPTIONS="-m pylint -j 0 --rcfile .pylintrc"
  52. help() {
  53. nvm.help
  54. cat <<EOF
  55. buildenv:
  56. rebuild ./utils/brand.env
  57. webapp.:
  58. run : run developer instance
  59. weblate.:
  60. push.translations: push translation changes from SearXNG to Weblate's counterpart
  61. to.translations: Update 'translations' branch with last additions from Weblate.
  62. data.:
  63. all : update searx/languages.py and ./data/*
  64. languages : update searx/data/engines_languages.json & searx/languages.py
  65. useragents: update searx/data/useragents.json with the most recent versions of Firefox.
  66. docs.:
  67. html : build HTML documentation
  68. live : autobuild HTML documentation while editing
  69. gh-pages : deploy on gh-pages branch
  70. prebuild : build reST include files (./${DOCS_BUILD}/includes)
  71. clean : clean documentation build
  72. docker.:
  73. build : build docker image
  74. push : build and push docker image
  75. gecko.driver:
  76. download & install geckodriver if not already installed (required for
  77. robot_tests)
  78. redis:
  79. build : build redis binaries at $(redis._get_dist)
  80. install : create user (${REDIS_USER}) and install systemd service (${REDIS_SERVICE_NAME})
  81. help : show more redis commands
  82. node.:
  83. env : download & install npm dependencies locally
  84. clean : drop locally npm installations
  85. py.:
  86. build : Build python packages at ./${PYDIST}
  87. clean : delete virtualenv and intermediate py files
  88. pyenv.:
  89. install : developer install of SearXNG into virtualenv
  90. uninstall : uninstall developer installation
  91. cmd ... : run command ... in virtualenv
  92. OK : test if virtualenv is OK
  93. pypi.upload:
  94. Upload python packages to PyPi (to test use pypi.upload.test)
  95. format.:
  96. python : format Python code source using black
  97. test.:
  98. yamllint : lint YAML files (YAMLLINT_FILES)
  99. pylint : lint PYLINT_FILES, searx/engines, searx & tests
  100. pyright : static type check of python sources
  101. black : check black code format
  102. unit : run unit tests
  103. coverage : run unit tests with coverage
  104. robot : run robot test
  105. rst : test .rst files incl. README.rst
  106. clean : clean intermediate test stuff
  107. themes.:
  108. all : build all themes
  109. simple : build simple theme
  110. pygments.:
  111. less : build LESS files for pygments
  112. EOF
  113. go.help
  114. static_help
  115. cat <<EOF
  116. environment ...
  117. SEARXNG_REDIS_URL : ${SEARXNG_REDIS_URL}
  118. EOF
  119. }
  120. if [ "$VERBOSE" = "1" ]; then
  121. SPHINX_VERBOSE="-v"
  122. PYLINT_VERBOSE="-v"
  123. fi
  124. # needed by sphinx-docs
  125. export DOCS_BUILD
  126. webapp.run() {
  127. local parent_proc="$$"
  128. (
  129. if [ "${LIVE_THEME}" ]; then
  130. ( themes.live "${LIVE_THEME}" )
  131. kill $parent_proc
  132. fi
  133. )&
  134. (
  135. sleep 3
  136. xdg-open http://127.0.0.1:8888/
  137. )&
  138. SEARXNG_DEBUG=1 pyenv.cmd python -m searx.webapp
  139. }
  140. buildenv() {
  141. # settings file from repository's working tree are used by default
  142. SEARXNG_SETTINGS_PATH="${REPO_ROOT}/searx/settings.yml"
  143. if [ -f /etc/searx/settings.yml ]; then
  144. err_msg "settings.yml in /etc/searx/ is deprecated, move file to folder /etc/searxng/"
  145. fi
  146. if [ -r '/etc/searxng/settings.yml' ]; then
  147. if ask_yn "should settings read from: /etc/searxng/settings.yml"; then
  148. SEARXNG_SETTINGS_PATH='/etc/searxng/settings.yml'
  149. fi
  150. fi
  151. export SEARXNG_SETTINGS_PATH
  152. (
  153. set -e
  154. SEARXNG_DEBUG=1 pyenv.cmd python utils/build_env.py 2>&1 \
  155. | prefix_stdout "${_Blue}BUILDENV${_creset} "
  156. )
  157. return "${PIPESTATUS[0]}"
  158. }
  159. TRANSLATIONS_WORKTREE="$CACHE/translations"
  160. weblate.translations.worktree() {
  161. # Create git worktree ${TRANSLATIONS_WORKTREE} and checkout branch
  162. # 'translations' from Weblate's counterpart (weblate) of the SearXNG
  163. # (origin).
  164. #
  165. # remote weblate https://translate.codeberg.org/git/searxng/searxng/
  166. ( set -e
  167. if ! git remote get-url weblate 2> /dev/null; then
  168. git remote add weblate https://translate.codeberg.org/git/searxng/searxng/
  169. fi
  170. if [ -d "${TRANSLATIONS_WORKTREE}" ]; then
  171. pushd .
  172. cd "${TRANSLATIONS_WORKTREE}"
  173. git reset --hard HEAD
  174. git pull origin translations
  175. popd
  176. else
  177. mkdir -p "${TRANSLATIONS_WORKTREE}"
  178. git worktree add "${TRANSLATIONS_WORKTREE}" translations
  179. fi
  180. )
  181. }
  182. weblate.to.translations() {
  183. # Update 'translations' branch of SearXNG (origin) with last additions from
  184. # Weblate.
  185. # 1. Check if Weblate is locked, if not die with error message
  186. # 2. On Weblate's counterpart (weblate), pull master and translations branch
  187. # from SearXNG (origin).
  188. # 3. Commit changes made in a Weblate object on Weblate's counterpart
  189. # (weblate).
  190. # 4. In translations worktree, merge changes of branch 'translations' from
  191. # remote 'weblate' and push it on branch 'translations' of 'origin'
  192. ( set -e
  193. pyenv.activate
  194. if [ "$(wlc lock-status)" != "locked: True" ]; then
  195. die 1 "weblate must be locked, currently: $(wlc lock-status)"
  196. fi
  197. # weblate: commit pending changes
  198. wlc pull
  199. wlc commit
  200. # get the translations in a worktree
  201. weblate.translations.worktree
  202. pushd "${TRANSLATIONS_WORKTREE}"
  203. git remote update weblate
  204. git merge weblate/translations
  205. git push
  206. popd
  207. )
  208. dump_return $?
  209. }
  210. weblate.translations.commit() {
  211. # Update 'translations' branch of SearXNG (origin) with last additions from
  212. # Weblate. Copy the changes to the master branch, compile translations and
  213. # create a commit in the local branch (master)
  214. local existing_commit_hash commit_body commit_message exitcode
  215. ( set -e
  216. pyenv.activate
  217. # lock change on weblate
  218. wlc lock
  219. # get translations branch in git worktree (TRANSLATIONS_WORKTREE)
  220. weblate.translations.worktree
  221. existing_commit_hash=$(cd "${TRANSLATIONS_WORKTREE}"; git log -n1 --pretty=format:'%h')
  222. # pull weblate commits
  223. weblate.to.translations
  224. # copy the changes to the master branch
  225. cp -rv --preserve=mode,timestamps "${TRANSLATIONS_WORKTREE}/searx/translations" "searx"
  226. # compile translations
  227. build_msg BABEL 'compile translation catalogs into binary MO files'
  228. pybabel compile --statistics \
  229. -d "searx/translations"
  230. # git add/commit (no push)
  231. commit_body=$(cd "${TRANSLATIONS_WORKTREE}"; git log --pretty=format:'%h - %as - %aN <%ae>' "${existing_commit_hash}..HEAD")
  232. commit_message=$(echo -e "[translations] update from Weblate\n\n${commit_body}")
  233. git add searx/translations
  234. git commit -m "${commit_message}"
  235. )
  236. exitcode=$?
  237. ( # make sure to always unlock weblate
  238. set -e
  239. pyenv.cmd wlc unlock
  240. )
  241. dump_return $exitcode
  242. }
  243. weblate.push.translations() {
  244. # Push *translation changes* from SearXNG (origin) to Weblate's counterpart
  245. # (weblate).
  246. # In branch master of SearXNG (origin) check for meaningful changes in
  247. # folder 'searx/translations', commit changes on branch 'translations' and
  248. # at least, pull updated branches on Weblate's counterpart (weblate).
  249. # 1. Create git worktree ${TRANSLATIONS_WORKTREE} and checkout branch
  250. # 'translations' from remote 'weblate'.
  251. # 2. Stop if there is no meaningful change in the 'master' branch (origin),
  252. # compared to the 'translations' branch (weblate), otherwise ...
  253. # 3. Update 'translations' branch of SearXNG (origin) with last additions
  254. # from Weblate.
  255. # 5. Notify Weblate to pull updated 'master' & 'translations' branch.
  256. local messages_pot diff_messages_pot last_commit_hash last_commit_detail \
  257. exitcode
  258. messages_pot="${TRANSLATIONS_WORKTREE}/searx/translations/messages.pot"
  259. ( set -e
  260. pyenv.activate
  261. # get translations branch in git worktree (TRANSLATIONS_WORKTREE)
  262. weblate.translations.worktree
  263. # update messages.pot in the master branch
  264. build_msg BABEL 'extract messages from source files and generate POT file'
  265. pybabel extract -F babel.cfg \
  266. -o "${messages_pot}" \
  267. "searx/"
  268. # stop if there is no meaningful change in the master branch
  269. diff_messages_pot=$(cd "${TRANSLATIONS_WORKTREE}";\
  270. git diff -- "searx/translations/messages.pot")
  271. if ! echo "$diff_messages_pot" | grep -qE "[\+\-](msgid|msgstr)"; then
  272. build_msg BABEL 'no changes detected, exiting'
  273. return 42
  274. fi
  275. return 0
  276. )
  277. exitcode=$?
  278. if [ "$exitcode" -eq 42 ]; then
  279. return 0
  280. fi
  281. if [ "$exitcode" -gt 0 ]; then
  282. return $exitcode
  283. fi
  284. (
  285. set -e
  286. pyenv.activate
  287. # lock change on weblate
  288. # weblate may add commit(s) since the call to "weblate.translations.worktree".
  289. # this is not a problem because after this line, "weblate.to.translations"
  290. # calls again "weblate.translations.worktree" which calls "git pull"
  291. wlc lock
  292. # save messages.pot in the translations branch for later
  293. pushd "${TRANSLATIONS_WORKTREE}"
  294. git stash push
  295. popd
  296. # merge weblate commits into the translations branch
  297. weblate.to.translations
  298. # restore messages.pot in the translations branch
  299. pushd "${TRANSLATIONS_WORKTREE}"
  300. git stash pop
  301. popd
  302. # update messages.po files in the master branch
  303. build_msg BABEL 'update existing message catalogs from POT file'
  304. pybabel update -N \
  305. -i "${messages_pot}" \
  306. -d "${TRANSLATIONS_WORKTREE}/searx/translations"
  307. # git add/commit/push
  308. last_commit_hash=$(git log -n1 --pretty=format:'%h')
  309. last_commit_detail=$(git log -n1 --pretty=format:'%h - %as - %aN <%ae>' "${last_commit_hash}")
  310. pushd "${TRANSLATIONS_WORKTREE}"
  311. git add searx/translations
  312. git commit \
  313. -m "[translations] update messages.pot and messages.po files" \
  314. -m "From ${last_commit_detail}"
  315. git push
  316. popd
  317. # notify weblate to pull updated master & translations branch
  318. wlc pull
  319. )
  320. exitcode=$?
  321. ( # make sure to always unlock weblate
  322. set -e
  323. pyenv.activate
  324. wlc unlock
  325. )
  326. dump_return $exitcode
  327. }
  328. data.all() {
  329. ( set -e
  330. pyenv.activate
  331. data.languages
  332. data.useragents
  333. data.osm_keys_tags
  334. build_msg DATA "update searx/data/ahmia_blacklist.txt"
  335. python searxng_extra/update/update_ahmia_blacklist.py
  336. build_msg DATA "update searx/data/wikidata_units.json"
  337. python searxng_extra/update/update_wikidata_units.py
  338. build_msg DATA "update searx/data/currencies.json"
  339. python searxng_extra/update/update_currencies.py
  340. )
  341. }
  342. data.languages() {
  343. ( set -e
  344. pyenv.activate
  345. build_msg ENGINES "fetch languages .."
  346. python searxng_extra/update/update_languages.py
  347. build_msg ENGINES "update update searx/languages.py"
  348. build_msg DATA "update searx/data/engines_languages.json"
  349. )
  350. dump_return $?
  351. }
  352. data.useragents() {
  353. build_msg DATA "update searx/data/useragents.json"
  354. pyenv.cmd python searxng_extra/update/update_firefox_version.py
  355. dump_return $?
  356. }
  357. data.osm_keys_tags() {
  358. build_msg DATA "update searx/data/osm_keys_tags.json"
  359. pyenv.cmd python searxng_extra/update/update_osm_keys_tags.py
  360. dump_return $?
  361. }
  362. docs.prebuild() {
  363. build_msg DOCS "build ${DOCS_BUILD}/includes"
  364. (
  365. set -e
  366. [ "$VERBOSE" = "1" ] && set -x
  367. mkdir -p "${DOCS_BUILD}/includes"
  368. ./utils/searxng.sh searxng.doc.rst > "${DOCS_BUILD}/includes/searxng.rst"
  369. pyenv.cmd searxng_extra/docs_prebuild
  370. )
  371. dump_return $?
  372. }
  373. docker.push() {
  374. docker.build push
  375. }
  376. docker.buildx() {
  377. docker.build buildx
  378. }
  379. # shellcheck disable=SC2119
  380. docker.build() {
  381. pyenv.install
  382. local SEARXNG_GIT_VERSION
  383. local VERSION_GITCOMMIT
  384. local GITHUB_USER
  385. local SEARXNG_IMAGE_NAME
  386. local BUILD
  387. build_msg DOCKER build
  388. # run installation in a subprocess and activate pyenv
  389. # See https://www.shellcheck.net/wiki/SC1001 and others ..
  390. # shellcheck disable=SC2031,SC2230,SC2002,SC2236,SC2143,SC1001
  391. ( set -e
  392. pyenv.activate
  393. # Check if it is a git repository
  394. if [ ! -d .git ]; then
  395. die 1 "This is not Git repository"
  396. fi
  397. if [ ! -x "$(which git)" ]; then
  398. die 1 "git is not installed"
  399. fi
  400. if ! git remote get-url origin 2> /dev/null; then
  401. die 1 "there is no remote origin"
  402. fi
  403. # This is a git repository
  404. git update-index -q --refresh
  405. python -m searx.version freeze
  406. eval "$(python -m searx.version)"
  407. # Get the last git commit id
  408. VERSION_GITCOMMIT=$(echo "$VERSION_TAG" | cut -d+ -f2)
  409. build_msg DOCKER "Last commit : $VERSION_GITCOMMIT"
  410. # define the docker image name
  411. GITHUB_USER=$(echo "${GIT_URL}" | sed 's/.*github\.com\/\([^\/]*\).*/\1/')
  412. SEARXNG_IMAGE_NAME="${SEARXNG_IMAGE_NAME:-${GITHUB_USER:-searxng}/searxng}"
  413. BUILD="build"
  414. if [ "$1" = "buildx" ]; then
  415. # buildx includes the push option
  416. CACHE_TAG="${SEARXNG_IMAGE_NAME}:latest-build-cache"
  417. 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"
  418. shift
  419. fi
  420. build_msg DOCKER "Build command: ${BUILD}"
  421. # build Docker image
  422. build_msg DOCKER "Building image ${SEARXNG_IMAGE_NAME}:${SEARXNG_GIT_VERSION}"
  423. # shellcheck disable=SC2086
  424. docker $BUILD \
  425. --build-arg BASE_IMAGE="${DEPENDENCIES_IMAGE_NAME}" \
  426. --build-arg GIT_URL="${GIT_URL}" \
  427. --build-arg SEARXNG_DOCKER_TAG="${DOCKER_TAG}" \
  428. --build-arg SEARXNG_GIT_VERSION="${VERSION_STRING}" \
  429. --build-arg VERSION_GITCOMMIT="${VERSION_GITCOMMIT}" \
  430. --build-arg LABEL_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
  431. --build-arg LABEL_VCS_REF="$(git rev-parse HEAD)" \
  432. --build-arg LABEL_VCS_URL="${GIT_URL}" \
  433. --build-arg TIMESTAMP_SETTINGS="$(git log -1 --format="%cd" --date=unix -- searx/settings.yml)" \
  434. --build-arg TIMESTAMP_UWSGI="$(git log -1 --format="%cd" --date=unix -- dockerfiles/uwsgi.ini)" \
  435. -t "${SEARXNG_IMAGE_NAME}:latest" -t "${SEARXNG_IMAGE_NAME}:${DOCKER_TAG}" .
  436. if [ "$1" = "push" ]; then
  437. docker push "${SEARXNG_IMAGE_NAME}:latest"
  438. docker push "${SEARXNG_IMAGE_NAME}:${DOCKER_TAG}"
  439. fi
  440. )
  441. dump_return $?
  442. }
  443. # shellcheck disable=SC2119
  444. gecko.driver() {
  445. pyenv.install
  446. build_msg INSTALL "gecko.driver"
  447. # run installation in a subprocess and activate pyenv
  448. ( set -e
  449. pyenv.activate
  450. INSTALLED_VERSION=$(geckodriver -V 2> /dev/null | head -1 | awk '{ print "v" $2}') || INSTALLED_VERSION=""
  451. set +e
  452. if [ "${INSTALLED_VERSION}" = "${GECKODRIVER_VERSION}" ]; then
  453. build_msg INSTALL "geckodriver already installed"
  454. return
  455. fi
  456. PLATFORM="$(python3 -c 'import platform; print(platform.system().lower(), platform.architecture()[0])')"
  457. case "$PLATFORM" in
  458. "linux 32bit" | "linux2 32bit") ARCH="linux32";;
  459. "linux 64bit" | "linux2 64bit") ARCH="linux64";;
  460. "windows 32 bit") ARCH="win32";;
  461. "windows 64 bit") ARCH="win64";;
  462. "mac 64bit") ARCH="macos";;
  463. esac
  464. GECKODRIVER_URL="https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-$ARCH.tar.gz";
  465. build_msg GECKO "Installing ${PY_ENV_BIN}/geckodriver from $GECKODRIVER_URL"
  466. FILE="$(mktemp)"
  467. wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "${PY_ENV_BIN}" -f "$FILE" geckodriver
  468. rm -- "$FILE"
  469. chmod 755 -- "${PY_ENV_BIN}/geckodriver"
  470. )
  471. dump_return $?
  472. }
  473. nodejs.ensure() {
  474. if ! nvm.min_node "${NODE_MINIMUM_VERSION}"; then
  475. info_msg "install Node.js by NVM"
  476. nvm.nodejs
  477. fi
  478. }
  479. node.env() {
  480. nodejs.ensure
  481. ( set -e
  482. build_msg INSTALL "searx/static/themes/simple/package.json"
  483. npm --prefix searx/static/themes/simple install
  484. )
  485. dump_return $?
  486. }
  487. node.env.devtools() {
  488. nodejs.ensure
  489. build_msg INSTALL "package.json: developer and CI tools"
  490. npm install
  491. }
  492. node.clean() {
  493. if ! required_commands npm 2>/dev/null; then
  494. build_msg CLEAN "npm is not installed / ignore npm dependencies"
  495. return 0
  496. fi
  497. build_msg CLEAN "themes -- locally installed npm dependencies"
  498. ( set -e
  499. npm --prefix searx/static/themes/simple run clean
  500. )
  501. dump_return $?
  502. }
  503. pygments.less() {
  504. build_msg PYGMENTS "searxng_extra/update/update_pygments.py"
  505. if ! pyenv.cmd python searxng_extra/update/update_pygments.py; then
  506. build_msg PYGMENTS "building LESS files for pygments failed"
  507. return 1
  508. fi
  509. return 0
  510. }
  511. py.build() {
  512. build_msg BUILD "python package ${PYDIST}"
  513. pyenv.cmd python setup.py \
  514. sdist -d "${PYDIST}" \
  515. bdist_wheel --bdist-dir "${PYBUILD}" -d "${PYDIST}"
  516. }
  517. py.clean() {
  518. build_msg CLEAN pyenv
  519. ( set -e
  520. pyenv.drop
  521. [ "$VERBOSE" = "1" ] && set -x
  522. rm -rf "${PYDIST}" "${PYBUILD}" "${PY_ENV}" ./.tox ./*.egg-info
  523. find . -name '*.pyc' -exec rm -f {} +
  524. find . -name '*.pyo' -exec rm -f {} +
  525. find . -name __pycache__ -exec rm -rf {} +
  526. )
  527. }
  528. pyenv.check() {
  529. cat <<EOF
  530. import yaml
  531. print('import yaml --> OK')
  532. EOF
  533. }
  534. pyenv.install() {
  535. if ! pyenv.OK; then
  536. py.clean > /dev/null
  537. fi
  538. if pyenv.install.OK > /dev/null; then
  539. return 0
  540. fi
  541. ( set -e
  542. pyenv
  543. build_msg PYENV "[install] pip install -e 'searx${PY_SETUP_EXTRAS}'"
  544. "${PY_ENV_BIN}/python" -m pip install -e ".${PY_SETUP_EXTRAS}"
  545. buildenv
  546. )
  547. local exit_val=$?
  548. if [ ! $exit_val -eq 0 ]; then
  549. die 42 "error while pip install (${PY_ENV_BIN})"
  550. fi
  551. }
  552. pyenv.uninstall() {
  553. build_msg PYENV "[pyenv.uninstall] uninstall packages: ${PYOBJECTS}"
  554. pyenv.cmd python setup.py develop --uninstall 2>&1 \
  555. | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
  556. }
  557. pypi.upload() {
  558. py.clean
  559. py.build
  560. # https://github.com/pypa/twine
  561. pyenv.cmd twine upload "${PYDIST}"/*
  562. }
  563. pypi.upload.test() {
  564. py.clean
  565. py.build
  566. pyenv.cmd twine upload -r testpypi "${PYDIST}"/*
  567. }
  568. format.python() {
  569. build_msg TEST "[format.python] black \$BLACK_TARGETS"
  570. pyenv.cmd black "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
  571. dump_return $?
  572. }
  573. test.yamllint() {
  574. build_msg TEST "[yamllint] \$YAMLLINT_FILES"
  575. pyenv.cmd yamllint --strict --format parsable "${YAMLLINT_FILES[@]}"
  576. dump_return $?
  577. }
  578. test.pylint() {
  579. # shellcheck disable=SC2086
  580. ( set -e
  581. build_msg TEST "[pylint] \$PYLINT_FILES"
  582. pyenv.activate
  583. python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  584. --additional-builtins="${PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES}" \
  585. "${PYLINT_FILES[@]}"
  586. build_msg TEST "[pylint] searx/engines"
  587. python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  588. --disable="${PYLINT_SEARXNG_DISABLE_OPTION}" \
  589. --additional-builtins="${PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES}" \
  590. searx/engines
  591. build_msg TEST "[pylint] searx tests"
  592. python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  593. --disable="${PYLINT_SEARXNG_DISABLE_OPTION}" \
  594. --ignore=searx/engines \
  595. searx tests
  596. )
  597. dump_return $?
  598. }
  599. test.pyright() {
  600. build_msg TEST "[pyright] static type check of python sources"
  601. node.env.devtools
  602. # We run Pyright in the virtual environment because Pyright
  603. # executes "python" to determine the Python version.
  604. build_msg TEST "[pyright] suppress warnings related to intentional monkey patching"
  605. pyenv.cmd npx --no-install pyright -p pyrightconfig-ci.json \
  606. | grep -v ".py$" \
  607. | grep -v '/engines/.*.py.* - warning: "logger" is not defined'\
  608. | grep -v '/plugins/.*.py.* - error: "logger" is not defined'\
  609. | grep -v '/engines/.*.py.* - warning: "supported_languages" is not defined' \
  610. | grep -v '/engines/.*.py.* - warning: "language_aliases" is not defined' \
  611. | grep -v '/engines/.*.py.* - warning: "categories" is not defined'
  612. dump_return $?
  613. }
  614. test.black() {
  615. build_msg TEST "[black] \$BLACK_TARGETS"
  616. pyenv.cmd black --check --diff "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
  617. dump_return $?
  618. }
  619. test.unit() {
  620. build_msg TEST 'tests/unit'
  621. pyenv.cmd python -m nose2 -s tests/unit
  622. dump_return $?
  623. }
  624. test.coverage() {
  625. build_msg TEST 'unit test coverage'
  626. ( set -e
  627. pyenv.activate
  628. python -m nose2 -C --log-capture --with-coverage --coverage searx -s tests/unit
  629. coverage report
  630. coverage html
  631. )
  632. dump_return $?
  633. }
  634. test.robot() {
  635. build_msg TEST 'robot'
  636. gecko.driver
  637. PYTHONPATH=. pyenv.cmd python -m tests.robot
  638. dump_return $?
  639. }
  640. test.rst() {
  641. build_msg TEST "[reST markup] ${RST_FILES[*]}"
  642. for rst in "${RST_FILES[@]}"; do
  643. pyenv.cmd rst2html.py --halt error "$rst" > /dev/null || die 42 "fix issue in $rst"
  644. done
  645. }
  646. test.pybabel() {
  647. TEST_BABEL_FOLDER="build/test/pybabel"
  648. build_msg TEST "[extract messages] pybabel"
  649. mkdir -p "${TEST_BABEL_FOLDER}"
  650. pyenv.cmd pybabel extract -F babel.cfg -o "${TEST_BABEL_FOLDER}/messages.pot" searx
  651. }
  652. test.clean() {
  653. build_msg CLEAN "test stuff"
  654. rm -rf geckodriver.log .coverage coverage/
  655. dump_return $?
  656. }
  657. themes.all() {
  658. ( set -e
  659. pygments.less
  660. node.env
  661. themes.simple
  662. )
  663. dump_return $?
  664. }
  665. themes.live() {
  666. local LIVE_THEME="${LIVE_THEME:-${1}}"
  667. case "${LIVE_THEME}" in
  668. simple)
  669. theme="searx/static/themes/${LIVE_THEME}"
  670. ;;
  671. '')
  672. die_caller 42 "missing theme argument"
  673. ;;
  674. *)
  675. die_caller 42 "unknown theme '${LIVE_THEME}' // [simple]'"
  676. ;;
  677. esac
  678. build_msg GRUNT "theme: $1 (live build)"
  679. nodejs.ensure
  680. cd "${theme}"
  681. {
  682. npm install
  683. npm run watch
  684. } 2>&1 \
  685. | prefix_stdout "${_Blue}THEME ${1} ${_creset} " \
  686. | grep -E --ignore-case --color 'error[s]?[:]? |warning[s]?[:]? |'
  687. }
  688. themes.simple() {
  689. ( set -e
  690. build_msg GRUNT "theme: simple"
  691. npm --prefix searx/static/themes/simple run build
  692. )
  693. dump_return $?
  694. }
  695. themes.simple.test() {
  696. build_msg TEST "theme: simple"
  697. nodejs.ensure
  698. npm --prefix searx/static/themes/simple install
  699. npm --prefix searx/static/themes/simple run test
  700. dump_return $?
  701. }
  702. PYLINT_FILES=()
  703. while IFS= read -r line; do
  704. PYLINT_FILES+=("$line")
  705. done <<< "$(pylint.FILES)"
  706. # shellcheck disable=SC2119
  707. main() {
  708. local _type
  709. local cmd="$1"; shift
  710. if [ "$cmd" == "" ]; then
  711. help
  712. err_msg "missing command"
  713. return 42
  714. fi
  715. case "$cmd" in
  716. --getenv) var="$1"; echo "${!var}";;
  717. --help) help;;
  718. --*)
  719. help
  720. err_msg "unknown option $cmd"
  721. return 42
  722. ;;
  723. *)
  724. _type="$(type -t "$cmd")"
  725. if [ "$_type" != 'function' ]; then
  726. err_msg "unknown command: $cmd / use --help"
  727. return 42
  728. else
  729. "$cmd" "$@"
  730. fi
  731. ;;
  732. esac
  733. }
  734. main "$@"