From ecd4d6ddea202af9cd3d1e20c059b1edb7781214 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 3 May 2012 19:26:04 +0530 Subject: [PATCH] ... --- src/calibre/debug.py | 4 +--- src/calibre/ebooks/tweak.py | 33 ++++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/calibre/debug.py b/src/calibre/debug.py index f2ae5d8eaf..1c1123d45f 100644 --- a/src/calibre/debug.py +++ b/src/calibre/debug.py @@ -59,9 +59,7 @@ Run an embedded python interpreter. 'files and metadata, which you can edit using standard HTML ' 'editing tools, and then rebuilds the file from the edited HTML. ' 'Makes no additional changes to the HTML, unlike a full calibre ' - 'conversion). Note that this tool will try to open the ' - 'folder containing the HTML files in the editor pointed to by the' - ' EDITOR environment variable.') + 'conversion).') parser.add_option('--test-build', help='Test binary modules in build', action='store_true', default=False) diff --git a/src/calibre/ebooks/tweak.py b/src/calibre/ebooks/tweak.py index 72e4c0a56c..8ff2e5e950 100644 --- a/src/calibre/ebooks/tweak.py +++ b/src/calibre/ebooks/tweak.py @@ -7,11 +7,11 @@ __license__ = 'GPL v3' __copyright__ = '2012, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import sys, os, shlex, subprocess +import sys, os, shlex, subprocess, shutil from calibre import prints, as_unicode, walk from calibre.constants import iswindows, __appname__ -from calibre.ptempfile import TemporaryDirectory +from calibre.ptempfile import TemporaryDirectory, TemporaryFile from calibre.libunzip import extract as zipextract from calibre.utils.zipfile import ZipFile, ZIP_DEFLATED, ZIP_STORED from calibre.utils.ipc.simple_worker import WorkerError @@ -108,21 +108,32 @@ def tweak(ebook_file): # The question was answered with No return - ed = os.environ.get('EDITOR', None) + ed = os.environ.get('EDITOR', 'dummy') + cmd = shlex.split(ed) + isvim = bool([x for x in cmd[0].split('/') if x.endswith('vim')]) + proceed = False - if ed is None: - prints('Book extracted to', tdir) + prints('Book extracted to', tdir) + + if not isvim: prints('Make your tweaks and once you are done,', __appname__, 'will rebuild', ebook_file, 'from', tdir) print() proceed = ask_cli_question('Rebuild ' + ebook_file + '?') else: - cmd = shlex.split(ed) - try: - subprocess.check_call(cmd + [tdir]) - except: - prints(ed, 'failed, aborting...') - raise SystemExit(1) + base = os.path.basename(ebook_file) + with TemporaryFile(base+'.zip') as zipf: + with ZipFile(zipf, 'w') as zf: + zf.add_dir(tdir) + try: + subprocess.check_call(cmd + [zipf]) + except: + prints(ed, 'failed, aborting...') + raise SystemExit(1) + with ZipFile(zipf, 'r') as zf: + shutil.rmtree(tdir) + os.mkdir(tdir) + zf.extractall(path=tdir) proceed = True if proceed: