Merge pull request #37 from pre-commit/fix_lots_of_files

Fix lots of files problem
This commit is contained in:
Anthony Sottile 2014-03-22 16:59:34 -07:00
commit 64745fb0b4
5 changed files with 76 additions and 28 deletions

View file

@ -1,6 +1,37 @@
import subprocess
def run_hook(env, hook, file_args):
return env.run(
' '.join([hook['entry']] + hook.get('args', []) + list(file_args)),
retcode=None,
)
' '.join(['xargs', hook['entry']] + hook.get('args', [])),
stdin='\n'.join(list(file_args) + ['']),
)
class Environment(object):
@property
def env_prefix(self):
"""env_prefix is a value that is prefixed to the command that is run.
Usually this is to source a virtualenv, etc.
Commands basically end up looking like:
bash -c '{env_prefix} {cmd}'
so you'll often want to end your prefix with &&
"""
raise NotImplementedError
def run(self, cmd, stdin=None, **kwargs):
"""Returns (returncode, stdout, stderr)."""
proc = subprocess.Popen(
['bash', '-c', ' '.join([self.env_prefix, cmd])],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = proc.communicate(stdin)
return proc.returncode, stdout, stderr

View file

@ -8,37 +8,42 @@ from pre_commit.languages import python
NODE_ENV = 'node_env'
class NodeEnv(object):
def __init__(self, py_env):
self.py_env = py_env
self.env_prefix = '. {0}/bin/activate &&'.format(NODE_ENV)
def run(self, cmd, **kwargs):
return self.py_env.run(' '.join([self.env_prefix, cmd]), **kwargs)
class NodeEnv(python.PythonEnv):
@property
def env_prefix(self):
base = super(NodeEnv, self).env_prefix
return ' '.join([base, '. {0}/bin/activate &&'.format(NODE_ENV)])
@contextlib.contextmanager
def in_env(py_env):
yield NodeEnv(py_env)
def in_env():
yield NodeEnv()
def install_environment():
assert local.path('package.json').exists()
if local.path('node_env').exists():
if local.path(NODE_ENV).exists():
return
local['virtualenv'][python.PY_ENV]()
with python.in_env() as python_env:
python_env.run('pip install nodeenv')
python_env.run('nodeenv --jobs 4 {0}'.format(NODE_ENV))
with in_env(python_env) as node_env:
try:
# Try and use the system level node executable first
python_env.run('nodeenv -n system {0}'.format(NODE_ENV))
except Exception:
# TODO: log exception here
# cleanup
local.path(NODE_ENV).remove()
python_env.run('nodeenv --jobs 4 {0}'.format(NODE_ENV))
with in_env() as node_env:
node_env.run('npm install -g')
def run_hook(hook, file_args):
with python.in_env() as py_env:
with in_env(py_env) as node_env:
return helpers.run_hook(node_env, hook, file_args)
with in_env() as node_env:
return helpers.run_hook(node_env, hook, file_args)

View file

@ -6,12 +6,10 @@ from pre_commit.languages import helpers
PY_ENV = 'py_env'
class PythonEnv(object):
def __init__(self):
self.env_prefix = '. {0}/bin/activate &&'.format(PY_ENV)
def run(self, cmd, **kwargs):
return local['bash']['-c', ' '.join([self.env_prefix, cmd])].run(**kwargs)
class PythonEnv(helpers.Environment):
@property
def env_prefix(self):
return '. {0}/bin/activate &&'.format(PY_ENV)
@contextlib.contextmanager