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