3 # Copyright 2015 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 FILE_REGEX="(vsperf|.*\.py)"
23 FIND_OPTIONS="-regextype posix-egrep -iregex (./)?$FILE_REGEX"
24 FILE_LIST="/tmp/vsperf_check_list.txt"
26 # print usage if requested
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
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.
41 -h, --help Script usage
42 -m, --modified Script will check python files, which have
43 been modified within current repository.
47 Check all python files in current VSPERF repository except EXCLUDE_MODULES
55 Check all modified files in current VSPERF repository
57 ./check vnfs/qemu core tools/pkt_gen
59 Check all python files in given directories
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 ]
73 # check if help is requested
74 if [ "x$1" == "x-h" -o "x$1" == "x--help" ] ; then
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"
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"
91 # get list of excluded modules
92 EXCLUDED_MODULES=`grep "^ *EXCLUDE_MODULES" conf/00_common.conf | tr '"' "'"`
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
104 if [ -d $item ] ; then
105 find $item -type f $FIND_OPTIONS >> $FILE_LIST
106 elif [ -f $item ] ; then
107 echo $item >> $FILE_LIST
109 echo "$item doesn't exist, thus check was aborted"
115 # check if there is anything to check
116 if [ -s $FILE_LIST ] ; then
117 for pyfile in `cat $FILE_LIST | sort` ; do
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"
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"
131 printf "%-70s \e[31m%-6s\e[0m\n" $pyfile $rating
135 echo "Nothing to check."
140 rm $FILE_LIST &> /dev/null