Skip to content

Commit aa181a1

Browse files
committed
Add function to recursively move files overwriting the target
This is needed in several places so it is better to factor it out. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 5c8f83a commit aa181a1

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

cookiecutter/hooks/post_gen_project.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,8 @@ def finish_api_setup() -> None:
381381
* Rename `src` to `py`
382382
* Rename `tests` to `pytests`
383383
"""
384-
# We can't do a simple rename because the target directory might not be empty if
385-
# overwriting an existing project.
386-
_shutil.copytree("src", "py", dirs_exist_ok=True)
387-
_shutil.rmtree("src")
388-
_shutil.copytree("tests", "pytests", dirs_exist_ok=True)
389-
_shutil.rmtree("tests")
384+
recursive_overwrite_move(_pathlib.Path("src"), _pathlib.Path("py"))
385+
recursive_overwrite_move(_pathlib.Path("tests"), _pathlib.Path("pytests"))
390386

391387

392388
def finish_app_setup() -> None:
@@ -471,6 +467,19 @@ def try_run(
471467
return result
472468

473469

470+
def recursive_overwrite_move(src: _pathlib.Path, dst: _pathlib.Path) -> None:
471+
"""Recursively move a directory overwriting the target files if they exist.
472+
473+
Useful when overwriting an existing project to update it.
474+
475+
Args:
476+
src: The source directory.
477+
dst: The target directory.
478+
"""
479+
_shutil.copytree(src, dst, dirs_exist_ok=True)
480+
_shutil.rmtree(src)
481+
482+
474483
def success(message: str) -> None:
475484
"""Print a success message.
476485

0 commit comments

Comments
 (0)