manage 15 KB

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