diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2014-10-19 15:32:02 +0300 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2014-10-19 15:32:02 +0300 |
commit | d9d6b51daf76298c2ae545487eae3012b6fa825e (patch) | |
tree | 4b4af8bfbc97c23a6d6899a1cc15741de3a45397 /lint.py | |
parent | 95667d09c010663b221366026cc8777adb14e0e3 (diff) | |
download | pylint-d9d6b51daf76298c2ae545487eae3012b6fa825e.tar.gz |
Allow linting multiple directories at once.
Patch by Buck Evan.
Diffstat (limited to 'lint.py')
-rw-r--r-- | lint.py | 67 |
1 files changed, 41 insertions, 26 deletions
@@ -33,6 +33,7 @@ from pylint.checkers import utils #pylint: disable=unused-import import sys import os import tokenize +from contextlib import contextmanager from operator import attrgetter from warnings import warn @@ -71,8 +72,9 @@ import six def _get_python_path(filepath): - dirname = os.path.dirname(os.path.realpath( - os.path.expanduser(filepath))) + dirname = os.path.realpath(os.path.expanduser(filepath)) + if not os.path.isdir(dirname): + dirname = os.path.dirname(dirname) while True: if not os.path.exists(os.path.join(dirname, "__init__.py")): return dirname @@ -411,17 +413,6 @@ class PyLinter(OptionsManagerMixIn, MessagesHandlerMixIn, ReportsHandlerMixIn, if not self.reporter: self._load_reporter() - def prepare_import_path(self, args): - """Prepare sys.path for running the linter checks.""" - if len(args) == 1: - sys.path.insert(0, _get_python_path(args[0])) - else: - sys.path.insert(0, os.getcwd()) - - def cleanup_import_path(self): - """Revert any changes made to sys.path in prepare_import_path.""" - sys.path.pop(0) - def load_plugin_modules(self, modnames): """take a list of module names which are pylint plugins and load and register them @@ -1015,6 +1006,31 @@ def preprocess_options(args, search_for): else: i += 1 + +@contextmanager +def fix_import_path(args): + """Prepare sys.path for running the linter checks. + + Within this context, each of the given arguments is importable. + Paths are added to sys.path in corresponding order to the arguments. + We avoid adding duplicate directories to sys.path. + `sys.path` is reset to its original value upon exitign this context. + """ + orig = list(sys.path) + changes = [] + for arg in args: + path = _get_python_path(arg) + if path in changes: + continue + else: + changes.append(path) + sys.path[:] = changes + sys.path + try: + yield + finally: + sys.path[:] = orig + + class Run(object): """helper class to use as main for pylint : @@ -1184,20 +1200,19 @@ group are mutually exclusive.'), # insert current working directory to the python path to have a correct # behaviour - linter.prepare_import_path(args) - if self.linter.config.profile: - print('** profiled run', file=sys.stderr) - import cProfile, pstats - cProfile.runctx('linter.check(%r)' % args, globals(), locals(), - 'stones.prof') - data = pstats.Stats('stones.prof') - data.strip_dirs() - data.sort_stats('time', 'calls') - data.print_stats(30) - else: - linter.check(args) + with fix_import_path(args): + if self.linter.config.profile: + print('** profiled run', file=sys.stderr) + import cProfile, pstats + cProfile.runctx('linter.check(%r)' % args, globals(), locals(), + 'stones.prof') + data = pstats.Stats('stones.prof') + data.strip_dirs() + data.sort_stats('time', 'calls') + data.print_stats(30) + else: + linter.check(args) linter.generate_reports() - linter.cleanup_import_path() if exit: sys.exit(self.linter.msg_status) |