fix in librarian
[wolnelektury.git] / apps / catalogue / management / commands / importbooks.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 import os
6 import sys
7 import time
8 from optparse import make_option
9 from django.conf import settings
10 from django.core.management.base import BaseCommand
11 from django.core.management.color import color_style
12 from django.core.files import File
13 from librarian.picture import ImageStore
14 from wolnelektury_core.management.profile import profile
15
16 from catalogue.models import Book
17 from picture.models import Picture
18
19 from search.index import Index
20
21
22 class Command(BaseCommand):
23     option_list = BaseCommand.option_list + (
24         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
25             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
26         make_option('-f', '--force', action='store_true', dest='force', default=False,
27             help='Overwrite works already in the catalogue'),
28         make_option('-D', '--dont-build', dest='dont_build',
29             metavar="FORMAT,...",
30             help="Skip building specified formats"),
31         make_option('-S', '--no-search-index', action='store_false', dest='search_index', default=True,
32             help='Skip indexing imported works for search'),
33         make_option('-w', '--wait-until', dest='wait_until', metavar='TIME',
34             help='Wait until specified time (Y-M-D h:m:s)'),
35         make_option('-p', '--picture', action='store_true', dest='import_picture', default=False,
36             help='Import pictures'),
37     )
38     help = 'Imports books from the specified directories.'
39     args = 'directory [directory ...]'
40
41     def import_book(self, file_path, options):
42         verbose = options.get('verbose')
43         if options.get('dont_build'):
44             dont_build = options.get('dont_build').lower().split(',')
45         else:
46             dont_build = None
47         file_base, ext = os.path.splitext(file_path)
48         book = Book.from_xml_file(file_path, overwrite=options.get('force'),
49                                   dont_build=dont_build,
50                                   search_index_tags=False)
51         for ebook_format in Book.ebook_formats:
52             if os.path.isfile(file_base + '.' + ebook_format):
53                 getattr(book, '%s_file' % ebook_format).save(
54                     '%s.%s' % (book.slug, ebook_format),
55                     File(file(file_base + '.' + ebook_format)),
56                     save=False
57                     )
58                 if verbose:
59                     print "Importing %s.%s" % (file_base, ebook_format)
60         book.save()
61
62     def import_picture(self, file_path, options, continue_on_error=True):
63         try:
64             image_store = ImageStore(os.path.dirname(file_path))
65             picture = Picture.from_xml_file(file_path, image_store=image_store, overwrite=options.get('force'))
66         except Exception, ex:
67             if continue_on_error:
68                 print "%s: %s" % (file_path, ex)
69                 return
70             else:
71                 raise ex
72         return picture
73
74     #    @profile
75     def handle(self, *directories, **options):
76         from django.db import transaction
77
78         self.style = color_style()
79
80         verbose = options.get('verbose')
81         import_picture = options.get('import_picture')
82
83         wait_until = None
84         if options.get('wait_until'):
85             wait_until = time.mktime(time.strptime(options.get('wait_until'), '%Y-%m-%d %H:%M:%S'))
86             if verbose > 0:
87                 print "Will wait until %s; it's %f seconds from now" % (
88                     time.strftime('%Y-%m-%d %H:%M:%S',
89                     time.localtime(wait_until)), wait_until - time.time())
90
91         index = None
92         if options.get('search_index') and not settings.NO_SEARCH_INDEX:
93             index = Index()
94             try:
95                 index.index_tags()
96                 index.index.commit()
97             except Exception, e:
98                 index.index.rollback()
99                 raise e
100
101         # Start transaction management.
102         with transaction.atomic():
103
104             files_imported = 0
105             files_skipped = 0
106
107             for dir_name in directories:
108                 if not os.path.isdir(dir_name):
109                     print self.style.ERROR("%s: Not a directory. Skipping." % dir_name)
110                 else:
111                     # files queue
112                     files = sorted(os.listdir(dir_name))
113                     postponed = {}
114                     while files:
115                         file_name = files.pop(0)
116                         file_path = os.path.join(dir_name, file_name)
117                         file_base, ext = os.path.splitext(file_path)
118
119                         # Skip files that are not XML files
120                         if not ext == '.xml':
121                             continue
122
123                         if verbose > 0:
124                             print "Parsing '%s'" % file_path
125                         else:
126                             sys.stdout.write('.')
127                             sys.stdout.flush()
128
129                         # Import book files
130                         try:
131                             if import_picture:
132                                 self.import_picture(file_path, options)
133                             else:
134                                 self.import_book(file_path, options)
135
136                             files_imported += 1
137
138                         except (Book.AlreadyExists, Picture.AlreadyExists):
139                             print self.style.ERROR('%s: Book or Picture already imported. Skipping. To overwrite use --force.' %
140                                 file_path)
141                             files_skipped += 1
142
143                         except Book.DoesNotExist, e:
144                             if file_name not in postponed or postponed[file_name] < files_imported:
145                                 # push it back into the queue, maybe the missing child will show up
146                                 if verbose:
147                                     print self.style.NOTICE('Waiting for missing children')
148                                 files.append(file_name)
149                                 postponed[file_name] = files_imported
150                             else:
151                                 # we're in a loop, nothing's being imported - some child is really missing
152                                 raise e
153
154             # Print results
155             print
156             print "Results: %d files imported, %d skipped, %d total." % (
157                 files_imported, files_skipped, files_imported + files_skipped)
158             print
159
160             if wait_until:
161                 print 'Waiting...'
162                 try:
163                     time.sleep(wait_until - time.time())
164                 except IOError:
165                     print "it's already too late"