This commit is contained in:
Kovid Goyal 2010-09-25 22:50:44 -06:00
parent 5b8a645050
commit cdc5127fa4
2 changed files with 13 additions and 5 deletions

View File

@ -10,7 +10,8 @@ Command line interface to the calibre database.
import sys, os, cStringIO, re import sys, os, cStringIO, re
from textwrap import TextWrapper from textwrap import TextWrapper
from calibre import terminal_controller, preferred_encoding, prints from calibre import terminal_controller, preferred_encoding, prints, \
isbytestring
from calibre.utils.config import OptionParser, prefs, tweaks from calibre.utils.config import OptionParser, prefs, tweaks
from calibre.ebooks.metadata.meta import get_metadata from calibre.ebooks.metadata.meta import get_metadata
from calibre.library.database2 import LibraryDatabase2 from calibre.library.database2 import LibraryDatabase2
@ -901,6 +902,12 @@ def command_restore_database(args, dbpath):
parser.print_help() parser.print_help()
return 1 return 1
if opts.library_path is not None:
dbpath = opts.library_path
if isbytestring(dbpath):
dbpath = dbpath.decode(preferred_encoding)
class Progress(object): class Progress(object):
def __init__(self): self.total = 1 def __init__(self): self.total = 1
@ -918,6 +925,7 @@ def command_restore_database(args, dbpath):
prints(r.tb) prints(r.tb)
else: else:
prints('Restoring database succeeded') prints('Restoring database succeeded')
prints('old database saved as', r.olddb)
if r.errors_occurred: if r.errors_occurred:
name = 'calibre_db_restore_report.txt' name = 'calibre_db_restore_report.txt'
open('calibre_db_restore_report.txt', open('calibre_db_restore_report.txt',

View File

@ -61,7 +61,7 @@ class Restore(Thread):
self.failed_restores] self.failed_restores]
if failures: if failures:
ans += 'Failed to restore the books in the following folders:\n' ans += 'Failed to restore the books in the following folders:\n'
wrap = TextWrapper(initial_indent='\t\t', width=85) wrap = TextWrapper(initial_indent='\t\t', width=1085)
for dirpath, tb in failures: for dirpath, tb in failures:
ans += '\t' + dirpath + ' with error:\n' ans += '\t' + dirpath + ' with error:\n'
ans += wrap.fill(tb) ans += wrap.fill(tb)
@ -103,7 +103,7 @@ class Restore(Thread):
self.process_dir(dirpath, filenames, book_id) self.process_dir(dirpath, filenames, book_id)
except: except:
self.failed_dirs.append((dirpath, traceback.format_exc())) self.failed_dirs.append((dirpath, traceback.format_exc()))
self.progress_callback(_('Processed') + repr(dirpath), i+1) self.progress_callback(_('Processed') + ' ' + dirpath, i+1)
def is_ebook_file(self, filename): def is_ebook_file(self, filename):
ext = os.path.splitext(filename)[1] ext = os.path.splitext(filename)[1]
@ -183,14 +183,14 @@ class Restore(Thread):
for fmt, size, name in book['formats']: for fmt, size, name in book['formats']:
db.conn.execute(''' db.conn.execute('''
INSERT INTO data (book,format,uncompressed_size,name) INSERT INTO data (book,format,uncompressed_size,name)
VALUES (?,?,?,?)''', (id, fmt, size, name)) VALUES (?,?,?,?)''', (book['id'], fmt, size, name))
db.conn.commit() db.conn.commit()
def replace_db(self): def replace_db(self):
dbpath = os.path.join(self.src_library_path, 'metadata.db') dbpath = os.path.join(self.src_library_path, 'metadata.db')
ndbpath = os.path.join(self.library_path, 'metadata.db') ndbpath = os.path.join(self.library_path, 'metadata.db')
save_path = os.path.splitext(dbpath)[0]+'_pre_restore.db' save_path = self.olddb = os.path.splitext(dbpath)[0]+'_pre_restore.db'
if os.path.exists(save_path): if os.path.exists(save_path):
os.remove(save_path) os.remove(save_path)
os.rename(dbpath, save_path) os.rename(dbpath, save_path)