manage 24 KB

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