Make moving the library location via the Preferences dialog faster

This commit is contained in:
Kovid Goyal 2009-08-19 14:45:22 -06:00
parent 642506bba0
commit d6124b6e36

View File

@ -1445,36 +1445,40 @@ class LibraryDatabase2(LibraryDatabase):
self.notify('add', [id]) self.notify('add', [id])
def move_library_to(self, newloc, progress=lambda x: x): 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): if not os.path.exists(newloc):
os.makedirs(newloc) os.makedirs(newloc)
items = os.listdir(self.library_path)
old_dirs = set([]) old_dirs = set([])
for i, book in enumerate(books): for i, x in enumerate(items):
path = book[1] src = os.path.join(self.library_path, x)
if not path: dest = os.path.join(newloc, x)
continue if os.path.isdir(src):
dir = path.split('/')[0] if os.path.exists(dest):
srcdir = os.path.join(self.library_path, dir) shutil.rmtree(dest)
tdir = os.path.join(newloc, dir) shutil.copytree(src, dest)
if os.path.exists(tdir): old_dirs.add(src)
shutil.rmtree(tdir) else:
if os.path.exists(srcdir): if os.path.exists(dest):
shutil.copytree(srcdir, tdir) os.remove(dest)
old_dirs.add(srcdir) shutil.copyfile(src, dest)
progress(book[2]) if not isinstance(x, unicode):
x = x.decode(filesystem_encoding, 'replace')
progress(x)
dbpath = os.path.join(newloc, os.path.basename(self.dbpath)) dbpath = os.path.join(newloc, os.path.basename(self.dbpath))
shutil.copyfile(self.dbpath, dbpath)
opath = self.dbpath opath = self.dbpath
self.conn.close() self.conn.close()
self.library_path, self.dbpath = newloc, dbpath self.library_path, self.dbpath = newloc, dbpath
self.connect() self.connect()
try: try:
os.unlink(opath) os.unlink(opath)
for dir in old_dirs:
shutil.rmtree(dir)
except: except:
pass pass
for dir in old_dirs:
try:
shutil.rmtree(dir)
except:
pass
def __iter__(self): def __iter__(self):
for record in self.data._data: for record in self.data._data: