blob: 902b051003caabdba66495a4c614c98a9433602b [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
10import pylint.lint
11from pylint.checkers import imports
12
13ROOT_MODULE = 'autotest_lib.'
14
15# need to put autotest root dir on sys.path so pylint will be happy
16autotest_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
17sys.path.insert(0, autotest_root)
18
19# patch up pylint import checker to handle our importing magic
20RealImportsChecker = imports.ImportsChecker
21
22class CustomImportsChecker(imports.ImportsChecker):
23 def visit_from(self, node):
24 if node.modname.startswith(ROOT_MODULE):
25 node.modname = node.modname[len(ROOT_MODULE):]
26 return RealImportsChecker.visit_from(self, node)
27
28imports.ImportsChecker = CustomImportsChecker
29
30# some files make pylint blow up, so make sure we ignore them
31blacklist = ['/contrib/*', '/frontend/afe/management.py']
32
33# only show errors
34pylint_base_opts = ['--disable-msg-cat=warning,refactor,convention',
35 '--reports=no',
36 '--include-ids=y']
37
38file_list = sys.argv[1:]
39if '--' in file_list:
40 index = file_list.index('--')
41 pylint_base_opts.extend(file_list[index+1:])
42 file_list = file_list[:index]
43
44
45def check_file(file_path):
46 if not file_path.endswith('.py'):
47 return
48 for blacklist_pattern in blacklist:
49 if fnmatch.fnmatch(os.path.abspath(file_path),
50 '*' + blacklist_pattern):
51 return
52 pylint.lint.Run(pylint_base_opts + [file_path])
53
54
55def visit(arg, dirname, filenames):
56 for filename in filenames:
57 check_file(os.path.join(dirname, filename))
58
59
60def check_dir(dir_path):
61 os.path.walk(dir_path, visit, None)
62
63
64if len(file_list) > 0:
65 for path in file_list:
66 if os.path.isdir(path):
67 check_dir(path)
68 else:
69 check_file(path)
70else:
71 check_dir('.')