checkCodingFormattingRules.sh 8.17 KB
Newer Older
1
#!/bin/bash
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#/*
# * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# * contributor license agreements.  See the NOTICE file distributed with
# * this work for additional information regarding copyright ownership.
# * The OpenAirInterface Software Alliance licenses this file to You under
# * the OAI Public License, Version 1.1  (the "License"); you may not use this file
# * except in compliance with the License.
# * You may obtain a copy of the License at
# *
# *      http://www.openairinterface.org/?page_id=698
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# *-------------------------------------------------------------------------------
# * For more information about the OpenAirInterface (OAI) Software Alliance:
# *      contact@openairinterface.org
# */
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
function usage {
    echo "OAI Coding / Formatting Guideline Check script"
    echo "   Original Author: Raphael Defosseux"
    echo ""
    echo "   Requirement: astyle shall be installed"
    echo ""
    echo "   By default (no options) the complete repository will be checked"
    echo "   In case of merge request, provided source and target branch,"
    echo "   the script will check only the modified files"
    echo ""
    echo "Usage:"
    echo "------"
    echo "    checkCodingFormattingRules.sh [OPTIONS]"
    echo ""
    echo "Options:"
    echo "--------"
    echo "    --src-branch #### OR -sb ####"
    echo "    Specify the source branch of the merge request."
    echo ""
    echo "    --target-branch #### OR -tb ####"
    echo "    Specify the target branch of the merge request (usually develop)."
    echo ""
    echo "    --help OR -h"
    echo "    Print this help message."
    echo ""
}

if [ $# -ne 4 ] && [ $# -ne 1 ] && [ $# -ne 0 ]
then
    echo "Syntax Error: not the correct number of arguments"
    echo ""
    usage
    exit 1
fi

58 59
if [ $# -eq 0 ]
then
60 61
    echo " ---- Checking the whole repository ----"
    echo ""
62
    NB_FILES_TO_FORMAT=`astyle --dry-run --options=ci-scripts/astyle-options.txt --recursive *.c *.h | grep -c Formatted `
63
    echo "Nb Files that do NOT follow OAI rules: $NB_FILES_TO_FORMAT"
64
    echo $NB_FILES_TO_FORMAT > ./oai_rules_result.txt
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

    # Testing Circular Dependencies protection
    awk '/#[ \t]*ifndef/ { gsub("^.*ifndef *",""); if (names[$1]!="") print "files with same {define ", FILENAME, names[$1]; names[$1]=FILENAME } /#[ \t]*define/ { gsub("^.*define *",""); if(names[$1]!=FILENAME) print "error in declaration", FILENAME, $1, names[$1]; nextfile }' `find openair* common targets executables -name *.h |grep -v LFDS` > header-files-w-incorrect-define.txt

    # Testing if explicit GNU GPL license banner
    egrep -irl --exclude-dir=.git --include=*.cpp --include=*.c --include=*.h "General Public License" . > files-w-gnu-gpl-license-banner.txt

    # Looking at exotic/suspect banner
    LIST_OF_FILES_W_BANNER=`egrep -irl --exclude-dir=.git --include=*.cpp --include=*.c --include=*.h "Copyright|copyleft" .`
    if [ -f ./files-w-suspect-banner.txt ]; then rm -f ./files-w-suspect-banner.txt; fi
    for FILE in $LIST_OF_FILES_W_BANNER
    do
       IS_NFAPI=`echo $FILE | egrep -c "nfapi/open-nFAPI|nfapi/oai_integration/vendor_ext"`
       IS_OAI_LICENCE_PRESENT=`egrep -c "OAI Public License" $FILE`
       IS_BSD_LICENCE_PRESENT=`egrep -c "the terms of the BSD Licence" $FILE`
80
       IS_EXCEPTION=`echo $FILE | egrep -c "common/utils/collection/tree.h|common/utils/collection/queue.h|common/utils/itti_analyzer/common/queue.h|openair3/UTILS/tree.h|openair3/UTILS/queue.h|openair3/GTPV1-U/nw-gtpv1u|openair2/UTIL/OPT/ws_"`
81 82 83 84 85 86 87 88
       if [ $IS_OAI_LICENCE_PRESENT -eq 0 ] && [ $IS_BSD_LICENCE_PRESENT -eq 0 ]
       then
           if [ $IS_NFAPI -eq 0 ] && [ $IS_EXCEPTION -eq 0 ]
           then
               echo $FILE >> ./files-w-suspect-banner.txt
           fi
       fi
    done
89 90 91
    exit 0
fi

92 93 94 95
checker=0
while [[ $# -gt 0 ]]
do
key="$1"
96

97 98 99 100
case $key in
    -h|--help)
    shift
    usage
101
    exit 0
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    ;;
    -sb|--src-branch)
    SOURCE_BRANCH="$2"
    let "checker|=0x1"
    shift
    shift
    ;;
    -tb|--target-branch)
    TARGET_BRANCH="$2"
    let "checker|=0x2"
    shift
    shift
    ;;
    *)
    echo "Syntax Error: unknown option: $key"
    echo ""
    usage
    exit 1
esac
121

122 123 124 125
done


if [ $checker -ne 3 ]
126
then
127 128 129 130 131 132
    echo "Source Branch is    : $SOURCE_BRANCH"
    echo "Target Branch is    : $TARGET_BRANCH"
    echo ""
    echo "Syntax Error: missing option"
    echo ""
    usage
133 134
    exit 1
fi
135 136 137

# Merge request scenario

138
MERGE_COMMMIT=`git log -n1 --pretty=format:%H`
139 140 141 142 143 144 145 146
TARGET_INIT_COMMIT=`cat .git/refs/remotes/origin/$TARGET_BRANCH`

echo " ---- Checking the modified files by the merge request ----"
echo ""
echo "Source Branch is    : $SOURCE_BRANCH"
echo "Target Branch is    : $TARGET_BRANCH"
echo "Merged Commit is    : $MERGE_COMMMIT"
echo "Target Init   is    : $TARGET_INIT_COMMIT"
147 148 149
echo ""
echo " ----------------------------------------------------------"
echo ""
150 151 152 153

# Retrieve the list of modified files since the latest develop commit
MODIFIED_FILES=`git log $TARGET_INIT_COMMIT..$MERGE_COMMMIT --oneline --name-status | egrep "^M|^A" | sed -e "s@^M\t*@@" -e "s@^A\t*@@" | sort | uniq`
NB_TO_FORMAT=0
154 155 156 157
if [ -f oai_rules_result_list.txt ]
then
    rm -f oai_rules_result_list.txt
fi
158 159 160 161 162 163 164 165 166 167 168 169 170 171
if [ -f header-files-w-incorrect-define.txt ]
then
    rm -f header-files-w-incorrect-define.txt
fi
if [ -f files-w-gnu-gpl-license-banner.txt ]
then
    rm -f files-w-gnu-gpl-license-banner.txt
fi
if [ -f files-w-suspect-banner.txt ]
then
    rm -f files-w-suspect-banner.txt
fi
awk '/#[ \t]*ifndef/ { gsub("^.*ifndef *",""); if (names[$1]!="") print "files with same {define ", FILENAME, names[$1]; names[$1]=FILENAME } /#[ \t]*define/ { gsub("^.*define *",""); if(names[$1]!=FILENAME) print "error in declaration", FILENAME, $1, names[$1]; nextfile }' `find openair* common targets executables -name *.h |grep -v LFDS` > header-files-w-incorrect-define-tmp.txt

172 173
for FULLFILE in $MODIFIED_FILES
do
174 175 176
    # sometimes, we remove files
    if [ ! -f $FULLFILE ]; then continue; fi

177 178 179 180 181 182
    filename=$(basename -- "$FULLFILE")
    EXT="${filename##*.}"
    if [ $EXT = "c" ] || [ $EXT = "h" ] || [ $EXT = "cpp" ] || [ $EXT = "hpp" ]
    then
        TO_FORMAT=`astyle --dry-run --options=ci-scripts/astyle-options.txt $FULLFILE | grep -c Formatted `
        NB_TO_FORMAT=$((NB_TO_FORMAT + TO_FORMAT))
183 184 185 186 187
        if [ $TO_FORMAT -ne 0 ]
        then
            echo $FULLFILE
            echo $FULLFILE >> ./oai_rules_result_list.txt
        fi
188 189 190 191 192 193 194 195 196
        # Testing if explicit GNU GPL license banner
        egrep -i "General Public License" $FULLFILE >> files-w-gnu-gpl-license-banner.txt
        # Looking at exotic/suspect banner
        IS_BANNER=`egrep -i -c "Copyright|copyleft" $FULLFILE`
        if [ $IS_BANNER -ne 0 ]
        then
            IS_NFAPI=`echo $FULLFILE | egrep -c "nfapi/open-nFAPI|nfapi/oai_integration/vendor_ext"`
            IS_OAI_LICENCE_PRESENT=`egrep -c "OAI Public License" $FULLFILE`
            IS_BSD_LICENCE_PRESENT=`egrep -c "the terms of the BSD Licence" $FULLFILE`
Raphael Defosseux's avatar
Raphael Defosseux committed
197
            IS_EXCEPTION=`echo $FULLFILE | egrep -c "common/utils/collection/tree.h|common/utils/collection/queue.h|common/utils/itti_analyzer/common/queue.h|openair3/UTILS/tree.h|openair3/UTILS/queue.h|openair3/GTPV1-U/nw-gtpv1u|openair2/UTIL/OPT/ws_"`
198 199 200 201
            if [ $IS_OAI_LICENCE_PRESENT -eq 0 ] && [ $IS_BSD_LICENCE_PRESENT -eq 0 ]
            then
                if [ $IS_NFAPI -eq 0 ] && [ $IS_EXCEPTION -eq 0 ]
                then
Raphael Defosseux's avatar
Raphael Defosseux committed
202
                    echo $FULLFILE >> ./files-w-suspect-banner.txt
203 204 205 206 207 208 209 210
                fi
            fi
        fi
    fi
    # Testing Circular Dependencies protection
    if [ $EXT = "h" ] || [ $EXT = "hpp" ]
    then
        grep $FULLFILE header-files-w-incorrect-define-tmp.txt >> header-files-w-incorrect-define.txt
211 212
    fi
done
213
rm -f header-files-w-incorrect-define-tmp.txt
214 215
echo ""
echo " ----------------------------------------------------------"
216 217 218 219
echo "Nb Files that do NOT follow OAI rules: $NB_TO_FORMAT"
echo $NB_TO_FORMAT > ./oai_rules_result.txt

exit 0