More explicit resource closage

This commit is contained in:
Kovid Goyal 2023-10-16 12:25:57 +05:30
parent 89f9b14fc2
commit 324a1e1aed
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 16 additions and 12 deletions

View File

@ -3480,18 +3480,17 @@ def import_library(library_key, importer, library_path, progress=None, abort=Non
cache._update_path((book_id,), mark_as_dirtied=False)
for fmt, fmtkey in iteritems(fmt_key_map):
if fmt == '.cover':
stream = importer.start_file(fmtkey, _('Cover for %s') % title)
path = cache._field_for('path', book_id).replace('/', os.sep)
cache.backend.set_cover(book_id, path, stream, no_processing=True)
with closing(importer.start_file(fmtkey, _('Cover for %s') % title)) as stream:
path = cache._field_for('path', book_id).replace('/', os.sep)
cache.backend.set_cover(book_id, path, stream, no_processing=True)
else:
stream = importer.start_file(fmtkey, _('{0} format for {1}').format(fmt.upper(), title))
size, fname = cache._do_add_format(book_id, fmt, stream, mtime=stream.mtime)
cache.fields['formats'].table.update_fmt(book_id, fmt, fname, size, cache.backend)
stream.close()
with closing(importer.start_file(fmtkey, _('{0} format for {1}').format(fmt.upper(), title))) as stream:
size, fname = cache._do_add_format(book_id, fmt, stream, mtime=stream.mtime)
cache.fields['formats'].table.update_fmt(book_id, fmt, fname, size, cache.backend)
for relpath, efkey in extra_files.get(book_id, {}).items():
stream = importer.start_file(efkey, _('Extra file {0} for book {1}').format(relpath, title))
path = cache._field_for('path', book_id).replace('/', os.sep)
cache.backend.add_extra_file(relpath, stream, path)
with closing(importer.start_file(efkey, _('Extra file {0} for book {1}').format(relpath, title))) as stream:
path = cache._field_for('path', book_id).replace('/', os.sep)
cache.backend.add_extra_file(relpath, stream, path)
cache.dump_metadata({book_id})
if progress is not None:
progress(_('Completed'), total, total)

View File

@ -239,6 +239,10 @@ class FilesystemTest(BaseTest):
def test_export_import(self):
from calibre.db.cache import import_library
from calibre.utils.exim import Exporter, Importer
def read(x, mode='r'):
with open(x, mode) as f:
return f.read()
cache = self.init_cache()
bookdir = os.path.dirname(cache.format_abspath(1, '__COVER_INTERNAL__'))
with open(os.path.join(bookdir, 'exf'), 'w') as f:
@ -262,8 +266,8 @@ class FilesystemTest(BaseTest):
self.assertEqual(cache.format(book_id, fmt), ic.format(book_id, fmt))
self.assertEqual(cache.format_metadata(book_id, fmt)['mtime'], cache.format_metadata(book_id, fmt)['mtime'])
bookdir = os.path.dirname(ic.format_abspath(1, '__COVER_INTERNAL__'))
self.assertEqual('exf', open(os.path.join(bookdir, 'exf')).read())
self.assertEqual('recurse', open(os.path.join(bookdir, 'sub', 'recurse')).read())
self.assertEqual('exf', read(os.path.join(bookdir, 'exf')))
self.assertEqual('recurse', read(os.path.join(bookdir, 'sub', 'recurse')))
r1 = cache.add_notes_resource(b'res1', 'res.jpg', mtime=time.time()-113)
r2 = cache.add_notes_resource(b'res2', 'res.jpg', mtime=time.time()-1115)
cache.set_notes_for('authors', 2, 'some notes', resource_hashes=(r1, r2))

View File

@ -256,6 +256,7 @@ class FileSource:
def close(self):
if self.check_hash and self.hasher.hexdigest() != self.digest:
self.importer.corrupted_files.append(self.description)
self.f.close()
self.hasher = self.f = None