Skip to content

Commit 2a9893d

Browse files
committed
mkdirp -> os.makedirs(..., exist_ok=True)
1 parent 67c2dcd commit 2a9893d

File tree

5 files changed

+9
-21
lines changed

5 files changed

+9
-21
lines changed

pre_commit/commands/install_uninstall.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pre_commit.repository import install_hook_envs
1515
from pre_commit.store import Store
1616
from pre_commit.util import make_executable
17-
from pre_commit.util import mkdirp
1817
from pre_commit.util import resource_text
1918

2019

@@ -78,7 +77,7 @@ def _install_hook_script(
7877
) -> None:
7978
hook_path, legacy_path = _hook_paths(hook_type, git_dir=git_dir)
8079

81-
mkdirp(os.path.dirname(hook_path))
80+
os.makedirs(os.path.dirname(hook_path), exist_ok=True)
8281

8382
# If we have an existing hook, move it to pre-commit.legacy
8483
if os.path.lexists(hook_path) and not is_our_script(hook_path):

pre_commit/staged_files_only.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pre_commit.util import CalledProcessError
99
from pre_commit.util import cmd_output
1010
from pre_commit.util import cmd_output_b
11-
from pre_commit.util import mkdirp
1211
from pre_commit.xargs import xargs
1312

1413

@@ -55,7 +54,7 @@ def _unstaged_changes_cleared(patch_dir: str) -> Generator[None, None, None]:
5554
f'Stashing unstaged files to {patch_filename}.',
5655
)
5756
# Save the current unstaged changes as a patch
58-
mkdirp(patch_dir)
57+
os.makedirs(patch_dir, exist_ok=True)
5958
with open(patch_filename, 'wb') as patch_file:
6059
patch_file.write(diff_stdout_binary)
6160

pre_commit/store.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from pre_commit.util import CalledProcessError
1717
from pre_commit.util import clean_path_on_failure
1818
from pre_commit.util import cmd_output_b
19-
from pre_commit.util import mkdirp
2019
from pre_commit.util import resource_text
2120
from pre_commit.util import rmtree
2221

@@ -45,7 +44,7 @@ def __init__(self, directory: Optional[str] = None) -> None:
4544
self.db_path = os.path.join(self.directory, 'db.db')
4645

4746
if not os.path.exists(self.directory):
48-
mkdirp(self.directory)
47+
os.makedirs(self.directory, exist_ok=True)
4948
with open(os.path.join(self.directory, 'README'), 'w') as f:
5049
f.write(
5150
'This directory is maintained by the pre-commit project.\n'

pre_commit/util.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@
2929
EnvironT = Union[Dict[str, str], 'os._Environ']
3030

3131

32-
def mkdirp(path: str) -> None:
33-
try:
34-
os.makedirs(path)
35-
except OSError:
36-
if not os.path.exists(path):
37-
raise
38-
39-
4032
@contextlib.contextmanager
4133
def clean_path_on_failure(path: str) -> Generator[None, None, None]:
4234
"""Cleans up the directory on an exceptional failure."""

tests/commands/install_uninstall_test.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pre_commit.parse_shebang import find_executable
1515
from pre_commit.util import cmd_output
1616
from pre_commit.util import make_executable
17-
from pre_commit.util import mkdirp
1817
from pre_commit.util import resource_text
1918
from testing.fixtures import git_dir
2019
from testing.fixtures import make_consuming_repo
@@ -307,7 +306,7 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
307306

308307

309308
def _write_legacy_hook(path):
310-
mkdirp(os.path.join(path, '.git/hooks'))
309+
os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)
311310
with open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
312311
f.write('#!/usr/bin/env bash\necho "legacy hook"\n')
313312
make_executable(f.name)
@@ -370,7 +369,7 @@ def test_failing_existing_hook_returns_1(tempdir_factory, store):
370369
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
371370
with cwd(path):
372371
# Write out a failing "old" hook
373-
mkdirp(os.path.join(path, '.git/hooks'))
372+
os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)
374373
with open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
375374
f.write('#!/usr/bin/env bash\necho "fail!"\nexit 1\n')
376375
make_executable(f.name)
@@ -432,7 +431,7 @@ def test_replace_old_commit_script(tempdir_factory, store):
432431
CURRENT_HASH, PRIOR_HASHES[-1],
433432
)
434433

435-
mkdirp(os.path.join(path, '.git/hooks'))
434+
os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)
436435
with open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
437436
f.write(new_contents)
438437
make_executable(f.name)
@@ -609,7 +608,7 @@ def test_pre_push_legacy(tempdir_factory, store):
609608
path = tempdir_factory.get()
610609
cmd_output('git', 'clone', upstream, path)
611610
with cwd(path):
612-
mkdirp(os.path.join(path, '.git/hooks'))
611+
os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)
613612
with open(os.path.join(path, '.git/hooks/pre-push'), 'w') as f:
614613
f.write(
615614
'#!/usr/bin/env bash\n'
@@ -658,7 +657,7 @@ def test_commit_msg_integration_passing(
658657

659658
def test_commit_msg_legacy(commit_msg_repo, tempdir_factory, store):
660659
hook_path = os.path.join(commit_msg_repo, '.git/hooks/commit-msg')
661-
mkdirp(os.path.dirname(hook_path))
660+
os.makedirs(os.path.dirname(hook_path), exist_ok=True)
662661
with open(hook_path, 'w') as hook_file:
663662
hook_file.write(
664663
'#!/usr/bin/env bash\n'
@@ -713,7 +712,7 @@ def test_prepare_commit_msg_legacy(
713712
hook_path = os.path.join(
714713
prepare_commit_msg_repo, '.git/hooks/prepare-commit-msg',
715714
)
716-
mkdirp(os.path.dirname(hook_path))
715+
os.makedirs(os.path.dirname(hook_path), exist_ok=True)
717716
with open(hook_path, 'w') as hook_file:
718717
hook_file.write(
719718
'#!/usr/bin/env bash\n'

0 commit comments

Comments
 (0)