From 4db38596c8bf22077faf23076cc83f8edd69b00f Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 13 Mar 2014 23:55:02 -0700 Subject: [PATCH 1/5] Attempt to improve performance by eliminating a sleep call --- pre_commit/languages/python.py | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index 77819d1f..c2d49cf9 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -1,18 +1,9 @@ -import contextlib from plumbum import local -from plumbum.machines.session import ShellSession PY_ENV = 'py_env' -@contextlib.contextmanager -def in_env(): - with ShellSession(local['bash'].popen()) as env: - env.run('source {0}/bin/activate'.format(PY_ENV)) - yield env - - def install_environment(): assert local.path('setup.py').exists() # Return immediately if we already have a virtualenv @@ -21,16 +12,14 @@ def install_environment(): # Install a virtualenv local['virtualenv'][PY_ENV]() - - with in_env() as env: - # Run their setup.py - env.run('pip install .') + local['bash']['-c', 'source {0}/bin/activate && pip install .'.format(PY_ENV)]() def run_hook(hook, file_args): - with in_env() as env: - # TODO: batch filenames - return env.run( - ' '.join([hook['entry']] + hook.get('args', []) + list(file_args)), - retcode=None, - ) \ No newline at end of file + # TODO: batch filenames + return local['bash'][ + '-c', ' '.join( + ['source {0}/bin/activate &&'.format(PY_ENV)] + + [hook['entry']] + hook.get('args', []) + list(file_args) + ) + ].run() \ No newline at end of file From 0f14d4c0726cac062719f6c2f0e46745477f4dfa Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 14 Mar 2014 00:04:15 -0700 Subject: [PATCH 2/5] Attempt with subprocess instead --- pre_commit/languages/python.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index c2d49cf9..3c8f8f01 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -1,5 +1,6 @@ from plumbum import local +import subprocess PY_ENV = 'py_env' @@ -7,7 +8,7 @@ PY_ENV = 'py_env' 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 @@ -17,6 +18,17 @@ def install_environment(): def run_hook(hook, file_args): # TODO: batch filenames + process = subprocess.Popen( + ['bash', '-c', ' '.join( + ['source {0}/bin/activate &&'.format(PY_ENV)] + + [hook['entry']] + hook.get('args', []) + list(file_args) + )], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + ) + ret = process.communicate() + + return (0,) + ret + return local['bash'][ '-c', ' '.join( ['source {0}/bin/activate &&'.format(PY_ENV)] + From 1a8095e71e81eff716524fa75eb9f07615ee61d2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 14 Mar 2014 00:05:12 -0700 Subject: [PATCH 3/5] Return the actual return code --- pre_commit/languages/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index 3c8f8f01..d5698f35 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -27,7 +27,7 @@ def run_hook(hook, file_args): ) ret = process.communicate() - return (0,) + ret + return (process.returncode,) + ret return local['bash'][ '-c', ' '.join( From 6b62ec47e8af3ff650ce9516f79805e90c1847e3 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 14 Mar 2014 00:30:26 -0700 Subject: [PATCH 4/5] Performance hack --- pre_commit/languages/python.py | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index d5698f35..71b89d6c 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -1,6 +1,5 @@ from plumbum import local -import subprocess PY_ENV = 'py_env' @@ -17,21 +16,9 @@ def install_environment(): def run_hook(hook, file_args): - # TODO: batch filenames - process = subprocess.Popen( - ['bash', '-c', ' '.join( - ['source {0}/bin/activate &&'.format(PY_ENV)] + - [hook['entry']] + hook.get('args', []) + list(file_args) - )], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - ) - ret = process.communicate() - - return (process.returncode,) + ret - return local['bash'][ '-c', ' '.join( ['source {0}/bin/activate &&'.format(PY_ENV)] + [hook['entry']] + hook.get('args', []) + list(file_args) ) - ].run() \ No newline at end of file + ].run() From 43767511e2df8bc0d90351e35d6c56741013f528 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 14 Mar 2014 00:31:24 -0700 Subject: [PATCH 5/5] Nicer interfaces --- pre_commit/languages/python.py | 50 +++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index d5698f35..53673a9a 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -1,10 +1,34 @@ +import contexlib from plumbum import local -import subprocess PY_ENV = 'py_env' +class PythonEnv(object): + def __init__(self): + self.env_prefix = '. {0}/bin/activate &&'.format(PY_ENV) + + def run(self, cmd): + return local['bash']['-c', ' '.join([self.env_prefix, cmd])]() + + +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): + return self.py_env.run(' '.join(self.env_prefix, cmd)) + + +@contexlib.contextmanager +def in_env(): + yield PythonEnv() + + def install_environment(): assert local.path('setup.py').exists() # Return immediately if we already have a virtualenv @@ -13,25 +37,13 @@ def install_environment(): # Install a virtualenv local['virtualenv'][PY_ENV]() - local['bash']['-c', 'source {0}/bin/activate && pip install .'.format(PY_ENV)]() + with in_env() as env: + env.run('pip install .') def run_hook(hook, file_args): # TODO: batch filenames - process = subprocess.Popen( - ['bash', '-c', ' '.join( - ['source {0}/bin/activate &&'.format(PY_ENV)] + - [hook['entry']] + hook.get('args', []) + list(file_args) - )], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - ) - ret = process.communicate() - - return (process.returncode,) + ret - - return local['bash'][ - '-c', ' '.join( - ['source {0}/bin/activate &&'.format(PY_ENV)] + - [hook['entry']] + hook.get('args', []) + list(file_args) - ) - ].run() \ No newline at end of file + with in_env() as env: + env = env + # MAGIC + pass \ No newline at end of file