manage 12 KB

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