manage 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. # config
  10. PYOBJECTS="searx"
  11. PY_SETUP_EXTRAS='[test]'
  12. GECKODRIVER_VERSION="v0.28.0"
  13. # SPHINXOPTS=
  14. pylint.FILES() {
  15. # List files tagged by comment:
  16. #
  17. # # lint: pylint
  18. #
  19. # These py files are linted by test.pylint(), all other files are linted by
  20. # test.pep8()
  21. grep -l -r --include \*.py '^#[[:blank:]]*lint:[[:blank:]]*pylint' searx searx_extra tests
  22. }
  23. YAMLLINT_FILES=()
  24. while IFS= read -r line; do
  25. YAMLLINT_FILES+=("$line")
  26. done <<< "$(git ls-files './tests/*.yml' './searx/*.yml' './utils/templates/etc/searx/*.yml')"
  27. PYLINT_SEARX_DISABLE_OPTION="\
  28. I,C,R,\
  29. W0105,W0212,W0511,W0603,W0613,W0621,W0702,W0703,W1401,\
  30. E1136"
  31. PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES="supported_languages,language_aliases"
  32. PYLINT_OPTIONS="-m pylint -j 0 --rcfile .pylintrc"
  33. help() {
  34. cat <<EOF
  35. buildenv:
  36. rebuild ./utils/brand.env
  37. babel.compile:
  38. pybabel compile ./searx/translations
  39. data.:
  40. all : update searx/languages.py and ./data/*
  41. languages : update searx/data/engines_languages.json & searx/languages.py
  42. useragents: update searx/data/useragents.json with the most recent versions of Firefox.
  43. docs.:
  44. html : build HTML documentation
  45. live : autobuild HTML documentation while editing
  46. gh-pages : deploy on gh-pages branch
  47. prebuild : build reST include files (./${DOCS_BUILD}/includes)
  48. clean : clean documentation build
  49. docker.:
  50. build : build docker image
  51. push : build and push docker image
  52. gecko.driver:
  53. download & install geckodriver if not already installed (required for
  54. robot_tests)
  55. node.:
  56. env : download & install npm dependencies locally
  57. clean : drop npm installations
  58. py.:
  59. build : Build python packages at ./${PYDIST}
  60. clean : delete virtualenv and intermediate py files
  61. pyenv.:
  62. install : developer install of searx into virtualenv
  63. uninstall : uninstall developer installation
  64. cmd ... : run command ... in virtualenv
  65. OK : test if virtualenv is OK
  66. pypi.upload:
  67. Upload python packages to PyPi (to test use pypi.upload.test)
  68. test.:
  69. pylint : lint PYLINT_FILES, searx/engines, searx & tests
  70. pep8 : pycodestyle (pep8) for all files except PYLINT_FILES
  71. unit : run unit tests
  72. coverage : run unit tests with coverage
  73. robot : run robot test
  74. clean : clean intermediate test stuff
  75. themes.:
  76. all : build all themes
  77. oscar : build oscar theme
  78. simple : build simple theme
  79. pygments.:
  80. less : build LESS files for pygments
  81. EOF
  82. static_help
  83. }
  84. if [ "$VERBOSE" = "1" ]; then
  85. SPHINX_VERBOSE="-v"
  86. PYLINT_VERBOSE="-v"
  87. fi
  88. # needed by sphinx-docs
  89. export DOCS_BUILD
  90. buildenv() {
  91. # settings file from repository's working tree are used by default
  92. SEARX_SETTINGS_PATH="${REPO_ROOT}/searx/settings.yml"
  93. if [ -r '/etc/searx/settings.yml' ]; then
  94. if ask_yn "should settings read from: /etc/searx/settings.yml"; then
  95. SEARX_SETTINGS_PATH='/etc/searx/settings.yml'
  96. fi
  97. fi
  98. export SEARX_SETTINGS_PATH
  99. (
  100. set -e
  101. SEARX_DEBUG=1 pyenv.cmd python utils/build_env.py 2>&1 \
  102. | prefix_stdout "${_Blue}BUILDENV${_creset} "
  103. )
  104. return "${PIPESTATUS[0]}"
  105. }
  106. babel.compile() {
  107. build_msg BABEL compile
  108. pyenv.cmd pybabel compile -d "${REPO_ROOT}/searx/translations"
  109. dump_return $?
  110. }
  111. data.all() {
  112. data.languages
  113. data.useragents
  114. data.osm_keys_tags
  115. build_msg DATA "update searx/data/ahmia_blacklist.txt"
  116. pyenv.cmd python searx_extra/update/update_ahmia_blacklist.py
  117. build_msg DATA "update searx/data/wikidata_units.json"
  118. pyenv.cmd python searx_extra/update/update_wikidata_units.py
  119. build_msg DATA "update searx/data/currencies.json"
  120. pyenv.cmd python searx_extra/update/update_currencies.py
  121. }
  122. data.languages() {
  123. ( set -e
  124. build_msg ENGINES "fetch languages .."
  125. pyenv.cmd python searx_extra/update/update_languages.py
  126. build_msg ENGINES "update update searx/languages.py"
  127. build_msg DATA "update searx/data/engines_languages.json"
  128. )
  129. dump_return $?
  130. }
  131. data.useragents() {
  132. build_msg DATA "update searx/data/useragents.json"
  133. pyenv.cmd python searx_extra/update/update_firefox_version.py
  134. dump_return $?
  135. }
  136. data.osm_keys_tags() {
  137. build_msg DATA "update searx/data/osm_keys_tags.json"
  138. pyenv.cmd python searx_extra/update/update_osm_keys_tags.py
  139. dump_return $?
  140. }
  141. docs.prebuild() {
  142. build_msg DOCS "build ${DOCS_BUILD}/includes"
  143. (
  144. set -e
  145. [ "$VERBOSE" = "1" ] && set -x
  146. mkdir -p "${DOCS_BUILD}/includes"
  147. ./utils/searx.sh doc | cat > "${DOCS_BUILD}/includes/searx.rst"
  148. ./utils/filtron.sh doc | cat > "${DOCS_BUILD}/includes/filtron.rst"
  149. ./utils/morty.sh doc | cat > "${DOCS_BUILD}/includes/morty.rst"
  150. )
  151. dump_return $?
  152. }
  153. docker.push() {
  154. docker.build push
  155. }
  156. docker.buildx() {
  157. docker.build buildx
  158. }
  159. # shellcheck disable=SC2119
  160. docker.build() {
  161. pyenv.install
  162. local SEARX_GIT_VERSION
  163. local VERSION_GITCOMMIT
  164. local GITHUB_USER
  165. local SEARX_IMAGE_NAME
  166. local BUILD
  167. build_msg DOCKER build
  168. # run installation in a subprocess and activate pyenv
  169. # See https://www.shellcheck.net/wiki/SC1001 and others ..
  170. # shellcheck disable=SC2031,SC2230,SC2002,SC2236,SC2143,SC1001
  171. ( set -e
  172. # shellcheck source=/dev/null
  173. source "${PY_ENV_BIN}/activate"
  174. # Check if it is a git repository
  175. if [ ! -d .git ]; then
  176. die 1 "This is not Git repository"
  177. fi
  178. if [ ! -x "$(which git)" ]; then
  179. die 1 "git is not installed"
  180. fi
  181. if ! git remote get-url origin 2> /dev/null; then
  182. die 1 "there is no remote origin"
  183. fi
  184. # This is a git repository
  185. git update-index -q --refresh
  186. pyenv.cmd python -m searx.version freeze
  187. eval "$(pyenv.cmd python -m searx.version)"
  188. # Get the last git commit id
  189. VERSION_GITCOMMIT=$(echo "$VERSION_STRING" | cut -d- -f3)
  190. build_msg DOCKER "Last commit : $VERSION_GITCOMMIT"
  191. # define the docker image name
  192. GITHUB_USER=$(echo "${GIT_URL}" | sed 's/.*github\.com\/\([^\/]*\).*/\1/')
  193. SEARX_IMAGE_NAME="${SEARX_IMAGE_NAME:-${GITHUB_USER:-searxng}/searxng}"
  194. BUILD="build"
  195. if [ "$1" = "buildx" ]; then
  196. # buildx includes the push option
  197. CACHE_TAG="${SEARX_IMAGE_NAME}:latest-build-cache"
  198. 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"
  199. shift
  200. fi
  201. build_msg DOCKER "Build command: ${BUILD}"
  202. # build Docker image
  203. build_msg DOCKER "Building image ${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}"
  204. # shellcheck disable=SC2086
  205. docker $BUILD \
  206. --build-arg BASE_IMAGE="${DEPENDENCIES_IMAGE_NAME}" \
  207. --build-arg GIT_URL="${GIT_URL}" \
  208. --build-arg SEARX_GIT_VERSION="${VERSION_STRING}" \
  209. --build-arg VERSION_GITCOMMIT="${VERSION_GITCOMMIT}" \
  210. --build-arg LABEL_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
  211. --build-arg LABEL_VCS_REF="$(git rev-parse HEAD)" \
  212. --build-arg LABEL_VCS_URL="${GIT_URL}" \
  213. --build-arg TIMESTAMP_SETTINGS="$(git log -1 --format="%cd" --date=unix -- searx/settings.yml)" \
  214. --build-arg TIMESTAMP_UWSGI="$(git log -1 --format="%cd" --date=unix -- dockerfiles/uwsgi.ini)" \
  215. -t "${SEARX_IMAGE_NAME}:latest" -t "${SEARX_IMAGE_NAME}:${VERSION_STRING}" .
  216. if [ "$1" = "push" ]; then
  217. docker push "${SEARX_IMAGE_NAME}:latest"
  218. docker push "${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}"
  219. fi
  220. )
  221. dump_return $?
  222. }
  223. # shellcheck disable=SC2119
  224. gecko.driver() {
  225. pyenv.install
  226. build_msg INSTALL "gecko.driver"
  227. # run installation in a subprocess and activate pyenv
  228. ( set -e
  229. # shellcheck source=/dev/null
  230. source "${PY_ENV_BIN}/activate"
  231. # TODO : check the current geckodriver version
  232. geckodriver -V > /dev/null 2>&1 || NOTFOUND=1
  233. set +e
  234. if [ -z "$NOTFOUND" ]; then
  235. build_msg INSTALL "geckodriver already installed"
  236. return
  237. fi
  238. PLATFORM="$(python3 -c 'import platform; print(platform.system().lower(), platform.architecture()[0])')"
  239. case "$PLATFORM" in
  240. "linux 32bit" | "linux2 32bit") ARCH="linux32";;
  241. "linux 64bit" | "linux2 64bit") ARCH="linux64";;
  242. "windows 32 bit") ARCH="win32";;
  243. "windows 64 bit") ARCH="win64";;
  244. "mac 64bit") ARCH="macos";;
  245. esac
  246. GECKODRIVER_URL="https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-$ARCH.tar.gz";
  247. build_msg GECKO "Installing ${PY_ENV_BIN}/geckodriver from $GECKODRIVER_URL"
  248. FILE="$(mktemp)"
  249. wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "${PY_ENV_BIN}" -f "$FILE" geckodriver
  250. rm -- "$FILE"
  251. chmod 755 -- "${PY_ENV_BIN}/geckodriver"
  252. )
  253. dump_return $?
  254. }
  255. node.env() {
  256. if ! required_commands npm; then
  257. info_msg "to install build tools use::"
  258. info_msg " sudo -H ./utils/searx.sh install buildhost"
  259. die 1 "install needed build tools first"
  260. fi
  261. ( set -e
  262. build_msg INSTALL "searx/static/themes/oscar/package.json"
  263. npm --prefix searx/static/themes/oscar install
  264. build_msg INSTALL "searx/static/themes/simple/package.json"
  265. npm --prefix searx/static/themes/simple install
  266. )
  267. dump_return $?
  268. }
  269. node.clean() {
  270. if ! required_commands npm 2>/dev/null; then
  271. build_msg CLEAN "npm is not installed / ignore npm dependencies"
  272. return 0
  273. fi
  274. build_msg CLEAN "locally installed npm dependencies"
  275. ( set -e
  276. npm --prefix searx/static/themes/oscar run clean
  277. npm --prefix searx/static/themes/simple run clean
  278. )
  279. dump_return $?
  280. }
  281. pygments.less() {
  282. build_msg PYGMENTS "searx_extra/update/update_pygments.py"
  283. if ! pyenv.cmd python searx_extra/update/update_pygments.py; then
  284. build_msg PYGMENTS "building LESS files for pygments failed"
  285. return 1
  286. fi
  287. return 0
  288. }
  289. py.build() {
  290. build_msg BUILD "python package ${PYDIST}"
  291. pyenv.cmd python setup.py \
  292. sdist -d "${PYDIST}" \
  293. bdist_wheel --bdist-dir "${PYBUILD}" -d "${PYDIST}"
  294. }
  295. py.clean() {
  296. build_msg CLEAN pyenv
  297. ( set -e
  298. pyenv.drop
  299. [ "$VERBOSE" = "1" ] && set -x
  300. rm -rf "${PYDIST}" "${PYBUILD}" "${PY_ENV}" ./.tox ./*.egg-info
  301. find . -name '*.pyc' -exec rm -f {} +
  302. find . -name '*.pyo' -exec rm -f {} +
  303. find . -name __pycache__ -exec rm -rf {} +
  304. )
  305. }
  306. pyenv.check() {
  307. cat <<EOF
  308. import yaml
  309. print('import yaml --> OK')
  310. EOF
  311. }
  312. pyenv.install() {
  313. if ! pyenv.OK; then
  314. py.clean > /dev/null
  315. fi
  316. if pyenv.install.OK > /dev/null; then
  317. return 0
  318. fi
  319. ( set -e
  320. pyenv
  321. build_msg PYENV "[install] pip install -e 'searx${PY_SETUP_EXTRAS}'"
  322. "${PY_ENV_BIN}/python" -m pip install -e ".${PY_SETUP_EXTRAS}"
  323. buildenv
  324. )
  325. local exit_val=$?
  326. if [ ! $exit_val -eq 0 ]; then
  327. die 42 "error while pip install (${PY_ENV_BIN})"
  328. fi
  329. }
  330. pyenv.uninstall() {
  331. build_msg PYENV "[pyenv.uninstall] uninstall packages: ${PYOBJECTS}"
  332. pyenv.cmd python setup.py develop --uninstall 2>&1 \
  333. | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
  334. }
  335. pypi.upload() {
  336. py.clean
  337. py.build
  338. # https://github.com/pypa/twine
  339. pyenv.cmd twine upload "${PYDIST}"/*
  340. }
  341. pypi.upload.test() {
  342. py.clean
  343. py.build
  344. pyenv.cmd twine upload -r testpypi "${PYDIST}"/*
  345. }
  346. test.yamllint() {
  347. build_msg TEST "[yamllint] \$YAMLLINT_FILES"
  348. pyenv.cmd yamllint --format parsable "${YAMLLINT_FILES[@]}"
  349. }
  350. test.pylint() {
  351. # shellcheck disable=SC2086
  352. ( set -e
  353. build_msg TEST "[pylint] \$PYLINT_FILES"
  354. pyenv.cmd python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  355. "${PYLINT_FILES[@]}"
  356. build_msg TEST "[pylint] searx/engines"
  357. pyenv.cmd python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  358. --disable="${PYLINT_SEARX_DISABLE_OPTION}" \
  359. --additional-builtins="${PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES}" \
  360. searx/engines
  361. build_msg TEST "[pylint] searx tests"
  362. pyenv.cmd python ${PYLINT_OPTIONS} ${PYLINT_VERBOSE} \
  363. --disable="${PYLINT_SEARX_DISABLE_OPTION}" \
  364. --ignore=searx/engines \
  365. searx tests
  366. )
  367. dump_return $?
  368. }
  369. test.pep8() {
  370. build_msg TEST 'pycodestyle (formerly pep8)'
  371. local _exclude=""
  372. printf -v _exclude '%s, ' "${PYLINT_FILES[@]}"
  373. pyenv.cmd pycodestyle \
  374. --exclude="searx/static, searx/languages.py, $_exclude " \
  375. --max-line-length=120 \
  376. --ignore "E117,E252,E402,E722,E741,W503,W504,W605" \
  377. searx tests
  378. dump_return $?
  379. }
  380. test.unit() {
  381. build_msg TEST 'tests/unit'
  382. pyenv.cmd python -m nose2 -s tests/unit
  383. dump_return $?
  384. }
  385. test.coverage() {
  386. build_msg TEST 'unit test coverage'
  387. ( set -e
  388. pyenv.cmd python -m nose2 -C --log-capture --with-coverage --coverage searx -s tests/unit
  389. pyenv.cmd coverage report
  390. pyenv.cmd coverage html
  391. )
  392. dump_return $?
  393. }
  394. test.robot() {
  395. build_msg TEST 'robot'
  396. gecko.driver
  397. PYTHONPATH=. pyenv.cmd python searx/testing.py robot
  398. dump_return $?
  399. }
  400. test.clean() {
  401. build_msg CLEAN "test stuff"
  402. rm -rf geckodriver.log .coverage coverage/
  403. dump_return $?
  404. }
  405. themes.all() {
  406. ( set -e
  407. pygments.less
  408. node.env
  409. themes.oscar
  410. themes.simple
  411. )
  412. dump_return $?
  413. }
  414. themes.oscar() {
  415. build_msg GRUNT "theme: oscar"
  416. npm --prefix searx/static/themes/oscar run build
  417. dump_return $?
  418. }
  419. themes.simple() {
  420. build_msg GRUNT "theme: simple"
  421. npm --prefix searx/static/themes/simple run build
  422. dump_return $?
  423. }
  424. PYLINT_FILES=()
  425. while IFS= read -r line; do
  426. PYLINT_FILES+=("$line")
  427. done <<< "$(pylint.FILES)"
  428. # shellcheck disable=SC2119
  429. main() {
  430. local _type
  431. local cmd="$1"; shift
  432. if [ "$cmd" == "" ]; then
  433. help
  434. err_msg "missing command"
  435. return 42
  436. fi
  437. case "$cmd" in
  438. --getenv) var="$1"; echo "${!var}";;
  439. --help) help;;
  440. --*)
  441. help
  442. err_msg "unknown option $cmd"
  443. return 42
  444. ;;
  445. *)
  446. _type="$(type -t "$cmd")"
  447. if [ "$_type" != 'function' ]; then
  448. err_msg "unknown command: $cmd / use --help"
  449. return 42
  450. else
  451. "$cmd" "$@"
  452. fi
  453. ;;
  454. esac
  455. }
  456. main "$@"