mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-07 09:30:18 -06:00
OMG we're running a hook
This commit is contained in:
@@ -5,8 +5,8 @@ HOOKS_WORKSPACE = '.pre-commit-files'
|
||||
|
||||
MANIFEST_FILE = 'manifest.yaml'
|
||||
|
||||
SUPPORTED_LANGUAGES = [
|
||||
SUPPORTED_LANGUAGES = set([
|
||||
'python',
|
||||
'ruby',
|
||||
'node',
|
||||
]
|
||||
])
|
||||
0
pre_commit/languages/__init__.py
Normal file
0
pre_commit/languages/__init__.py
Normal file
24
pre_commit/languages/all.py
Normal file
24
pre_commit/languages/all.py
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
from pre_commit.languages import node
|
||||
from pre_commit.languages import python
|
||||
from pre_commit.languages import ruby
|
||||
|
||||
# A language implements the following two functions in its module:
|
||||
#
|
||||
# def install_environment():
|
||||
# """Installs a repository in the given repository. Note that the current
|
||||
# working directory will already be inside the repository.
|
||||
# """
|
||||
#
|
||||
# def run_hook(hook, file_args):
|
||||
# """Runs a hook and returns the returncode and output of running that hook.
|
||||
#
|
||||
# Returns:
|
||||
# (returncode, stdout, stderr)
|
||||
# """
|
||||
|
||||
languages = {
|
||||
'node': node,
|
||||
'python': python,
|
||||
'ruby': ruby,
|
||||
}
|
||||
7
pre_commit/languages/node.py
Normal file
7
pre_commit/languages/node.py
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
def install_environment():
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def run_hook(hook, file_args):
|
||||
raise NotImplementedError
|
||||
32
pre_commit/languages/python.py
Normal file
32
pre_commit/languages/python.py
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
import contextlib
|
||||
from plumbum import local
|
||||
from plumbum.machines.session import ShellSession
|
||||
|
||||
PY_ENV = 'py_env'
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env():
|
||||
with ShellSession(local['bash'].popen()) as env:
|
||||
env.run('source {0}/bin/activate'.format(PY_ENV))
|
||||
yield env
|
||||
|
||||
|
||||
def install_environment():
|
||||
assert local.path('setup.py').exists()
|
||||
# Install a virtualenv
|
||||
local['virtualenv'][PY_ENV]()
|
||||
|
||||
with in_env() as env:
|
||||
# Run their setup.py
|
||||
env.run('pip install .')
|
||||
|
||||
|
||||
def run_hook(hook, file_args):
|
||||
with in_env() as env:
|
||||
# TODO: batch filenames
|
||||
return env.run(
|
||||
' '.join([hook['entry']] + hook.get('args', []) + list(file_args)),
|
||||
retcode=None,
|
||||
)
|
||||
7
pre_commit/languages/ruby.py
Normal file
7
pre_commit/languages/ruby.py
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
def install_environment():
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def run_hook(hook, file_args):
|
||||
raise NotImplementedError
|
||||
@@ -5,30 +5,10 @@ from plumbum import local
|
||||
import pre_commit.constants as C
|
||||
from pre_commit.clientlib.validate_manifest import validate_manifest
|
||||
from pre_commit.hooks_workspace import in_hooks_workspace
|
||||
from pre_commit.languages.all import languages
|
||||
from pre_commit.util import cached_property
|
||||
|
||||
|
||||
def install_python(repo):
|
||||
assert local.path('setup.py').exists()
|
||||
local['virtualenv']['py_env']()
|
||||
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
|
||||
|
||||
|
||||
def install_ruby(repo):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def install_node(repo):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
language_to_repo_setup_strategy = {
|
||||
'python': install_python,
|
||||
'ruby': install_ruby,
|
||||
'node': install_node,
|
||||
}
|
||||
|
||||
|
||||
class Repository(object):
|
||||
def __init__(self, repo_config):
|
||||
self.repo_config = repo_config
|
||||
@@ -84,4 +64,9 @@ class Repository(object):
|
||||
with self.in_checkout():
|
||||
for language in C.SUPPORTED_LANGUAGES:
|
||||
if language in self.languages:
|
||||
language_to_repo_setup_strategy[language](self)
|
||||
languages[language].install_environment()
|
||||
|
||||
def run_hook(self, hook_id, file_args):
|
||||
with self.in_checkout():
|
||||
hook = self.hooks[hook_id]
|
||||
return languages[hook['language']].run_hook(hook, file_args)
|
||||
Reference in New Issue
Block a user