1#!/usr/bin/env bash 2# Copyright (c) 2024 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15set -e 16 17if [[ -z "$1" ]]; then 18 echo "Usage: $0 <sources>" 19 echo " <sources> path where the sources to be checked are located" 20 exit 1 21fi 22 23root_dir=$(realpath "$1") 24# For pylint, we only enable certain rules 25# - C0121 - comparison to True / False / None 26# - C0123 - use instanceof() instead of type 27# - C0304 - final newline 28# - C0305 - trailing newlines 29# - C0321 - one statement in a line 30# - C0410 - one import in a line 31# - C0411 - import ordering 32# - C1801 - Do not use `len(SEQUENCE)` to determine if a sequence is empty 33# - E0303 - __len__ method returns something which is not a non-negative integer 34# - E0304 - __bool__ doesn't return bool 35# - E0701 - bad 'except' order 36# - E1111 - Assigning to function call which doesn’t return 37# - R1708 - do not raise StopIteration in the generator 38# - R1710 - number and types of return values in all function branches should be the same 39# - W0101 - no dead code 40# - W0102 - do not use variable objects as default parameters 41# - W0109 - keys in dict must be unique 42# - W0123 - no eval() or exec() for untrusted code 43# - W0150 - no return/break/continue in the final block 44# - W0201 - do not define class properties outside __init__ 45# - W0212 - avoid access to protected members 46# - W0221 - do not changes signature when overriding method 47# - W0231 - correctly call __init__ of parent 48# - W0601 - 'global' shoul reference existing variables 49# - W0640 - do not use variables deined in outer loop 50# - W0706 - except handler raises immediately 51# - W1201 - string interpolation for logging 52# - W3101 - timeout is used in certain functions 53 54PYLINT_RULES=${PYLINT_RULES:-"C0121,C0123,C0304,C0305,C0321,C0410,\ 55C0411,C1801,E0303,E0304,E0701,E1111,R1708,R1710,W0101,W0102,W0109,\ 56W0123,W0150,W0201,W0212,W0221,W0231,W0601,W0706,W0640,W1201,W3101"} 57 58# flake8 rules - can't be checked by pylint 59# - S506 - unsafe-yaml-load 60# - S602 - subprocess popen with shell 61# - C101 - coding magic comment required 62# - EXE022 - executable files have shebang 63FLAKE8_RULES=${FLAKE8_RULES:-"C101,EXE002,S506,S602"} 64 65function save_exit_code() { 66 return $(($1 + $2)) 67} 68 69source "${root_dir}/scripts/python/venv-utils.sh" 70activate_venv 71 72set +e 73 74EXIT_CODE=0 75 76skip_options="^${root_dir}/third_party/" 77if [ -n "$SKIP_FOLDERS" ]; then 78 for pt in $SKIP_FOLDERS; do 79 skip_options="${skip_options}\|^${root_dir}/${pt}/" 80 done 81fi 82 83# Check all files with '.py' extensions except *conf.py which are config files for sphinx 84while read file_to_check; do 85 pylint -s false --timeout-methods subprocess.Popen.communicate --disable=all -e "${PYLINT_RULES}" "${file_to_check}" 86 save_exit_code ${EXIT_CODE} $? 87 EXIT_CODE=$? 88 flake8 --select "${FLAKE8_RULES}" "${file_to_check}" 89 save_exit_code ${EXIT_CODE} $? 90 EXIT_CODE=$? 91done <<<$(find "${root_dir}" -name "*.py" -type f | grep -v "${skip_options}") 92 93num_checked=$(find "${root_dir}" -name "*.py" -type f | grep -c -v "${skip_options}") 94echo "Checked ${num_checked} files" 95 96deactivate_venv 97exit ${EXIT_CODE} 98