Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 871ab4d

Browse files
committed
OMG we're running a hook
1 parent 47bad12 commit 871ab4d

11 files changed

Lines changed: 105 additions & 25 deletions

File tree

pre_commit/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
MANIFEST_FILE = 'manifest.yaml'
77

8-
SUPPORTED_LANGUAGES = [
8+
SUPPORTED_LANGUAGES = set([
99
'python',
1010
'ruby',
1111
'node',
12-
]
12+
])

pre_commit/languages/__init__.py

Whitespace-only changes.

pre_commit/languages/all.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
from pre_commit.languages import node
3+
from pre_commit.languages import python
4+
from pre_commit.languages import ruby
5+
6+
# A language implements the following two functions in its module:
7+
#
8+
# def install_environment():
9+
# """Installs a repository in the given repository. Note that the current
10+
# working directory will already be inside the repository.
11+
# """
12+
#
13+
# def run_hook(hook, file_args):
14+
# """Runs a hook and returns the returncode and output of running that hook.
15+
#
16+
# Returns:
17+
# (returncode, stdout, stderr)
18+
# """
19+
20+
languages = {
21+
'node': node,
22+
'python': python,
23+
'ruby': ruby,
24+
}

pre_commit/languages/node.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
def install_environment():
3+
raise NotImplementedError
4+
5+
6+
def run_hook(hook, file_args):
7+
raise NotImplementedError

pre_commit/languages/python.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
import contextlib
3+
from plumbum import local
4+
from plumbum.machines.session import ShellSession
5+
6+
PY_ENV = 'py_env'
7+
8+
9+
@contextlib.contextmanager
10+
def in_env():
11+
with ShellSession(local['bash'].popen()) as env:
12+
env.run('source {0}/bin/activate'.format(PY_ENV))
13+
yield env
14+
15+
16+
def install_environment():
17+
assert local.path('setup.py').exists()
18+
# Install a virtualenv
19+
local['virtualenv'][PY_ENV]()
20+
21+
with in_env() as env:
22+
# Run their setup.py
23+
env.run('pip install .')
24+
25+
26+
def run_hook(hook, file_args):
27+
with in_env() as env:
28+
# TODO: batch filenames
29+
return env.run(
30+
' '.join([hook['entry']] + hook.get('args', []) + list(file_args)),
31+
retcode=None,
32+
)

pre_commit/languages/ruby.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
def install_environment():
3+
raise NotImplementedError
4+
5+
6+
def run_hook(hook, file_args):
7+
raise NotImplementedError

pre_commit/repository.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,10 @@
55
import pre_commit.constants as C
66
from pre_commit.clientlib.validate_manifest import validate_manifest
77
from pre_commit.hooks_workspace import in_hooks_workspace
8+
from pre_commit.languages.all import languages
89
from pre_commit.util import cached_property
910

1011

11-
def install_python(repo):
12-
assert local.path('setup.py').exists()
13-
local['virtualenv']['py_env']()
14-
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
15-
16-
17-
def install_ruby(repo):
18-
raise NotImplementedError
19-
20-
21-
def install_node(repo):
22-
raise NotImplementedError
23-
24-
25-
language_to_repo_setup_strategy = {
26-
'python': install_python,
27-
'ruby': install_ruby,
28-
'node': install_node,
29-
}
30-
31-
3212
class Repository(object):
3313
def __init__(self, repo_config):
3414
self.repo_config = repo_config
@@ -84,4 +64,9 @@ def install(self):
8464
with self.in_checkout():
8565
for language in C.SUPPORTED_LANGUAGES:
8666
if language in self.languages:
87-
language_to_repo_setup_strategy[language](self)
67+
languages[language].install_environment()
68+
69+
def run_hook(self, hook_id, file_args):
70+
with self.in_checkout():
71+
hook = self.hooks[hook_id]
72+
return languages[hook['language']].run_hook(hook, file_args)

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def python_pre_commit_git_repo(dummy_git_repo):
6666
local.path('__init__.py').write('')
6767
local.path('main.py').write("""
6868
def func():
69+
print 'Hello World'
6970
return 0
7071
""")
7172

tests/languages/__init__.py

Whitespace-only changes.

tests/languages/all_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import pytest
3+
4+
import pre_commit.constants as C
5+
from pre_commit.languages.all import languages
6+
7+
8+
def test_all_languages_have_repo_setups():
9+
assert set(languages.keys()) == C.SUPPORTED_LANGUAGES
10+
11+
12+
@pytest.mark.parametrize('language', C.SUPPORTED_LANGUAGES)
13+
def test_all_languages_support_interface(language):
14+
assert hasattr(languages[language], 'install_environment')
15+
assert hasattr(languages[language], 'run_hook')

0 commit comments

Comments
 (0)