summaryrefslogtreecommitdiff
path: root/src/flake8/main/vcs.py
blob: f7f139a5d88694f5008b1711937fccc4af254589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Module containing some of the logic for our VCS installation logic."""
from flake8 import exceptions as exc
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/3/library/optparse.html#optparse-option-callbacks
    """
    installer = _INSTALLERS.get(value)
    errored = False
    successful = False
    try:
        successful = installer()
    except exc.HookInstallationError as hook_error:
        print(str(hook_error))
        errored = True

    if not successful:
        print("Could not find the {0} directory".format(value))
    raise SystemExit(not successful and errored)


def choices():
    """Return the list of VCS choices."""
    return list(_INSTALLERS)