From becebc1c0e3ff8fbe1d6e0ef85a7689abed63219 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 1 Nov 2024 16:52:01 +0530 Subject: [PATCH] When renaming authors fix empty author folder not being removed if it contained file explorer metadata but was otherwise empty --- src/calibre/db/backend.py | 2 +- src/calibre/db/tests/writing.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index 34977661e1..14d37546eb 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -1997,7 +1997,7 @@ class DB: copy_tree(os.path.abspath(spath), tpath, delete_source=True, transform_destination_filename=transform_format_filenames) parent = os.path.dirname(spath) with suppress(OSError): - os.rmdir(parent) # remove empty parent directory + remove_dir_if_empty(parent, ignore_metadata_caches=True) else: os.makedirs(tpath) update_paths_in_db() diff --git a/src/calibre/db/tests/writing.py b/src/calibre/db/tests/writing.py index 2f81eb4d5d..27ba21e845 100644 --- a/src/calibre/db/tests/writing.py +++ b/src/calibre/db/tests/writing.py @@ -589,6 +589,24 @@ class WritingTest(BaseTest): def test_rename_items(self): # {{{ ' Test renaming of many-(many,one) items ' + # Test renaming authors removes folders with junk in them + cl = self.cloned_library + cache = self.init_cache(cl) + fmtpath = cache.format_abspath(1, 'FMT1') + bookpath = os.path.dirname(fmtpath) + authorpath = os.path.dirname(bookpath) + self.assertTrue(os.path.exists(authorpath)) + author = cache.field_for('authors', 1)[0] + os.mkdir(os.path.join(authorpath, '.DS_Store')) + open(os.path.join(authorpath, 'Thumbs.db'), 'wb').close() + amap = {v:k for k, v in cache.get_id_map('authors').items()} + cache.rename_items('authors', {amap[author]: 'renamed'}) + try: + items = os.listdir(authorpath) + except FileNotFoundError: + items = [] + self.assertFalse(items, 'Items in author folder: ' + ' '.join(items)) + cl = self.cloned_library cache = self.init_cache(cl) # Check that renaming authors updates author sort and path @@ -681,6 +699,7 @@ class WritingTest(BaseTest): self.assertEqual(c.field_for('tags', 1), ('r', 'q', 'c')) self.assertEqual(c.field_for('tags', 2), ('X', 'y', 'z')) self.assertEqual(c.field_for('tags', 3), ('a', 'X', 'z')) + # }}} def test_composite_cache(self): # {{{