manage.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #!/bin/sh
  2. export LANG=C
  3. BASE_DIR="$(dirname -- "`readlink -f -- "$0"`")"
  4. cd -- "$BASE_DIR"
  5. set -e
  6. # subshell
  7. PYTHONPATH="$BASE_DIR"
  8. SEARX_DIR="$BASE_DIR/searx"
  9. ACTION="$1"
  10. #
  11. # Python
  12. #
  13. update_packages() {
  14. pip install --upgrade pip
  15. pip install --upgrade setuptools
  16. pip install -r "$BASE_DIR/requirements.txt"
  17. }
  18. update_dev_packages() {
  19. update_packages
  20. pip install -r "$BASE_DIR/requirements-dev.txt"
  21. }
  22. install_geckodriver() {
  23. echo '[!] Checking geckodriver'
  24. # TODO : check the current geckodriver version
  25. set -e
  26. geckodriver -V > /dev/null 2>&1 || NOTFOUND=1
  27. set +e
  28. if [ -z "$NOTFOUND" ]; then
  29. return
  30. fi
  31. GECKODRIVER_VERSION="v0.24.0"
  32. PLATFORM="`python -c "import six; import platform; six.print_(platform.system().lower(), platform.architecture()[0])"`"
  33. case "$PLATFORM" in
  34. "linux 32bit" | "linux2 32bit") ARCH="linux32";;
  35. "linux 64bit" | "linux2 64bit") ARCH="linux64";;
  36. "windows 32 bit") ARCH="win32";;
  37. "windows 64 bit") ARCH="win64";;
  38. "mac 64bit") ARCH="macos";;
  39. esac
  40. GECKODRIVER_URL="https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-$ARCH.tar.gz";
  41. if [ -z "$1" ]; then
  42. if [ -z "$VIRTUAL_ENV" ]; then
  43. printf "geckodriver can't be installed because VIRTUAL_ENV is not set, you should download it from\n %s" "$GECKODRIVER_URL"
  44. exit
  45. else
  46. GECKODRIVER_DIR="$VIRTUAL_ENV/bin"
  47. fi
  48. else
  49. GECKODRIVER_DIR="$1"
  50. mkdir -p -- "$GECKODRIVER_DIR"
  51. fi
  52. printf "Installing %s/geckodriver from\n %s" "$GECKODRIVER_DIR" "$GECKODRIVER_URL"
  53. FILE="`mktemp`"
  54. wget -qO "$FILE" -- "$GECKODRIVER_URL" && tar xz -C "$GECKODRIVER_DIR" -f "$FILE" geckodriver
  55. rm -- "$FILE"
  56. chmod 777 -- "$GECKODRIVER_DIR/geckodriver"
  57. }
  58. locales() {
  59. pybabel compile -d "$SEARX_DIR/translations"
  60. }
  61. update_useragents() {
  62. echo '[!] Updating user agent versions'
  63. python utils/fetch_firefox_version.py
  64. }
  65. pep8_check() {
  66. echo '[!] Running pep8 check'
  67. # ignored rules:
  68. # E402 module level import not at top of file
  69. # W503 line break before binary operator
  70. pep8 --exclude=searx/static --max-line-length=120 --ignore "E402,W503" "$SEARX_DIR" "$BASE_DIR/tests"
  71. }
  72. unit_tests() {
  73. echo '[!] Running unit tests'
  74. python -m nose2 -s "$BASE_DIR/tests/unit"
  75. }
  76. py_test_coverage() {
  77. echo '[!] Running python test coverage'
  78. PYTHONPATH="`pwd`" python -m nose2 -C --log-capture --with-coverage --coverage "$SEARX_DIR" -s "$BASE_DIR/tests/unit" \
  79. && coverage report \
  80. && coverage html
  81. }
  82. robot_tests() {
  83. echo '[!] Running robot tests'
  84. PYTHONPATH="`pwd`" python "$SEARX_DIR/testing.py" robot
  85. }
  86. tests() {
  87. set -e
  88. pep8_check
  89. unit_tests
  90. install_geckodriver
  91. robot_tests
  92. set +e
  93. }
  94. #
  95. # Web
  96. #
  97. npm_path_setup() {
  98. which npm || (printf 'Error: npm is not found\n'; exit 1)
  99. export PATH="$(npm bin)":$PATH
  100. }
  101. npm_packages() {
  102. npm_path_setup
  103. echo '[!] install NPM packages'
  104. cd -- "$BASE_DIR"
  105. npm install less@2.7 less-plugin-clean-css grunt-cli
  106. echo '[!] install NPM packages for oscar theme'
  107. cd -- "$BASE_DIR/searx/static/themes/oscar"
  108. npm install
  109. echo '[!] install NPM packages for simple theme'
  110. cd -- "$BASE_DIR/searx/static/themes/simple"
  111. npm install
  112. }
  113. build_style() {
  114. npm_path_setup
  115. lessc --clean-css="--s1 --advanced --compatibility=ie9" "$BASE_DIR/searx/static/$1" "$BASE_DIR/searx/static/$2"
  116. }
  117. styles() {
  118. npm_path_setup
  119. echo '[!] Building legacy style'
  120. build_style themes/legacy/less/style.less themes/legacy/css/style.css
  121. build_style themes/legacy/less/style-rtl.less themes/legacy/css/style-rtl.css
  122. echo '[!] Building courgette style'
  123. build_style themes/courgette/less/style.less themes/courgette/css/style.css
  124. build_style themes/courgette/less/style-rtl.less themes/courgette/css/style-rtl.css
  125. echo '[!] Building pix-art style'
  126. build_style themes/pix-art/less/style.less themes/pix-art/css/style.css
  127. echo '[!] Building bootstrap style'
  128. build_style less/bootstrap/bootstrap.less css/bootstrap.min.css
  129. }
  130. grunt_build() {
  131. npm_path_setup
  132. echo '[!] Grunt build : oscar theme'
  133. grunt --gruntfile "$SEARX_DIR/static/themes/oscar/gruntfile.js"
  134. echo '[!] Grunt build : simple theme'
  135. grunt --gruntfile "$SEARX_DIR/static/themes/simple/gruntfile.js"
  136. }
  137. docker_build() {
  138. # Check if it is a git repository
  139. if [ ! -d .git ]; then
  140. echo "This is not Git repository"
  141. exit 1
  142. fi
  143. if [ ! -x "$(which git)" ]; then
  144. echo "git is not installed"
  145. exit 1
  146. fi
  147. if [ ! git remote get-url origin 2> /dev/null ]; then
  148. echo "there is no remote origin"
  149. exit 1
  150. fi
  151. # This is a git repository
  152. # "git describe" to get the Docker version (for example : v0.15.0-89-g0585788e)
  153. # awk to remove the "v" and the "g"
  154. SEARX_GIT_VERSION=$(git describe --match "v[0-9]*\.[0-9]*\.[0-9]*" HEAD 2>/dev/null | awk -F'-' '{OFS="-"; $1=substr($1, 2); $3=substr($3, 2); print}')
  155. # add the suffix "-dirty" if the repository has uncommited change
  156. git update-index -q --refresh
  157. if [ ! -z "$(git diff-index --name-only HEAD --)" ]; then
  158. SEARX_GIT_VERSION="${SEARX_GIT_VERSION}-dirty"
  159. fi
  160. # Get the last git commit id, will be added to the Searx version (see Dockerfile)
  161. VERSION_GITCOMMIT=$(echo $SEARX_GIT_VERSION | cut -d- -f2-4)
  162. echo "Last commit : $VERSION_GITCOMMIT"
  163. # Check consistency between the git tag and the searx/version.py file
  164. # /!\ HACK : parse Python file with bash /!\
  165. # otherwise it is not possible build the docker image without all Python dependencies ( version.py loads __init__.py )
  166. # SEARX_PYTHON_VERSION=$(python -c "import six; import searx.version; six.print_(searx.version.VERSION_STRING)")
  167. SEARX_PYTHON_VERSION=$(cat searx/version.py | grep "\(VERSION_MAJOR\|VERSION_MINOR\|VERSION_BUILD\) =" | cut -d\= -f2 | sed -e 's/^[[:space:]]*//' | paste -sd "." -)
  168. if [ $(echo "$SEARX_GIT_VERSION" | cut -d- -f1) != "$SEARX_PYTHON_VERSION" ]; then
  169. echo "Inconsistency between the last git tag and the searx/version.py file"
  170. echo "git tag: $SEARX_GIT_VERSION"
  171. echo "searx/version.py: $SEARX_PYTHON_VERSION"
  172. exit 1
  173. fi
  174. # define the docker image name
  175. # /!\ HACK to get the user name /!\
  176. GITHUB_USER=$(git remote get-url origin | sed 's/.*github\.com\/\([^\/]*\).*/\1/')
  177. SEARX_IMAGE_NAME="${GITHUB_USER:-searx}/searx"
  178. # build Docker image
  179. echo "Building image ${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}"
  180. sudo docker build \
  181. --build-arg SEARX_GIT_VERSION="${SEARX_GIT_VERSION}" \
  182. --build-arg VERSION_GITCOMMIT="${VERSION_GITCOMMIT}" \
  183. --build-arg LABEL_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
  184. --build-arg LABEL_VCS_REF=$(git rev-parse HEAD) \
  185. --build-arg LABEL_VCS_URL=$(git remote get-url origin) \
  186. --build-arg TIMESTAMP_SETTINGS=$(git log -1 --format="%cd" --date=unix -- searx/settings.yml) \
  187. --build-arg TIMESTAMP_UWSGI=$(git log -1 --format="%cd" --date=unix -- dockerfiles/uwsgi.ini) \
  188. -t ${SEARX_IMAGE_NAME}:latest -t ${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION} .
  189. if [ "$1" = "push" ]; then
  190. sudo docker push ${SEARX_IMAGE_NAME}:latest
  191. sudo docker push ${SEARX_IMAGE_NAME}:${SEARX_GIT_VERSION}
  192. fi
  193. }
  194. #
  195. # Help
  196. #
  197. help() {
  198. [ -z "$1" ] || printf 'Error: %s\n' "$1"
  199. echo "Searx manage.sh help
  200. Commands
  201. ========
  202. help - This text
  203. Build requirements
  204. ------------------
  205. update_packages - Check & update production dependency changes
  206. update_dev_packages - Check & update development and production dependency changes
  207. install_geckodriver - Download & install geckodriver if not already installed (required for robot_tests)
  208. npm_packages - Download & install npm dependencies
  209. update_useragents - Update useragents.json with the most recent versions of Firefox
  210. Build
  211. -----
  212. locales - Compile locales
  213. styles - Build less files
  214. grunt_build - Build files for themes
  215. docker_build - Build Docker image
  216. Tests
  217. -----
  218. unit_tests - Run unit tests
  219. pep8_check - Pep8 validation
  220. robot_tests - Run selenium tests
  221. tests - Run all python tests (pep8, unit, robot_tests)
  222. py_test_coverage - Unit test coverage
  223. "
  224. }
  225. [ "$(command -V "$ACTION" | grep ' function$')" = "" ] \
  226. && help "action not found" \
  227. || "$ACTION" "$2"