3 # Copyright 2017 Intel Corporation.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 PYLINT_RATING_GATE="10"
22 PYLINT_RATING_MIN=$PYLINT_RATING_GATE
23 FILE_REGEX="(^vsperf|\.py)$"
24 FILE_LIST="/tmp/vsperf_check_list.txt"
27 # print usage if requested
32 Performs code check for defined TARGETs. Target can be file or directory.
33 In case that directory is specified, then it will be searched recursively
35 If TARGET is not specified, then all python files from current VSPERF
36 repository will be checked.
37 Files listed in EXCLUDE_MODULES defined in conf/00_common.conf will be skipped.
38 File will pass check if its pylint rating is greater or equal to $PYLINT_RATING_GATE.
39 Otherwise gained pylint rating will be displayed.
42 -h, --help Script usage
43 -b, --black Suppress colours. Output will be black&white.
44 -m, --modified Script will check python files, which have
45 been modified within current repository.
50 Check all python files in current VSPERF repository except EXCLUDE_MODULES
58 Check all modified files in current VSPERF repository
60 ./check vnfs/qemu core tools/pkt_gen
62 Check all python files in given directories
67 # compare pylint result with predefined gate
68 function rating_is_ok() {
69 # bc is not part of basic Centos installation
70 # so let us check if it is available
71 if [ "x$BC" == "x" ] ; then
72 # no bc, so do integer comparison only
73 int_rating=`echo $1 | sed -e 's/\..*$//'`
74 int_rating_min=`echo $PYLINT_RATING_MIN | sed -e 's/\..*$//'`
75 [ $int_rating -lt $int_rating_min ] && PYLINT_RATING_MIN=$int_rating
76 if [ $int_rating -lt $PYLINT_RATING_GATE ] ; then
82 if (( $(echo "$1<$PYLINT_RATING_MIN" | bc -l) )) ; then
85 if (( $(echo "$1<$PYLINT_RATING_GATE" | bc -l) )) ; then
94 # check if help is requested
95 if [ "x$1" == "x-h" -o "x$1" == "x--help" ] ; then
101 if [ "x$1" == "x-b" -o "x$1" == "x--black" ] ; then
113 # check if pylint is available
114 if ! which $PYLINT &>/dev/null ; then
115 echo "$PYLINT is not available, thus check can't be executed"
119 # check if we were run within vsperf directory
120 if [ ! -x ./vsperf 2> /dev/null ] ; then
121 echo "`basename $0` must be run from vsperf root directory"
125 # get list of excluded modules
126 EXCLUDED_MODULES=`grep "^ *EXCLUDE_MODULES" conf/00_common.conf | tr '"' "'"`
128 # get list of files to be checked
129 rm $FILE_LIST &> /dev/null
130 if [ "x$1" == "x-m" -o "x$1" == "x--modified" ] ; then
131 # check of modified files requested
132 git status --porcelain | cut -b4- | egrep -i "${FILE_REGEX}" | sort > $FILE_LIST
133 elif [ "x$*" == "x" ] ; then
134 # list is empty, check all python files
135 git ls-tree --name-only -r HEAD | egrep -i "${FILE_REGEX}" | sort > $FILE_LIST
138 if [ -d $item ] ; then
139 git ls-tree --name-only -r HEAD $item | egrep -i "${FILE_REGEX}" | sort >> $FILE_LIST
140 elif [ -f $item ] ; then
141 echo $item >> $FILE_LIST
143 echo "$item doesn't exist, thus check was aborted"
149 # check if there is anything to check
150 echo "Execution of pylint checks:"
151 if [ -s $FILE_LIST ] ; then
152 for pyfile in `cat $FILE_LIST | sort` ; do
154 pyfile_basename="'"`basename $pyfile .py`"'"
155 # and check if it should be excluded or not
156 if ( echo $EXCLUDED_MODULES | grep -w $pyfile_basename > /dev/null ) ; then
157 printf " %-70s %-6s\n" $pyfile "EXCLUDED"
160 # run pylint and extract final rating
161 output=`$PYLINT --rcfile $PYLINT_RC $pyfile 2>/dev/null`
162 rating=`echo -e $output | tail -n3 | grep rated | sed -e 's/^.*rated at \([0-9.]*\).*$/\1/'`
163 # evaluate and display aquired rating
164 if [ "x$rating" == "x" ] ; then
165 # rating is not available for files without python statements
166 printf " %-70s %-6s\n" $pyfile "NA"
167 elif rating_is_ok $rating ; then
168 printf " %-70s ${GREEN}%-6s${BLACK}\n" $pyfile "OK"
170 echo -e "$output" | awk '/^*+ Module|^[A-Z]\:/'
171 printf " %-70s ${RED}%-6s${BLACK}\n" $pyfile $rating
175 echo "Nothing to check."
180 rm $FILE_LIST &> /dev/null
182 if [ "$PYLINT_RATING_MIN" != "$PYLINT_RATING_GATE" ] ; then
183 echo -e "Pylint check has failed. All files must have score ${PYLINT_RATING_GATE}.\n"