Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add template for pipenv, update install routine #20

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ $ git commit
This allows the user to choose whether to execute the hooks by activating the
virtual environment or to ignore them by deactivating it.

### Auto Installation as an alternative

There might be use cases where you want the Git hooks to be installed
automatically at build time of the `Pipenv`. To enable this, you have to add
an option
```
auto-install = true
```
to your `pyproject.toml`. Furthermore, the automatic installation
only works if `autohooks` is installed via the Git repository
as explained above. Otherwise, you have to run `pipenv run autohooks activate`
manually.

## Plugins

* Python code formatting via [black](https://github.com/greenbone/autohooks-plugin-black)
Expand Down
4 changes: 4 additions & 0 deletions autohooks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def get_pre_commit_script_names(self):
def get_config(self):
return self._config

def get_auto_install(self):
LeoIV marked this conversation as resolved.
Show resolved Hide resolved
if self.has_autohooks_config():
return self._autohooks_config.get_value('auto-install', False)


def load_config_from_pyproject_toml(pyproject_toml=None):
if pyproject_toml is None:
Expand Down
16 changes: 12 additions & 4 deletions autohooks/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@

import shutil

from setuptools.command.install import install
from setuptools.command.develop import develop
from setuptools.command.install import install

from autohooks.config import load_config_from_pyproject_toml
from autohooks.utils import (
get_git_hook_directory_path,
get_autohooks_directory_path,
)
get_pyproject_toml_path)

pyproject_toml = get_pyproject_toml_path()
config = load_config_from_pyproject_toml(pyproject_toml)
LeoIV marked this conversation as resolved.
Show resolved Hide resolved


def get_pre_commit_hook_path():
Expand All @@ -32,8 +36,12 @@ def get_pre_commit_hook_path():


def get_pre_commit_hook_template_path():
setup_dir_path = get_autohooks_directory_path()
return setup_dir_path / 'precommit' / 'template'
auto_install = config.get_auto_install()

setup_dir_path = get_autohooks_directory_path() / 'precommit'
if auto_install:
return setup_dir_path / 'template_pipenv'
return setup_dir_path / 'template'


def install_pre_commit_hook(pre_commit_hook_file, pre_commit_hook):
Expand Down
11 changes: 11 additions & 0 deletions autohooks/precommit/template_pipenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env pipenv run python3

import sys

try:
from autohooks.precommit import run
sys.exit(run())
except ImportError:
print('autohooks not installed. Ignoring pre-commit hook.')
sys.exit(0)