manage 23 KB

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