From 704b6b1b633b371e434c265b608a4b0daa31d1e9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 2 May 2017 12:29:23 +0530 Subject: [PATCH] Port calibredb clone --- src/calibre/db/cli/cmd_clone.py | 53 +++++++++++++++++++++++++++++---- src/calibre/db/cli/main.py | 4 +-- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/calibre/db/cli/cmd_clone.py b/src/calibre/db/cli/cmd_clone.py index 2f6f22ae56..8dcf78bc68 100644 --- a/src/calibre/db/cli/cmd_clone.py +++ b/src/calibre/db/cli/cmd_clone.py @@ -4,19 +4,60 @@ from __future__ import absolute_import, division, print_function, unicode_literals -readonly = False +import os + +from calibre import patheq +from calibre.constants import iswindows +from calibre.db.legacy import LibraryDatabase + +readonly = True version = 0 # change this if you change signature of implementation() -def implementation(db, notify_changes, *args): - is_remote = notify_changes is not None - is_remote +def implementation(db, notify_changes): + return db.backend.prefs.copy(), db.backend.library_path def option_parser(get_parser, args): - pass + return get_parser( + _( + '''\ +%prog clone path/to/new/library + +Create a clone of the current library. This creates a new, empty library that has all the +same custom columns, virtual libraries and other settings as the current library. + +The cloned library will contain no books. If you want to create a full duplicate, including +all books, then simply use your filesystem tools to copy the library folder. + ''' + ) + ) def main(opts, args, dbctx): - raise NotImplementedError('TODO: implement this') + if len(args) < 1: + raise SystemExit(_('Error: You must specify the path to the cloned library')) + prefs, library_path = dbctx.run('clone') + loc = os.path.abspath(args[0]) + if not os.path.exists(loc): + os.makedirs(loc) + + if patheq(loc, library_path): + raise SystemExit( + _('The location for the new library is the same as the current library') + ) + empty = not os.listdir(loc) + if not empty: + raise SystemExit( + _( + '%s is not empty. You must choose an empty directory for the new library.' + ) % loc + ) + if iswindows and len(loc) > LibraryDatabase.WINDOWS_LIBRARY_PATH_LIMIT: + raise SystemExit( + _('Path to library too long. Must be less than' + ' %d characters.') % LibraryDatabase.WINDOWS_LIBRARY_PATH_LIMIT + ) + LibraryDatabase(loc, default_prefs=prefs) + return 0 diff --git a/src/calibre/db/cli/main.py b/src/calibre/db/cli/main.py index 4fe24cd244..75bc727f59 100644 --- a/src/calibre/db/cli/main.py +++ b/src/calibre/db/cli/main.py @@ -22,8 +22,8 @@ COMMANDS = ( 'list', 'add', 'remove', 'add_format', 'remove_format', 'show_metadata', 'set_metadata', 'export', 'catalog', 'saved_searches', 'add_custom_column', 'custom_columns', 'remove_custom_column', 'set_custom', 'restore_database', - 'check_library', 'list_categories', 'backup_metadata', - # 'clone', 'embed_metadata', 'search' + 'check_library', 'list_categories', 'backup_metadata', 'clone', + # 'embed_metadata', 'search' )