manage 25 KB

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