manage 23 KB

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