manage 15 KB

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