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.
This commit is contained in:
Ian Cordasco 2016-06-13 10:33:15 -05:00
parent 8b9b9bbe89
commit 6f08d9d4a4
No known key found for this signature in database
GPG key ID: 656D3395E4A9791A
4 changed files with 64 additions and 6 deletions

View file

@ -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()

View file

@ -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(

16
flake8/main/mercurial.py Normal file
View file

@ -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

27
flake8/main/vcs.py Normal file
View file

@ -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()