mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-04 07:50:00 -06:00
Repository now parses languages and manifests
This commit is contained in:
@@ -48,4 +48,6 @@ def get_validator(
|
||||
|
||||
additional_validation_strategy(obj)
|
||||
|
||||
return obj
|
||||
|
||||
return validate
|
||||
@@ -2,24 +2,50 @@
|
||||
import contextlib
|
||||
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.util import cached_property
|
||||
|
||||
|
||||
class Repository(object):
|
||||
def __init__(self, repo_config):
|
||||
self.repo_config = repo_config
|
||||
|
||||
@property
|
||||
@cached_property
|
||||
def repo_url(self):
|
||||
return self.repo_config['repo']
|
||||
|
||||
@property
|
||||
@cached_property
|
||||
def sha(self):
|
||||
return self.repo_config['sha']
|
||||
|
||||
@cached_property
|
||||
def languages(self):
|
||||
return set(filter(None, (
|
||||
hook.get('language') for hook in self.hooks.values()
|
||||
)))
|
||||
|
||||
@cached_property
|
||||
def hooks(self):
|
||||
return dict(
|
||||
(hook['id'], dict(hook, **self.manifest[hook['id']]))
|
||||
for hook in self.repo_config['hooks']
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def manifest(self):
|
||||
with self.in_checkout():
|
||||
return dict(
|
||||
(hook['id'], hook)
|
||||
for hook in validate_manifest(C.MANIFEST_FILE)
|
||||
)
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_checkout(self):
|
||||
with in_hooks_workspace():
|
||||
# SMELL:
|
||||
self.create()
|
||||
with local.cwd(self.sha):
|
||||
yield
|
||||
|
||||
@@ -33,6 +59,19 @@ class Repository(object):
|
||||
with self.in_checkout():
|
||||
local['git']['checkout', self.sha]()
|
||||
|
||||
# TODO: make this shit polymorphic
|
||||
|
||||
def _install_python(self):
|
||||
assert local.path('setup.py').exists()
|
||||
local['virtualenv']['py_env']()
|
||||
local['bash']['-c', 'source py_env/bin/activate && pip install .']()
|
||||
|
||||
def _install_ruby(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def _install_node(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def install(self):
|
||||
# Create if we have not already
|
||||
self.create()
|
||||
|
||||
16
pre_commit/util.py
Normal file
16
pre_commit/util.py
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
class cached_property(object):
|
||||
"""Like @property, but caches the value."""
|
||||
|
||||
def __init__(self, func):
|
||||
self.__name__ = func.__name__
|
||||
self.__module__ = func.__module__
|
||||
self.__doc__ = func.__doc__
|
||||
self._func = func
|
||||
|
||||
def __get__(self, obj, cls):
|
||||
if obj is None:
|
||||
return self
|
||||
value = self._func(obj)
|
||||
obj.__dict__[self.__name__] = value
|
||||
return value
|
||||
Reference in New Issue
Block a user