1#!/usr/local/bin/python3.8
2# vim:fileencoding=utf-8
3# License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
4
5
6import os
7
8from calibre import patheq
9from calibre.constants import iswindows
10from calibre.db.legacy import LibraryDatabase
11
12readonly = True
13version = 0  # change this if you change signature of implementation()
14
15
16def implementation(db, notify_changes):
17    return db.backend.prefs.copy(), db.backend.library_path
18
19
20def option_parser(get_parser, args):
21    return get_parser(
22        _(
23            '''\
24%prog clone path/to/new/library
25
26Create a clone of the current library. This creates a new, empty library that has all the
27same custom columns, Virtual libraries and other settings as the current library.
28
29The cloned library will contain no books. If you want to create a full duplicate, including
30all books, then simply use your filesystem tools to copy the library folder.
31    '''
32        )
33    )
34
35
36def main(opts, args, dbctx):
37    if len(args) < 1:
38        raise SystemExit(_('Error: You must specify the path to the cloned library'))
39    prefs, library_path = dbctx.run('clone')
40    loc = os.path.abspath(args[0])
41    if not os.path.exists(loc):
42        os.makedirs(loc)
43
44    if patheq(loc, library_path):
45        raise SystemExit(
46            _('The location for the new library is the same as the current library')
47        )
48    empty = not os.listdir(loc)
49    if not empty:
50        raise SystemExit(
51            _(
52                '%s is not empty. You must choose an empty folder for the new library.'
53            ) % loc
54        )
55    if iswindows and len(loc) > LibraryDatabase.WINDOWS_LIBRARY_PATH_LIMIT:
56        raise SystemExit(
57            _('Path to library too long. It must be less than'
58              ' %d characters.') % LibraryDatabase.WINDOWS_LIBRARY_PATH_LIMIT
59        )
60    LibraryDatabase(loc, default_prefs=prefs)
61
62    return 0
63