manage 25 KB

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