Fix moving libraries via calibre leaving behind a copy of the metadata_db_prefs_backup.json file in the original library folder

This commit is contained in:
Kovid Goyal 2015-12-10 09:58:01 +05:30
parent abf86abf5e
commit 022b011889
2 changed files with 16 additions and 3 deletions

View File

@ -1669,7 +1669,7 @@ class DB(object):
def move_library_to(self, all_paths, newloc, progress=lambda x: x):
if not os.path.exists(newloc):
os.makedirs(newloc)
old_dirs = set()
old_dirs, old_files = set(), set()
items, path_map = self.get_top_level_move_items(all_paths)
for x in items:
src = os.path.join(self.library_path, x)
@ -1683,13 +1683,14 @@ class DB(object):
if os.path.exists(dest):
os.remove(dest)
shutil.copyfile(src, dest)
old_files.add(src)
x = path_map[x]
if not isinstance(x, unicode):
x = x.decode(filesystem_encoding, 'replace')
progress(x)
dbpath = os.path.join(newloc, os.path.basename(self.dbpath))
opath = self.dbpath
opath, odir = self.dbpath, self.library_path
self.conn.close()
self.library_path, self.dbpath = newloc, dbpath
if self._conn is not None:
@ -1703,7 +1704,16 @@ class DB(object):
for loc in old_dirs:
try:
shutil.rmtree(loc)
except:
except EnvironmentError:
pass
for loc in old_files:
try:
os.remove(loc)
except EnvironmentError:
pass
try:
os.rmdir(odir)
except EnvironmentError:
pass
def restore_book(self, book_id, path, formats):

View File

@ -108,6 +108,7 @@ class FilesystemTest(BaseTest):
all_ids = cache.all_book_ids()
fmt1 = cache.format(1, 'FMT1')
cov = cache.cover(1)
odir = cache.backend.library_path
with TemporaryDirectory('moved_lib') as tdir:
cache.move_library_to(tdir)
self.assertIn('moved_lib', cache.backend.library_path)
@ -117,6 +118,8 @@ class FilesystemTest(BaseTest):
cache.reload_from_db()
self.assertEqual(all_ids, cache.all_book_ids())
cache.backend.close()
self.assertFalse(os.path.exists(odir))
os.mkdir(odir) # needed otherwise tearDown() fails
def test_long_filenames(self):
' Test long file names '