From b485697742734a8543e29b9f49872ded9209e5eb Mon Sep 17 00:00:00 2001 From: Tino de Bruijn Date: Fri, 28 Jul 2017 11:35:20 +0200 Subject: [PATCH] Implement --only and --exclude flags --- git_lint/git_lint.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/git_lint/git_lint.py b/git_lint/git_lint.py index 884c81d..296ab5c 100644 --- a/git_lint/git_lint.py +++ b/git_lint/git_lint.py @@ -19,7 +19,7 @@ try: # noqa: F401 from typing import Dict, List, Text, Any, Optional, Union, Callable, Tuple # noqa: F401 except: # noqa: F401 pass # noqa: F401 - + _ = gettext.gettext @@ -46,36 +46,36 @@ def load_config(options, base): def find_config_file(options, base): """ Returns the configuration file from a prioritized list of locations. - + Locations are prioritized as: 1. From the command line. Fail if specified but not found 2. The repository's root directory, as the file .git-lint 3. The repository's root directory, as the file .git-lint/config 4. The user's home directory, as file .git-lint 5. The user's home directory, as the file .git-lint/config - + If no configuration file is found, this is an error. """ - + if 'config' in options: config = options['config'] configpath = os.path.abspath(config) if not os.path.isfile(configpath): sys.exit(_('Configuration file not found: {}\n').format(config)) return configpath - + home = os.environ.get('HOME', None) possibles = [os.path.join(base, '.git-lint'), os.path.join(base, '.git-lint/config')] + ((home and [ os.path.join(home, '.git-lint'), os.path.join(home, '.git-lint/config')]) or []) - + matches = [p for p in possibles if os.path.isfile(p)] if len(matches) == 0: sys.exit(_('No configuration file found, tried: {}').format(':'.join(possibles))) - + return matches[0] - + Linter = namedtuple('Linter', ['name', 'linter']) path = find_config_file(options, base) configloader = configparser.SafeConfigParser() @@ -201,7 +201,7 @@ def get_linter_status(config): def get_working_linter_names(config): return [i.name for i in config if linter_exists(i.linter['command'], i.name)] - + working_linter_names = get_working_linter_names(config) broken_linter_names = (set([i.name for i in config]) - set(working_linter_names)) return working_linter_names, broken_linter_names @@ -449,6 +449,16 @@ def run_linters(options, config, extras=[]): unlintable_filenames = set(all_filenames) - lintable_filenames + # Filter the linter config down to the selected ones. + if 'only' in options: + config = [linter for linter in config + if linter.name in options['only']] + elif 'exclude' in options: + config = [linter for linter in config + if linter.name not in options['exclude']] + if not len(config): + raise RuntimeError('No linters left to run! Be less strict with --only and --exclude.') + working_linter_names, broken_linter_names = get_linter_status(config) cant_lint_filter = MatchFilter(build_config_subset(