From f81c6bd8a12babad3248775d22d9e0b98d7c9cec Mon Sep 17 00:00:00 2001 From: blissful Date: Tue, 10 Oct 2023 12:40:18 -0400 Subject: [PATCH] fix bug where dotted dirs are renamed incorrectly --- rose/__main__.py | 17 +++++++++++++---- rose/cache/update.py | 6 +++++- rose/cache/update_test.py | 10 ++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/rose/__main__.py b/rose/__main__.py index 6b915c4..1efbd6a 100644 --- a/rose/__main__.py +++ b/rose/__main__.py @@ -4,6 +4,7 @@ import click from rose.cache.database import migrate_database +from rose.cache.update import update_cache_for_all_releases from rose.foundation.conf import Config @@ -29,14 +30,22 @@ def cli(clickctx: click.Context, verbose: bool) -> None: @cli.group() -@click.pass_obj -def cache(_: Context) -> None: +def cache() -> None: """Manage the cached metadata.""" @cache.command() -def reset() -> None: - """Reset the cache and empty the database.""" +@click.pass_obj +def refresh(c: Context) -> None: + """Refresh the cached data from disk.""" + update_cache_for_all_releases(c.config) + + +@cache.command() +@click.pass_obj +def clear(c: Context) -> None: + """Clear the cache; empty the database.""" + c.config.cache_database_path.unlink() if __name__ == "__main__": diff --git a/rose/cache/update.py b/rose/cache/update.py index 65a4fd8..befaf18 100644 --- a/rose/cache/update.py +++ b/rose/cache/update.py @@ -244,4 +244,8 @@ def _parse_uuid_from_path(path: Path) -> str | None: def _rename_with_uuid(src: Path, uuid: str) -> Path: - return src.rename(src.with_stem(src.stem + f" {{id={uuid}}}")) + if src.is_dir(): + dst = src.with_name(src.name + f" {{id={uuid}}}") + else: + dst = src.with_stem(src.stem + f" {{id={uuid}}}") + return src.rename(dst) diff --git a/rose/cache/update_test.py b/rose/cache/update_test.py index c4fcbf4..8af2a97 100644 --- a/rose/cache/update_test.py +++ b/rose/cache/update_test.py @@ -153,3 +153,13 @@ def test_update_cache_for_all_releases(config: Config) -> None: assert cursor.fetchone()[0] == 2 cursor = conn.execute("SELECT COUNT(*) FROM tracks") assert cursor.fetchone()[0] == 4 + + +def test_update_cache_with_dotted_dirname(config: Config) -> None: + # Regression test: If we use with_stem on a directory with a dot, then the directory will be + # renamed to like Put.ID.After.The {id=abc}.Dot" which we don't want. + release_dir = config.music_source_dir / "Put.ID.After.The.Dot" + shutil.copytree(TEST_RELEASE_1, release_dir) + updated_release_dir = update_cache_for_release(config, release_dir) + m = ID_REGEX.search(updated_release_dir.name) + assert m is not None