manage 23 KB

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