manage 24 KB

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