liyujie
2025-08-29 87c7c0d90966d729ca3d39cbfca77a39a43960eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
# -*- coding:utf-8 -*-
# Copyright 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
#
# 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.
 
"""Wrapper to run pylint with the right settings."""
 
from __future__ import print_function
 
import argparse
import errno
import os
import sys
 
 
DEFAULT_PYLINTRC_PATH = os.path.join(
    os.path.dirname(os.path.realpath(__file__)), 'pylintrc')
 
 
def get_parser():
    """Return a command line parser."""
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--init-hook', help='Init hook commands to run.')
    parser.add_argument('--executable-path',
                        help='The path of the pylint executable.',
                        default='pylint')
    parser.add_argument('--no-rcfile',
                        help='Specify to use the executable\'s default '
                        'configuration.',
                        action='store_true')
    parser.add_argument('files', nargs='+')
    return parser
 
 
def main(argv):
    """The main entry."""
    parser = get_parser()
    opts, unknown = parser.parse_known_args(argv)
 
    cmd = [opts.executable_path]
    if not opts.no_rcfile:
        # We assume pylint is running in the top directory of the project,
        # so load the pylintrc file from there if it's available.
        pylintrc = os.path.abspath('pylintrc')
        if not os.path.exists(pylintrc):
            pylintrc = DEFAULT_PYLINTRC_PATH
            # If we pass a non-existent rcfile to pylint, it'll happily ignore
            # it.
            assert os.path.exists(pylintrc), 'Could not find %s' % pylintrc
        cmd += ['--rcfile', pylintrc]
 
    cmd += unknown + opts.files
 
    if opts.init_hook:
        cmd += ['--init-hook', opts.init_hook]
 
    try:
        os.execvp(cmd[0], cmd)
    except OSError as e:
        if e.errno == errno.ENOENT:
            print('%s: unable to run `%s`: %s' % (__file__, cmd[0], e),
                  file=sys.stderr)
            print('%s: Try installing pylint: sudo apt-get install %s' %
                  (__file__, os.path.basename(cmd[0])), file=sys.stderr)
            return 1
        else:
            raise
 
 
if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))