Remove the use of tox-pip-version
[releng.git] / jjb / kuberef / kuberef-run-linting.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2020 Samsung Electronics
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set -o nounset
12 set -o pipefail
13 set -o xtrace
14
15 # _vercmp() - Function that compares two versions
16 function _vercmp {
17     local v1=$1
18     local op=$2
19     local v2=$3
20     local result
21
22     # sort the two numbers with sort's "-V" argument.  Based on if v2
23     # swapped places with v1, we can determine ordering.
24     result=$(echo -e "$v1\n$v2" | sort -V | head -1)
25
26     case $op in
27         "==")
28             [ "$v1" = "$v2" ]
29             return
30             ;;
31         ">")
32             [ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
33             return
34             ;;
35         "<")
36             [ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
37             return
38             ;;
39         ">=")
40             [ "$result" = "$v2" ]
41             return
42             ;;
43         "<=")
44             [ "$result" = "$v1" ]
45             return
46             ;;
47         *)
48             die $LINENO "unrecognised op: $op"
49             ;;
50     esac
51 }
52
53 echo "Requirements validation"
54 # shellcheck disable=SC1091
55 source /etc/os-release || source /usr/lib/os-release
56
57 min_shellcheck_version=0.4.0
58 min_tox_version=3.5
59
60 pkgs=""
61 if ! command -v shellcheck; then
62     case ${ID,,} in
63         *suse*|rhel|centos|fedora)
64             pkgs="ShellCheck"
65         ;;
66         ubuntu|debian)
67             pkgs="shellcheck"
68         ;;
69     esac
70 fi
71 if ! command -v pip; then
72     case ${ID,,} in
73         *suse*|rhel|centos|fedora)
74             pkgs+=" python3-pip python3-setuptools"
75         ;;
76         ubuntu|debian)
77             if _vercmp "${VERSION_ID}" '<=' "18.04"; then
78                 pkgs+=" python-pip python-setuptools"
79             else
80                 pkgs+=" python3-pip python3-setuptools"
81             fi
82         ;;
83     esac
84 fi
85
86 if [ -n "$pkgs" ]; then
87     echo "Requirements installation"
88     case ${ID,,} in
89         *suse*)
90             sudo zypper install --gpg-auto-import-keys refresh
91             # shellcheck disable=SC2086
92             sudo -H -E zypper install -y --no-recommends $pkgs
93         ;;
94         ubuntu|debian)
95             sudo apt-get update
96             # shellcheck disable=SC2086
97             sudo -H -E apt-get -y --no-install-recommends install $pkgs
98         ;;
99         rhel|centos|fedora)
100             PKG_MANAGER=$(command -v dnf || command -v yum)
101             if ! sudo "$PKG_MANAGER" repolist | grep "epel/"; then
102                 sudo -H -E "$PKG_MANAGER" -q -y install epel-release
103             fi
104             sudo "$PKG_MANAGER" updateinfo --assumeyes
105             # shellcheck disable=SC2086
106             sudo -H -E "$PKG_MANAGER" -y install $pkgs
107         ;;
108     esac
109     if ! command -v pip && command -v pip3 ; then
110         sudo ln -s "$(command -v pip3)" /usr/bin/pip
111     fi
112     sudo "$(command -v pip)" install --upgrade pip
113 fi
114
115 if ! command -v tox || _vercmp "$(tox --version | awk '{print $1}')" '<' "$min_tox_version"; then
116     sudo "$(command -v pip)" install tox==$min_tox_version
117 fi
118
119 echo "Server tools information:"
120 python -V
121 tox --version
122 shellcheck -V
123
124 echo "Linting process execution"
125 tox -e lint
126 if _vercmp "$(shellcheck --version | awk 'FNR==2{print $2}')" '<' "$min_shellcheck_version"; then
127     bash -c 'shopt -s globstar; shellcheck **/*.sh'
128 else
129     bash -c 'shopt -s globstar; shellcheck -x **/*.sh'
130 fi