mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | import os, sys, fnmatch |
| 4 | import common |
| 5 | |
| 6 | pylintrc_path = os.path.expanduser('~/.pylintrc') |
| 7 | if not os.path.exists(pylintrc_path): |
| 8 | open(pylintrc_path, 'w').close() |
| 9 | |
| 10 | import pylint.lint |
| 11 | from pylint.checkers import imports |
| 12 | |
| 13 | ROOT_MODULE = 'autotest_lib.' |
| 14 | |
| 15 | # need to put autotest root dir on sys.path so pylint will be happy |
| 16 | autotest_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 17 | sys.path.insert(0, autotest_root) |
| 18 | |
| 19 | # patch up pylint import checker to handle our importing magic |
| 20 | RealImportsChecker = imports.ImportsChecker |
| 21 | |
| 22 | class 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 | |
| 28 | imports.ImportsChecker = CustomImportsChecker |
| 29 | |
| 30 | # some files make pylint blow up, so make sure we ignore them |
| 31 | blacklist = ['/contrib/*', '/frontend/afe/management.py'] |
| 32 | |
| 33 | # only show errors |
| 34 | pylint_base_opts = ['--disable-msg-cat=warning,refactor,convention', |
| 35 | '--reports=no', |
| 36 | '--include-ids=y'] |
| 37 | |
| 38 | file_list = sys.argv[1:] |
| 39 | if '--' 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 | |
| 45 | def 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 | |
| 55 | def visit(arg, dirname, filenames): |
| 56 | for filename in filenames: |
| 57 | check_file(os.path.join(dirname, filename)) |
| 58 | |
| 59 | |
| 60 | def check_dir(dir_path): |
| 61 | os.path.walk(dir_path, visit, None) |
| 62 | |
| 63 | |
| 64 | if 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) |
| 70 | else: |
| 71 | check_dir('.') |