Beispiel #1
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())))
Beispiel #2
0
    def test_load_config_dict_from_toml_file(self):
        config_path = get_test_config_path('pyproject.test2.toml')
        self.assertTrue(config_path.is_file())

        autohooksconfig = load_config_from_pyproject_toml(config_path)
        self.assertTrue(autohooksconfig.has_config())

        config = autohooksconfig.get_config()
        self.assertEqual(
            config.get('tool')
            .get('autohooks')
            .get('plugins')
            .get('foo')
            .get_value('bar'),
            'ipsum',
        )
        self.assertEqual(
            config.get('tool')
            .get('autohooks')
            .get('plugins')
            .get('foo')
            .get_value('lorem'),
            'dolor',
        )
        self.assertEqual(
            config.get('tool')
            .get('autohooks')
            .get('plugins')
            .get('bar')
            .get_value('foo'),
            'bar',
        )
Beispiel #3
0
 def install_git_hook(self) -> None:
     try:
         pre_commit_hook = PreCommitHook()
         if not pre_commit_hook.exists():
             config = load_config_from_pyproject_toml()
             pre_commit_hook.write(mode=config.get_mode())
     except Exception:  # pylint: disable=broad-except
         pass
Beispiel #4
0
def run() -> int:
    term = Terminal()

    _set_terminal(term)

    config = load_config_from_pyproject_toml()

    pre_commit_hook = PreCommitHook()

    check_hook_is_current(term, pre_commit_hook)

    if config.has_autohooks_config():
        check_hook_mode(term, config.get_mode(), pre_commit_hook.read_mode())

    plugins = get_project_autohooks_plugins_path()
    plugins_dir_name = str(plugins)

    if plugins.is_dir():
        sys.path.append(plugins_dir_name)

    term.bold_info('autohooks => pre-commit')

    with autohooks_module_path(), term.indent():
        for name in config.get_pre_commit_script_names():
            term.info('Running {}'.format(name))

            with term.indent():
                try:
                    plugin = load_plugin(name)
                    if not has_precommit_function(plugin):
                        term.fail(
                            'No precommit function found in plugin {}. '
                            'Your autohooks settings may be invalid.'.format(
                                name))
                        return 1

                    if has_precommit_parameters(plugin):
                        retval = plugin.precommit(config=config.get_config())
                    else:
                        term.warning(
                            'precommit function without kwargs is deprecated. '
                            'Please update {} to a newer version.'.format(
                                name))
                        retval = plugin.precommit()

                    if retval:
                        return retval

                except ImportError as e:
                    term.error('An error occurred while importing pre-commit '
                               'hook {}. {}.'.format(name, e))
                    return 1
                except Exception as e:  # pylint: disable=broad-except
                    term.error('An error occurred while running pre-commit '
                               'hook {}. {}.'.format(name, e))
                    return 1

    return 0
Beispiel #5
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))
    def test_get_pylint_config(self):
        config_path = get_test_config_path('pyproject.test.toml')
        self.assertTrue(config_path.is_file())

        autohooksconfig = load_config_from_pyproject_toml(config_path)
        self.assertTrue(autohooksconfig.has_config())

        pylint_config = get_pylint_config(autohooksconfig.get_config())
        self.assertEqual(pylint_config.get_value('foo'), 'bar')
Beispiel #7
0
    def test_load_from_non_existing_toml_file(self):
        config_path = Path('foo')
        self.assertFalse(config_path.exists())

        config = load_config_from_pyproject_toml(config_path)

        self.assertFalse(config.has_config())
        self.assertFalse(config.has_autohooks_config())
        self.assertFalse(config.is_autohooks_enabled())

        self.assertEqual(len(config.get_pre_commit_script_names()), 0)
Beispiel #8
0
    def test_load_from_toml_file(self):
        config_path = get_test_config_path('pyproject.test1.toml')
        self.assertTrue(config_path.is_file())

        config = load_config_from_pyproject_toml(config_path)

        self.assertTrue(config.has_config())
        self.assertTrue(config.has_autohooks_config())
        self.assertTrue(config.is_autohooks_enabled())

        self.assertListEqual(config.get_pre_commit_script_names(),
                             ['foo', 'bar'])
Beispiel #9
0
def run():
    print('autohooks => pre-commit')

    config = load_config_from_pyproject_toml()

    plugins = get_project_autohooks_plugins_path()
    plugins_dir_name = str(plugins)

    if plugins.is_dir():
        sys.path.append(plugins_dir_name)

    with autohooks_module_path():
        for name in config.get_pre_commit_script_names():
            try:
                plugin = load_plugin(name)
                if not has_precommit_function(plugin):
                    error(
                        'No precommit function found in plugin {}'.format(name),
                    )
                    return 0

                if has_precommit_parameters(plugin):
                    retval = plugin.precommit(config=config.get_config())
                else:
                    warning(
                        'precommit function without kwargs is deprecated.',
                    )
                    retval = plugin.precommit()

                if retval:
                    return retval

            except ImportError as e:
                error(
                    'An error occurred while importing pre-commit '
                    'hook {}. {}. The hook will be ignored.'.format(name, e),
                )
            except Exception as e:  # pylint: disable=broad-except
                error(
                    'An error occurred while running pre-commit '
                    'hook {}. {}. The hook will be ignored.'.format(name, e),
                )

    return 0
Beispiel #10
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)))
Beispiel #11
0
def check_config(
    term: Terminal,
    pyproject_toml: Path,
    pre_commit_hook: PreCommitHook,
) -> None:
    if not pyproject_toml.exists():
        term.error(
            f'Missing {str(pyproject_toml)} file. Please add a pyproject.toml '
            f'file and include a "{AUTOHOOKS_SECTION}" section.'
        )
    else:
        config = load_config_from_pyproject_toml(pyproject_toml)
        if not config.is_autohooks_enabled():
            term.error(
                f'autohooks is not enabled in your {str(pyproject_toml)} file.'
                f' Please add a "{AUTOHOOKS_SECTION}" section.'
            )
        elif pre_commit_hook.exists():
            config_mode = config.get_mode()
            hook_mode = pre_commit_hook.read_mode()

            if config_mode == Mode.UNDEFINED:
                term.warning(
                    f'autohooks mode is not defined in {str(pyproject_toml)}.'
                )
            elif config_mode == Mode.UNKNOWN:
                term.warning(
                    f'Unknown autohooks mode in {str(pyproject_toml)}.'
                )

            if (
                config_mode.get_effective_mode()
                != hook_mode.get_effective_mode()
            ):
                term.warning(
                    f'autohooks mode "{str(hook_mode)}" in pre-commit '
                    f'hook {str(pre_commit_hook)} differs from '
                    f'mode "{str(config_mode)}" in {str(pyproject_toml)}.'
                )

            term.info(
                f'Using autohooks mode "{str(hook_mode.get_effective_mode())}".'
            )

            plugins = config.get_pre_commit_script_names()
            if not plugins:
                term.error(
                    'No autohooks plugin is activated in '
                    f'{str(pyproject_toml)} for your pre commit hook. Please '
                    'add a "pre-commit = [plugin1, plugin2]" setting.'
                )
            else:
                with autohooks_module_path():
                    for name in plugins:
                        try:
                            plugin = load_plugin(name)
                            if not has_precommit_function(plugin):
                                term.error(
                                    f'Plugin "{name}" has no precommit '
                                    'function. The function is required to run'
                                    ' the plugin as git pre commit hook.'
                                )
                            elif not has_precommit_parameters(plugin):
                                term.warning(
                                    f'Plugin "{name}" uses a deprecated '
                                    'signature for its precommit function. It '
                                    'is missing the **kwargs parameter.'
                                )
                            else:
                                term.ok(f'Plugin "{name}" active and loadable.')
                        except ImportError as e:
                            term.error(
                                f'"{name}" is not a valid autohooks '
                                f'plugin. {e}'
                            )
Beispiel #12
0
def check_config(
    term: Terminal, pyproject_toml: Path, pre_commit_hook: PreCommitHook,
) -> None:
    if not pyproject_toml.exists():
        term.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():
            term.error(
                'autohooks is not enabled in your {} file. Please add '
                'a "{}" section.'.format(str(pyproject_toml), AUTOHOOKS_SECTION)
            )
        elif pre_commit_hook.exists():
            config_mode = config.get_mode()
            hook_mode = pre_commit_hook.read_mode()

            if config_mode == Mode.UNDEFINED:
                term.warning(
                    'autohooks mode is not defined in {}.'.format(
                        str(pyproject_toml)
                    )
                )
            elif config_mode == Mode.UNKNOWN:
                term.warning(
                    'Unknown autohooks mode in {}.'.format(str(pyproject_toml))
                )

            if (
                config_mode.get_effective_mode()
                != hook_mode.get_effective_mode()
            ):
                term.warning(
                    'autohooks mode "{}" in pre-commit hook {} differs from '
                    'mode "{}" in {}.'.format(
                        str(hook_mode),
                        str(pre_commit_hook),
                        str(config_mode),
                        str(pyproject_toml),
                    )
                )

            term.info(
                'Using autohooks mode "{}".'.format(
                    str(hook_mode.get_effective_mode())
                )
            )

            plugins = config.get_pre_commit_script_names()
            if not plugins:
                term.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):
                                term.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):
                                term.warning(
                                    'Plugin "{}" uses a deprecated signature '
                                    'for its precommit function. It is missing '
                                    'the **kwargs parameter.'.format(name)
                                )
                            else:
                                term.ok(
                                    'Plugin "{}" active and loadable.'.format(
                                        name
                                    )
                                )
                        except ImportError as e:
                            term.error(
                                '"{}" is not a valid autohooks '
                                'plugin. {}'.format(name, e)
                            )
Beispiel #13
0
def conf3() -> Config:
    config_path = get_test_config_path('test_config_3.toml')
    assert config_path.is_file()

    return load_config_from_pyproject_toml(config_path).get_config()