manage 23 KB

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