mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
abf86abf5e
commit
022b011889
@ -1669,7 +1669,7 @@ class DB(object):
|
|||||||
def move_library_to(self, all_paths, newloc, progress=lambda x: x):
|
def move_library_to(self, all_paths, newloc, progress=lambda x: x):
|
||||||
if not os.path.exists(newloc):
|
if not os.path.exists(newloc):
|
||||||
os.makedirs(newloc)
|
os.makedirs(newloc)
|
||||||
old_dirs = set()
|
old_dirs, old_files = set(), set()
|
||||||
items, path_map = self.get_top_level_move_items(all_paths)
|
items, path_map = self.get_top_level_move_items(all_paths)
|
||||||
for x in items:
|
for x in items:
|
||||||
src = os.path.join(self.library_path, x)
|
src = os.path.join(self.library_path, x)
|
||||||
@ -1683,13 +1683,14 @@ class DB(object):
|
|||||||
if os.path.exists(dest):
|
if os.path.exists(dest):
|
||||||
os.remove(dest)
|
os.remove(dest)
|
||||||
shutil.copyfile(src, dest)
|
shutil.copyfile(src, dest)
|
||||||
|
old_files.add(src)
|
||||||
x = path_map[x]
|
x = path_map[x]
|
||||||
if not isinstance(x, unicode):
|
if not isinstance(x, unicode):
|
||||||
x = x.decode(filesystem_encoding, 'replace')
|
x = x.decode(filesystem_encoding, 'replace')
|
||||||
progress(x)
|
progress(x)
|
||||||
|
|
||||||
dbpath = os.path.join(newloc, os.path.basename(self.dbpath))
|
dbpath = os.path.join(newloc, os.path.basename(self.dbpath))
|
||||||
opath = self.dbpath
|
opath, odir = self.dbpath, self.library_path
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
self.library_path, self.dbpath = newloc, dbpath
|
self.library_path, self.dbpath = newloc, dbpath
|
||||||
if self._conn is not None:
|
if self._conn is not None:
|
||||||
@ -1703,7 +1704,16 @@ class DB(object):
|
|||||||
for loc in old_dirs:
|
for loc in old_dirs:
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(loc)
|
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
|
pass
|
||||||
|
|
||||||
def restore_book(self, book_id, path, formats):
|
def restore_book(self, book_id, path, formats):
|
||||||
|
@ -108,6 +108,7 @@ class FilesystemTest(BaseTest):
|
|||||||
all_ids = cache.all_book_ids()
|
all_ids = cache.all_book_ids()
|
||||||
fmt1 = cache.format(1, 'FMT1')
|
fmt1 = cache.format(1, 'FMT1')
|
||||||
cov = cache.cover(1)
|
cov = cache.cover(1)
|
||||||
|
odir = cache.backend.library_path
|
||||||
with TemporaryDirectory('moved_lib') as tdir:
|
with TemporaryDirectory('moved_lib') as tdir:
|
||||||
cache.move_library_to(tdir)
|
cache.move_library_to(tdir)
|
||||||
self.assertIn('moved_lib', cache.backend.library_path)
|
self.assertIn('moved_lib', cache.backend.library_path)
|
||||||
@ -117,6 +118,8 @@ class FilesystemTest(BaseTest):
|
|||||||
cache.reload_from_db()
|
cache.reload_from_db()
|
||||||
self.assertEqual(all_ids, cache.all_book_ids())
|
self.assertEqual(all_ids, cache.all_book_ids())
|
||||||
cache.backend.close()
|
cache.backend.close()
|
||||||
|
self.assertFalse(os.path.exists(odir))
|
||||||
|
os.mkdir(odir) # needed otherwise tearDown() fails
|
||||||
|
|
||||||
def test_long_filenames(self):
|
def test_long_filenames(self):
|
||||||
' Test long file names '
|
' Test long file names '
|
||||||
|
Loading…
x
Reference in New Issue
Block a user