Skip to content

Commit d05b788

Browse files
committed
move clean_path_on_failure out of each hook install
1 parent 0224be8 commit d05b788

File tree

15 files changed

+296
-331
lines changed

15 files changed

+296
-331
lines changed

pre_commit/languages/conda.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from pre_commit.hook import Hook
1414
from pre_commit.languages import helpers
1515
from pre_commit.prefix import Prefix
16-
from pre_commit.util import clean_path_on_failure
1716
from pre_commit.util import cmd_output_b
1817

1918
ENVIRONMENT_DIR = 'conda'
@@ -71,16 +70,15 @@ def install_environment(
7170
conda_exe = _conda_exe()
7271

7372
env_dir = prefix.path(directory)
74-
with clean_path_on_failure(env_dir):
73+
cmd_output_b(
74+
conda_exe, 'env', 'create', '-p', env_dir, '--file',
75+
'environment.yml', cwd=prefix.prefix_dir,
76+
)
77+
if additional_dependencies:
7578
cmd_output_b(
76-
conda_exe, 'env', 'create', '-p', env_dir, '--file',
77-
'environment.yml', cwd=prefix.prefix_dir,
79+
conda_exe, 'install', '-p', env_dir, *additional_dependencies,
80+
cwd=prefix.prefix_dir,
7881
)
79-
if additional_dependencies:
80-
cmd_output_b(
81-
conda_exe, 'install', '-p', env_dir, *additional_dependencies,
82-
cwd=prefix.prefix_dir,
83-
)
8482

8583

8684
def run_hook(

pre_commit/languages/coursier.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pre_commit.languages import helpers
1313
from pre_commit.parse_shebang import find_executable
1414
from pre_commit.prefix import Prefix
15-
from pre_commit.util import clean_path_on_failure
1615

1716
ENVIRONMENT_DIR = 'coursier'
1817

@@ -38,21 +37,20 @@ def install_environment(
3837

3938
envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
4039
channel = prefix.path('.pre-commit-channel')
41-
with clean_path_on_failure(envdir):
42-
for app_descriptor in os.listdir(channel):
43-
_, app_file = os.path.split(app_descriptor)
44-
app, _ = os.path.splitext(app_file)
45-
helpers.run_setup_cmd(
46-
prefix,
47-
(
48-
executable,
49-
'install',
50-
'--default-channels=false',
51-
f'--channel={channel}',
52-
app,
53-
f'--dir={envdir}',
54-
),
55-
)
40+
for app_descriptor in os.listdir(channel):
41+
_, app_file = os.path.split(app_descriptor)
42+
app, _ = os.path.splitext(app_file)
43+
helpers.run_setup_cmd(
44+
prefix,
45+
(
46+
executable,
47+
'install',
48+
'--default-channels=false',
49+
f'--channel={channel}',
50+
app,
51+
f'--dir={envdir}',
52+
),
53+
)
5654

5755

5856
def get_env_patch(target_dir: str) -> PatchesT: # pragma: win32 no cover

pre_commit/languages/dart.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pre_commit.hook import Hook
1515
from pre_commit.languages import helpers
1616
from pre_commit.prefix import Prefix
17-
from pre_commit.util import clean_path_on_failure
1817
from pre_commit.util import win_exe
1918
from pre_commit.util import yaml_load
2019

@@ -67,38 +66,37 @@ def _install_dir(prefix_p: Prefix, pub_cache: str) -> None:
6766
env=dart_env,
6867
)
6968

70-
with clean_path_on_failure(envdir):
71-
os.makedirs(bin_dir)
69+
os.makedirs(bin_dir)
7270

73-
with tempfile.TemporaryDirectory() as tmp:
74-
_install_dir(prefix, tmp)
71+
with tempfile.TemporaryDirectory() as tmp:
72+
_install_dir(prefix, tmp)
7573

76-
for dep_s in additional_dependencies:
77-
with tempfile.TemporaryDirectory() as dep_tmp:
78-
dep, _, version = dep_s.partition(':')
79-
if version:
80-
dep_cmd: tuple[str, ...] = (dep, '--version', version)
81-
else:
82-
dep_cmd = (dep,)
74+
for dep_s in additional_dependencies:
75+
with tempfile.TemporaryDirectory() as dep_tmp:
76+
dep, _, version = dep_s.partition(':')
77+
if version:
78+
dep_cmd: tuple[str, ...] = (dep, '--version', version)
79+
else:
80+
dep_cmd = (dep,)
8381

84-
helpers.run_setup_cmd(
85-
prefix,
86-
('dart', 'pub', 'cache', 'add', *dep_cmd),
87-
env={**os.environ, 'PUB_CACHE': dep_tmp},
88-
)
82+
helpers.run_setup_cmd(
83+
prefix,
84+
('dart', 'pub', 'cache', 'add', *dep_cmd),
85+
env={**os.environ, 'PUB_CACHE': dep_tmp},
86+
)
8987

90-
# try and find the 'pubspec.yaml' that just got added
91-
for root, _, filenames in os.walk(dep_tmp):
92-
if 'pubspec.yaml' in filenames:
93-
with tempfile.TemporaryDirectory() as copied:
94-
pkg = os.path.join(copied, 'pkg')
95-
shutil.copytree(root, pkg)
96-
_install_dir(Prefix(pkg), dep_tmp)
97-
break
98-
else:
99-
raise AssertionError(
100-
f'could not find pubspec.yaml for {dep_s}',
101-
)
88+
# try and find the 'pubspec.yaml' that just got added
89+
for root, _, filenames in os.walk(dep_tmp):
90+
if 'pubspec.yaml' in filenames:
91+
with tempfile.TemporaryDirectory() as copied:
92+
pkg = os.path.join(copied, 'pkg')
93+
shutil.copytree(root, pkg)
94+
_install_dir(Prefix(pkg), dep_tmp)
95+
break
96+
else:
97+
raise AssertionError(
98+
f'could not find pubspec.yaml for {dep_s}',
99+
)
102100

103101

104102
def run_hook(

pre_commit/languages/docker.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pre_commit.languages import helpers
1111
from pre_commit.prefix import Prefix
1212
from pre_commit.util import CalledProcessError
13-
from pre_commit.util import clean_path_on_failure
1413
from pre_commit.util import cmd_output_b
1514

1615
ENVIRONMENT_DIR = 'docker'
@@ -101,9 +100,8 @@ def install_environment(
101100

102101
# Docker doesn't really have relevant disk environment, but pre-commit
103102
# still needs to cleanup its state files on failure
104-
with clean_path_on_failure(directory):
105-
build_docker_image(prefix, pull=True)
106-
os.mkdir(directory)
103+
build_docker_image(prefix, pull=True)
104+
os.mkdir(directory)
107105

108106

109107
def get_docker_user() -> tuple[str, ...]: # pragma: win32 no cover

pre_commit/languages/dotnet.py

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from pre_commit.hook import Hook
1717
from pre_commit.languages import helpers
1818
from pre_commit.prefix import Prefix
19-
from pre_commit.util import clean_path_on_failure
2019

2120
ENVIRONMENT_DIR = 'dotnetenv'
2221
BIN_DIR = 'bin'
@@ -64,59 +63,58 @@ def install_environment(
6463
helpers.assert_no_additional_deps('dotnet', additional_dependencies)
6564

6665
envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
67-
with clean_path_on_failure(envdir):
68-
build_dir = 'pre-commit-build'
69-
70-
# Build & pack nupkg file
71-
helpers.run_setup_cmd(
72-
prefix,
73-
(
74-
'dotnet', 'pack',
75-
'--configuration', 'Release',
76-
'--output', build_dir,
77-
),
78-
)
79-
80-
nupkg_dir = prefix.path(build_dir)
81-
nupkgs = [x for x in os.listdir(nupkg_dir) if x.endswith('.nupkg')]
82-
83-
if not nupkgs:
84-
raise AssertionError('could not find any build outputs to install')
85-
86-
for nupkg in nupkgs:
87-
with zipfile.ZipFile(os.path.join(nupkg_dir, nupkg)) as f:
88-
nuspec, = (x for x in f.namelist() if x.endswith('.nuspec'))
89-
with f.open(nuspec) as spec:
90-
tree = xml.etree.ElementTree.parse(spec)
91-
92-
namespace = re.match(r'{.*}', tree.getroot().tag)
93-
if not namespace:
94-
raise AssertionError('could not parse namespace from nuspec')
95-
96-
tool_id_element = tree.find(f'.//{namespace[0]}id')
97-
if tool_id_element is None:
98-
raise AssertionError('expected to find an "id" element')
99-
100-
tool_id = tool_id_element.text
101-
if not tool_id:
102-
raise AssertionError('"id" element missing tool name')
103-
104-
# Install to bin dir
105-
with _nuget_config_no_sources() as nuget_config:
106-
helpers.run_setup_cmd(
107-
prefix,
108-
(
109-
'dotnet', 'tool', 'install',
110-
'--configfile', nuget_config,
111-
'--tool-path', os.path.join(envdir, BIN_DIR),
112-
'--add-source', build_dir,
113-
tool_id,
114-
),
115-
)
116-
117-
# Clean the git dir, ignoring the environment dir
118-
clean_cmd = ('git', 'clean', '-ffxd', '-e', f'{ENVIRONMENT_DIR}-*')
119-
helpers.run_setup_cmd(prefix, clean_cmd)
66+
build_dir = 'pre-commit-build'
67+
68+
# Build & pack nupkg file
69+
helpers.run_setup_cmd(
70+
prefix,
71+
(
72+
'dotnet', 'pack',
73+
'--configuration', 'Release',
74+
'--output', build_dir,
75+
),
76+
)
77+
78+
nupkg_dir = prefix.path(build_dir)
79+
nupkgs = [x for x in os.listdir(nupkg_dir) if x.endswith('.nupkg')]
80+
81+
if not nupkgs:
82+
raise AssertionError('could not find any build outputs to install')
83+
84+
for nupkg in nupkgs:
85+
with zipfile.ZipFile(os.path.join(nupkg_dir, nupkg)) as f:
86+
nuspec, = (x for x in f.namelist() if x.endswith('.nuspec'))
87+
with f.open(nuspec) as spec:
88+
tree = xml.etree.ElementTree.parse(spec)
89+
90+
namespace = re.match(r'{.*}', tree.getroot().tag)
91+
if not namespace:
92+
raise AssertionError('could not parse namespace from nuspec')
93+
94+
tool_id_element = tree.find(f'.//{namespace[0]}id')
95+
if tool_id_element is None:
96+
raise AssertionError('expected to find an "id" element')
97+
98+
tool_id = tool_id_element.text
99+
if not tool_id:
100+
raise AssertionError('"id" element missing tool name')
101+
102+
# Install to bin dir
103+
with _nuget_config_no_sources() as nuget_config:
104+
helpers.run_setup_cmd(
105+
prefix,
106+
(
107+
'dotnet', 'tool', 'install',
108+
'--configfile', nuget_config,
109+
'--tool-path', os.path.join(envdir, BIN_DIR),
110+
'--add-source', build_dir,
111+
tool_id,
112+
),
113+
)
114+
115+
# Clean the git dir, ignoring the environment dir
116+
clean_cmd = ('git', 'clean', '-ffxd', '-e', f'{ENVIRONMENT_DIR}-*')
117+
helpers.run_setup_cmd(prefix, clean_cmd)
120118

121119

122120
def run_hook(

pre_commit/languages/golang.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pre_commit.hook import Hook
1515
from pre_commit.languages import helpers
1616
from pre_commit.prefix import Prefix
17-
from pre_commit.util import clean_path_on_failure
1817
from pre_commit.util import cmd_output
1918
from pre_commit.util import cmd_output_b
2019
from pre_commit.util import rmtree
@@ -65,31 +64,30 @@ def install_environment(
6564
helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT),
6665
)
6766

68-
with clean_path_on_failure(directory):
69-
remote = git.get_remote_url(prefix.prefix_dir)
70-
repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote))
71-
72-
# Clone into the goenv we'll create
73-
cmd = ('git', 'clone', '--recursive', '.', repo_src_dir)
74-
helpers.run_setup_cmd(prefix, cmd)
75-
76-
if sys.platform == 'cygwin': # pragma: no cover
77-
_, gopath, _ = cmd_output('cygpath', '-w', directory)
78-
gopath = gopath.strip()
79-
else:
80-
gopath = directory
81-
env = dict(os.environ, GOPATH=gopath)
82-
env.pop('GOBIN', None)
83-
cmd_output_b('go', 'install', './...', cwd=repo_src_dir, env=env)
84-
for dependency in additional_dependencies:
85-
cmd_output_b(
86-
'go', 'install', dependency, cwd=repo_src_dir, env=env,
87-
)
88-
# Same some disk space, we don't need these after installation
89-
rmtree(prefix.path(directory, 'src'))
90-
pkgdir = prefix.path(directory, 'pkg')
91-
if os.path.exists(pkgdir): # pragma: no cover (go<1.10)
92-
rmtree(pkgdir)
67+
remote = git.get_remote_url(prefix.prefix_dir)
68+
repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote))
69+
70+
# Clone into the goenv we'll create
71+
cmd = ('git', 'clone', '--recursive', '.', repo_src_dir)
72+
helpers.run_setup_cmd(prefix, cmd)
73+
74+
if sys.platform == 'cygwin': # pragma: no cover
75+
_, gopath, _ = cmd_output('cygpath', '-w', directory)
76+
gopath = gopath.strip()
77+
else:
78+
gopath = directory
79+
env = dict(os.environ, GOPATH=gopath)
80+
env.pop('GOBIN', None)
81+
cmd_output_b('go', 'install', './...', cwd=repo_src_dir, env=env)
82+
for dependency in additional_dependencies:
83+
cmd_output_b(
84+
'go', 'install', dependency, cwd=repo_src_dir, env=env,
85+
)
86+
# Same some disk space, we don't need these after installation
87+
rmtree(prefix.path(directory, 'src'))
88+
pkgdir = prefix.path(directory, 'pkg')
89+
if os.path.exists(pkgdir): # pragma: no cover (go<1.10)
90+
rmtree(pkgdir)
9391

9492

9593
def run_hook(

pre_commit/languages/lua.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from pre_commit.hook import Hook
1414
from pre_commit.languages import helpers
1515
from pre_commit.prefix import Prefix
16-
from pre_commit.util import clean_path_on_failure
1716
from pre_commit.util import cmd_output
1817

1918
ENVIRONMENT_DIR = 'lua_env'
@@ -64,22 +63,21 @@ def install_environment(
6463
helpers.assert_version_default('lua', version)
6564

6665
envdir = _envdir(prefix)
67-
with clean_path_on_failure(envdir):
68-
with in_env(prefix):
69-
# luarocks doesn't bootstrap a tree prior to installing
70-
# so ensure the directory exists.
71-
os.makedirs(envdir, exist_ok=True)
72-
73-
# Older luarocks (e.g., 2.4.2) expect the rockspec as an arg
74-
for rockspec in prefix.star('.rockspec'):
75-
make_cmd = ('luarocks', '--tree', envdir, 'make', rockspec)
76-
helpers.run_setup_cmd(prefix, make_cmd)
77-
78-
# luarocks can't install multiple packages at once
79-
# so install them individually.
80-
for dependency in additional_dependencies:
81-
cmd = ('luarocks', '--tree', envdir, 'install', dependency)
82-
helpers.run_setup_cmd(prefix, cmd)
66+
with in_env(prefix):
67+
# luarocks doesn't bootstrap a tree prior to installing
68+
# so ensure the directory exists.
69+
os.makedirs(envdir, exist_ok=True)
70+
71+
# Older luarocks (e.g., 2.4.2) expect the rockspec as an arg
72+
for rockspec in prefix.star('.rockspec'):
73+
make_cmd = ('luarocks', '--tree', envdir, 'make', rockspec)
74+
helpers.run_setup_cmd(prefix, make_cmd)
75+
76+
# luarocks can't install multiple packages at once
77+
# so install them individually.
78+
for dependency in additional_dependencies:
79+
cmd = ('luarocks', '--tree', envdir, 'install', dependency)
80+
helpers.run_setup_cmd(prefix, cmd)
8381

8482

8583
def run_hook(

0 commit comments

Comments
 (0)