manage 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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=SC2034
  5. main_cmd="$(basename "$0")"
  6. # shellcheck source=utils/lib.sh
  7. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib.sh"
  8. # shellcheck source=utils/lib.sh
  9. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_nvm.sh"
  10. # shellcheck source=utils/lib_sxng_data.sh
  11. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_sxng_data.sh"
  12. # shellcheck source=utils/lib_sxng_weblate.sh
  13. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_sxng_weblate.sh"
  14. # shellcheck source=utils/lib_sxng_static.sh
  15. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_sxng_static.sh"
  16. # shellcheck source=utils/lib_sxng_node.sh
  17. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_sxng_node.sh"
  18. # shellcheck source=utils/lib_sxng_themes.sh
  19. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_sxng_themes.sh"
  20. # shellcheck source=utils/lib_sxng_test.sh
  21. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_sxng_test.sh"
  22. # shellcheck source=utils/lib_go.sh
  23. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_go.sh"
  24. # shellcheck source=utils/lib_redis.sh
  25. source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_redis.sh"
  26. PATH="${REPO_ROOT}/node_modules/.bin:${PATH}"
  27. # config
  28. PYOBJECTS="searx"
  29. PY_SETUP_EXTRAS='[test]'
  30. GECKODRIVER_VERSION="v0.34.0"
  31. # SPHINXOPTS=
  32. BLACK_OPTIONS=("--target-version" "py311" "--line-length" "120" "--skip-string-normalization")
  33. BLACK_TARGETS=("--exclude" "(searx/static|searx/languages.py)" "--include" 'searxng.msg|\.pyi?$' "searx" "searxng_extra" "tests")
  34. _dev_redis_sock="/usr/local/searxng-redis/run/redis.sock"
  35. # set SEARXNG_REDIS_URL if it is not defined and "{_dev_redis_sock}" exists.
  36. if [ -S "${_dev_redis_sock}" ] && [ -z "${SEARXNG_REDIS_URL}" ]; then
  37. export SEARXNG_REDIS_URL="unix://${_dev_redis_sock}?db=0"
  38. fi
  39. pylint.FILES() {
  40. # List files tagged by comment:
  41. #
  42. # # lint: pylint
  43. #
  44. # These py files are linted by test.pylint()
  45. grep -l -r --include \*.py '^#[[:blank:]]*lint:[[:blank:]]*pylint' searx searxng_extra tests
  46. find . -name searxng.msg
  47. }
  48. YAMLLINT_FILES=()
  49. while IFS= read -r line; do
  50. YAMLLINT_FILES+=("$line")
  51. done <<< "$(git ls-files './tests/*.yml' './searx/*.yml' './utils/templates/etc/searxng/*.yml')"
  52. RST_FILES=(
  53. 'README.rst'
  54. )
  55. PYLINT_SEARXNG_DISABLE_OPTION="\
  56. I,C,R,\
  57. W0105,W0212,W0511,W0603,W0613,W0621,W0702,W0703,W1401,\
  58. E1136"
  59. PYLINT_ADDITIONAL_BUILTINS_FOR_ENGINES="traits,supported_languages,language_aliases,logger,categories"
  60. PYLINT_OPTIONS="-m pylint -j 0 --rcfile .pylintrc"
  61. help() {
  62. nvm.help
  63. cat <<EOF
  64. webapp.:
  65. run : run developer instance
  66. docs.:
  67. html : build HTML documentation
  68. live : autobuild HTML documentation while editing
  69. gh-pages : deploy on gh-pages branch
  70. prebuild : build reST include files (./${DOCS_BUILD}/includes)
  71. clean : clean documentation build
  72. docker.:
  73. build : build docker image
  74. push : build and push docker image
  75. gecko.driver:
  76. download & install geckodriver if not already installed (required for
  77. robot_tests)
  78. redis:
  79. build : build redis binaries at $(redis._get_dist)
  80. install : create user (${REDIS_USER}) and install systemd service (${REDIS_SERVICE_NAME})
  81. help : show more redis commands
  82. py.:
  83. build : Build python packages at ./${PYDIST}
  84. clean : delete virtualenv and intermediate py files
  85. pyenv.:
  86. install : developer install of SearXNG into virtualenv
  87. uninstall : uninstall developer installation
  88. cmd ... : run command ... in virtualenv
  89. OK : test if virtualenv is OK
  90. format.:
  91. python : format Python code source using black
  92. pygments.:
  93. less : build LESS files for pygments
  94. EOF
  95. go.help
  96. node.help
  97. weblate.help
  98. data.help
  99. test.help
  100. themes.help
  101. static.help
  102. cat <<EOF
  103. environment ...
  104. SEARXNG_REDIS_URL : ${SEARXNG_REDIS_URL}
  105. EOF
  106. }
  107. if [ "$VERBOSE" = "1" ]; then
  108. SPHINX_VERBOSE="-v"
  109. PYLINT_VERBOSE="-v"
  110. fi
  111. # needed by sphinx-docs
  112. export DOCS_BUILD
  113. webapp.run() {
  114. local parent_proc="$$"
  115. (
  116. if [ "${LIVE_THEME}" ]; then
  117. ( themes.live "${LIVE_THEME}" )
  118. kill $parent_proc
  119. fi
  120. )&
  121. (
  122. sleep 3
  123. xdg-open http://127.0.0.1:8888/
  124. )&
  125. SEARXNG_DEBUG=1 pyenv.cmd python -m searx.webapp
  126. }
  127. docker.push() {
  128. docker.build push
  129. }
  130. docker.buildx() {
  131. docker.build buildx
  132. }
  133. # shellcheck disable=SC2119
  134. docker.build() {
  135. pyenv.install
  136. local SEARXNG_GIT_VERSION
  137. local VERSION_GITCOMMIT
  138. local GITHUB_USER
  139. local SEARXNG_IMAGE_NAME
  140. local BUILD
  141. build_msg DOCKER build
  142. # run installation in a subprocess and activate pyenv
  143. # See https://www.shellcheck.net/wiki/SC1001 and others ..
  144. # shellcheck disable=SC2031,SC2230,SC2002,SC2236,SC2143,SC1001
  145. ( set -e
  146. pyenv.activate
  147. # Check if it is a git repository
  148. if [ ! -d .git ]; then
  149. die 1 "This is not Git repository"
  150. fi
  151. if [ ! -x "$(which git)" ]; then
  152. die 1 "git is not installed"
  153. fi
  154. if ! git remote get-url origin 2> /dev/null; then
  155. die 1 "there is no remote origin"
  156. fi
  157. # This is a git repository
  158. git update-index -q --refresh
  159. python -m searx.version freeze
  160. eval "$(python -m searx.version)"
  161. # Get the last git commit id
  162. VERSION_GITCOMMIT=$(echo "$VERSION_TAG" | cut -d+ -f2)
  163. build_msg DOCKER "Last commit : $VERSION_GITCOMMIT"
  164. # define the docker image name
  165. GITHUB_USER=$(echo "${GIT_URL}" | sed 's/.*github\.com\/\([^\/]*\).*/\1/')
  166. SEARXNG_IMAGE_NAME="${SEARXNG_IMAGE_NAME:-${GITHUB_USER:-searxng}/searxng}"
  167. BUILD="build"
  168. if [ "$1" = "buildx" ]; then
  169. # buildx includes the push option
  170. CACHE_TAG="${SEARXNG_IMAGE_NAME}:latest-build-cache"
  171. 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"
  172. shift
  173. fi
  174. build_msg DOCKER "Build command: ${BUILD}"
  175. # build Docker image
  176. build_msg DOCKER "Building image ${SEARXNG_IMAGE_NAME}:${SEARXNG_GIT_VERSION}"
  177. # shellcheck disable=SC2086
  178. docker $BUILD \
  179. --build-arg BASE_IMAGE="${DEPENDENCIES_IMAGE_NAME}" \
  180. --build-arg GIT_URL="${GIT_URL}" \
  181. --build-arg SEARXNG_DOCKER_TAG="${DOCKER_TAG}" \
  182. --build-arg SEARXNG_GIT_VERSION="${VERSION_STRING}" \
  183. --build-arg VERSION_GITCOMMIT="${VERSION_GITCOMMIT}" \
  184. --build-arg LABEL_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
  185. --build-arg LABEL_VCS_REF="$(git rev-parse HEAD)" \
  186. --build-arg LABEL_VCS_URL="${GIT_URL}" \
  187. --build-arg TIMESTAMP_SETTINGS="$(git log -1 --format="%cd" --date=unix -- searx/settings.yml)" \
  188. --build-arg TIMESTAMP_UWSGI="$(git log -1 --format="%cd" --date=unix -- dockerfiles/uwsgi.ini)" \
  189. -t "${SEARXNG_IMAGE_NAME}:latest" -t "${SEARXNG_IMAGE_NAME}:${DOCKER_TAG}" .
  190. if [ "$1" = "push" ]; then
  191. docker push "${SEARXNG_IMAGE_NAME}:latest"
  192. docker push "${SEARXNG_IMAGE_NAME}:${DOCKER_TAG}"
  193. fi
  194. )
  195. dump_return $?
  196. }
  197. # shellcheck disable=SC2119
  198. gecko.driver() {
  199. pyenv.install
  200. build_msg INSTALL "gecko.driver"
  201. # run installation in a subprocess and activate pyenv
  202. ( set -e
  203. pyenv.activate
  204. INSTALLED_VERSION=$(geckodriver -V 2> /dev/null | head -1 | awk '{ print "v" $2}') || INSTALLED_VERSION=""
  205. set +e
  206. if [ "${INSTALLED_VERSION}" = "${GECKODRIVER_VERSION}" ]; then
  207. build_msg INSTALL "geckodriver already installed"
  208. return
  209. fi
  210. PLATFORM="$(python3 -c 'import platform; print(platform.system().lower(), platform.architecture()[0])')"
  211. case "$PLATFORM" in
  212. "linux 32bit" | "linux2 32bit") ARCH="linux32";;
  213. "linux 64bit" | "linux2 64bit") ARCH="linux64";;
  214. "windows 32 bit") ARCH="win32";;
  215. "windows 64 bit") ARCH="win64";;
  216. "mac 64bit") ARCH="macos";;
  217. esac
  218. GECKODRIVER_URL="https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-$ARCH.tar.gz";
  219. build_msg GECKO "Installing ${PY_ENV_BIN}/geckodriver from $GECKODRIVER_URL"
  220. FILE="$(mktemp)"
  221. wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "${PY_ENV_BIN}" -f "$FILE" geckodriver
  222. rm -- "$FILE"
  223. chmod 755 -- "${PY_ENV_BIN}/geckodriver"
  224. )
  225. dump_return $?
  226. }
  227. pygments.less() {
  228. build_msg PYGMENTS "searxng_extra/update/update_pygments.py"
  229. if ! pyenv.cmd python searxng_extra/update/update_pygments.py; then
  230. build_msg PYGMENTS "building LESS files for pygments failed"
  231. return 1
  232. fi
  233. return 0
  234. }
  235. py.build() {
  236. build_msg BUILD "python package ${PYDIST}"
  237. pyenv.cmd python setup.py \
  238. sdist -d "${PYDIST}" \
  239. bdist_wheel --bdist-dir "${PYBUILD}" -d "${PYDIST}"
  240. }
  241. py.clean() {
  242. build_msg CLEAN pyenv
  243. ( set -e
  244. pyenv.drop
  245. [ "$VERBOSE" = "1" ] && set -x
  246. rm -rf "${PYDIST}" "${PYBUILD}" "${PY_ENV}" ./.tox ./*.egg-info
  247. find . -name '*.pyc' -exec rm -f {} +
  248. find . -name '*.pyo' -exec rm -f {} +
  249. find . -name __pycache__ -exec rm -rf {} +
  250. )
  251. }
  252. pyenv.check() {
  253. cat <<EOF
  254. import yaml
  255. print('import yaml --> OK')
  256. EOF
  257. }
  258. pyenv.install() {
  259. if ! pyenv.OK; then
  260. py.clean > /dev/null
  261. fi
  262. if pyenv.install.OK > /dev/null; then
  263. return 0
  264. fi
  265. ( set -e
  266. pyenv
  267. build_msg PYENV "[install] pip install -e 'searx${PY_SETUP_EXTRAS}'"
  268. "${PY_ENV_BIN}/python" -m pip install -e ".${PY_SETUP_EXTRAS}"
  269. )
  270. local exit_val=$?
  271. if [ ! $exit_val -eq 0 ]; then
  272. die 42 "error while pip install (${PY_ENV_BIN})"
  273. fi
  274. }
  275. pyenv.uninstall() {
  276. build_msg PYENV "[pyenv.uninstall] uninstall packages: ${PYOBJECTS}"
  277. pyenv.cmd python setup.py develop --uninstall 2>&1 \
  278. | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
  279. }
  280. format.python() {
  281. build_msg TEST "[format.python] black \$BLACK_TARGETS"
  282. pyenv.cmd black "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
  283. dump_return $?
  284. }
  285. PYLINT_FILES=()
  286. while IFS= read -r line; do
  287. PYLINT_FILES+=("$line")
  288. done <<< "$(pylint.FILES)"
  289. # shellcheck disable=SC2119
  290. main() {
  291. local _type
  292. local cmd="$1"; shift
  293. if [ "$cmd" == "" ]; then
  294. help
  295. err_msg "missing command"
  296. return 42
  297. fi
  298. case "$cmd" in
  299. --getenv) var="$1"; echo "${!var}";;
  300. --help) help;;
  301. --*)
  302. help
  303. err_msg "unknown option $cmd"
  304. return 42
  305. ;;
  306. *)
  307. _type="$(type -t "$cmd")"
  308. if [ "$_type" != 'function' ]; then
  309. err_msg "unknown command: $cmd / use --help"
  310. return 42
  311. else
  312. "$cmd" "$@"
  313. fi
  314. ;;
  315. esac
  316. }
  317. main "$@"