From d6124b6e36c0b27e339a9022ddc4e447bf793f9c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 19 Aug 2009 14:45:22 -0600 Subject: [PATCH] Make moving the library location via the Preferences dialog faster --- src/calibre/library/database2.py | 38 ++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index f1a9ea18dd..d347d2408b 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1445,36 +1445,40 @@ class LibraryDatabase2(LibraryDatabase): self.notify('add', [id]) def move_library_to(self, newloc, progress=lambda x: x): - books = self.conn.get('SELECT id, path, title FROM books') if not os.path.exists(newloc): os.makedirs(newloc) + items = os.listdir(self.library_path) old_dirs = set([]) - for i, book in enumerate(books): - path = book[1] - if not path: - continue - dir = path.split('/')[0] - srcdir = os.path.join(self.library_path, dir) - tdir = os.path.join(newloc, dir) - if os.path.exists(tdir): - shutil.rmtree(tdir) - if os.path.exists(srcdir): - shutil.copytree(srcdir, tdir) - old_dirs.add(srcdir) - progress(book[2]) + for i, x in enumerate(items): + src = os.path.join(self.library_path, x) + dest = os.path.join(newloc, x) + if os.path.isdir(src): + if os.path.exists(dest): + shutil.rmtree(dest) + shutil.copytree(src, dest) + old_dirs.add(src) + else: + if os.path.exists(dest): + os.remove(dest) + shutil.copyfile(src, dest) + if not isinstance(x, unicode): + x = x.decode(filesystem_encoding, 'replace') + progress(x) dbpath = os.path.join(newloc, os.path.basename(self.dbpath)) - shutil.copyfile(self.dbpath, dbpath) opath = self.dbpath self.conn.close() self.library_path, self.dbpath = newloc, dbpath self.connect() try: os.unlink(opath) - for dir in old_dirs: - shutil.rmtree(dir) except: pass + for dir in old_dirs: + try: + shutil.rmtree(dir) + except: + pass def __iter__(self): for record in self.data._data: