manage 11 KB

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