manage 15 KB

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