Clean up how the environments work.

This commit is contained in:
Anthony Sottile
2014-03-22 16:33:24 -07:00
parent a1ba715b50
commit 6cda5bfe27
4 changed files with 61 additions and 23 deletions

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)