A spot of refactoring

This commit is contained in:
Kovid Goyal 2017-05-03 13:46:47 +05:30
parent 45e45cfb38
commit 235227c544
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 35 additions and 29 deletions

View File

@ -12,6 +12,36 @@ from calibre.constants import iswindows
from calibre import prints 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(): def option_parser():
parser = OptionParser(usage=_('''\ parser = OptionParser(usage=_('''\
{0} {0}

View File

@ -349,38 +349,14 @@ class GuiRunner(QObject):
self.initialize_db() self.initialize_db()
def get_debug_executable(): def run_in_debug_mode():
e = sys.executable if getattr(sys, 'frozen', False) else sys.argv[0] from calibre.debug import run_calibre_debug
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):
import tempfile, subprocess import tempfile, subprocess
fd, logpath = tempfile.mkstemp('.txt') fd, logpath = tempfile.mkstemp('.txt')
os.close(fd) os.close(fd)
run_calibre_debug(
exe = get_debug_executable() '--gui-debug', logpath, stdout=lopen(logpath, 'w'),
print 'Starting debug executable:', exe stderr=subprocess.STDOUT, stdin=lopen(os.devnull, 'r'))
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)
def shellquote(s): def shellquote(s):