• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
63# - N801 - class names should use CapWords convention
64# - N802 - function name should be lowercase
65# - N803 - argument name should be lowercase
66# - N805 - first argument of a method should be named ‘self’
67# - N806 - variable in function should be lowercase
68# - N807 - function name should not start and end with ‘__’
69# - N815 - mixedCase variable in class scope
70# - N816 - mixedCase variable in global scope
71# - N817 - camelcase imported as acronym
72
73FLAKE8_RULES=${FLAKE8_RULES:-"C101,EXE002,S506,S602,N801,N802,N803,N805,N806,N807,N815,N816,N817"}
74
75function save_exit_code() {
76    return $(($1 + $2))
77}
78
79source "${root_dir}/scripts/python/venv-utils.sh"
80activate_venv
81
82set +e
83
84EXIT_CODE=0
85
86skip_options="^${root_dir}/third_party/\|^${root_dir}/plugins/ets/tests/debugger/src/arkdb/"
87if [ -n "$SKIP_FOLDERS" ]; then
88    for pt in $SKIP_FOLDERS; do
89        skip_options="${skip_options}\|^${root_dir}/${pt}/"
90    done
91fi
92
93# Check all files with '.py' extensions except *conf.py which are config files for sphinx
94while read file_to_check; do
95    pylint -s false --timeout-methods subprocess.Popen.communicate --disable=all -e "${PYLINT_RULES}" "${file_to_check}"
96    save_exit_code ${EXIT_CODE} $?
97    EXIT_CODE=$?
98    flake8 --select "${FLAKE8_RULES}" "${file_to_check}"
99    save_exit_code ${EXIT_CODE} $?
100    EXIT_CODE=$?
101done <<<$(find "${root_dir}" -name "*.py" -type f | grep -v "${skip_options}")
102
103num_checked=$(find "${root_dir}" -name "*.py" -type f | grep -c -v "${skip_options}")
104echo "Checked ${num_checked} files"
105
106deactivate_venv
107exit ${EXIT_CODE}
108