manage 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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" "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. }
  33. YAMLLINT_FILES=()
  34. while IFS= read -r line; do
  35. YAMLLINT_FILES+=("$line")
  36. done <<< "$(git ls-files './tests/*.yml' './searx/*.yml' './utils/templates/etc/searxng/*.yml')"
  37. RST_FILES=(
  38. 'README.rst'
  39. )
  40. PYLINT_SEARXNG_DISABLE_OPTION="\
  41. I,C,R,\
  42. W0105,W0212,W0511,W0603,W0613,W0621,W0702,W0703,W1401,\
  43. E1136"
  44. PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES="supported_languages,language_aliases,logger,categories"
  45. PYLINT_OPTIONS="-m pylint -j 0 --rcfile .pylintrc"
  46. help() {
  47. nvm.help
  48. cat <<EOF
  49. buildenv:
  50. rebuild ./utils/brand.env
  51. weblate.:
  52. push.translations: push translation changes from SearXNG to Weblate's counterpart
  53. to.translations: Update 'translations' branch with last additions from Weblate.
  54. data.:
  55. all : update searx/languages.py and ./data/*
  56. languages : update searx/data/engines_languages.json & searx/languages.py
  57. useragents: update searx/data/useragents.json with the most recent versions of Firefox.
  58. docs.:
  59. html : build HTML documentation
  60. live : autobuild HTML documentation while editing
  61. gh-pages : deploy on gh-pages branch
  62. prebuild : build reST include files (./${DOCS_BUILD}/includes)
  63. clean : clean documentation build
  64. docker.:
  65. build : build docker image
  66. push : build and push docker image
  67. gecko.driver:
  68. download & install geckodriver if not already installed (required for
  69. robot_tests)
  70. redis:
  71. build : build redis binaries at $(redis._get_dist)
  72. install : create user (${REDIS_USER}) and install systemd service (${REDIS_SERVICE_NAME})
  73. help : show more redis commands
  74. node.:
  75. env : download & install npm dependencies locally
  76. clean : drop locally npm installations
  77. py.:
  78. build : Build python packages at ./${PYDIST}
  79. clean : delete virtualenv and intermediate py files
  80. pyenv.:
  81. install : developer install of SearXNG into virtualenv
  82. uninstall : uninstall developer installation
  83. cmd ... : run command ... in virtualenv
  84. OK : test if virtualenv is OK
  85. pypi.upload:
  86. Upload python packages to PyPi (to test use pypi.upload.test)
  87. format.:
  88. python : format Python code source using black
  89. test.:
  90. yamllint : lint YAML files (YAMLLINT_FILES)
  91. pylint : lint PYLINT_FILES, searx/engines, searx & tests
  92. pyright : static type check of python sources
  93. black : check black code format
  94. unit : run unit tests
  95. coverage : run unit tests with coverage
  96. robot : run robot test
  97. rst : test .rst files incl. README.rst
  98. clean : clean intermediate test stuff
  99. themes.:
  100. all : build all themes
  101. oscar : build oscar theme
  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. )
  361. dump_return $?
  362. }
  363. docker.push() {
  364. docker.build push
  365. }
  366. docker.buildx() {
  367. docker.build buildx
  368. }
  369. # shellcheck disable=SC2119
  370. docker.build() {
  371. pyenv.install
  372. local SEARXNG_GIT_VERSION
  373. local VERSION_GITCOMMIT
  374. local GITHUB_USER
  375. local SEARXNG_IMAGE_NAME
  376. local BUILD
  377. build_msg DOCKER build
  378. # run installation in a subprocess and activate pyenv
  379. # See https://www.shellcheck.net/wiki/SC1001 and others ..
  380. # shellcheck disable=SC2031,SC2230,SC2002,SC2236,SC2143,SC1001
  381. ( set -e
  382. pyenv.activate
  383. # Check if it is a git repository
  384. if [ ! -d .git ]; then
  385. die 1 "This is not Git repository"
  386. fi
  387. if [ ! -x "$(which git)" ]; then
  388. die 1 "git is not installed"
  389. fi
  390. if ! git remote get-url origin 2> /dev/null; then
  391. die 1 "there is no remote origin"
  392. fi
  393. # This is a git repository
  394. git update-index -q --refresh
  395. python -m searx.version freeze
  396. eval "$(python -m searx.version)"
  397. # Get the last git commit id
  398. VERSION_GITCOMMIT=$(echo "$VERSION_STRING" | cut -d- -f3)
  399. build_msg DOCKER "Last commit : $VERSION_GITCOMMIT"
  400. # define the docker image name
  401. GITHUB_USER=$(echo "${GIT_URL}" | sed 's/.*github\.com\/\([^\/]*\).*/\1/')
  402. SEARXNG_IMAGE_NAME="${SEARXNG_IMAGE_NAME:-${GITHUB_USER:-searxng}/searxng}"
  403. BUILD="build"
  404. if [ "$1" = "buildx" ]; then
  405. # buildx includes the push option
  406. CACHE_TAG="${SEARXNG_IMAGE_NAME}:latest-build-cache"
  407. 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"
  408. shift
  409. fi
  410. build_msg DOCKER "Build command: ${BUILD}"
  411. # build Docker image
  412. build_msg DOCKER "Building image ${SEARXNG_IMAGE_NAME}:${SEARXNG_GIT_VERSION}"
  413. # shellcheck disable=SC2086
  414. docker $BUILD \
  415. --build-arg BASE_IMAGE="${DEPENDENCIES_IMAGE_NAME}" \
  416. --build-arg GIT_URL="${GIT_URL}" \
  417. --build-arg SEARXNG_GIT_VERSION="${VERSION_STRING}" \
  418. --build-arg VERSION_GITCOMMIT="${VERSION_GITCOMMIT}" \
  419. --build-arg LABEL_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
  420. --build-arg LABEL_VCS_REF="$(git rev-parse HEAD)" \
  421. --build-arg LABEL_VCS_URL="${GIT_URL}" \
  422. --build-arg TIMESTAMP_SETTINGS="$(git log -1 --format="%cd" --date=unix -- searx/settings.yml)" \
  423. --build-arg TIMESTAMP_UWSGI="$(git log -1 --format="%cd" --date=unix -- dockerfiles/uwsgi.ini)" \
  424. -t "${SEARXNG_IMAGE_NAME}:latest" -t "${SEARXNG_IMAGE_NAME}:${VERSION_STRING}" .
  425. if [ "$1" = "push" ]; then
  426. docker push "${SEARXNG_IMAGE_NAME}:latest"
  427. docker push "${SEARXNG_IMAGE_NAME}:${SEARXNG_GIT_VERSION}"
  428. fi
  429. )
  430. dump_return $?
  431. }
  432. # shellcheck disable=SC2119
  433. gecko.driver() {
  434. pyenv.install
  435. build_msg INSTALL "gecko.driver"
  436. # run installation in a subprocess and activate pyenv
  437. ( set -e
  438. pyenv.activate
  439. INSTALLED_VERSION=$(geckodriver -V 2> /dev/null | head -1 | awk '{ print "v" $2}') || INSTALLED_VERSION=""
  440. set +e
  441. if [ "${INSTALLED_VERSION}" = "${GECKODRIVER_VERSION}" ]; then
  442. build_msg INSTALL "geckodriver already installed"
  443. return
  444. fi
  445. PLATFORM="$(python3 -c 'import platform; print(platform.system().lower(), platform.architecture()[0])')"
  446. case "$PLATFORM" in
  447. "linux 32bit" | "linux2 32bit") ARCH="linux32";;
  448. "linux 64bit" | "linux2 64bit") ARCH="linux64";;
  449. "windows 32 bit") ARCH="win32";;
  450. "windows 64 bit") ARCH="win64";;
  451. "mac 64bit") ARCH="macos";;
  452. esac
  453. GECKODRIVER_URL="https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-$ARCH.tar.gz";
  454. build_msg GECKO "Installing ${PY_ENV_BIN}/geckodriver from $GECKODRIVER_URL"
  455. FILE="$(mktemp)"
  456. wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "${PY_ENV_BIN}" -f "$FILE" geckodriver
  457. rm -- "$FILE"
  458. chmod 755 -- "${PY_ENV_BIN}/geckodriver"
  459. )
  460. dump_return $?
  461. }
  462. nodejs.ensure() {
  463. if ! nvm.min_node "${NODE_MINIMUM_VERSION}"; then
  464. info_msg "install Node.js by NVM"
  465. nvm.nodejs
  466. fi
  467. }
  468. node.env() {
  469. nodejs.ensure
  470. ( set -e
  471. build_msg INSTALL "searx/static/themes/oscar/package.json"
  472. npm --prefix searx/static/themes/oscar install
  473. build_msg INSTALL "searx/static/themes/simple/package.json"
  474. npm --prefix searx/static/themes/simple install
  475. )
  476. dump_return $?
  477. }
  478. node.env.devtools() {
  479. nodejs.ensure
  480. build_msg INSTALL "package.json: developer and CI tools"
  481. npm install
  482. }
  483. node.clean() {
  484. if ! required_commands npm 2>/dev/null; then
  485. build_msg CLEAN "npm is not installed / ignore npm dependencies"
  486. return 0
  487. fi
  488. build_msg CLEAN "themes -- locally installed npm dependencies"
  489. ( set -e
  490. npm --prefix searx/static/themes/oscar run clean
  491. npm --prefix searx/static/themes/simple run clean
  492. )
  493. dump_return $?
  494. }
  495. pygments.less() {
  496. build_msg PYGMENTS "searxng_extra/update/update_pygments.py"
  497. if ! pyenv.cmd python searxng_extra/update/update_pygments.py; then
  498. build_msg PYGMENTS "building LESS files for pygments failed"
  499. return 1
  500. fi
  501. return 0
  502. }
  503. py.build() {
  504. build_msg BUILD "python package ${PYDIST}"
  505. pyenv.cmd python setup.py \
  506. sdist -d "${PYDIST}" \
  507. bdist_wheel --bdist-dir "${PYBUILD}" -d "${PYDIST}"
  508. }
  509. py.clean() {
  510. build_msg CLEAN pyenv
  511. ( set -e
  512. pyenv.drop
  513. [ "$VERBOSE" = "1" ] && set -x
  514. rm -rf "${PYDIST}" "${PYBUILD}" "${PY_ENV}" ./.tox ./*.egg-info
  515. find . -name '*.pyc' -exec rm -f {} +
  516. find . -name '*.pyo' -exec rm -f {} +
  517. find . -name __pycache__ -exec rm -rf {} +
  518. )
  519. }
  520. pyenv.check() {
  521. cat <<EOF
  522. import yaml
  523. print('import yaml --> OK')
  524. EOF
  525. }
  526. pyenv.install() {
  527. if ! pyenv.OK; then
  528. py.clean > /dev/null
  529. fi
  530. if pyenv.install.OK > /dev/null; then
  531. return 0
  532. fi
  533. ( set -e
  534. pyenv
  535. build_msg PYENV "[install] pip install -e 'searx${PY_SETUP_EXTRAS}'"
  536. "${PY_ENV_BIN}/python" -m pip install -e ".${PY_SETUP_EXTRAS}"
  537. buildenv
  538. )
  539. local exit_val=$?
  540. if [ ! $exit_val -eq 0 ]; then
  541. die 42 "error while pip install (${PY_ENV_BIN})"
  542. fi
  543. }
  544. pyenv.uninstall() {
  545. build_msg PYENV "[pyenv.uninstall] uninstall packages: ${PYOBJECTS}"
  546. pyenv.cmd python setup.py develop --uninstall 2>&1 \
  547. | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
  548. }
  549. pypi.upload() {
  550. py.clean
  551. py.build
  552. # https://github.com/pypa/twine
  553. pyenv.cmd twine upload "${PYDIST}"/*
  554. }
  555. pypi.upload.test() {
  556. py.clean
  557. py.build
  558. pyenv.cmd twine upload -r testpypi "${PYDIST}"/*
  559. }
  560. format.python() {
  561. build_msg TEST "[format.python] black \$BLACK_TARGETS"
  562. pyenv.cmd black "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
  563. dump_return $?
  564. }
  565. test.yamllint() {
  566. build_msg TEST "[yamllint] \$YAMLLINT_FILES"
  567. pyenv.cmd yamllint --format parsable "${YAMLLINT_FILES[@]}"
  568. }
  569. test.pylint() {
  570. # shellcheck disable=SC2086
  571. ( set -e
  572. build_msg TEST "[pylint] \$PYLINT_FILES"
  573. pyenv.activate
  574. python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  575. --additional-builtins="${PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES}" \
  576. "${PYLINT_FILES[@]}"
  577. build_msg TEST "[pylint] searx/engines"
  578. python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  579. --disable="${PYLINT_SEARXNG_DISABLE_OPTION}" \
  580. --additional-builtins="${PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES}" \
  581. searx/engines
  582. build_msg TEST "[pylint] searx tests"
  583. python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  584. --disable="${PYLINT_SEARXNG_DISABLE_OPTION}" \
  585. --ignore=searx/engines \
  586. searx tests
  587. )
  588. dump_return $?
  589. }
  590. test.pyright() {
  591. build_msg TEST "[pyright] static type check of python sources"
  592. node.env.devtools
  593. # We run Pyright in the virtual environment because Pyright
  594. # executes "python" to determine the Python version.
  595. pyenv.cmd npx --no-install pyright -p pyrightconfig-ci.json
  596. dump_return $?
  597. }
  598. test.black() {
  599. build_msg TEST "[black] \$BLACK_TARGETS"
  600. pyenv.cmd black --check --diff "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
  601. dump_return $?
  602. }
  603. test.unit() {
  604. build_msg TEST 'tests/unit'
  605. pyenv.cmd python -m nose2 -s tests/unit
  606. dump_return $?
  607. }
  608. test.coverage() {
  609. build_msg TEST 'unit test coverage'
  610. ( set -e
  611. pyenv.activate
  612. python -m nose2 -C --log-capture --with-coverage --coverage searx -s tests/unit
  613. coverage report
  614. coverage html
  615. )
  616. dump_return $?
  617. }
  618. test.robot() {
  619. build_msg TEST 'robot'
  620. gecko.driver
  621. PYTHONPATH=. pyenv.cmd python -m tests.robot
  622. dump_return $?
  623. }
  624. test.rst() {
  625. build_msg TEST "[reST markup] ${RST_FILES[*]}"
  626. for rst in "${RST_FILES[@]}"; do
  627. pyenv.cmd rst2html.py --halt error "$rst" > /dev/null || die 42 "fix issue in $rst"
  628. done
  629. }
  630. test.clean() {
  631. build_msg CLEAN "test stuff"
  632. rm -rf geckodriver.log .coverage coverage/
  633. dump_return $?
  634. }
  635. themes.all() {
  636. ( set -e
  637. pygments.less
  638. node.env
  639. themes.oscar
  640. themes.simple
  641. )
  642. dump_return $?
  643. }
  644. themes.live() {
  645. local LIVE_THEME="${LIVE_THEME:-${1}}"
  646. case "${LIVE_THEME}" in
  647. simple|oscar)
  648. theme="searx/static/themes/${LIVE_THEME}"
  649. ;;
  650. '')
  651. die_caller 42 "missing theme argument"
  652. ;;
  653. *)
  654. die_caller 42 "unknown theme '${LIVE_THEME}' // [simple|oscar]'"
  655. ;;
  656. esac
  657. build_msg GRUNT "theme: $1 (live build)"
  658. nodejs.ensure
  659. cd "${theme}"
  660. {
  661. npm install
  662. npm run watch
  663. } 2>&1 \
  664. | prefix_stdout "${_Blue}THEME ${1} ${_creset} " \
  665. | grep -E --ignore-case --color 'error[s]?[:]? |warning[s]?[:]? |'
  666. }
  667. themes.oscar() {
  668. build_msg GRUNT "theme: oscar"
  669. npm --prefix searx/static/themes/oscar run build
  670. dump_return $?
  671. }
  672. themes.simple() {
  673. ( set -e
  674. build_msg GRUNT "theme: simple"
  675. npm --prefix searx/static/themes/simple run build
  676. )
  677. dump_return $?
  678. }
  679. themes.simple.test() {
  680. build_msg TEST "theme: simple"
  681. nodejs.ensure
  682. npm --prefix searx/static/themes/simple install
  683. npm --prefix searx/static/themes/simple run test
  684. dump_return $?
  685. }
  686. PYLINT_FILES=()
  687. while IFS= read -r line; do
  688. PYLINT_FILES+=("$line")
  689. done <<< "$(pylint.FILES)"
  690. # shellcheck disable=SC2119
  691. main() {
  692. local _type
  693. local cmd="$1"; shift
  694. if [ "$cmd" == "" ]; then
  695. help
  696. err_msg "missing command"
  697. return 42
  698. fi
  699. case "$cmd" in
  700. --getenv) var="$1"; echo "${!var}";;
  701. --help) help;;
  702. --*)
  703. help
  704. err_msg "unknown option $cmd"
  705. return 42
  706. ;;
  707. *)
  708. _type="$(type -t "$cmd")"
  709. if [ "$_type" != 'function' ]; then
  710. err_msg "unknown command: $cmd / use --help"
  711. return 42
  712. else
  713. "$cmd" "$@"
  714. fi
  715. ;;
  716. esac
  717. }
  718. main "$@"