manage 23 KB

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