mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Nicer interfaces
This commit is contained in:
parent
1a8095e71e
commit
43767511e2
1 changed files with 31 additions and 19 deletions
|
|
@ -1,10 +1,34 @@
|
||||||
|
|
||||||
|
import contexlib
|
||||||
from plumbum import local
|
from plumbum import local
|
||||||
import subprocess
|
|
||||||
|
|
||||||
PY_ENV = 'py_env'
|
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():
|
def install_environment():
|
||||||
assert local.path('setup.py').exists()
|
assert local.path('setup.py').exists()
|
||||||
# Return immediately if we already have a virtualenv
|
# Return immediately if we already have a virtualenv
|
||||||
|
|
@ -13,25 +37,13 @@ def install_environment():
|
||||||
|
|
||||||
# Install a virtualenv
|
# Install a virtualenv
|
||||||
local['virtualenv'][PY_ENV]()
|
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):
|
def run_hook(hook, file_args):
|
||||||
# TODO: batch filenames
|
# TODO: batch filenames
|
||||||
process = subprocess.Popen(
|
with in_env() as env:
|
||||||
['bash', '-c', ' '.join(
|
env = env
|
||||||
['source {0}/bin/activate &&'.format(PY_ENV)] +
|
# MAGIC
|
||||||
[hook['entry']] + hook.get('args', []) + list(file_args)
|
pass
|
||||||
)],
|
|
||||||
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()
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue