Esempio n. 1
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
Esempio n. 2
0
def autohooks_module_path() -> Generator:
    plugins = get_project_autohooks_plugins_path()
    plugins_dir_name = str(plugins)

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

    yield

    if plugins_dir_name in sys.path:
        sys.path.remove(plugins_dir_name)
Esempio n. 3
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
Esempio n. 4
0
    def test_with_env_pwd(self):
        os.environ['PWD'] = str(self.temp_path)

        autohooks_plugins_path = get_project_autohooks_plugins_path()
        self.assertEqual(autohooks_plugins_path, self.temp_path / '.autohooks')
Esempio n. 5
0
    def test_with_subpath(self):
        sub_path = self.temp_path / 'foo'
        sub_path.mkdir()

        autohooks_plugins_path = get_project_autohooks_plugins_path(sub_path)
        self.assertEqual(autohooks_plugins_path, self.temp_path / '.autohooks')
Esempio n. 6
0
    def test_with_env_pwd(self):
        os.chdir(str(self.temp_path))

        autohooks_plugins_path = get_project_autohooks_plugins_path()
        self.assertEqual(autohooks_plugins_path, self.temp_path / '.autohooks')