INFRA: Switch to tox for JJB verify and merge jobs
[infra/cicd.git] / jjb / infra / lint.sh
1 #!/bin/bash
2
3 set -o nounset
4 set -o errexit
5 set -o pipefail
6
7 #-------------------------------------------------------------------------------
8 # Various tests are run using tox such as jjbtest, yamllint, and shellehck.
9 #-------------------------------------------------------------------------------
10 run_tox() {
11
12   echo "Info  : Preparing to run tox for the repo $GERRIT_PROJECT"
13
14   # set and export defaults
15   LINT_TYPE="${LINT_TYPE:-jjbtest}"
16   VERBOSITY=${VERBOSITY:-false}
17   export LINT_TYPE VERBOSITY
18
19   # ensure we are in job build WORKSPACE
20   cd "$WORKSPACE"
21
22   # set DEBIAN_FRONTEND to run apt non-interactively
23   DEBIAN_FRONTEND=noninteractive
24   export DEBIAN_FRONTEND
25
26   # Wait for other apt auto-update process to finish by checking the dpkg lock file.
27   DPKG_LOCK="/var/lib/dpkg/lock-frontend"
28   try=0
29   while sudo lsof "${DPKG_LOCK}"  > /dev/null 2>&1 ; do
30     echo "DPKG file locked: ${DPKG_LOCK}."
31     echo "   Waiting for another pkg instalaltion process to finish ..."
32     sleep 10
33     if [[ ${try} -gt 120 ]] ; then
34       echo "ERROR: Max number of re-tries reached, exiting..."
35       exit 1
36     fi
37     try=$((try + 1))
38   done
39
40   # install dependencies
41   echo "Info  : Install python3.7-minimal python3-distutils virtualenv using apt"
42   redirect_cmd sudo apt update
43   redirect_cmd sudo apt install -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew -y \
44      python3.7-minimal python3-distutils virtualenv
45
46   # create and activate virtualenv
47   echo "Info  : Create and activate python virtualenv"
48   redirect_cmd virtualenv -p python3 .venv
49   set +u
50   redirect_cmd source .venv/bin/activate
51   set -u
52
53   # install only tox since the rest of the requirements are installed by tox itself
54   TOX_PACKAGE=$(grep "^tox==" jjb/infra/test-requirements.txt)
55   echo "Info  : Install $TOX_PACKAGE"
56   redirect_cmd pip install --force-reinstall "$TOX_PACKAGE"
57
58   # run tox
59   echo "Info  : Run $LINT_TYPE using tox"
60   echo "----------------------------------------------------"
61   tox -e "$LINT_TYPE"
62   echo "----------------------------------------------------"
63   echo "Info  : Done!"
64
65 }
66 #-------------------------------------------------------------------------------
67 # In some cases, it is useful to see all the output generated by commands so
68 # this function makes it possible for users to achieve that by not redirecting
69 # output to /dev/null when verbosity is enabled
70 #-------------------------------------------------------------------------------
71 redirect_cmd() {
72
73   if [[ "$VERBOSITY" == "false" ]]; then
74     "$@" > /dev/null 2>&1
75   else
76     "$@"
77   fi
78
79 }
80 #-------------------------------------------------------------------------------
81 # run tox
82 #-------------------------------------------------------------------------------
83
84 run_tox "$@"
85
86 # vim: set ts=2 sw=2 ft=bash  expandtab: