mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
simplify environment_dir
This commit is contained in:
parent
10f835c501
commit
05c8911363
17 changed files with 77 additions and 103 deletions
|
|
@ -44,8 +44,7 @@ def in_env(
|
|||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, language_version)
|
||||
envdir = prefix.path(directory)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
|
@ -65,11 +64,10 @@ def install_environment(
|
|||
additional_dependencies: Sequence[str],
|
||||
) -> None:
|
||||
helpers.assert_version_default('conda', version)
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||
|
||||
conda_exe = _conda_exe()
|
||||
|
||||
env_dir = prefix.path(directory)
|
||||
env_dir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
cmd_output_b(
|
||||
conda_exe, 'env', 'create', '-p', env_dir, '--file',
|
||||
'environment.yml', cwd=prefix.prefix_dir,
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ def install_environment(
|
|||
'executables in the application search path',
|
||||
)
|
||||
|
||||
envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
channel = prefix.path('.pre-commit-channel')
|
||||
for app_descriptor in os.listdir(channel):
|
||||
_, app_file = os.path.split(app_descriptor)
|
||||
|
|
@ -62,11 +62,10 @@ def get_env_patch(target_dir: str) -> PatchesT: # pragma: win32 no cover
|
|||
@contextlib.contextmanager
|
||||
def in_env(
|
||||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]: # pragma: win32 no cover
|
||||
target_dir = prefix.path(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, get_default_version()),
|
||||
)
|
||||
with envcontext(get_env_patch(target_dir)):
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
||||
|
|
@ -75,5 +74,5 @@ def run_hook(
|
|||
file_args: Sequence[str],
|
||||
color: bool,
|
||||
) -> tuple[int, bytes]: # pragma: win32 no cover
|
||||
with in_env(hook.prefix):
|
||||
with in_env(hook.prefix, hook.language_version):
|
||||
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
|
||||
|
|
|
|||
|
|
@ -31,8 +31,7 @@ def get_env_patch(venv: str) -> PatchesT:
|
|||
|
||||
@contextlib.contextmanager
|
||||
def in_env(prefix: Prefix) -> Generator[None, None, None]:
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT)
|
||||
envdir = prefix.path(directory)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
|
@ -44,7 +43,7 @@ def install_environment(
|
|||
) -> None:
|
||||
helpers.assert_version_default('dart', version)
|
||||
|
||||
envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
bin_dir = os.path.join(envdir, 'bin')
|
||||
|
||||
def _install_dir(prefix_p: Prefix, pub_cache: str) -> None:
|
||||
|
|
|
|||
|
|
@ -94,9 +94,7 @@ def install_environment(
|
|||
helpers.assert_version_default('docker', version)
|
||||
helpers.assert_no_additional_deps('docker', additional_dependencies)
|
||||
|
||||
directory = prefix.path(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT),
|
||||
)
|
||||
directory = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
|
||||
# Docker doesn't really have relevant disk environment, but pre-commit
|
||||
# still needs to cleanup its state files on failure
|
||||
|
|
|
|||
|
|
@ -32,8 +32,7 @@ def get_env_patch(venv: str) -> PatchesT:
|
|||
|
||||
@contextlib.contextmanager
|
||||
def in_env(prefix: Prefix) -> Generator[None, None, None]:
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT)
|
||||
envdir = prefix.path(directory)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
|
@ -62,7 +61,7 @@ def install_environment(
|
|||
helpers.assert_version_default('dotnet', version)
|
||||
helpers.assert_no_additional_deps('dotnet', additional_dependencies)
|
||||
|
||||
envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
build_dir = 'pre-commit-build'
|
||||
|
||||
# Build & pack nupkg file
|
||||
|
|
|
|||
|
|
@ -31,9 +31,7 @@ def get_env_patch(venv: str) -> PatchesT:
|
|||
|
||||
@contextlib.contextmanager
|
||||
def in_env(prefix: Prefix) -> Generator[None, None, None]:
|
||||
envdir = prefix.path(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT),
|
||||
)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
|
@ -60,9 +58,7 @@ def install_environment(
|
|||
additional_dependencies: Sequence[str],
|
||||
) -> None:
|
||||
helpers.assert_version_default('golang', version)
|
||||
directory = prefix.path(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT),
|
||||
)
|
||||
directory = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
|
||||
remote = git.get_remote_url(prefix.prefix_dir)
|
||||
repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote))
|
||||
|
|
|
|||
|
|
@ -47,8 +47,8 @@ def run_setup_cmd(prefix: Prefix, cmd: tuple[str, ...], **kwargs: Any) -> None:
|
|||
cmd_output_b(*cmd, cwd=prefix.prefix_dir, **kwargs)
|
||||
|
||||
|
||||
def environment_dir(d: str, language_version: str) -> str:
|
||||
return f'{d}-{language_version}'
|
||||
def environment_dir(prefix: Prefix, d: str, language_version: str) -> str:
|
||||
return prefix.path(f'{d}-{language_version}')
|
||||
|
||||
|
||||
def assert_version_default(binary: str, version: str) -> None:
|
||||
|
|
|
|||
|
|
@ -44,14 +44,10 @@ def get_env_patch(d: str) -> PatchesT: # pragma: win32 no cover
|
|||
)
|
||||
|
||||
|
||||
def _envdir(prefix: Prefix) -> str: # pragma: win32 no cover
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT)
|
||||
return prefix.path(directory)
|
||||
|
||||
|
||||
@contextlib.contextmanager # pragma: win32 no cover
|
||||
def in_env(prefix: Prefix) -> Generator[None, None, None]:
|
||||
with envcontext(get_env_patch(_envdir(prefix))):
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
||||
|
|
@ -62,7 +58,7 @@ def install_environment(
|
|||
) -> None: # pragma: win32 no cover
|
||||
helpers.assert_version_default('lua', version)
|
||||
|
||||
envdir = _envdir(prefix)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
with in_env(prefix):
|
||||
# luarocks doesn't bootstrap a tree prior to installing
|
||||
# so ensure the directory exists.
|
||||
|
|
|
|||
|
|
@ -36,11 +36,6 @@ def get_default_version() -> str:
|
|||
return C.DEFAULT
|
||||
|
||||
|
||||
def _envdir(prefix: Prefix, version: str) -> str:
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||
return prefix.path(directory)
|
||||
|
||||
|
||||
def get_env_patch(venv: str) -> PatchesT:
|
||||
if sys.platform == 'cygwin': # pragma: no cover
|
||||
_, win_venv, _ = cmd_output('cygpath', '-w', venv)
|
||||
|
|
@ -68,7 +63,8 @@ def in_env(
|
|||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
with envcontext(get_env_patch(_envdir(prefix, language_version))):
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
||||
|
|
@ -85,7 +81,7 @@ def install_environment(
|
|||
prefix: Prefix, version: str, additional_dependencies: Sequence[str],
|
||||
) -> None:
|
||||
assert prefix.exists('package.json')
|
||||
envdir = _envdir(prefix, version)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
|
||||
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx?f=255&MSPPError=-2147217396#maxpath
|
||||
if sys.platform == 'win32': # pragma: no cover
|
||||
|
|
|
|||
|
|
@ -18,11 +18,6 @@ get_default_version = helpers.basic_get_default_version
|
|||
health_check = helpers.basic_health_check
|
||||
|
||||
|
||||
def _envdir(prefix: Prefix, version: str) -> str:
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||
return prefix.path(directory)
|
||||
|
||||
|
||||
def get_env_patch(venv: str) -> PatchesT:
|
||||
return (
|
||||
('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))),
|
||||
|
|
@ -42,7 +37,8 @@ def in_env(
|
|||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
with envcontext(get_env_patch(_envdir(prefix, language_version))):
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -156,15 +156,13 @@ def in_env(
|
|||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, language_version)
|
||||
envdir = prefix.path(directory)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
||||
def health_check(prefix: Prefix, language_version: str) -> str | None:
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, language_version)
|
||||
envdir = prefix.path(directory)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
pyvenv_cfg = os.path.join(envdir, 'pyvenv.cfg')
|
||||
|
||||
# created with "old" virtualenv
|
||||
|
|
@ -207,7 +205,7 @@ def install_environment(
|
|||
version: str,
|
||||
additional_dependencies: Sequence[str],
|
||||
) -> None:
|
||||
envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
venv_cmd = [sys.executable, '-mvirtualenv', envdir]
|
||||
python = norm_version(version)
|
||||
if python is not None:
|
||||
|
|
|
|||
|
|
@ -34,15 +34,11 @@ def in_env(
|
|||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
envdir = _get_env_dir(prefix, language_version)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
||||
def _get_env_dir(prefix: Prefix, version: str) -> str:
|
||||
return prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
|
||||
|
||||
|
||||
def _prefix_if_file_entry(entry: list[str], prefix: Prefix) -> Sequence[str]:
|
||||
if entry[1] == '-e':
|
||||
return entry[1:]
|
||||
|
|
@ -93,7 +89,7 @@ def install_environment(
|
|||
version: str,
|
||||
additional_dependencies: Sequence[str],
|
||||
) -> None:
|
||||
env_dir = _get_env_dir(prefix, version)
|
||||
env_dir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
os.makedirs(env_dir, exist_ok=True)
|
||||
shutil.copy(prefix.path('renv.lock'), env_dir)
|
||||
shutil.copytree(prefix.path('renv'), os.path.join(env_dir, 'renv'))
|
||||
|
|
|
|||
|
|
@ -71,9 +71,7 @@ def in_env(
|
|||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
envdir = prefix.path(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, language_version),
|
||||
)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir, language_version)):
|
||||
yield
|
||||
|
||||
|
|
@ -88,14 +86,14 @@ def _install_rbenv(
|
|||
prefix: Prefix,
|
||||
version: str,
|
||||
) -> None: # pragma: win32 no cover
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
|
||||
_extract_resource('rbenv.tar.gz', prefix.path('.'))
|
||||
shutil.move(prefix.path('rbenv'), prefix.path(directory))
|
||||
shutil.move(prefix.path('rbenv'), envdir)
|
||||
|
||||
# Only install ruby-build if the version is specified
|
||||
if version != C.DEFAULT:
|
||||
plugins_dir = prefix.path(directory, 'plugins')
|
||||
plugins_dir = os.path.join(envdir, 'plugins')
|
||||
_extract_resource('ruby-download.tar.gz', plugins_dir)
|
||||
_extract_resource('ruby-build.tar.gz', plugins_dir)
|
||||
|
||||
|
|
|
|||
|
|
@ -48,11 +48,6 @@ def _rust_toolchain(language_version: str) -> str:
|
|||
return language_version
|
||||
|
||||
|
||||
def _envdir(prefix: Prefix, version: str) -> str:
|
||||
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
|
||||
return prefix.path(directory)
|
||||
|
||||
|
||||
def get_env_patch(target_dir: str, version: str) -> PatchesT:
|
||||
return (
|
||||
('CARGO_HOME', target_dir),
|
||||
|
|
@ -71,9 +66,8 @@ def in_env(
|
|||
prefix: Prefix,
|
||||
language_version: str,
|
||||
) -> Generator[None, None, None]:
|
||||
with envcontext(
|
||||
get_env_patch(_envdir(prefix, language_version), language_version),
|
||||
):
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, language_version)
|
||||
with envcontext(get_env_patch(envdir, language_version)):
|
||||
yield
|
||||
|
||||
|
||||
|
|
@ -125,7 +119,7 @@ def install_environment(
|
|||
version: str,
|
||||
additional_dependencies: Sequence[str],
|
||||
) -> None:
|
||||
directory = _envdir(prefix, version)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version)
|
||||
|
||||
# There are two cases where we might want to specify more dependencies:
|
||||
# as dependencies for the library being built, and as binary packages
|
||||
|
|
@ -160,7 +154,7 @@ def install_environment(
|
|||
|
||||
for args in packages_to_install:
|
||||
cmd_output_b(
|
||||
'cargo', 'install', '--bins', '--root', directory, *args,
|
||||
'cargo', 'install', '--bins', '--root', envdir, *args,
|
||||
cwd=prefix.prefix_dir,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -28,9 +28,7 @@ def get_env_patch(venv: str) -> PatchesT: # pragma: win32 no cover
|
|||
|
||||
@contextlib.contextmanager # pragma: win32 no cover
|
||||
def in_env(prefix: Prefix) -> Generator[None, None, None]:
|
||||
envdir = prefix.path(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT),
|
||||
)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
with envcontext(get_env_patch(envdir)):
|
||||
yield
|
||||
|
||||
|
|
@ -40,17 +38,15 @@ def install_environment(
|
|||
) -> None: # pragma: win32 no cover
|
||||
helpers.assert_version_default('swift', version)
|
||||
helpers.assert_no_additional_deps('swift', additional_dependencies)
|
||||
directory = prefix.path(
|
||||
helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT),
|
||||
)
|
||||
envdir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT)
|
||||
|
||||
# Build the swift package
|
||||
os.mkdir(directory)
|
||||
os.mkdir(envdir)
|
||||
cmd_output_b(
|
||||
'swift', 'build',
|
||||
'-C', prefix.prefix_dir,
|
||||
'-c', BUILD_CONFIG,
|
||||
'--build-path', os.path.join(directory, BUILD_DIR),
|
||||
'--build-path', os.path.join(envdir, BUILD_DIR),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -45,8 +45,11 @@ def _hook_installed(hook: Hook) -> bool:
|
|||
if lang.ENVIRONMENT_DIR is None:
|
||||
return True
|
||||
|
||||
venv = environment_dir(lang.ENVIRONMENT_DIR, hook.language_version)
|
||||
venv = hook.prefix.path(venv)
|
||||
venv = environment_dir(
|
||||
hook.prefix,
|
||||
lang.ENVIRONMENT_DIR,
|
||||
hook.language_version,
|
||||
)
|
||||
return (
|
||||
_read_state(venv) == _state(hook.additional_dependencies) and
|
||||
not lang.health_check(hook.prefix, hook.language_version)
|
||||
|
|
@ -61,8 +64,11 @@ def _hook_install(hook: Hook) -> None:
|
|||
lang = languages[hook.language]
|
||||
assert lang.ENVIRONMENT_DIR is not None
|
||||
|
||||
venv = environment_dir(lang.ENVIRONMENT_DIR, hook.language_version)
|
||||
venv = hook.prefix.path(venv)
|
||||
venv = environment_dir(
|
||||
hook.prefix,
|
||||
lang.ENVIRONMENT_DIR,
|
||||
hook.language_version,
|
||||
)
|
||||
|
||||
# There's potentially incomplete cleanup from previous runs
|
||||
# Clean it up!
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue