manage 26 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/sxng_locales.py and searx/data/*
  64. traits : update searx/data/engine_traits.json & searx/sxng_locales.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.traits
  333. data.useragents
  334. build_msg DATA "update searx/data/osm_keys_tags.json"
  335. pyenv.cmd python searxng_extra/update/update_osm_keys_tags.py
  336. build_msg DATA "update searx/data/ahmia_blacklist.txt"
  337. python searxng_extra/update/update_ahmia_blacklist.py
  338. build_msg DATA "update searx/data/wikidata_units.json"
  339. python searxng_extra/update/update_wikidata_units.py
  340. build_msg DATA "update searx/data/currencies.json"
  341. python searxng_extra/update/update_currencies.py
  342. build_msg DATA "update searx/data/external_bangs.json"
  343. python searxng_extra/update/update_external_bangs.py
  344. build_msg DATA "update searx/data/engine_descriptions.json"
  345. python searxng_extra/update/update_engine_descriptions.py
  346. )
  347. }
  348. data.traits() {
  349. ( set -e
  350. pyenv.activate
  351. build_msg DATA "update searx/data/engine_traits.json"
  352. python searxng_extra/update/update_engine_traits.py
  353. build_msg ENGINES "update searx/sxng_locales.py"
  354. )
  355. dump_return $?
  356. }
  357. data.useragents() {
  358. build_msg DATA "update searx/data/useragents.json"
  359. pyenv.cmd python searxng_extra/update/update_firefox_version.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.dev() {
  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. build_msg CLEAN "locally installed developer and CI tools"
  502. ( set -e
  503. npm --prefix . run clean
  504. )
  505. dump_return $?
  506. }
  507. pygments.less() {
  508. build_msg PYGMENTS "searxng_extra/update/update_pygments.py"
  509. if ! pyenv.cmd python searxng_extra/update/update_pygments.py; then
  510. build_msg PYGMENTS "building LESS files for pygments failed"
  511. return 1
  512. fi
  513. return 0
  514. }
  515. py.build() {
  516. build_msg BUILD "python package ${PYDIST}"
  517. pyenv.cmd python setup.py \
  518. sdist -d "${PYDIST}" \
  519. bdist_wheel --bdist-dir "${PYBUILD}" -d "${PYDIST}"
  520. }
  521. py.clean() {
  522. build_msg CLEAN pyenv
  523. ( set -e
  524. pyenv.drop
  525. [ "$VERBOSE" = "1" ] && set -x
  526. rm -rf "${PYDIST}" "${PYBUILD}" "${PY_ENV}" ./.tox ./*.egg-info
  527. find . -name '*.pyc' -exec rm -f {} +
  528. find . -name '*.pyo' -exec rm -f {} +
  529. find . -name __pycache__ -exec rm -rf {} +
  530. )
  531. }
  532. pyenv.check() {
  533. cat <<EOF
  534. import yaml
  535. print('import yaml --> OK')
  536. EOF
  537. }
  538. pyenv.install() {
  539. if ! pyenv.OK; then
  540. py.clean > /dev/null
  541. fi
  542. if pyenv.install.OK > /dev/null; then
  543. return 0
  544. fi
  545. ( set -e
  546. pyenv
  547. build_msg PYENV "[install] pip install -e 'searx${PY_SETUP_EXTRAS}'"
  548. "${PY_ENV_BIN}/python" -m pip install -e ".${PY_SETUP_EXTRAS}"
  549. buildenv
  550. )
  551. local exit_val=$?
  552. if [ ! $exit_val -eq 0 ]; then
  553. die 42 "error while pip install (${PY_ENV_BIN})"
  554. fi
  555. }
  556. pyenv.uninstall() {
  557. build_msg PYENV "[pyenv.uninstall] uninstall packages: ${PYOBJECTS}"
  558. pyenv.cmd python setup.py develop --uninstall 2>&1 \
  559. | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
  560. }
  561. pypi.upload() {
  562. py.clean
  563. py.build
  564. # https://github.com/pypa/twine
  565. pyenv.cmd twine upload "${PYDIST}"/*
  566. }
  567. pypi.upload.test() {
  568. py.clean
  569. py.build
  570. pyenv.cmd twine upload -r testpypi "${PYDIST}"/*
  571. }
  572. format.python() {
  573. build_msg TEST "[format.python] black \$BLACK_TARGETS"
  574. pyenv.cmd black "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
  575. dump_return $?
  576. }
  577. test.yamllint() {
  578. build_msg TEST "[yamllint] \$YAMLLINT_FILES"
  579. pyenv.cmd yamllint --strict --format parsable "${YAMLLINT_FILES[@]}"
  580. dump_return $?
  581. }
  582. test.pylint() {
  583. # shellcheck disable=SC2086
  584. ( set -e
  585. build_msg TEST "[pylint] \$PYLINT_FILES"
  586. pyenv.activate
  587. python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  588. --additional-builtins="${PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES}" \
  589. "${PYLINT_FILES[@]}"
  590. build_msg TEST "[pylint] searx/engines"
  591. python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  592. --disable="${PYLINT_SEARXNG_DISABLE_OPTION}" \
  593. --additional-builtins="${PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES}" \
  594. searx/engines
  595. build_msg TEST "[pylint] searx tests"
  596. python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  597. --disable="${PYLINT_SEARXNG_DISABLE_OPTION}" \
  598. --ignore=searx/engines \
  599. searx tests
  600. )
  601. dump_return $?
  602. }
  603. test.pyright() {
  604. build_msg TEST "[pyright] static type check of python sources"
  605. node.env.dev
  606. # We run Pyright in the virtual environment because Pyright
  607. # executes "python" to determine the Python version.
  608. build_msg TEST "[pyright] suppress warnings related to intentional monkey patching"
  609. pyenv.cmd npx --no-install pyright -p pyrightconfig-ci.json \
  610. | grep -v ".py$" \
  611. | grep -v '/engines/.*.py.* - warning: "logger" is not defined'\
  612. | grep -v '/plugins/.*.py.* - error: "logger" is not defined'\
  613. | grep -v '/engines/.*.py.* - warning: "supported_languages" is not defined' \
  614. | grep -v '/engines/.*.py.* - warning: "language_aliases" is not defined' \
  615. | grep -v '/engines/.*.py.* - warning: "categories" is not defined'
  616. dump_return $?
  617. }
  618. test.black() {
  619. build_msg TEST "[black] \$BLACK_TARGETS"
  620. pyenv.cmd black --check --diff "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
  621. dump_return $?
  622. }
  623. test.unit() {
  624. build_msg TEST 'tests/unit'
  625. pyenv.cmd python -m nose2 -s tests/unit
  626. dump_return $?
  627. }
  628. test.coverage() {
  629. build_msg TEST 'unit test coverage'
  630. ( set -e
  631. pyenv.activate
  632. python -m nose2 -C --log-capture --with-coverage --coverage searx -s tests/unit
  633. coverage report
  634. coverage html
  635. )
  636. dump_return $?
  637. }
  638. test.robot() {
  639. build_msg TEST 'robot'
  640. gecko.driver
  641. PYTHONPATH=. pyenv.cmd python -m tests.robot
  642. dump_return $?
  643. }
  644. test.rst() {
  645. build_msg TEST "[reST markup] ${RST_FILES[*]}"
  646. for rst in "${RST_FILES[@]}"; do
  647. pyenv.cmd rst2html.py --halt error "$rst" > /dev/null || die 42 "fix issue in $rst"
  648. done
  649. }
  650. test.pybabel() {
  651. TEST_BABEL_FOLDER="build/test/pybabel"
  652. build_msg TEST "[extract messages] pybabel"
  653. mkdir -p "${TEST_BABEL_FOLDER}"
  654. pyenv.cmd pybabel extract -F babel.cfg -o "${TEST_BABEL_FOLDER}/messages.pot" searx
  655. }
  656. test.clean() {
  657. build_msg CLEAN "test stuff"
  658. rm -rf geckodriver.log .coverage coverage/
  659. dump_return $?
  660. }
  661. themes.all() {
  662. ( set -e
  663. pygments.less
  664. node.env
  665. themes.simple
  666. )
  667. dump_return $?
  668. }
  669. themes.live() {
  670. local LIVE_THEME="${LIVE_THEME:-${1}}"
  671. case "${LIVE_THEME}" in
  672. simple)
  673. theme="searx/static/themes/${LIVE_THEME}"
  674. ;;
  675. '')
  676. die_caller 42 "missing theme argument"
  677. ;;
  678. *)
  679. die_caller 42 "unknown theme '${LIVE_THEME}' // [simple]'"
  680. ;;
  681. esac
  682. build_msg GRUNT "theme: $1 (live build)"
  683. nodejs.ensure
  684. cd "${theme}"
  685. {
  686. npm install
  687. npm run watch
  688. } 2>&1 \
  689. | prefix_stdout "${_Blue}THEME ${1} ${_creset} " \
  690. | grep -E --ignore-case --color 'error[s]?[:]? |warning[s]?[:]? |'
  691. }
  692. themes.simple() {
  693. ( set -e
  694. build_msg GRUNT "theme: simple"
  695. npm --prefix searx/static/themes/simple run build
  696. )
  697. dump_return $?
  698. }
  699. themes.simple.test() {
  700. build_msg TEST "theme: simple"
  701. nodejs.ensure
  702. npm --prefix searx/static/themes/simple install
  703. npm --prefix searx/static/themes/simple run test
  704. dump_return $?
  705. }
  706. PYLINT_FILES=()
  707. while IFS= read -r line; do
  708. PYLINT_FILES+=("$line")
  709. done <<< "$(pylint.FILES)"
  710. # shellcheck disable=SC2119
  711. main() {
  712. local _type
  713. local cmd="$1"; shift
  714. if [ "$cmd" == "" ]; then
  715. help
  716. err_msg "missing command"
  717. return 42
  718. fi
  719. case "$cmd" in
  720. --getenv) var="$1"; echo "${!var}";;
  721. --help) help;;
  722. --*)
  723. help
  724. err_msg "unknown option $cmd"
  725. return 42
  726. ;;
  727. *)
  728. _type="$(type -t "$cmd")"
  729. if [ "$_type" != 'function' ]; then
  730. err_msg "unknown command: $cmd / use --help"
  731. return 42
  732. else
  733. "$cmd" "$@"
  734. fi
  735. ;;
  736. esac
  737. }
  738. main "$@"