manage 15 KB

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