manage 11 KB

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