forked from autotest/autotest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_pylint.py
executable file
·136 lines (109 loc) · 4.18 KB
/
run_pylint.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python -u
import os, sys, fnmatch
# do a basic check to see if pylint is even installed
try:
from pylint.__pkginfo__ import version as pylint_version
except ImportError:
print "Unable to import pylint, it may need to be installed"
sys.exit(1)
# Classes of errors we ignore on quiet runs
IGNORED_ERRORS = 'E1002,E1101,E1103,E1120,F0401,I0011'
# By default, complain about all things
LINT_VERBOSE = True
def set_verbosity(verbose):
'''
Changes the verbosity level
'''
global LINT_VERBOSE
LINT_VERBOSE = verbose
pylintrc_path = os.path.expanduser('~/.pylintrc')
if not os.path.exists(pylintrc_path):
open(pylintrc_path, 'w').close()
major, minor, _ = pylint_version.split('.')
pylint_version = float("%s.%s" % (major, minor))
# patch up the logilab module lookup tools to understand autotest.* trash
import logilab.common.modutils
_ffm = logilab.common.modutils.file_from_modpath
def file_from_modpath(modpath, path=None, context_file=None):
if modpath[0] == "autotest":
if modpath[1:]:
return _ffm(modpath[1:], path, context_file)
return _ffm(modpath, path, context_file)
logilab.common.modutils.file_from_modpath = file_from_modpath
import pylint.lint
from pylint.checkers import imports
ROOT_MODULE = 'autotest.'
# need to put autotest root dir on sys.path so pylint will be happy
autotest_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, autotest_root)
# patch up pylint import checker to handle our importing magic
RealImportsChecker = imports.ImportsChecker
class CustomImportsChecker(imports.ImportsChecker):
def visit_from(self, node):
if node.modname.startswith(ROOT_MODULE):
node.modname = node.modname[len(ROOT_MODULE):]
return RealImportsChecker.visit_from(self, node)
imports.ImportsChecker = CustomImportsChecker
# some files make pylint blow up, so make sure we ignore them
blacklist = ['/contrib/*', '/frontend/afe/management.py', ]
def get_pylint_opts():
"""
If VERBOSE is set, show all complaints. If not, only errors.
There are three major sources of E1103/E1120 false positives:
* shared.enum.Enum objects
* DB model objects (scheduler models are the worst, but Django models also
generate some errors)
"""
disable_new = ['--disable=W,R,C,%s' % IGNORED_ERRORS]
disable_old = ['--disable-msg-cat=W,R,C', '--disable-msg=%s' %
IGNORED_ERRORS]
if LINT_VERBOSE:
opts = []
else:
if pylint_version >= 0.21:
opts = disable_new
else:
opts = disable_old
return opts + ['--reports=no', '--include-ids=y', '--rcfile=/dev/null', '--good-names=i,j,k,Run,_,vm']
def check_file(file_path):
if not file_path.endswith('.py'):
return 0
for blacklist_pattern in blacklist:
if fnmatch.fnmatch(os.path.abspath(file_path),
'*' + blacklist_pattern):
return 0
pylint_opts = get_pylint_opts()
if pylint_version >= 0.21:
runner = pylint.lint.Run(pylint_opts + [file_path], exit=False)
else:
runner = pylint.lint.Run(pylint_opts + [file_path])
return runner.linter.msg_status
def visit(arg, dirname, filenames):
for filename in filenames:
check_file(os.path.join(dirname, filename))
def check_dir(dir_path):
os.path.walk(dir_path, visit, None)
if __name__ == "__main__":
import optparse
usage = "usage: %prog [options] [list of files]"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-q", "--quiet",
action="store_true", dest="quiet",
help="Ignore pylint errors %s" % IGNORED_ERRORS)
options, args = parser.parse_args()
verbose = not options.quiet
set_verbosity(verbose)
file_list = args
pylint_base_opts = get_pylint_opts()
if '--' in file_list:
index = file_list.index('--')
pylint_base_opts.extend(file_list[index+1:])
file_list = file_list[:index]
if len(file_list) > 0:
for path in file_list:
if os.path.isdir(path):
check_dir(path)
else:
check_file(path)
else:
check_dir('.')