manage 10 KB

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