Example #1
0
 def test_get_poetry_multiline_mode_from_string(self):
     self.assertEqual(
         Mode.from_string('poetry_multiline'), Mode.POETRY_MULTILINE
     )
     self.assertEqual(
         Mode.from_string('POETRY_MULTILINE'), Mode.POETRY_MULTILINE
     )
Example #2
0
 def test_get_pipenv_multiline_mode_from_string(self):
     self.assertEqual(
         Mode.from_string('pipenv_multiline'), Mode.PIPENV_MULTILINE
     )
     self.assertEqual(
         Mode.from_string('PIPENV_MULTILINE'), Mode.PIPENV_MULTILINE
     )
Example #3
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())))
Example #4
0
    def render(self, *, mode: Mode) -> str:
        mode = mode.get_effective_mode()

        params = dict(VERSION=TEMPLATE_VERSION)

        if mode == Mode.PIPENV:
            params['SHEBANG'] = PIPENV_SHEBANG
        elif mode == Mode.POETRY:
            params['SHEBANG'] = POETRY_SHEBANG
        else:
            params['SHEBANG'] = PYTHON3_SHEBANG

        return self._template.safe_substitute(params)
Example #5
0
    def get_mode(self) -> Mode:
        if self.has_autohooks_config():
            mode = self._autohooks_config.get_value('mode')
            if not mode:
                return Mode.UNDEFINED

            mode = Mode.from_string(mode.upper())
            is_virtual_env = mode == Mode.PIPENV or mode == Mode.POETRY
            if is_virtual_env and not is_split_env():
                if mode == Mode.POETRY:
                    mode = Mode.POETRY_MULTILINE
                else:
                    mode = Mode.PIPENV_MULTILINE
            return mode

        return Mode.UNDEFINED
Example #6
0
def check_pre_commit_hook(
    term: Terminal, pre_commit_hook: PreCommitHook, hook_mode: Mode,
) -> None:
    if pre_commit_hook.exists():
        if pre_commit_hook.is_autohooks_pre_commit_hook():
            term.ok('autohooks pre-commit hook is active.')

            if pre_commit_hook.is_current_autohooks_pre_commit_hook():
                term.ok('autohooks pre-commit hook is up-to-date.')
            else:
                term.warning(
                    'autohooks pre-commit hook is outdated. Please run '
                    '\'autohooks activate --force\' to update your pre-commit '
                    'hook.'
                )

            hook_mode = pre_commit_hook.read_mode()
            if hook_mode == Mode.UNKNOWN:
                term.warning(
                    'Unknown autohooks mode in {}. Falling back to "{}" '
                    'mode.'.format(
                        str(pre_commit_hook),
                        str(hook_mode.get_effective_mode()),
                    )
                )
        else:
            term.error(
                'autohooks pre-commit hook is not active. But a different '
                'pre-commit hook has been found at {}.'.format(
                    str(pre_commit_hook)
                )
            )

    else:
        term.error(
            'autohooks pre-commit hook not active. Please run \'autohooks '
            'activate\'.'
        )
Example #7
0
def check_hook_mode(term: Terminal, config_mode: Mode,
                    hook_mode: Mode) -> None:
    if config_mode.get_effective_mode() != hook_mode.get_effective_mode():
        term.warning(
            f'autohooks mode "{str(hook_mode)}" in pre-commit hook differs '
            f'from mode "{str(config_mode)}" in pyproject.toml file.')
Example #8
0
 def test_get_invalid_mode_from_string(self):
     self.assertEqual(Mode.from_string('foo'), Mode.UNKNOWN)
     self.assertEqual(Mode.from_string(None), Mode.UNDEFINED)
     self.assertEqual(Mode.from_string(''), Mode.UNDEFINED)
Example #9
0
 def test_get_poetry_mode_from_string(self):
     self.assertEqual(Mode.from_string('poetry'), Mode.POETRY)
     self.assertEqual(Mode.from_string('POETRY'), Mode.POETRY)
Example #10
0
 def test_get_pythonpathvenv_mode_from_string(self):
     self.assertEqual(Mode.from_string('pythonpathvenv'),
                      Mode.PYTHONPATHVENV)
     self.assertEqual(Mode.from_string('PYTHONPATHVENV'),
                      Mode.PYTHONPATHVENV)
Example #11
0
 def test_get_pythonpath_mode_from_string(self):
     self.assertEqual(Mode.from_string('pythonpath'), Mode.PYTHONPATH)
     self.assertEqual(Mode.from_string('PYTHONPATH'), Mode.PYTHONPATH)
Example #12
0
 def test_get_pipenv_mode_from_string(self):
     self.assertEqual(Mode.from_string('pipenv'), Mode.PIPENV)
     self.assertEqual(Mode.from_string('PIPENV'), Mode.PIPENV)
Example #13
0
def check_config(
    term: Terminal,
    pyproject_toml: Path,
    pre_commit_hook: PreCommitHook,
    hook_mode: Mode,
) -> 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)
            )
        else:
            config_mode = config.get_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)
                            )
Example #14
0
def check_hook_mode(term: Terminal, config_mode: Mode,
                    hook_mode: Mode) -> None:
    if config_mode.get_effective_mode() != hook_mode.get_effective_mode():
        term.warning('autohooks mode "{}" in pre-commit hook differs from '
                     'mode "{}" in pyproject.toml file.'.format(
                         str(hook_mode), str(config_mode)))