manage 16 KB

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