blob: 0bf5f957364faad5dab28745e294a7576bcbc997 [file] [log] [blame]
mbligh99d2ded2008-06-23 16:17:36 +00001#!/usr/bin/python -u
2
3import os, sys, fnmatch
4import common
5
mbligh65e06b12008-08-22 18:12:49 +00006# do a basic check to see if pylint is even installed
7try:
8 import pylint
9except ImportError:
10 print "Unable to import pylint, it may need to be installed"
11 sys.exit(1)
12
mbligh99d2ded2008-06-23 16:17:36 +000013pylintrc_path = os.path.expanduser('~/.pylintrc')
14if not os.path.exists(pylintrc_path):
15 open(pylintrc_path, 'w').close()
16
jadmanski94a64932008-07-22 14:03:10 +000017
18# patch up the logilab module lookup tools to understand autotest_lib.* trash
19import logilab.common.modutils
20_ffm = logilab.common.modutils.file_from_modpath
21def file_from_modpath(modpath, path=None, context_file=None):
22 if modpath[0] == "autotest_lib":
23 return _ffm(modpath[1:], path, context_file)
24 else:
25 return _ffm(modpath, path, context_file)
26logilab.common.modutils.file_from_modpath = file_from_modpath
27
28
mbligh99d2ded2008-06-23 16:17:36 +000029import pylint.lint
30from pylint.checkers import imports
31
32ROOT_MODULE = 'autotest_lib.'
33
34# need to put autotest root dir on sys.path so pylint will be happy
35autotest_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
36sys.path.insert(0, autotest_root)
37
38# patch up pylint import checker to handle our importing magic
39RealImportsChecker = imports.ImportsChecker
40
41class CustomImportsChecker(imports.ImportsChecker):
42 def visit_from(self, node):
43 if node.modname.startswith(ROOT_MODULE):
44 node.modname = node.modname[len(ROOT_MODULE):]
45 return RealImportsChecker.visit_from(self, node)
46
47imports.ImportsChecker = CustomImportsChecker
48
49# some files make pylint blow up, so make sure we ignore them
50blacklist = ['/contrib/*', '/frontend/afe/management.py']
51
52# only show errors
lmr2d6decc2009-12-02 00:05:54 +000053# there are two major sources of E1101/E1103 false positives:
54# * common_lib.enum.Enum objects
55# * DB model objects (scheduler models are the worst, but Django models also
56# generate some errors)
mbligh99d2ded2008-06-23 16:17:36 +000057pylint_base_opts = ['--disable-msg-cat=warning,refactor,convention',
lmr2d6decc2009-12-02 00:05:54 +000058 '--disable-msg=E1101,E1103',
mbligh99d2ded2008-06-23 16:17:36 +000059 '--reports=no',
60 '--include-ids=y']
61
62file_list = sys.argv[1:]
63if '--' in file_list:
64 index = file_list.index('--')
65 pylint_base_opts.extend(file_list[index+1:])
66 file_list = file_list[:index]
67
68
69def check_file(file_path):
70 if not file_path.endswith('.py'):
71 return
72 for blacklist_pattern in blacklist:
73 if fnmatch.fnmatch(os.path.abspath(file_path),
74 '*' + blacklist_pattern):
75 return
76 pylint.lint.Run(pylint_base_opts + [file_path])
77
78
79def visit(arg, dirname, filenames):
80 for filename in filenames:
81 check_file(os.path.join(dirname, filename))
82
83
84def check_dir(dir_path):
85 os.path.walk(dir_path, visit, None)
86
87
88if len(file_list) > 0:
89 for path in file_list:
90 if os.path.isdir(path):
91 check_dir(path)
92 else:
93 check_file(path)
94else:
95 check_dir('.')