blob: e0115428ec191aff4dc18d417a5a79fde9584cda [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
53pylint_base_opts = ['--disable-msg-cat=warning,refactor,convention',
54 '--reports=no',
55 '--include-ids=y']
56
57file_list = sys.argv[1:]
58if '--' in file_list:
59 index = file_list.index('--')
60 pylint_base_opts.extend(file_list[index+1:])
61 file_list = file_list[:index]
62
63
64def check_file(file_path):
65 if not file_path.endswith('.py'):
66 return
67 for blacklist_pattern in blacklist:
68 if fnmatch.fnmatch(os.path.abspath(file_path),
69 '*' + blacklist_pattern):
70 return
71 pylint.lint.Run(pylint_base_opts + [file_path])
72
73
74def visit(arg, dirname, filenames):
75 for filename in filenames:
76 check_file(os.path.join(dirname, filename))
77
78
79def check_dir(dir_path):
80 os.path.walk(dir_path, visit, None)
81
82
83if len(file_list) > 0:
84 for path in file_list:
85 if os.path.isdir(path):
86 check_dir(path)
87 else:
88 check_file(path)
89else:
90 check_dir('.')