manage 25 KB

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