Implement --only and --exclude flags

This commit is contained in:
Tino de Bruijn 2017-07-28 11:35:20 +02:00
parent 1e26d46d17
commit b485697742
1 changed files with 19 additions and 9 deletions

View File

@ -19,7 +19,7 @@ try: # noqa: F401
from typing import Dict, List, Text, Any, Optional, Union, Callable, Tuple # noqa: F401 from typing import Dict, List, Text, Any, Optional, Union, Callable, Tuple # noqa: F401
except: # noqa: F401 except: # noqa: F401
pass # noqa: F401 pass # noqa: F401
_ = gettext.gettext _ = gettext.gettext
@ -46,36 +46,36 @@ def load_config(options, base):
def find_config_file(options, base): def find_config_file(options, base):
""" Returns the configuration file from a prioritized list of locations. """ Returns the configuration file from a prioritized list of locations.
Locations are prioritized as: Locations are prioritized as:
1. From the command line. Fail if specified but not found 1. From the command line. Fail if specified but not found
2. The repository's root directory, as the file .git-lint 2. The repository's root directory, as the file .git-lint
3. The repository's root directory, as the file .git-lint/config 3. The repository's root directory, as the file .git-lint/config
4. The user's home directory, as file .git-lint 4. The user's home directory, as file .git-lint
5. The user's home directory, as the file .git-lint/config 5. The user's home directory, as the file .git-lint/config
If no configuration file is found, this is an error. If no configuration file is found, this is an error.
""" """
if 'config' in options: if 'config' in options:
config = options['config'] config = options['config']
configpath = os.path.abspath(config) configpath = os.path.abspath(config)
if not os.path.isfile(configpath): if not os.path.isfile(configpath):
sys.exit(_('Configuration file not found: {}\n').format(config)) sys.exit(_('Configuration file not found: {}\n').format(config))
return configpath return configpath
home = os.environ.get('HOME', None) home = os.environ.get('HOME', None)
possibles = [os.path.join(base, '.git-lint'), possibles = [os.path.join(base, '.git-lint'),
os.path.join(base, '.git-lint/config')] + ((home and [ os.path.join(base, '.git-lint/config')] + ((home and [
os.path.join(home, '.git-lint'), os.path.join(home, '.git-lint'),
os.path.join(home, '.git-lint/config')]) or []) os.path.join(home, '.git-lint/config')]) or [])
matches = [p for p in possibles if os.path.isfile(p)] matches = [p for p in possibles if os.path.isfile(p)]
if len(matches) == 0: if len(matches) == 0:
sys.exit(_('No configuration file found, tried: {}').format(':'.join(possibles))) sys.exit(_('No configuration file found, tried: {}').format(':'.join(possibles)))
return matches[0] return matches[0]
Linter = namedtuple('Linter', ['name', 'linter']) Linter = namedtuple('Linter', ['name', 'linter'])
path = find_config_file(options, base) path = find_config_file(options, base)
configloader = configparser.SafeConfigParser() configloader = configparser.SafeConfigParser()
@ -201,7 +201,7 @@ def get_linter_status(config):
def get_working_linter_names(config): def get_working_linter_names(config):
return [i.name for i in config return [i.name for i in config
if linter_exists(i.linter['command'], i.name)] if linter_exists(i.linter['command'], i.name)]
working_linter_names = get_working_linter_names(config) working_linter_names = get_working_linter_names(config)
broken_linter_names = (set([i.name for i in config]) - set(working_linter_names)) broken_linter_names = (set([i.name for i in config]) - set(working_linter_names))
return working_linter_names, broken_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 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) working_linter_names, broken_linter_names = get_linter_status(config)
cant_lint_filter = MatchFilter(build_config_subset( cant_lint_filter = MatchFilter(build_config_subset(