Environments are now installed to version-specific locations. Resolves #229

This commit is contained in:
Anthony Sottile
2015-05-16 19:16:23 -04:00
parent 154d918ff1
commit 45d4a195ef
9 changed files with 71 additions and 44 deletions

View File

@@ -3,6 +3,13 @@ from __future__ import unicode_literals
import pipes
def environment_dir(ENVIRONMENT_DIR, language_version):
if ENVIRONMENT_DIR is None:
return None
else:
return '{0}-{1}'.format(ENVIRONMENT_DIR, language_version)
def file_args_to_stdin(file_args):
return '\0'.join(list(file_args) + [''])
@@ -19,8 +26,9 @@ def run_hook(env, hook, file_args):
class Environment(object):
def __init__(self, repo_cmd_runner):
def __init__(self, repo_cmd_runner, language_version):
self.repo_cmd_runner = repo_cmd_runner
self.language_version = language_version
@property
def env_prefix(self):

View File

@@ -13,22 +13,25 @@ ENVIRONMENT_DIR = 'node_env'
class NodeEnv(helpers.Environment):
@property
def env_prefix(self):
return ". '{{prefix}}{0}/bin/activate' &&".format(ENVIRONMENT_DIR)
return ". '{{prefix}}{0}/bin/activate' &&".format(
helpers.environment_dir(ENVIRONMENT_DIR, self.language_version),
)
@contextlib.contextmanager
def in_env(repo_cmd_runner):
yield NodeEnv(repo_cmd_runner)
def in_env(repo_cmd_runner, language_version):
yield NodeEnv(repo_cmd_runner, language_version)
def install_environment(repo_cmd_runner, version='default'):
assert repo_cmd_runner.exists('package.json')
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
env_dir = repo_cmd_runner.path(ENVIRONMENT_DIR)
env_dir = repo_cmd_runner.path(directory)
with clean_path_on_failure(env_dir):
cmd = [
sys.executable, '-m', 'nodeenv', '--prebuilt',
'{{prefix}}{0}'.format(ENVIRONMENT_DIR),
'{{prefix}}{0}'.format(directory),
]
if version != 'default':
@@ -36,10 +39,10 @@ def install_environment(repo_cmd_runner, version='default'):
repo_cmd_runner.run(cmd)
with in_env(repo_cmd_runner) as node_env:
with in_env(repo_cmd_runner, version) as node_env:
node_env.run("cd '{prefix}' && npm install -g")
def run_hook(repo_cmd_runner, hook, file_args):
with in_env(repo_cmd_runner) as env:
with in_env(repo_cmd_runner, hook['language_version']) as env:
return helpers.run_hook(env, hook, file_args)

View File

@@ -19,15 +19,15 @@ class PythonEnv(helpers.Environment):
def env_prefix(self):
return ". '{{prefix}}{0}activate' &&".format(
virtualenv.path_locations(
ENVIRONMENT_DIR,
helpers.environment_dir(ENVIRONMENT_DIR, self.language_version)
)[-1].rstrip(os.sep) + os.sep,
'activate',
)
@contextlib.contextmanager
def in_env(repo_cmd_runner):
yield PythonEnv(repo_cmd_runner)
def in_env(repo_cmd_runner, language_version):
yield PythonEnv(repo_cmd_runner, language_version)
def norm_version(version):
@@ -41,20 +41,21 @@ def norm_version(version):
def install_environment(repo_cmd_runner, version='default'):
assert repo_cmd_runner.exists('setup.py')
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
# Install a virtualenv
with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)):
with clean_path_on_failure(repo_cmd_runner.path(directory)):
venv_cmd = [
sys.executable, '-m', 'virtualenv',
'{{prefix}}{0}'.format(ENVIRONMENT_DIR)
'{{prefix}}{0}'.format(directory)
]
if version != 'default':
venv_cmd.extend(['-p', norm_version(version)])
repo_cmd_runner.run(venv_cmd)
with in_env(repo_cmd_runner) as env:
with in_env(repo_cmd_runner, version) as env:
env.run("cd '{prefix}' && pip install .")
def run_hook(repo_cmd_runner, hook, file_args):
with in_env(repo_cmd_runner) as env:
with in_env(repo_cmd_runner, hook['language_version']) as env:
return helpers.run_hook(env, hook, file_args)

View File

@@ -2,6 +2,7 @@ from __future__ import unicode_literals
import contextlib
import io
import shutil
from pre_commit.languages import helpers
from pre_commit.util import CalledProcessError
@@ -16,29 +17,36 @@ ENVIRONMENT_DIR = 'rbenv'
class RubyEnv(helpers.Environment):
@property
def env_prefix(self):
return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
return '. {{prefix}}{0}/bin/activate &&'.format(
helpers.environment_dir(ENVIRONMENT_DIR, self.language_version)
)
@contextlib.contextmanager
def in_env(repo_cmd_runner):
yield RubyEnv(repo_cmd_runner)
def in_env(repo_cmd_runner, language_version):
yield RubyEnv(repo_cmd_runner, language_version)
def _install_rbenv(repo_cmd_runner, version='default'):
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
with tarfile_open(resource_filename('rbenv.tar.gz')) as tf:
tf.extractall(repo_cmd_runner.path('.'))
shutil.move(
repo_cmd_runner.path('rbenv'), repo_cmd_runner.path(directory),
)
# Only install ruby-build if the version is specified
if version != 'default':
# ruby-download
with tarfile_open(resource_filename('ruby-download.tar.gz')) as tf:
tf.extractall(repo_cmd_runner.path('rbenv', 'plugins'))
tf.extractall(repo_cmd_runner.path(directory, 'plugins'))
# ruby-build
with tarfile_open(resource_filename('ruby-build.tar.gz')) as tf:
tf.extractall(repo_cmd_runner.path('rbenv', 'plugins'))
tf.extractall(repo_cmd_runner.path(directory, 'plugins'))
activate_path = repo_cmd_runner.path('rbenv', 'bin', 'activate')
activate_path = repo_cmd_runner.path(directory, 'bin', 'activate')
with io.open(activate_path, 'w') as activate_file:
# This is similar to how you would install rbenv to your home directory
# However we do a couple things to make the executables exposed and
@@ -54,7 +62,7 @@ def _install_rbenv(repo_cmd_runner, version='default'):
# directory
"export GEM_HOME='{0}/gems'\n"
'export PATH="$GEM_HOME/bin:$PATH"\n'
'\n'.format(repo_cmd_runner.path('rbenv'))
'\n'.format(repo_cmd_runner.path(directory))
)
# If we aren't using the system ruby, add a version here
@@ -71,11 +79,12 @@ def _install_ruby(environment, version):
def install_environment(repo_cmd_runner, version='default'):
with clean_path_on_failure(repo_cmd_runner.path('rbenv')):
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
with clean_path_on_failure(repo_cmd_runner.path(directory)):
# TODO: this currently will fail if there's no version specified and
# there's no system ruby installed. Is this ok?
_install_rbenv(repo_cmd_runner, version=version)
with in_env(repo_cmd_runner) as ruby_env:
with in_env(repo_cmd_runner, version) as ruby_env:
if version != 'default':
_install_ruby(ruby_env, version)
ruby_env.run(
@@ -84,5 +93,5 @@ def install_environment(repo_cmd_runner, version='default'):
def run_hook(repo_cmd_runner, hook, file_args):
with in_env(repo_cmd_runner) as env:
with in_env(repo_cmd_runner, hook['language_version']) as env:
return helpers.run_hook(env, hook, file_args)