예제 #1
0
def check_hooks(term: Terminal) -> None:
    pre_commit_hook = PreCommitHook()
    check_pre_commit_hook(term, pre_commit_hook)

    pyproject_toml = get_pyproject_toml_path()

    check_config(term, pyproject_toml, pre_commit_hook)
예제 #2
0
def install_hooks(term: Terminal, args: Namespace) -> None:
    pre_commit_hook = PreCommitHook()
    pyproject_toml = get_pyproject_toml_path()
    config = load_config_from_pyproject_toml(pyproject_toml)

    if pre_commit_hook.exists() and not args.force:
        term.ok('autohooks pre-commit hook is already installed at {}.'.format(
            str(pre_commit_hook)))
        with term.indent():
            term.print()
            term.info(
                "Run 'autohooks activate --force' to override the current "
                "installed pre-commit hook.")
            term.info(
                "Run 'autohooks check' to validate the current status of "
                "the installed pre-commit hook.")
    else:
        if not config.is_autohooks_enabled():
            term.warning('autohooks is not enabled in your {} file. '
                         'Run \'autohooks check\' for more '
                         'details.'.format(str(pyproject_toml)))

        if args.mode:
            mode = Mode.from_string(args.mode)
        else:
            mode = config.get_mode()

        pre_commit_hook.write(mode=mode)

        term.ok(
            'autohooks pre-commit hook installed at {} using {} mode.'.format(
                str(pre_commit_hook), str(mode.get_effective_mode())))
예제 #3
0
def check_hooks():
    pre_commit_hook = get_pre_commit_hook_path()
    pre_commit_template = get_pre_commit_hook_template_path()

    template = pre_commit_template.read_text()

    if pre_commit_hook.is_file():
        hook = pre_commit_hook.read_text()
        if hook == template:
            ok('autohooks pre-commit hook is active.')
        else:
            error('autohooks pre-commit hook is not active. But a different '
                  'pre-commit hook has been found at {}.'.format(
                      str(pre_commit_hook)))

    else:
        error('autohooks pre-commit hook not active. Please run \'autohooks '
              'activate\'.')

    pyproject_toml = get_pyproject_toml_path()
    if not pyproject_toml.exists():
        error('Missing {} file. Please add a pyproject.toml file and include '
              'a "{}" section.'.format(str(pyproject_toml), AUTOHOOKS_SECTION))
    else:
        config = load_config_from_pyproject_toml(pyproject_toml)
        if not config.is_autohooks_enabled():
            error('autohooks is not enabled in your {} file. Please add '
                  'a "{}" section.'.format(str(pyproject_toml),
                                           AUTOHOOKS_SECTION))
        else:
            plugins = config.get_pre_commit_script_names()
            if not plugins:
                error('No autohooks plugin is activated in {} for your pre '
                      'commit hook. Please add a '
                      '"pre-commit = [plugin1, plugin2]" '
                      'setting.'.format(str(pyproject_toml)))
            else:
                with autohooks_module_path():
                    for name in plugins:
                        try:
                            plugin = load_plugin(name)
                            if not has_precommit_function(plugin):
                                error('Plugin "{}" has no precommit function. '
                                      'The function is required to run the '
                                      'plugin as git pre commit hook.'.format(
                                          name))
                            elif not has_precommit_parameters(plugin):
                                warning(
                                    'Plugin "{}" uses a deprecated signature '
                                    'for its precommit function. It is missing '
                                    'the **kwargs parameter.'.format(name))
                            else:
                                ok('Plugin "{}" active and loadable'.format(
                                    name))
                        except ImportError as e:
                            error('"{}" is not a valid autohooks '
                                  'plugin. {}'.format(name, e))
예제 #4
0
def check_hooks(term: Terminal) -> None:
    pre_commit_hook = PreCommitHook()
    hook_mode = pre_commit_hook.read_mode()

    check_pre_commit_hook(term, pre_commit_hook, hook_mode)

    pyproject_toml = get_pyproject_toml_path()

    check_config(term, pyproject_toml, pre_commit_hook, hook_mode)
예제 #5
0
def install_hooks(args):
    pre_commit_hook = get_pre_commit_hook_path()
    pyproject_toml = get_pyproject_toml_path()
    config = load_config_from_pyproject_toml(pyproject_toml)

    if pre_commit_hook.exists() and not args.force:
        print('pre-commit hook is already installed at {}. --force to '
              'override.'.format(str(pre_commit_hook)))
    else:
        if not config.is_autohooks_enabled():
            print(
                'Warning: autohooks is not enabled in your {} file. Please add '
                'a "{}" section. Run autohooks check for more details.'.format(
                    str(pyproject_toml), AUTOHOOKS_SECTION),
                file=sys.stderr,
            )

        pre_commit_hook_template = get_pre_commit_hook_template_path()
        install_pre_commit_hook(pre_commit_hook_template, pre_commit_hook)

        print('pre-commit hook installed at {}'.format(str(pre_commit_hook)))