xref: /aosp_15_r20/external/grpc-grpc/tools/distrib/pylint_code.sh (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/bin/bash
2# Copyright 2017 The gRPC Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16set -ex
17
18# NOTE(rbellevi): We ignore generated code.
19IGNORE_PATTERNS=--ignore-patterns='.*pb2\.py,.*pb2_grpc\.py'
20
21# change to root directory
22cd "$(dirname "$0")/../.."
23
24DIRS=(
25    'src/python/grpcio/grpc'
26    'src/python/grpcio_channelz/grpc_channelz'
27    'src/python/grpcio_health_checking/grpc_health'
28    'src/python/grpcio_reflection/grpc_reflection'
29    'src/python/grpcio_testing/grpc_testing'
30    'src/python/grpcio_status/grpc_status'
31    'src/python/grpcio_observability/grpc_observability'
32)
33
34TEST_DIRS=(
35    'src/python/grpcio_tests/tests'
36    'src/python/grpcio_tests/tests_gevent'
37)
38
39VIRTUALENV=python_pylint_venv
40python3 -m virtualenv $VIRTUALENV -p $(which python3)
41
42PYTHON=$VIRTUALENV/bin/python
43
44$PYTHON -m pip install --upgrade pip==19.3.1
45
46# TODO(https://github.com/grpc/grpc/issues/23394): Update Pylint.
47$PYTHON -m pip install --upgrade astroid==2.3.3 \
48  pylint==2.2.2 \
49  toml==0.10.2 \
50  "isort>=4.3.0,<5.0.0"
51
52EXIT=0
53for dir in "${DIRS[@]}"; do
54  $PYTHON -m pylint --rcfile=.pylintrc -rn "$dir" ${IGNORE_PATTERNS}  || EXIT=1
55done
56
57for dir in "${TEST_DIRS[@]}"; do
58  $PYTHON -m pylint --rcfile=.pylintrc-tests -rn "$dir" ${IGNORE_PATTERNS} || EXIT=1
59done
60
61find examples/python \
62  -iname "*.py" \
63  -not -name "*_pb2.py" \
64  -not -name "*_pb2_grpc.py" \
65  | xargs $PYTHON -m pylint --rcfile=.pylintrc-examples -rn ${IGNORE_PATTERNS}
66
67exit $EXIT
68