From ecd94450e95c1311114ce6f9be7632a87569e38e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 9 Oct 2015 07:44:22 +0530 Subject: [PATCH] Allow ebook-metadata to work with read-only files when no options are specified to change metadata. Fixes #1504345 [ebook-meta does not work on read-only files](https://bugs.launchpad.net/calibre/+bug/1504345) --- src/calibre/ebooks/metadata/cli.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/calibre/ebooks/metadata/cli.py b/src/calibre/ebooks/metadata/cli.py index 8b1d9f7d9c..5ffc84bbf6 100644 --- a/src/calibre/ebooks/metadata/cli.py +++ b/src/calibre/ebooks/metadata/cli.py @@ -165,7 +165,6 @@ def main(args=sys.argv): prints(_('No file specified'), file=sys.stderr) return 1 path = args[1] - stream = open(path, 'r+b') stream_type = os.path.splitext(path)[1].replace('.', '').lower() trying_to_set = False @@ -175,7 +174,8 @@ def main(args=sys.argv): if getattr(opts, pref.name) is not None: trying_to_set = True break - mi = get_metadata(stream, stream_type, force_read_metadata=True) + with open(path, 'rb') as stream: + mi = get_metadata(stream, stream_type, force_read_metadata=True) if trying_to_set: prints(_('Original metadata')+'::') metadata = unicode(mi) @@ -184,16 +184,16 @@ def main(args=sys.argv): prints(metadata, safe_encode=True) if trying_to_set: - stream.seek(0) - do_set_metadata(opts, mi, stream, stream_type) - stream.seek(0) - stream.flush() - lrf = None - if stream_type == 'lrf': - if opts.lrf_bookid is not None: - lrf = LRFMetaFile(stream) - lrf.book_id = opts.lrf_bookid - mi = get_metadata(stream, stream_type, force_read_metadata=True) + with open(path, 'r+b') as stream: + do_set_metadata(opts, mi, stream, stream_type) + stream.seek(0) + stream.flush() + lrf = None + if stream_type == 'lrf': + if opts.lrf_bookid is not None: + lrf = LRFMetaFile(stream) + lrf.book_id = opts.lrf_bookid + mi = get_metadata(stream, stream_type, force_read_metadata=True) prints('\n' + _('Changed metadata') + '::') metadata = unicode(mi) metadata = '\t'+'\n\t'.join(metadata.split('\n'))