manage 23 KB

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