diff --git a/pre_commit/languages/node.py b/pre_commit/languages/node.py index 9f15f43f..bc4fa007 100644 --- a/pre_commit/languages/node.py +++ b/pre_commit/languages/node.py @@ -1,17 +1,25 @@ import contextlib from plumbum import local -from plumbum.machines.session import ShellSession -from pre_commit.languages import helpers +from pre_commit.languages import helpers 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) + + @contextlib.contextmanager -def in_env(): - with ShellSession(local['bash'].popen()) as env: - env.run('. {0}/bin/activate'.format(NODE_ENV)) - yield env +def in_env(py_env): + yield NodeEnv(py_env) def install_environment(): @@ -20,21 +28,17 @@ def install_environment(): if local.path('node_env').exists(): return - # Install a virtualenv local['virtualenv'][python.PY_ENV]() with python.in_env() as python_env: python_env.run('pip install nodeenv') + python_env.run('nodeenv {0}'.format(NODE_ENV)) - print "Creating nodeenv" - local['nodeenv'][NODE_ENV]() - print "Done nodeenv" - - with in_env() as node_env: + with in_env(python_env) as node_env: node_env.run('npm install -g') def run_hook(hook, file_args): - with python.in_env(): - with in_env() as node_env: + with python.in_env() as py_env: + with in_env(py_env) as node_env: return helpers.run_hook(node_env, hook, file_args) \ No newline at end of file diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index 9626f108..850ad660 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -1,34 +1,37 @@ import contextlib from plumbum import local -from plumbum.machines.session import ShellSession 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) + + @contextlib.contextmanager def in_env(): - with ShellSession(local['bash'].popen()) as env: - env.run('source {0}/bin/activate'.format(PY_ENV)) - yield env + yield PythonEnv() def install_environment(): assert local.path('setup.py').exists() # Return immediately if we already have a virtualenv - if local.path('py_env').exists(): + if local.path(PY_ENV).exists(): return # 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): + # TODO: batch filenames with in_env() as env: - # TODO: batch filenames - return helpers.run_hook(env, hook, file_args) \ No newline at end of file + return helpers.run_hook(env, hook, file_args)