janitor

janitor
git clone https://git.prime8.dev/janitor.git
Log | Files | Refs | README | LICENSE

check_style_cpplint.sh (3115B)


      1 #!/bin/bash
      2 #
      3 # Copyright      2020  Mobvoi Inc. (authors: Fangjun Kuang)
      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 # Usage:
     18 #
     19 # (1) To check files of the last commit
     20 #  ./scripts/check_style_cpplint.sh
     21 #
     22 # (2) To check changed files not committed yet
     23 #  ./scripts/check_style_cpplint.sh 1
     24 #
     25 # (3) To check all files in the project
     26 #  ./scripts/check_style_cpplint.sh 2
     27 
     28 
     29 cpplint_version="1.5.4"
     30 cur_dir=$(cd $(dirname $BASH_SOURCE) && pwd)
     31 kaldi_native_fbank_dir=$(cd $cur_dir/.. && pwd)
     32 
     33 build_dir=$kaldi_native_fbank_dir/build
     34 mkdir -p $build_dir
     35 
     36 cpplint_src=$build_dir/cpplint-${cpplint_version}/cpplint.py
     37 
     38 if [ ! -d "$build_dir/cpplint-${cpplint_version}" ]; then
     39   pushd $build_dir
     40   if command -v wget &> /dev/null; then
     41     wget https://github.com/cpplint/cpplint/archive/${cpplint_version}.tar.gz
     42   elif command -v curl &> /dev/null; then
     43     curl -O -SL https://github.com/cpplint/cpplint/archive/${cpplint_version}.tar.gz
     44   else
     45     echo "Please install wget or curl to download cpplint"
     46     exit 1
     47   fi
     48   tar xf ${cpplint_version}.tar.gz
     49   rm ${cpplint_version}.tar.gz
     50 
     51   # cpplint will report the following error for: __host__ __device__ (
     52   #
     53   #     Extra space before ( in function call  [whitespace/parens] [4]
     54   #
     55   # the following patch disables the above error
     56   sed -i "3490i\        not Search(r'__host__ __device__\\\s+\\\(', fncall) and" $cpplint_src
     57   popd
     58 fi
     59 
     60 source $kaldi_native_fbank_dir/scripts/utils.sh
     61 
     62 # return true if the given file is a c++ source file
     63 # return false otherwise
     64 function is_source_code_file() {
     65   case "$1" in
     66     *.cc|*.h|*.cu)
     67       echo true;;
     68     *)
     69       echo false;;
     70   esac
     71 }
     72 
     73 function check_style() {
     74   python3 $cpplint_src $1 || abort $1
     75 }
     76 
     77 function check_last_commit() {
     78   files=$(git diff HEAD^1 --name-only --diff-filter=ACDMRUXB)
     79   echo $files
     80 }
     81 
     82 function check_current_dir() {
     83   files=$(git status -s -uno --porcelain | awk '{
     84   if (NF == 4) {
     85     # a file has been renamed
     86     print $NF
     87   } else {
     88     print $2
     89   }}')
     90 
     91   echo $files
     92 }
     93 
     94 function do_check() {
     95   case "$1" in
     96     1)
     97       echo "Check changed files"
     98       files=$(check_current_dir)
     99       ;;
    100     2)
    101       echo "Check all files"
    102       files=$(find $kaldi_native_fbank_dir/kaldi-native-fbank -name "*.h" -o -name "*.cc" -o -name "*.cu")
    103       ;;
    104     *)
    105       echo "Check last commit"
    106       files=$(check_last_commit)
    107       ;;
    108   esac
    109 
    110   for f in $files; do
    111     need_check=$(is_source_code_file $f)
    112     if $need_check; then
    113       [[ -f $f ]] && check_style $f
    114     fi
    115   done
    116 }
    117 
    118 function main() {
    119   do_check $1
    120 
    121   ok "Great! Style check passed!"
    122 }
    123 
    124 cd $kaldi_native_fbank_dir
    125 
    126 main $1