xena_version: Add versioning info for report file from Xena
[vswitchperf.git] / check
1 #!/bin/bash
2
3 # Copyright 2015 Intel Corporation.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #   http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # VSPERF code checker
18
19 PYLINT="pylint"
20 PYLINT_RC='pylintrc'
21 PYLINT_RATING_GATE="10"
22 FILE_REGEX="(vsperf|.*\.py)"
23 FIND_OPTIONS="-regextype posix-egrep -iregex (./)?$FILE_REGEX"
24 FILE_LIST="/tmp/vsperf_check_list.txt"
25
26 # print usage if requested
27 function usage() {
28     cat <<EOM
29 Usage: $0 [TARGET]...
30
31 Performs code check for defined TARGETs. Target can be file or directory.
32 In case that directory is specified, then it will be searched recursively
33 for all python files.
34 If TARGET is not specified, then all python files from current VSPERF
35 repository will be checked.
36 Files listed in EXCLUDE_MODULES defined in conf/00_common.conf will be skipped.
37 File will pass check if its pylint rating is greater or equal to $PYLINT_RATING_GATE.
38 Otherwise gained pylint rating will be displayed.
39
40
41     -h, --help                  Script usage
42     -m, --modified              Script will check python files, which have
43                                 been modified within current repository.
44 Examples:
45     ./check
46
47     Check all python files in current VSPERF repository except EXCLUDE_MODULES
48
49     ./check vsperf
50
51     Check just one file.
52
53     ./check -m
54
55     Check all modified files in current VSPERF repository
56
57     ./check vnfs/qemu core tools/pkt_gen
58
59     Check all python files in given directories
60
61 EOM
62 }
63
64 # compare pylint result with predefined gate
65 function rating_is_ok() {
66     # bc is not part of basic Centos installation
67     # so let us do integer comparison only
68     int_rating=`echo $1 | sed -e 's/\..*$//'`
69     [ $int_rating -ge $PYLINT_RATING_GATE ]
70 }
71
72 ##### MAIN #####
73 # check if help is requested
74 if [ "x$1" == "x-h" -o "x$1" == "x--help" ] ; then
75     usage
76     exit 0
77 fi
78
79 # check if pylint is available
80 if ! which $PYLINT &>/dev/null ; then
81     echo "$PYLINT is not available, thus check can't be executed"
82     exit 1
83 fi
84
85 # check if we were run within vsperf directory
86 if [ ! -x ./vsperf 2> /dev/null ] ; then
87     echo "`basename $0` must be run from vsperf root directory"
88     exit 2
89 fi
90
91 # get list of excluded modules
92 EXCLUDED_MODULES=`grep "^ *EXCLUDE_MODULES" conf/00_common.conf | tr '"' "'"`
93
94 # get list of files to be checked
95 rm $FILE_LIST &> /dev/null
96 if [ "x$1" == "x-m" -o "x$1" == "x--modified" ] ; then
97     # check of modified files requested
98     git status --porcelain | cut -b4- | egrep -i "^${FILE_REGEX}$" > $FILE_LIST
99 elif [ "x$*" == "x" ] ; then
100     # list is empty, check all python files
101     find . -type f $FIND_OPTIONS > $FILE_LIST
102 else
103     for item in $* ; do
104         if [ -d $item ] ; then
105             find $item -type f $FIND_OPTIONS >> $FILE_LIST
106         elif [ -f $item ] ; then
107             echo $item >> $FILE_LIST
108         else
109             echo "$item doesn't exist, thus check was aborted"
110             exit 3
111         fi
112     done
113 fi
114
115 # check if there is anything to check
116 if [ -s $FILE_LIST ] ; then
117     for pyfile in `cat $FILE_LIST | sort` ; do
118         # get base name
119         pyfile_basename="'"`basename $pyfile .py`"'"
120         # and check if it should be excluded or not
121         if ( echo $EXCLUDED_MODULES | grep -w $pyfile_basename > /dev/null ) ; then
122             printf "%-70s \e[32m%-6s\e[0m\n" $pyfile "EXCLUDED"
123             continue
124         fi
125         # run pylint and extract final rating
126         rating=`$PYLINT --rcfile $PYLINT_RC $pyfile 2>/dev/null | tail -n3 | grep rated | sed -e 's/^.*rated at \([0-9.]*\).*$/\1/'`
127         # evaluate and display aquired rating
128         if rating_is_ok $rating ; then
129             printf "%-70s \e[32m%-6s\e[0m\n" $pyfile "OK"
130         else
131             printf "%-70s \e[31m%-6s\e[0m\n" $pyfile $rating
132         fi
133     done
134 else
135     echo "Nothing to check."
136     exit 4
137 fi
138
139 # clean up
140 rm $FILE_LIST &> /dev/null
141
142 exit 0
143 ##### MAIN end #####