From 6f08d9d4a44dbab667f28c940698176950b0e59c Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Mon, 13 Jun 2016 10:33:15 -0500 Subject: [PATCH] Move git integration into flake8.main Also add the --install-hook option and plumb it's installation through flake8.main.vcs's function that understands how to install the desired VCS integration bits. Finally, we also mock out the mercurial integration. --- flake8/main/cli.py | 24 +++++++++++++++++++----- flake8/{api => main}/git.py | 3 ++- flake8/main/mercurial.py | 16 ++++++++++++++++ flake8/main/vcs.py | 27 +++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 6 deletions(-) rename flake8/{api => main}/git.py (99%) create mode 100644 flake8/main/mercurial.py create mode 100644 flake8/main/vcs.py diff --git a/flake8/main/cli.py b/flake8/main/cli.py index 23135c4..531527d 100644 --- a/flake8/main/cli.py +++ b/flake8/main/cli.py @@ -9,6 +9,7 @@ from flake8 import checker from flake8 import defaults from flake8 import style_guide from flake8 import utils +from flake8.main import vcs from flake8.options import aggregator from flake8.options import manager from flake8.plugins import manager as plugin_manager @@ -16,7 +17,7 @@ from flake8.plugins import manager as plugin_manager LOG = logging.getLogger(__name__) -def register_default_options(option_manager): +def register_default_options(option_manager, formatters=None): """Register the default options on our OptionManager. The default options include: @@ -97,7 +98,7 @@ def register_default_options(option_manager): add_option( '--format', metavar='format', default='default', - parse_from_config=True, + parse_from_config=True, choices=(formatters or []), help='Format errors according to the chosen formatter.', ) @@ -160,6 +161,13 @@ def register_default_options(option_manager): help='Exit with status code "0" even if there are errors.', ) + add_option( + '--install-hook', action='callback', type='string', + choices=vcs.choices(), callback=vcs.install, + help='Install a hook that is run prior to a commit for the supported ' + 'version control systema.' + ) + add_option( '-j', '--jobs', type='string', default='auto', parse_from_config=True, help='Number of subprocesses to use to run checks in parallel. ' @@ -215,10 +223,11 @@ class Application(object): self.version = version #: The instance of :class:`flake8.options.manager.OptionManager` used #: to parse and handle the options and arguments passed by the user - self.option_manager = manager.OptionManager( + self.option_manager = None + temp_option_manager = manager.OptionManager( prog='flake8', version=flake8.__version__ ) - register_default_options(self.option_manager) + register_default_options(temp_option_manager) # We haven't found or registered our plugins yet, so let's defer # printing the version until we aggregate options from config files @@ -242,7 +251,7 @@ class Application(object): except ValueError: pass - preliminary_opts, _ = self.option_manager.parse_args(args) + preliminary_opts, _ = temp_option_manager.parse_args(args) # Set the verbosity of the program flake8.configure_logging(preliminary_opts.verbose, preliminary_opts.output_file) @@ -418,7 +427,12 @@ class Application(object): This finds the plugins, registers their options, and parses the command-line arguments. """ + self.option_manager = manager.OptionManager( + prog='flake8', version=flake8.__version__ + ) self.find_plugins() + register_default_options(self.option_manager, + self.formatting_plugins.names) self.register_plugin_options() self.parse_configuration_and_cli(argv) self.make_formatter() diff --git a/flake8/api/git.py b/flake8/main/git.py similarity index 99% rename from flake8/api/git.py rename to flake8/main/git.py index 087e31b..61312d4 100644 --- a/flake8/api/git.py +++ b/flake8/main/git.py @@ -1,6 +1,7 @@ """Module containing the main git hook interface and helpers. .. autofunction:: hook +.. autofunction:: install """ import contextlib @@ -180,7 +181,7 @@ _HOOK_TEMPLATE = """#!/usr/bin/env python import os import sys -from flake8.api import git +from flake8.main import git if __name__ == '__main__': sys.exit( diff --git a/flake8/main/mercurial.py b/flake8/main/mercurial.py new file mode 100644 index 0000000..9767f34 --- /dev/null +++ b/flake8/main/mercurial.py @@ -0,0 +1,16 @@ +"""Module containing the main mecurial hook interface and helpers. + +.. autofunction:: hook +.. autofunction:: install + +""" + +__all__ = ('hook', 'install') + + +def hook(lazy=False, strict=False): + pass + + +def install(): + pass diff --git a/flake8/main/vcs.py b/flake8/main/vcs.py new file mode 100644 index 0000000..95cbfa1 --- /dev/null +++ b/flake8/main/vcs.py @@ -0,0 +1,27 @@ +"""Module containing some of the logic for our VCS installation logic.""" +from flake8.main import git +from flake8.main import mercurial + + +# NOTE(sigmavirus24): In the future, we may allow for VCS hooks to be defined +# as plugins, e.g., adding a flake8.vcs entry-point. In that case, this +# dictionary should disappear, and this module might contain more code for +# managing those bits (in conjuntion with flake8.plugins.manager). +_INSTALLERS = { + 'git': git.install, + 'mercurial': mercurial.install, +} + + +def install(option, option_string, value, parser): + """Determine which version control hook to install. + + For more information about the callback signature, see: + https://docs.python.org/2/library/optparse.html#optparse-option-callbacks + """ + installer = _INSTALLERS.get(value) + installer() + + +def choices(): + return _INSTALLERS.keys()