mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 16:24:40 +04:00
Factor out bash and activate files
This commit is contained in:
parent
00a3a9a09b
commit
a5b56bd9e3
13 changed files with 149 additions and 189 deletions
|
|
@ -15,7 +15,7 @@ from pre_commit.languages import system
|
|||
# def install_environment(
|
||||
# repo_cmd_runner,
|
||||
# version='default',
|
||||
# additional_dependencies=None,
|
||||
# additional_dependencies=(),
|
||||
# ):
|
||||
# """Installs a repository in the given repository. Note that the current
|
||||
# working directory will already be inside the repository.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import pipes
|
||||
from pre_commit.util import cmd_output
|
||||
|
||||
|
||||
def run_setup_cmd(runner, cmd):
|
||||
cmd_output(*cmd, cwd=runner.prefix_dir, encoding=None)
|
||||
|
||||
|
||||
def environment_dir(ENVIRONMENT_DIR, language_version):
|
||||
|
|
@ -14,39 +18,12 @@ def file_args_to_stdin(file_args):
|
|||
return '\0'.join(list(file_args) + [''])
|
||||
|
||||
|
||||
def run_hook(env, hook, file_args):
|
||||
quoted_args = [pipes.quote(arg) for arg in hook['args']]
|
||||
return env.run(
|
||||
def run_hook(cmd_args, file_args):
|
||||
return cmd_output(
|
||||
# Use -s 4000 (slightly less than posix mandated minimum)
|
||||
# This is to prevent "xargs: ... Bad file number" on windows
|
||||
' '.join(['xargs', '-0', '-s4000', hook['entry']] + quoted_args),
|
||||
'xargs', '-0', '-s4000', *cmd_args,
|
||||
stdin=file_args_to_stdin(file_args),
|
||||
retcode=None,
|
||||
encoding=None,
|
||||
encoding=None
|
||||
)
|
||||
|
||||
|
||||
class Environment(object):
|
||||
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):
|
||||
"""env_prefix is a value that is prefixed to the command that is run.
|
||||
|
||||
Usually this is to source a virtualenv, etc.
|
||||
|
||||
Commands basically end up looking like:
|
||||
|
||||
bash -c '{env_prefix} {cmd}'
|
||||
|
||||
so you'll often want to end your prefix with &&
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def run(self, cmd, **kwargs):
|
||||
"""Returns (returncode, stdout, stderr)."""
|
||||
return self.repo_cmd_runner.run(
|
||||
['bash', '-c', ' '.join([self.env_prefix, cmd])], **kwargs
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,34 +1,44 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
import sys
|
||||
|
||||
from pre_commit.envcontext import envcontext
|
||||
from pre_commit.envcontext import Var
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.util import clean_path_on_failure
|
||||
from pre_commit.util import shell_escape
|
||||
|
||||
|
||||
ENVIRONMENT_DIR = 'node_env'
|
||||
|
||||
|
||||
class NodeEnv(helpers.Environment):
|
||||
@property
|
||||
def env_prefix(self):
|
||||
return ". '{{prefix}}{0}/bin/activate' &&".format(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, self.language_version),
|
||||
)
|
||||
def get_env_patch(venv):
|
||||
return (
|
||||
('NODE_VIRTUAL_ENV', venv),
|
||||
('NPM_CONFIG_PREFIX', venv),
|
||||
('npm_config_prefix', venv),
|
||||
('NODE_PATH', os.path.join(venv, 'lib', 'node_modules')),
|
||||
('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))),
|
||||
)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(repo_cmd_runner, language_version):
|
||||
yield NodeEnv(repo_cmd_runner, language_version)
|
||||
envdir = os.path.join(
|
||||
repo_cmd_runner.prefix_dir,
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
|
||||
)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
||||
def install_environment(
|
||||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=None,
|
||||
additional_dependencies=(),
|
||||
):
|
||||
additional_dependencies = tuple(additional_dependencies)
|
||||
assert repo_cmd_runner.exists('package.json')
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||
|
||||
|
|
@ -44,18 +54,15 @@ def install_environment(
|
|||
|
||||
repo_cmd_runner.run(cmd)
|
||||
|
||||
with in_env(repo_cmd_runner, version) as node_env:
|
||||
node_env.run("cd '{prefix}' && npm install -g", encoding=None)
|
||||
if additional_dependencies:
|
||||
node_env.run(
|
||||
"cd '{prefix}' && npm install -g " +
|
||||
' '.join(
|
||||
shell_escape(dep) for dep in additional_dependencies
|
||||
),
|
||||
encoding=None,
|
||||
)
|
||||
with in_env(repo_cmd_runner, version):
|
||||
helpers.run_setup_cmd(
|
||||
repo_cmd_runner,
|
||||
('npm', 'install', '-g', '.') + additional_dependencies,
|
||||
)
|
||||
|
||||
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
with in_env(repo_cmd_runner, hook['language_version']) as env:
|
||||
return helpers.run_hook(env, hook, file_args)
|
||||
with in_env(repo_cmd_runner, hook['language_version']):
|
||||
return helpers.run_hook(
|
||||
(hook['entry'],) + tuple(hook['args']), file_args,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import unicode_literals
|
|||
|
||||
from sys import platform
|
||||
|
||||
from pre_commit.languages.helpers import file_args_to_stdin
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.util import shell_escape
|
||||
|
||||
|
||||
|
|
@ -12,29 +12,28 @@ ENVIRONMENT_DIR = None
|
|||
def install_environment(
|
||||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=None,
|
||||
additional_dependencies=(),
|
||||
):
|
||||
"""Installation for pcre type is a noop."""
|
||||
raise AssertionError('Cannot install pcre repo.')
|
||||
|
||||
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
grep_command = 'grep -H -n -P'
|
||||
if platform == 'darwin': # pragma: no cover (osx)
|
||||
grep_command = 'ggrep -H -n -P'
|
||||
grep_command = '{0} -H -n -P'.format(
|
||||
'ggrep' if platform == 'darwin' else 'grep',
|
||||
)
|
||||
|
||||
# For PCRE the entry is the regular expression to match
|
||||
return repo_cmd_runner.run(
|
||||
[
|
||||
'xargs', '-0', 'sh', '-c',
|
||||
return helpers.run_hook(
|
||||
(
|
||||
'sh', '-c',
|
||||
# Grep usually returns 0 for matches, and nonzero for non-matches
|
||||
# so we flip it here.
|
||||
'! {0} {1} {2} $@'.format(
|
||||
grep_command, ' '.join(hook['args']),
|
||||
shell_escape(hook['entry'])),
|
||||
shell_escape(hook['entry']),
|
||||
),
|
||||
'--',
|
||||
],
|
||||
stdin=file_args_to_stdin(file_args),
|
||||
retcode=None,
|
||||
encoding=None,
|
||||
),
|
||||
file_args,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -5,9 +5,11 @@ import distutils.spawn
|
|||
import os
|
||||
import sys
|
||||
|
||||
from pre_commit.envcontext import envcontext
|
||||
from pre_commit.envcontext import UNSET
|
||||
from pre_commit.envcontext import Var
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.util import clean_path_on_failure
|
||||
from pre_commit.util import shell_escape
|
||||
|
||||
|
||||
ENVIRONMENT_DIR = 'py_env'
|
||||
|
|
@ -15,26 +17,26 @@ ENVIRONMENT_DIR = 'py_env'
|
|||
|
||||
def bin_dir(venv):
|
||||
"""On windows there's a different directory for the virtualenv"""
|
||||
if os.name == 'nt': # pragma: no cover (windows)
|
||||
return os.path.join(venv, 'Scripts')
|
||||
else:
|
||||
return os.path.join(venv, 'bin')
|
||||
bin_part = 'Scripts' if os.name == 'nt' else 'bin'
|
||||
return os.path.join(venv, bin_part)
|
||||
|
||||
|
||||
class PythonEnv(helpers.Environment):
|
||||
@property
|
||||
def env_prefix(self):
|
||||
return ". '{{prefix}}{0}{1}activate' &&".format(
|
||||
bin_dir(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, self.language_version)
|
||||
),
|
||||
os.sep,
|
||||
)
|
||||
def get_env_patch(venv):
|
||||
return (
|
||||
('PYTHONHOME', UNSET),
|
||||
('VIRTUAL_ENV', venv),
|
||||
('PATH', (bin_dir(venv), os.pathsep, Var('PATH'))),
|
||||
)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(repo_cmd_runner, language_version):
|
||||
yield PythonEnv(repo_cmd_runner, language_version)
|
||||
envdir = os.path.join(
|
||||
repo_cmd_runner.prefix_dir,
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
|
||||
)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
||||
def norm_version(version):
|
||||
|
|
@ -55,9 +57,9 @@ def norm_version(version):
|
|||
def install_environment(
|
||||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=None,
|
||||
additional_dependencies=(),
|
||||
):
|
||||
assert repo_cmd_runner.exists('setup.py')
|
||||
additional_dependencies = tuple(additional_dependencies)
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||
|
||||
# Install a virtualenv
|
||||
|
|
@ -69,18 +71,15 @@ def install_environment(
|
|||
if version != 'default':
|
||||
venv_cmd.extend(['-p', norm_version(version)])
|
||||
repo_cmd_runner.run(venv_cmd)
|
||||
with in_env(repo_cmd_runner, version) as env:
|
||||
env.run("cd '{prefix}' && pip install .", encoding=None)
|
||||
if additional_dependencies:
|
||||
env.run(
|
||||
"cd '{prefix}' && pip install " +
|
||||
' '.join(
|
||||
shell_escape(dep) for dep in additional_dependencies
|
||||
),
|
||||
encoding=None,
|
||||
)
|
||||
with in_env(repo_cmd_runner, version):
|
||||
helpers.run_setup_cmd(
|
||||
repo_cmd_runner,
|
||||
('pip', 'install', '.') + additional_dependencies,
|
||||
)
|
||||
|
||||
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
with in_env(repo_cmd_runner, hook['language_version']) as env:
|
||||
return helpers.run_hook(env, hook, file_args)
|
||||
with in_env(repo_cmd_runner, hook['language_version']):
|
||||
return helpers.run_hook(
|
||||
(hook['entry'],) + tuple(hook['args']), file_args,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -2,30 +2,42 @@ from __future__ import unicode_literals
|
|||
|
||||
import contextlib
|
||||
import io
|
||||
import os.path
|
||||
import shutil
|
||||
|
||||
from pre_commit.envcontext import envcontext
|
||||
from pre_commit.envcontext import Var
|
||||
from pre_commit.languages import helpers
|
||||
from pre_commit.util import CalledProcessError
|
||||
from pre_commit.util import clean_path_on_failure
|
||||
from pre_commit.util import resource_filename
|
||||
from pre_commit.util import shell_escape
|
||||
from pre_commit.util import tarfile_open
|
||||
|
||||
|
||||
ENVIRONMENT_DIR = 'rbenv'
|
||||
|
||||
|
||||
class RubyEnv(helpers.Environment):
|
||||
@property
|
||||
def env_prefix(self):
|
||||
return '. {{prefix}}{0}/bin/activate &&'.format(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, self.language_version)
|
||||
)
|
||||
def get_env_patch(venv, language_version):
|
||||
return (
|
||||
('GEM_HOME', os.path.join(venv, 'gems')),
|
||||
('RBENV_ROOT', venv),
|
||||
('RBENV_VERSION', language_version),
|
||||
('PATH', (
|
||||
os.path.join(venv, 'gems', 'bin'), os.pathsep,
|
||||
os.path.join(venv, 'shims'), os.pathsep,
|
||||
os.path.join(venv, 'bin'), os.pathsep, Var('PATH'),
|
||||
)),
|
||||
)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_env(repo_cmd_runner, language_version):
|
||||
yield RubyEnv(repo_cmd_runner, language_version)
|
||||
envdir = os.path.join(
|
||||
repo_cmd_runner.prefix_dir,
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
|
||||
)
|
||||
with envcontext(get_env_patch(envdir, language_version)):
|
||||
yield
|
||||
|
||||
|
||||
def _install_rbenv(repo_cmd_runner, version='default'):
|
||||
|
|
@ -71,42 +83,48 @@ def _install_rbenv(repo_cmd_runner, version='default'):
|
|||
activate_file.write('export RBENV_VERSION="{0}"\n'.format(version))
|
||||
|
||||
|
||||
def _install_ruby(environment, version):
|
||||
def _install_ruby(runner, version):
|
||||
try:
|
||||
environment.run('rbenv download {0}'.format(version))
|
||||
helpers.run_setup_cmd(runner, ('rbenv', 'download', version))
|
||||
except CalledProcessError: # pragma: no cover (usually find with download)
|
||||
# Failed to download from mirror for some reason, build it instead
|
||||
environment.run('rbenv install {0}'.format(version))
|
||||
helpers.run_setup_cmd(runner, ('rbenv', 'install', version))
|
||||
|
||||
|
||||
def install_environment(
|
||||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=None,
|
||||
additional_dependencies=(),
|
||||
):
|
||||
additional_dependencies = tuple(additional_dependencies)
|
||||
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, version) as ruby_env:
|
||||
with in_env(repo_cmd_runner, version):
|
||||
# Need to call this before installing so rbenv's directories are
|
||||
# set up
|
||||
helpers.run_setup_cmd(repo_cmd_runner, ('rbenv', 'init', '-'))
|
||||
if version != 'default':
|
||||
_install_ruby(ruby_env, version)
|
||||
ruby_env.run(
|
||||
'cd {prefix} && gem build *.gemspec && '
|
||||
'gem install --no-ri --no-rdoc *.gem',
|
||||
encoding=None,
|
||||
_install_ruby(repo_cmd_runner, version)
|
||||
# Need to call this after installing to set up the shims
|
||||
helpers.run_setup_cmd(repo_cmd_runner, ('rbenv', 'rehash'))
|
||||
helpers.run_setup_cmd(
|
||||
repo_cmd_runner,
|
||||
('gem', 'build') + repo_cmd_runner.star('.gemspec'),
|
||||
)
|
||||
helpers.run_setup_cmd(
|
||||
repo_cmd_runner,
|
||||
(
|
||||
('gem', 'install', '--no-ri', '--no-rdoc') +
|
||||
repo_cmd_runner.star('.gem') + additional_dependencies
|
||||
),
|
||||
)
|
||||
if additional_dependencies:
|
||||
ruby_env.run(
|
||||
'cd {prefix} && gem install --no-ri --no-rdoc ' +
|
||||
' '.join(
|
||||
shell_escape(dep) for dep in additional_dependencies
|
||||
),
|
||||
encoding=None,
|
||||
)
|
||||
|
||||
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
with in_env(repo_cmd_runner, hook['language_version']) as env:
|
||||
return helpers.run_hook(env, hook, file_args)
|
||||
with in_env(repo_cmd_runner, hook['language_version']):
|
||||
return helpers.run_hook(
|
||||
(hook['entry'],) + tuple(hook['args']), file_args,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from pre_commit.languages.helpers import file_args_to_stdin
|
||||
from pre_commit.languages import helpers
|
||||
|
||||
|
||||
ENVIRONMENT_DIR = None
|
||||
|
|
@ -9,16 +9,14 @@ ENVIRONMENT_DIR = None
|
|||
def install_environment(
|
||||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=None,
|
||||
additional_dependencies=(),
|
||||
):
|
||||
"""Installation for script type is a noop."""
|
||||
raise AssertionError('Cannot install script repo.')
|
||||
|
||||
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
return repo_cmd_runner.run(
|
||||
['xargs', '-0', '{{prefix}}{0}'.format(hook['entry'])] + hook['args'],
|
||||
stdin=file_args_to_stdin(file_args),
|
||||
retcode=None,
|
||||
encoding=None,
|
||||
return helpers.run_hook(
|
||||
(repo_cmd_runner.prefix_dir + hook['entry'],) + tuple(hook['args']),
|
||||
file_args,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import unicode_literals
|
|||
|
||||
import shlex
|
||||
|
||||
from pre_commit.languages.helpers import file_args_to_stdin
|
||||
from pre_commit.languages import helpers
|
||||
|
||||
|
||||
ENVIRONMENT_DIR = None
|
||||
|
|
@ -11,16 +11,13 @@ ENVIRONMENT_DIR = None
|
|||
def install_environment(
|
||||
repo_cmd_runner,
|
||||
version='default',
|
||||
additional_dependencies=None,
|
||||
additional_dependencies=(),
|
||||
):
|
||||
"""Installation for system type is a noop."""
|
||||
raise AssertionError('Cannot install system repo.')
|
||||
|
||||
|
||||
def run_hook(repo_cmd_runner, hook, file_args):
|
||||
return repo_cmd_runner.run(
|
||||
['xargs', '-0'] + shlex.split(hook['entry']) + hook['args'],
|
||||
stdin=file_args_to_stdin(file_args),
|
||||
retcode=None,
|
||||
encoding=None,
|
||||
return helpers.run_hook(
|
||||
tuple(shlex.split(hook['entry'])) + tuple(hook['args']), file_args,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue