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 | |
mbligh | 65e06b1 | 2008-08-22 18:12:49 +0000 | [diff] [blame^] | 6 | # do a basic check to see if pylint is even installed |
| 7 | try: |
| 8 | import pylint |
| 9 | except ImportError: |
| 10 | print "Unable to import pylint, it may need to be installed" |
| 11 | sys.exit(1) |
| 12 | |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 13 | pylintrc_path = os.path.expanduser('~/.pylintrc') |
| 14 | if not os.path.exists(pylintrc_path): |
| 15 | open(pylintrc_path, 'w').close() |
| 16 | |
jadmanski | 94a6493 | 2008-07-22 14:03:10 +0000 | [diff] [blame] | 17 | |
| 18 | # patch up the logilab module lookup tools to understand autotest_lib.* trash |
| 19 | import logilab.common.modutils |
| 20 | _ffm = logilab.common.modutils.file_from_modpath |
| 21 | def 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) |
| 26 | logilab.common.modutils.file_from_modpath = file_from_modpath |
| 27 | |
| 28 | |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 29 | import pylint.lint |
| 30 | from pylint.checkers import imports |
| 31 | |
| 32 | ROOT_MODULE = 'autotest_lib.' |
| 33 | |
| 34 | # need to put autotest root dir on sys.path so pylint will be happy |
| 35 | autotest_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 36 | sys.path.insert(0, autotest_root) |
| 37 | |
| 38 | # patch up pylint import checker to handle our importing magic |
| 39 | RealImportsChecker = imports.ImportsChecker |
| 40 | |
| 41 | class 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 | |
| 47 | imports.ImportsChecker = CustomImportsChecker |
| 48 | |
| 49 | # some files make pylint blow up, so make sure we ignore them |
| 50 | blacklist = ['/contrib/*', '/frontend/afe/management.py'] |
| 51 | |
| 52 | # only show errors |
| 53 | pylint_base_opts = ['--disable-msg-cat=warning,refactor,convention', |
| 54 | '--reports=no', |
| 55 | '--include-ids=y'] |
| 56 | |
| 57 | file_list = sys.argv[1:] |
| 58 | if '--' 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 | |
| 64 | def 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 | |
| 74 | def visit(arg, dirname, filenames): |
| 75 | for filename in filenames: |
| 76 | check_file(os.path.join(dirname, filename)) |
| 77 | |
| 78 | |
| 79 | def check_dir(dir_path): |
| 80 | os.path.walk(dir_path, visit, None) |
| 81 | |
| 82 | |
| 83 | if 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) |
| 89 | else: |
| 90 | check_dir('.') |