manage 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. EOF
  80. go.help
  81. node.help
  82. weblate.help
  83. data.help
  84. test.help
  85. themes.help
  86. static.help
  87. cat <<EOF
  88. environment ...
  89. SEARXNG_REDIS_URL : ${SEARXNG_REDIS_URL}
  90. EOF
  91. }
  92. if [ "$VERBOSE" = "1" ]; then
  93. SPHINX_VERBOSE="-v"
  94. PYLINT_VERBOSE="-v"
  95. fi
  96. # needed by sphinx-docs
  97. export DOCS_BUILD
  98. webapp.run() {
  99. local parent_proc="$$"
  100. (
  101. if [ "${LIVE_THEME}" ]; then
  102. ( themes.live "${LIVE_THEME}" )
  103. kill $parent_proc
  104. fi
  105. )&
  106. (
  107. sleep 3
  108. xdg-open http://127.0.0.1:8888/
  109. )&
  110. SEARXNG_DEBUG=1 pyenv.cmd python -m searx.webapp
  111. }
  112. docker.push() {
  113. docker.build push
  114. }
  115. docker.buildx() {
  116. docker.build buildx
  117. }
  118. # shellcheck disable=SC2119
  119. docker.build() {
  120. pyenv.install
  121. local SEARXNG_GIT_VERSION
  122. local VERSION_GITCOMMIT
  123. local GITHUB_USER
  124. local SEARXNG_IMAGE_NAME
  125. local BUILD
  126. build_msg DOCKER build
  127. # run installation in a subprocess and activate pyenv
  128. # See https://www.shellcheck.net/wiki/SC1001 and others ..
  129. # shellcheck disable=SC2031,SC2230,SC2002,SC2236,SC2143,SC1001
  130. ( set -e
  131. pyenv.activate
  132. # Check if it is a git repository
  133. if [ ! -d .git ]; then
  134. die 1 "This is not Git repository"
  135. fi
  136. if [ ! -x "$(which git)" ]; then
  137. die 1 "git is not installed"
  138. fi
  139. if ! git remote get-url origin 2> /dev/null; then
  140. die 1 "there is no remote origin"
  141. fi
  142. # This is a git repository
  143. git update-index -q --refresh
  144. python -m searx.version freeze
  145. eval "$(python -m searx.version)"
  146. # Get the last git commit id
  147. VERSION_GITCOMMIT=$(echo "$VERSION_TAG" | cut -d+ -f2)
  148. build_msg DOCKER "Last commit : $VERSION_GITCOMMIT"
  149. # define the docker image name
  150. GITHUB_USER=$(echo "${GIT_URL}" | sed 's/.*github\.com\/\([^\/]*\).*/\1/')
  151. SEARXNG_IMAGE_NAME="${SEARXNG_IMAGE_NAME:-${GITHUB_USER:-searxng}/searxng}"
  152. BUILD="build"
  153. if [ "$1" = "buildx" ]; then
  154. # buildx includes the push option
  155. CACHE_TAG="${SEARXNG_IMAGE_NAME}:latest-build-cache"
  156. 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"
  157. shift
  158. fi
  159. build_msg DOCKER "Build command: ${BUILD}"
  160. # build Docker image
  161. build_msg DOCKER "Building image ${SEARXNG_IMAGE_NAME}:${SEARXNG_GIT_VERSION}"
  162. # shellcheck disable=SC2086
  163. docker $BUILD \
  164. --build-arg BASE_IMAGE="${DEPENDENCIES_IMAGE_NAME}" \
  165. --build-arg GIT_URL="${GIT_URL}" \
  166. --build-arg SEARXNG_DOCKER_TAG="${DOCKER_TAG}" \
  167. --build-arg SEARXNG_GIT_VERSION="${VERSION_STRING}" \
  168. --build-arg VERSION_GITCOMMIT="${VERSION_GITCOMMIT}" \
  169. --build-arg LABEL_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
  170. --build-arg LABEL_VCS_REF="$(git rev-parse HEAD)" \
  171. --build-arg LABEL_VCS_URL="${GIT_URL}" \
  172. --build-arg TIMESTAMP_SETTINGS="$(git log -1 --format="%cd" --date=unix -- searx/settings.yml)" \
  173. --build-arg TIMESTAMP_UWSGI="$(git log -1 --format="%cd" --date=unix -- dockerfiles/uwsgi.ini)" \
  174. -t "${SEARXNG_IMAGE_NAME}:latest" -t "${SEARXNG_IMAGE_NAME}:${DOCKER_TAG}" .
  175. if [ "$1" = "push" ]; then
  176. docker push "${SEARXNG_IMAGE_NAME}:latest"
  177. docker push "${SEARXNG_IMAGE_NAME}:${DOCKER_TAG}"
  178. fi
  179. )
  180. dump_return $?
  181. }
  182. # shellcheck disable=SC2119
  183. gecko.driver() {
  184. pyenv.install
  185. build_msg INSTALL "gecko.driver"
  186. # run installation in a subprocess and activate pyenv
  187. ( set -e
  188. pyenv.activate
  189. INSTALLED_VERSION=$(geckodriver -V 2> /dev/null | head -1 | awk '{ print "v" $2}') || INSTALLED_VERSION=""
  190. set +e
  191. if [ "${INSTALLED_VERSION}" = "${GECKODRIVER_VERSION}" ]; then
  192. build_msg INSTALL "geckodriver already installed"
  193. return
  194. fi
  195. PLATFORM="$(python -c 'import platform; print(platform.system().lower(), platform.architecture()[0])')"
  196. case "$PLATFORM" in
  197. "linux 32bit" | "linux2 32bit") ARCH="linux32";;
  198. "linux 64bit" | "linux2 64bit") ARCH="linux64";;
  199. "windows 32 bit") ARCH="win32";;
  200. "windows 64 bit") ARCH="win64";;
  201. "mac 64bit") ARCH="macos";;
  202. esac
  203. GECKODRIVER_URL="https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-$ARCH.tar.gz";
  204. build_msg GECKO "Installing ${PY_ENV_BIN}/geckodriver from $GECKODRIVER_URL"
  205. FILE="$(mktemp)"
  206. wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "${PY_ENV_BIN}" -f "$FILE" geckodriver
  207. rm -- "$FILE"
  208. chmod 755 -- "${PY_ENV_BIN}/geckodriver"
  209. )
  210. dump_return $?
  211. }
  212. py.build() {
  213. build_msg BUILD "python package ${PYDIST}"
  214. pyenv.cmd python setup.py \
  215. sdist -d "${PYDIST}" \
  216. bdist_wheel --bdist-dir "${PYBUILD}" -d "${PYDIST}"
  217. }
  218. py.clean() {
  219. build_msg CLEAN pyenv
  220. ( set -e
  221. pyenv.drop
  222. [ "$VERBOSE" = "1" ] && set -x
  223. rm -rf "${PYDIST}" "${PYBUILD}" "${PY_ENV}" ./.tox ./*.egg-info
  224. find . -name '*.pyc' -exec rm -f {} +
  225. find . -name '*.pyo' -exec rm -f {} +
  226. find . -name __pycache__ -exec rm -rf {} +
  227. )
  228. }
  229. pyenv.check() {
  230. cat <<EOF
  231. import yaml
  232. print('import yaml --> OK')
  233. EOF
  234. }
  235. pyenv.install() {
  236. if ! pyenv.OK; then
  237. py.clean > /dev/null
  238. fi
  239. if pyenv.install.OK > /dev/null; then
  240. return 0
  241. fi
  242. ( set -e
  243. pyenv
  244. build_msg PYENV "[install] pip install --use-pep517 --no-build-isolation -e 'searx${PY_SETUP_EXTRAS}'"
  245. "${PY_ENV_BIN}/python" -m pip install --use-pep517 --no-build-isolation -e ".${PY_SETUP_EXTRAS}"
  246. )
  247. local exit_val=$?
  248. if [ ! $exit_val -eq 0 ]; then
  249. die 42 "error while pip install (${PY_ENV_BIN})"
  250. fi
  251. }
  252. pyenv.uninstall() {
  253. build_msg PYENV "[pyenv.uninstall] uninstall packages: ${PYOBJECTS}"
  254. pyenv.cmd python setup.py develop --uninstall 2>&1 \
  255. | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
  256. }
  257. format.python() {
  258. build_msg TEST "[format.python] black \$BLACK_TARGETS"
  259. pyenv.cmd black "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
  260. dump_return $?
  261. }
  262. # shellcheck disable=SC2119
  263. main() {
  264. local _type
  265. local cmd="$1"; shift
  266. if [ "$cmd" == "" ]; then
  267. help
  268. err_msg "missing command"
  269. return 42
  270. fi
  271. case "$cmd" in
  272. --getenv) var="$1"; echo "${!var}";;
  273. --help) help;;
  274. --*)
  275. help
  276. err_msg "unknown option $cmd"
  277. return 42
  278. ;;
  279. *)
  280. _type="$(type -t "$cmd")"
  281. if [ "$_type" != 'function' ]; then
  282. err_msg "unknown command: $cmd / use --help"
  283. return 42
  284. else
  285. "$cmd" "$@"
  286. fi
  287. ;;
  288. esac
  289. }
  290. main "$@"