Move PrefixedCommandRunner -> Prefix

This commit is contained in:
Anthony Sottile 2018-01-11 21:41:48 -08:00
parent c751f629a6
commit 7d87da8acd
23 changed files with 270 additions and 372 deletions

View file

@ -23,25 +23,25 @@ from pre_commit.languages import system
# return 'default' if there is no better option.
# """
#
# def healthy(repo_cmd_runner, language_version):
# def healthy(prefix, language_version):
# """Return whether or not the environment is considered functional."""
#
# def install_environment(repo_cmd_runner, version, additional_dependencies):
# def install_environment(prefix, version, additional_dependencies):
# """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.
# prefix - `Prefix` bound to the repository.
# version - A version specified in the hook configuration or
# 'default'.
# """
#
# def run_hook(repo_cmd_runner, hook, file_args):
# def run_hook(prefix, 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.
# prefix - `Prefix` bound to the repository.
# hook - Hook dictionary
# file_args - The files to be run
#
@ -62,6 +62,4 @@ languages = {
'swift': swift,
'system': system,
}
all_languages = languages.keys()
all_languages = sorted(languages)

View file

@ -22,10 +22,9 @@ def md5(s): # pragma: windows no cover
return hashlib.md5(five.to_bytes(s)).hexdigest()
def docker_tag(repo_cmd_runner): # pragma: windows no cover
return 'pre-commit-{}'.format(
md5(os.path.basename(repo_cmd_runner.path())),
).lower()
def docker_tag(prefix): # pragma: windows no cover
md5sum = md5(os.path.basename(prefix.prefix_dir)).lower()
return 'pre-commit-{}'.format(md5sum)
def docker_is_running(): # pragma: windows no cover
@ -41,39 +40,36 @@ def assert_docker_available(): # pragma: windows no cover
)
def build_docker_image(repo_cmd_runner, **kwargs): # pragma: windows no cover
def build_docker_image(prefix, **kwargs): # pragma: windows no cover
pull = kwargs.pop('pull')
assert not kwargs, kwargs
cmd = (
'docker', 'build',
'--tag', docker_tag(repo_cmd_runner),
'--tag', docker_tag(prefix),
'--label', PRE_COMMIT_LABEL,
)
if pull:
cmd += ('--pull',)
# This must come last for old versions of docker. See #477
cmd += ('.',)
helpers.run_setup_cmd(repo_cmd_runner, cmd)
helpers.run_setup_cmd(prefix, cmd)
def install_environment(
repo_cmd_runner, version, additional_dependencies,
prefix, version, additional_dependencies,
): # pragma: windows no cover
assert repo_cmd_runner.exists('Dockerfile'), (
'No Dockerfile was found in the hook repository'
)
helpers.assert_version_default('docker', version)
helpers.assert_no_additional_deps('docker', additional_dependencies)
assert_docker_available()
directory = repo_cmd_runner.path(
directory = prefix.path(
helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
)
# Docker doesn't really have relevant disk environment, but pre-commit
# still needs to cleanup it's state files on failure
with clean_path_on_failure(directory):
build_docker_image(repo_cmd_runner, pull=True)
build_docker_image(prefix, pull=True)
os.mkdir(directory)
@ -90,15 +86,15 @@ def docker_cmd():
)
def run_hook(repo_cmd_runner, hook, file_args): # pragma: windows no cover
def run_hook(prefix, hook, file_args): # pragma: windows no cover
assert_docker_available()
# Rebuild the docker image in case it has gone missing, as many people do
# automated cleanup of docker images.
build_docker_image(repo_cmd_runner, pull=False)
build_docker_image(prefix, pull=False)
hook_cmd = helpers.to_cmd(hook)
entry_exe, cmd_rest = hook_cmd[0], hook_cmd[1:]
entry_tag = ('--entrypoint', entry_exe, docker_tag(repo_cmd_runner))
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
cmd = docker_cmd() + entry_tag + cmd_rest
return xargs(cmd, file_args)

View file

@ -13,7 +13,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(repo_cmd_runner, hook, file_args): # pragma: windows no cover
def run_hook(prefix, hook, file_args): # pragma: windows no cover
assert_docker_available()
cmd = docker_cmd() + helpers.to_cmd(hook)
return xargs(cmd, file_args)

View file

@ -26,8 +26,8 @@ def get_env_patch(venv):
@contextlib.contextmanager
def in_env(repo_cmd_runner):
envdir = repo_cmd_runner.path(
def in_env(prefix):
envdir = prefix.path(
helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
)
with envcontext(get_env_patch(envdir)):
@ -50,20 +50,18 @@ def guess_go_dir(remote_url):
return 'unknown_src_dir'
def install_environment(repo_cmd_runner, version, additional_dependencies):
def install_environment(prefix, version, additional_dependencies):
helpers.assert_version_default('golang', version)
directory = repo_cmd_runner.path(
directory = prefix.path(
helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
)
with clean_path_on_failure(directory):
remote = git.get_remote_url(repo_cmd_runner.path())
remote = git.get_remote_url(prefix.prefix_dir)
repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote))
# Clone into the goenv we'll create
helpers.run_setup_cmd(
repo_cmd_runner, ('git', 'clone', '.', repo_src_dir),
)
helpers.run_setup_cmd(prefix, ('git', 'clone', '.', repo_src_dir))
if sys.platform == 'cygwin': # pragma: no cover
_, gopath, _ = cmd_output('cygpath', '-w', directory)
@ -75,10 +73,10 @@ def install_environment(repo_cmd_runner, version, additional_dependencies):
for dependency in additional_dependencies:
cmd_output('go', 'get', dependency, cwd=repo_src_dir, env=env)
# Same some disk space, we don't need these after installation
rmtree(repo_cmd_runner.path(directory, 'src'))
rmtree(repo_cmd_runner.path(directory, 'pkg'))
rmtree(prefix.path(directory, 'src'))
rmtree(prefix.path(directory, 'pkg'))
def run_hook(repo_cmd_runner, hook, file_args):
with in_env(repo_cmd_runner):
def run_hook(prefix, hook, file_args):
with in_env(prefix):
return xargs(helpers.to_cmd(hook), file_args)

View file

@ -5,8 +5,8 @@ import shlex
from pre_commit.util import cmd_output
def run_setup_cmd(runner, cmd):
cmd_output(*cmd, cwd=runner.prefix_dir, encoding=None)
def run_setup_cmd(prefix, cmd):
cmd_output(*cmd, cwd=prefix.prefix_dir, encoding=None)
def environment_dir(ENVIRONMENT_DIR, language_version):
@ -39,9 +39,9 @@ def basic_get_default_version():
return 'default'
def basic_healthy(repo_cmd_runner, language_version):
def basic_healthy(prefix, language_version):
return True
def no_install(repo_cmd_runner, version, additional_dependencies):
def no_install(prefix, version, additional_dependencies):
raise AssertionError('This type is not installable')

View file

@ -33,8 +33,8 @@ def get_env_patch(venv): # pragma: windows no cover
@contextlib.contextmanager
def in_env(repo_cmd_runner, language_version): # pragma: windows no cover
envdir = repo_cmd_runner.path(
def in_env(prefix, language_version): # pragma: windows no cover
envdir = prefix.path(
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
)
with envcontext(get_env_patch(envdir)):
@ -42,31 +42,26 @@ def in_env(repo_cmd_runner, language_version): # pragma: windows no cover
def install_environment(
repo_cmd_runner, version, additional_dependencies,
prefix, version, additional_dependencies,
): # pragma: windows no cover
additional_dependencies = tuple(additional_dependencies)
assert repo_cmd_runner.exists('package.json')
assert prefix.exists('package.json')
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
env_dir = repo_cmd_runner.path(directory)
env_dir = prefix.path(directory)
with clean_path_on_failure(env_dir):
cmd = [
sys.executable, '-m', 'nodeenv', '--prebuilt',
'{{prefix}}{}'.format(directory),
]
cmd = [sys.executable, '-m', 'nodeenv', '--prebuilt', env_dir]
if version != 'default':
cmd.extend(['-n', version])
cmd_output(*cmd)
repo_cmd_runner.run(cmd)
with in_env(repo_cmd_runner, version):
with in_env(prefix, version):
helpers.run_setup_cmd(
repo_cmd_runner,
prefix,
('npm', 'install', '-g', '.') + additional_dependencies,
)
def run_hook(repo_cmd_runner, hook, file_args): # pragma: windows no cover
with in_env(repo_cmd_runner, hook['language_version']):
def run_hook(prefix, hook, file_args): # pragma: windows no cover
with in_env(prefix, hook['language_version']):
return xargs(helpers.to_cmd(hook), file_args)

View file

@ -13,7 +13,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(repo_cmd_runner, hook, file_args):
def run_hook(prefix, hook, file_args):
# For PCRE the entry is the regular expression to match
cmd = (GREP, '-H', '-n', '-P') + tuple(hook['args']) + (hook['entry'],)

View file

@ -27,7 +27,7 @@ def _process_filename_by_line(pattern, filename):
return retv
def run_hook(repo_cmd_runner, hook, file_args):
def run_hook(prefix, hook, file_args):
exe = (sys.executable, '-m', __name__)
exe += tuple(hook['args']) + (hook['entry'],)
return xargs(exe, file_args)

View file

@ -33,8 +33,8 @@ def get_env_patch(venv):
@contextlib.contextmanager
def in_env(repo_cmd_runner, language_version):
envdir = repo_cmd_runner.path(
def in_env(prefix, language_version):
envdir = prefix.path(
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
)
with envcontext(get_env_patch(envdir)):
@ -98,8 +98,8 @@ def get_default_version():
return get_default_version()
def healthy(repo_cmd_runner, language_version):
with in_env(repo_cmd_runner, language_version):
def healthy(prefix, language_version):
with in_env(prefix, language_version):
retcode, _, _ = cmd_output(
'python', '-c', 'import ctypes, datetime, io, os, ssl, weakref',
retcode=None,
@ -127,29 +127,26 @@ def norm_version(version):
return os.path.expanduser(version)
def install_environment(repo_cmd_runner, version, additional_dependencies):
def install_environment(prefix, version, additional_dependencies):
additional_dependencies = tuple(additional_dependencies)
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
# Install a virtualenv
with clean_path_on_failure(repo_cmd_runner.path(directory)):
venv_cmd = [
sys.executable, '-m', 'virtualenv',
'{{prefix}}{}'.format(directory),
]
env_dir = prefix.path(directory)
with clean_path_on_failure(env_dir):
venv_cmd = [sys.executable, '-m', 'virtualenv', env_dir]
if version != 'default':
venv_cmd.extend(['-p', norm_version(version)])
else:
venv_cmd.extend(['-p', os.path.realpath(sys.executable)])
venv_env = dict(os.environ, VIRTUALENV_NO_DOWNLOAD='1')
repo_cmd_runner.run(venv_cmd, cwd='/', env=venv_env)
with in_env(repo_cmd_runner, version):
cmd_output(*venv_cmd, cwd='/', env=venv_env)
with in_env(prefix, version):
helpers.run_setup_cmd(
repo_cmd_runner,
('pip', 'install', '.') + additional_dependencies,
prefix, ('pip', 'install', '.') + additional_dependencies,
)
def run_hook(repo_cmd_runner, hook, file_args):
with in_env(repo_cmd_runner, hook['language_version']):
def run_hook(prefix, hook, file_args):
with in_env(prefix, hook['language_version']):
return xargs(helpers.to_cmd(hook), file_args)

View file

@ -39,36 +39,32 @@ def get_env_patch(venv, language_version): # pragma: windows no cover
@contextlib.contextmanager
def in_env(repo_cmd_runner, language_version): # pragma: windows no cover
envdir = repo_cmd_runner.path(
def in_env(prefix, language_version): # pragma: windows no cover
envdir = prefix.path(
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
)
with envcontext(get_env_patch(envdir, language_version)):
yield
def _install_rbenv(
repo_cmd_runner, version='default',
): # pragma: windows no cover
def _install_rbenv(prefix, version='default'): # pragma: windows no cover
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),
)
tf.extractall(prefix.path('.'))
shutil.move(prefix.path('rbenv'), prefix.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(directory, 'plugins'))
tf.extractall(prefix.path(directory, 'plugins'))
# ruby-build
with tarfile.open(resource_filename('ruby-build.tar.gz')) as tf:
tf.extractall(repo_cmd_runner.path(directory, 'plugins'))
tf.extractall(prefix.path(directory, 'plugins'))
activate_path = repo_cmd_runner.path(directory, 'bin', 'activate')
activate_path = prefix.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
@ -84,7 +80,7 @@ def _install_rbenv(
# directory
"export GEM_HOME='{directory}/gems'\n"
'export PATH="$GEM_HOME/bin:$PATH"\n'
'\n'.format(directory=repo_cmd_runner.path(directory)),
'\n'.format(directory=prefix.path(directory)),
)
# If we aren't using the system ruby, add a version here
@ -101,35 +97,32 @@ def _install_ruby(runner, version): # pragma: windows no cover
def install_environment(
repo_cmd_runner, version, additional_dependencies,
prefix, version, additional_dependencies,
): # pragma: windows no cover
additional_dependencies = tuple(additional_dependencies)
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
with clean_path_on_failure(repo_cmd_runner.path(directory)):
with clean_path_on_failure(prefix.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):
_install_rbenv(prefix, version=version)
with in_env(prefix, version):
# Need to call this before installing so rbenv's directories are
# set up
helpers.run_setup_cmd(repo_cmd_runner, ('rbenv', 'init', '-'))
helpers.run_setup_cmd(prefix, ('rbenv', 'init', '-'))
if version != 'default':
_install_ruby(repo_cmd_runner, version)
_install_ruby(prefix, 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(prefix, ('rbenv', 'rehash'))
helpers.run_setup_cmd(
repo_cmd_runner,
('gem', 'build') + repo_cmd_runner.star('.gemspec'),
prefix, ('gem', 'build') + prefix.star('.gemspec'),
)
helpers.run_setup_cmd(
repo_cmd_runner,
(
('gem', 'install', '--no-ri', '--no-rdoc') +
repo_cmd_runner.star('.gem') + additional_dependencies
),
prefix,
('gem', 'install', '--no-ri', '--no-rdoc') +
prefix.star('.gem') + additional_dependencies,
)
def run_hook(repo_cmd_runner, hook, file_args): # pragma: windows no cover
with in_env(repo_cmd_runner, hook['language_version']):
def run_hook(prefix, hook, file_args): # pragma: windows no cover
with in_env(prefix, hook['language_version']):
return xargs(helpers.to_cmd(hook), file_args)

View file

@ -10,7 +10,7 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(repo_cmd_runner, hook, file_args):
def run_hook(prefix, hook, file_args):
cmd = helpers.to_cmd(hook)
cmd = (repo_cmd_runner.prefix_dir + cmd[0],) + cmd[1:]
cmd = (prefix.prefix_dir + cmd[0],) + cmd[1:]
return xargs(cmd, file_args)

View file

@ -7,6 +7,7 @@ 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 cmd_output
from pre_commit.xargs import xargs
ENVIRONMENT_DIR = 'swift_env'
@ -22,8 +23,8 @@ def get_env_patch(venv): # pragma: windows no cover
@contextlib.contextmanager
def in_env(repo_cmd_runner): # pragma: windows no cover
envdir = repo_cmd_runner.path(
def in_env(prefix): # pragma: windows no cover
envdir = prefix.path(
helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
)
with envcontext(get_env_patch(envdir)):
@ -31,25 +32,25 @@ def in_env(repo_cmd_runner): # pragma: windows no cover
def install_environment(
repo_cmd_runner, version, additional_dependencies,
prefix, version, additional_dependencies,
): # pragma: windows no cover
helpers.assert_version_default('swift', version)
helpers.assert_no_additional_deps('swift', additional_dependencies)
directory = repo_cmd_runner.path(
directory = prefix.path(
helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
)
# Build the swift package
with clean_path_on_failure(directory):
os.mkdir(directory)
repo_cmd_runner.run((
cmd_output(
'swift', 'build',
'-C', '{prefix}',
'-C', prefix.prefix_dir,
'-c', BUILD_CONFIG,
'--build-path', os.path.join(directory, BUILD_DIR),
))
)
def run_hook(repo_cmd_runner, hook, file_args): # pragma: windows no cover
with in_env(repo_cmd_runner):
def run_hook(prefix, hook, file_args): # pragma: windows no cover
with in_env(prefix):
return xargs(helpers.to_cmd(hook), file_args)

View file

@ -10,5 +10,5 @@ healthy = helpers.basic_healthy
install_environment = helpers.no_install
def run_hook(repo_cmd_runner, hook, file_args):
def run_hook(prefix, hook, file_args):
return xargs(helpers.to_cmd(hook), file_args)