blob: be757ff43e571300f6cbc0501836eadcdfa08029 [file] [log] [blame]
mbligh99d2ded2008-06-23 16:17:36 +00001#!/usr/bin/python -u
2
3import os, sys, fnmatch
4import common
5
6pylintrc_path = os.path.expanduser('~/.pylintrc')
7if not os.path.exists(pylintrc_path):
8 open(pylintrc_path, 'w').close()
9
jadmanski94a64932008-07-22 14:03:10 +000010
11# patch up the logilab module lookup tools to understand autotest_lib.* trash
12import logilab.common.modutils
13_ffm = logilab.common.modutils.file_from_modpath
14def file_from_modpath(modpath, path=None, context_file=None):
15 if modpath[0] == "autotest_lib":
16 return _ffm(modpath[1:], path, context_file)
17 else:
18 return _ffm(modpath, path, context_file)
19logilab.common.modutils.file_from_modpath = file_from_modpath
20
21
mbligh99d2ded2008-06-23 16:17:36 +000022import pylint.lint
23from pylint.checkers import imports
24
25ROOT_MODULE = 'autotest_lib.'
26
27# need to put autotest root dir on sys.path so pylint will be happy
28autotest_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
29sys.path.insert(0, autotest_root)
30
31# patch up pylint import checker to handle our importing magic
32RealImportsChecker = imports.ImportsChecker
33
34class CustomImportsChecker(imports.ImportsChecker):
35 def visit_from(self, node):
36 if node.modname.startswith(ROOT_MODULE):
37 node.modname = node.modname[len(ROOT_MODULE):]
38 return RealImportsChecker.visit_from(self, node)
39
40imports.ImportsChecker = CustomImportsChecker
41
42# some files make pylint blow up, so make sure we ignore them
43blacklist = ['/contrib/*', '/frontend/afe/management.py']
44
45# only show errors
46pylint_base_opts = ['--disable-msg-cat=warning,refactor,convention',
47 '--reports=no',
48 '--include-ids=y']
49
50file_list = sys.argv[1:]
51if '--' in file_list:
52 index = file_list.index('--')
53 pylint_base_opts.extend(file_list[index+1:])
54 file_list = file_list[:index]
55
56
57def check_file(file_path):
58 if not file_path.endswith('.py'):
59 return
60 for blacklist_pattern in blacklist:
61 if fnmatch.fnmatch(os.path.abspath(file_path),
62 '*' + blacklist_pattern):
63 return
64 pylint.lint.Run(pylint_base_opts + [file_path])
65
66
67def visit(arg, dirname, filenames):
68 for filename in filenames:
69 check_file(os.path.join(dirname, filename))
70
71
72def check_dir(dir_path):
73 os.path.walk(dir_path, visit, None)
74
75
76if len(file_list) > 0:
77 for path in file_list:
78 if os.path.isdir(path):
79 check_dir(path)
80 else:
81 check_file(path)
82else:
83 check_dir('.')