From 589475e38b022711eced26b058fe67e040ac8533 Mon Sep 17 00:00:00 2001 From: "Augusto W. Andreoli" Date: Tue, 20 Jul 2021 23:56:45 +0200 Subject: [PATCH] fix(rename-slugify): directory not empty --- Makefile | 4 ++-- src/clib/files.py | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 393fe2c..8999def 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ build: clear pre-commit run --all-files - # pytest + poetry run pytest .PHONY: build help: @@ -55,7 +55,7 @@ lt: lint test ltd: lint test docs test: - python setup.py test + poetry run pytest test-all: tox diff --git a/src/clib/files.py b/src/clib/files.py index a42b807..e7c419f 100644 --- a/src/clib/files.py +++ b/src/clib/files.py @@ -372,7 +372,13 @@ def rename_batch(yes: bool, dry_run: bool, is_dir: bool, root_dir: Path, items: # Don't rename files with the exact same name that already exist click.secho(f"New file already exists! {new}", err=True, fg="red") else: - os.rename(original, new) + try: + os.rename(original, new) + except OSError as err: + if err.errno == 66: # Directory not empty + merge_directories(new, original) + else: + raise err click.secho(f"{pretty_root}: {which_type.capitalize()} renamed succesfully.", fg="yellow") return bool(pairs) @@ -465,8 +471,16 @@ def merge_directories(target_dir: PathOrStr, *source_dirs: PathOrStr, dry_run: b source_color = "bright_blue" echo(f"Target: {target_dir}", fg=target_color) + if not Path(target_dir).is_dir(): + click.secho("Target is not a directory", err=True, fg="red") + return False + for source_dir in source_dirs: echo(f"Source: {source_dir}", fg=source_color) + if not Path(source_dir).is_dir(): + click.secho("Source is not a directory", err=True, fg="red") + continue + for path in sorted(Path(source_dir).rglob("*")): if path.is_dir() or path.stem in IGNORE_FILES_ON_MERGE: continue