From 235227c544c644e6ccfb7f3e2d7a767495eaaa09 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 3 May 2017 13:46:47 +0530 Subject: [PATCH] A spot of refactoring --- src/calibre/debug.py | 30 ++++++++++++++++++++++++++++++ src/calibre/gui2/main.py | 34 +++++----------------------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/calibre/debug.py b/src/calibre/debug.py index 6b67d1cf61..bf54dbb0ce 100644 --- a/src/calibre/debug.py +++ b/src/calibre/debug.py @@ -12,6 +12,36 @@ from calibre.constants import iswindows from calibre import prints +def get_debug_executable(): + e = sys.executable if getattr(sys, 'frozen', False) else sys.argv[0] + if hasattr(sys, 'frameworks_dir'): + base = os.path.dirname(sys.frameworks_dir) + if 'calibre-debug.app' not in base: + base = os.path.join(base, 'calibre-debug.app', 'Contents') + exe = os.path.basename(e) + if '-debug' not in exe: + exe += '-debug' + exe = os.path.join(base, 'MacOS', exe) + else: + exe = e + if '-debug' not in exe: + base, ext = os.path.splitext(e) + exe = base + '-debug' + ext + return exe + + +def run_calibre_debug(*args, **kw): + import subprocess + creationflags = 0 + if iswindows: + import win32process + creationflags = win32process.CREATE_NO_WINDOW + exe = get_debug_executable() + cmd = [exe] + list(args) + kw['creationflags'] = creationflags + subprocess.Popen(cmd, **kw) + + def option_parser(): parser = OptionParser(usage=_('''\ {0} diff --git a/src/calibre/gui2/main.py b/src/calibre/gui2/main.py index b6836d914f..9f1bcf3506 100644 --- a/src/calibre/gui2/main.py +++ b/src/calibre/gui2/main.py @@ -349,38 +349,14 @@ class GuiRunner(QObject): self.initialize_db() -def get_debug_executable(): - e = sys.executable if getattr(sys, 'frozen', False) else sys.argv[0] - if hasattr(sys, 'frameworks_dir'): - base = os.path.dirname(sys.frameworks_dir) - if 'calibre-debug.app' not in base: - base = os.path.join(base, 'calibre-debug.app', 'Contents') - exe = os.path.basename(e) - if '-debug' not in exe: - exe += '-debug' - exe = os.path.join(base, 'MacOS', exe) - else: - exe = e - if '-debug' not in exe: - base, ext = os.path.splitext(e) - exe = base + '-debug' + ext - return exe - - -def run_in_debug_mode(logpath=None): +def run_in_debug_mode(): + from calibre.debug import run_calibre_debug import tempfile, subprocess fd, logpath = tempfile.mkstemp('.txt') os.close(fd) - - exe = get_debug_executable() - print 'Starting debug executable:', exe - creationflags = 0 - if iswindows: - import win32process - creationflags = win32process.CREATE_NO_WINDOW - subprocess.Popen([exe, '--gui-debug', logpath], stdout=open(logpath, 'w'), - stderr=subprocess.STDOUT, stdin=open(os.devnull, 'r'), - creationflags=creationflags) + run_calibre_debug( + '--gui-debug', logpath, stdout=lopen(logpath, 'w'), + stderr=subprocess.STDOUT, stdin=lopen(os.devnull, 'r')) def shellquote(s):