mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-05 07:49:29 -06:00
Resolves cwd problem
This commit is contained in:
@@ -5,14 +5,22 @@ from pre_commit.languages import ruby
|
||||
|
||||
# A language implements the following two functions in its module:
|
||||
#
|
||||
# def install_environment():
|
||||
# def install_environment(repo_cmd_runner):
|
||||
# """Installs a repository in the given repository. Note that the current
|
||||
# working directory will already be inside the repository.
|
||||
#
|
||||
# Args:
|
||||
# repo_cmd_runner - `PrefixedCommandRunner` bound to the repository.
|
||||
# """
|
||||
#
|
||||
# def run_hook(hook, file_args):
|
||||
# def run_hook(repo_cmd_runner, hook, file_args):
|
||||
# """Runs a hook and returns the returncode and output of running that hook.
|
||||
#
|
||||
# Args:
|
||||
# repo_cmd_runner - `PrefixedCommandRunner` bound to the repository.
|
||||
# hook - Hook dictionary
|
||||
# file_args - The files to be run
|
||||
#
|
||||
# Returns:
|
||||
# (returncode, stdout, stderr)
|
||||
# """
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
|
||||
import subprocess
|
||||
|
||||
|
||||
def run_hook(env, hook, file_args):
|
||||
return env.run(
|
||||
' '.join(['xargs', hook['entry']] + hook.get('args', [])),
|
||||
stdin='\n'.join(list(file_args) + ['']),
|
||||
retcode=None,
|
||||
)
|
||||
|
||||
|
||||
class Environment(object):
|
||||
def __init__(self, repo_cmd_runner):
|
||||
self.repo_cmd_runner = repo_cmd_runner
|
||||
|
||||
@property
|
||||
def env_prefix(self):
|
||||
"""env_prefix is a value that is prefixed to the command that is run.
|
||||
@@ -24,14 +26,8 @@ class Environment(object):
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def run(self, cmd, stdin=None):
|
||||
def run(self, cmd, **kwargs):
|
||||
"""Returns (returncode, stdout, stderr)."""
|
||||
proc = subprocess.Popen(
|
||||
['bash', '-c', ' '.join([self.env_prefix, cmd])],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
return self.repo_cmd_runner.run(
|
||||
['bash', '-c', ' '.join([self.env_prefix, cmd])], **kwargs
|
||||
)
|
||||
stdout, stderr = proc.communicate(stdin)
|
||||
|
||||
return proc.returncode, stdout, stderr
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import contextlib
|
||||
from plumbum import local
|
||||
import subprocess
|
||||
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.languages import python
|
||||
@@ -12,37 +12,44 @@ class NodeEnv(python.PythonEnv):
|
||||
@property
|
||||
def env_prefix(self):
|
||||
base = super(NodeEnv, self).env_prefix
|
||||
return ' '.join([base, '. {0}/bin/activate &&'.format(NODE_ENV)])
|
||||
return ' '.join([
|
||||
base,
|
||||
'. {{prefix}}{0}/bin/activate &&'.format(NODE_ENV)]
|
||||
)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env():
|
||||
yield NodeEnv()
|
||||
def in_env(repo_cmd_runner):
|
||||
yield NodeEnv(repo_cmd_runner)
|
||||
|
||||
|
||||
def install_environment():
|
||||
assert local.path('package.json').exists()
|
||||
def install_environment(repo_cmd_runner):
|
||||
assert repo_cmd_runner.exists('package.json')
|
||||
|
||||
if local.path(NODE_ENV).exists():
|
||||
# Return immediately if we already have a virtualenv
|
||||
if repo_cmd_runner.exists(NODE_ENV):
|
||||
return
|
||||
|
||||
local['virtualenv'][python.PY_ENV]()
|
||||
repo_cmd_runner.run(['virtualenv', '{{prefix}}{0}'.format(python.PY_ENV)])
|
||||
|
||||
with python.in_env() as python_env:
|
||||
with python.in_env(repo_cmd_runner) as python_env:
|
||||
python_env.run('pip install nodeenv')
|
||||
|
||||
# Try and use the system level node executable first
|
||||
retcode, _, _ = python_env.run('nodeenv -n system {0}'.format(NODE_ENV))
|
||||
# TODO: log failure here
|
||||
# cleanup
|
||||
if retcode:
|
||||
local.path(NODE_ENV).delete()
|
||||
python_env.run('nodeenv --jobs 4 {0}'.format(NODE_ENV))
|
||||
try:
|
||||
python_env.run('nodeenv -n system {{prefix}}{0}'.format(NODE_ENV))
|
||||
except subprocess.CalledProcessError:
|
||||
# TODO: log failure here
|
||||
# cleanup
|
||||
# TODO: local.path(NODE_ENV).delete()
|
||||
python_env.run('nodeenv --jobs 4 {{prefix}}{0}'.format(NODE_ENV))
|
||||
|
||||
with in_env() as node_env:
|
||||
node_env.run('npm install -g')
|
||||
with in_env(repo_cmd_runner) as node_env:
|
||||
node_env.run(
|
||||
'cd {0} && npm install -g'.format(repo_cmd_runner.prefix_dir),
|
||||
)
|
||||
|
||||
|
||||
def run_hook(hook, file_args):
|
||||
with in_env() as node_env:
|
||||
return helpers.run_hook(node_env, hook, file_args)
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
with in_env(repo_cmd_runner) as env:
|
||||
return helpers.run_hook(env, hook, file_args)
|
||||
|
||||
@@ -1,34 +1,35 @@
|
||||
|
||||
import contextlib
|
||||
from plumbum import local
|
||||
|
||||
from pre_commit.languages import helpers
|
||||
|
||||
|
||||
PY_ENV = 'py_env'
|
||||
|
||||
|
||||
class PythonEnv(helpers.Environment):
|
||||
@property
|
||||
def env_prefix(self):
|
||||
return '. {0}/bin/activate &&'.format(PY_ENV)
|
||||
return '. {{prefix}}{0}/bin/activate &&'.format(PY_ENV)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env():
|
||||
yield PythonEnv()
|
||||
def in_env(repo_cmd_runner):
|
||||
yield PythonEnv(repo_cmd_runner)
|
||||
|
||||
|
||||
def install_environment():
|
||||
assert local.path('setup.py').exists()
|
||||
def install_environment(repo_cmd_runner):
|
||||
assert repo_cmd_runner.exists('setup.py')
|
||||
# Return immediately if we already have a virtualenv
|
||||
if local.path(PY_ENV).exists():
|
||||
if repo_cmd_runner.exists(PY_ENV):
|
||||
return
|
||||
|
||||
# Install a virtualenv
|
||||
local['virtualenv'][PY_ENV]()
|
||||
with in_env() as env:
|
||||
env.run('pip install .')
|
||||
repo_cmd_runner.run(['virtualenv', '{{prefix}}{0}'.format(PY_ENV)])
|
||||
with in_env(repo_cmd_runner) as env:
|
||||
env.run('cd {0} && pip install .'.format(repo_cmd_runner.prefix_dir))
|
||||
|
||||
|
||||
def run_hook(hook, file_args):
|
||||
with in_env() as env:
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
with in_env(repo_cmd_runner) as env:
|
||||
return helpers.run_hook(env, hook, file_args)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
import contextlib
|
||||
from plumbum import local
|
||||
|
||||
from pre_commit.languages import helpers
|
||||
|
||||
@@ -8,29 +7,27 @@ from pre_commit.languages import helpers
|
||||
RVM_ENV = 'rvm_env'
|
||||
|
||||
|
||||
class RubyEnv(object):
|
||||
def __init__(self):
|
||||
self.env_prefix = '. {0}/.rvm/scripts/rvm'.format(RVM_ENV)
|
||||
|
||||
def run(self, cmd, **kwargs):
|
||||
return local['bash']['-c', ' '.join([self.env_prefix, cmd])].run(**kwargs)
|
||||
class RubyEnv(helpers.Environment):
|
||||
@property
|
||||
def env_prefix(self):
|
||||
return '. {{prefix}}{0}/bin/activate &&'.format(RVM_ENV)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env():
|
||||
yield RubyEnv()
|
||||
def in_env(repo_cmd_runner):
|
||||
yield RubyEnv(repo_cmd_runner)
|
||||
|
||||
|
||||
def install_environment():
|
||||
def install_environment(repo_cmd_runner):
|
||||
# Return immediately if we already have a virtualenv
|
||||
if local.path(RVM_ENV).exists():
|
||||
if repo_cmd_runner.exists(RVM_ENV):
|
||||
return
|
||||
|
||||
local['__rvm-env.sh'][RVM_ENV]()
|
||||
with in_env() as env:
|
||||
env.run('bundle install')
|
||||
repo_cmd_runner.run(['__rvm-env.sh', '{{prefix}}{0}'.format(RVM_ENV)])
|
||||
with in_env(repo_cmd_runner) as env:
|
||||
env.run('bundle install', cwd=repo_cmd_runner.prefix_dir)
|
||||
|
||||
|
||||
def run_hook(hook, file_args):
|
||||
with in_env() as env:
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
with in_env(repo_cmd_runner) as env:
|
||||
return helpers.run_hook(env, hook, file_args)
|
||||
|
||||
Reference in New Issue
Block a user