manage 23 KB

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