Better error message when startup fails because of singleinstance failing

This commit is contained in:
Kovid Goyal 2014-12-19 09:38:49 +05:30
parent 99b8a76b00
commit 6703980121
2 changed files with 20 additions and 6 deletions

View File

@ -378,8 +378,11 @@ def run_gui(opts, args, listener, app, gui_debug=None):
open_local_file(debugfile)
return ret
singleinstance_name = 'calibre_GUI'
def cant_start(msg=_('If you are sure it is not running')+', ',
det_msg=_('Timed out waiting for response from running calibre')):
det_msg=_('Timed out waiting for response from running calibre'),
listener_failed=False):
base = '<p>%s</p><p>%s %s'
where = __appname__ + ' '+_('may be running in the system tray, in the')+' '
if isosx:
@ -388,8 +391,17 @@ def cant_start(msg=_('If you are sure it is not running')+', ',
where += _('lower right region of the screen.')
if iswindows or islinux:
what = _('try rebooting your computer.')
if islinux and not listener_failed:
from calibre.utils.lock import singleinstance_path
path = singleinstance_path(singleinstance_name)
what = _('try deleting the file: "%s"') % path
else:
what = _('try deleting the file')+': '+ gui_socket_address()
if listener_failed:
path = gui_socket_address()
else:
from calibre.utils.lock import singleinstance_path
path = singleinstance_path(singleinstance_name)
what = _('try deleting the file: "%s"') % path
info = base%(where, msg, what)
error_dialog(None, _('Cannot Start ')+__appname__,
@ -416,7 +428,7 @@ def shutdown_other(rc=None):
rc.conn.send('shutdown:')
prints(_('Shutdown command sent, waiting for shutdown...'))
for i in xrange(50):
if singleinstance('calibre GUI'):
if singleinstance(singleinstance_name):
return
time.sleep(0.1)
prints(_('Failed to shutdown running calibre instance'))
@ -460,13 +472,13 @@ def main(args=sys.argv):
listener = create_listener()
except socket.error:
if iswindows or islinux:
cant_start(det_msg=traceback.format_exc())
cant_start(det_msg=traceback.format_exc(), listener_failed=True)
if os.path.exists(gui_socket_address()):
os.remove(gui_socket_address())
try:
listener = create_listener()
except socket.error:
cant_start(det_msg=traceback.format_exc())
cant_start(det_msg=traceback.format_exc(), listener_failed=True)
else:
return run_gui(opts, args, listener, app,
gui_debug=gui_debug)

View File

@ -191,6 +191,8 @@ def _clean_lock_file(file):
except:
pass
def singleinstance_path(name):
return os.path.expanduser('~/.'+__appname__+'_'+name+'.lock')
def singleinstance(name):
'''
@ -211,7 +213,7 @@ def singleinstance(name):
atexit.register(win32api.CloseHandle, mutex)
return not err == winerror.ERROR_ALREADY_EXISTS
else:
path = os.path.expanduser('~/.'+__appname__+'_'+name+'.lock')
path = singleinstance_path(name)
try:
f = open(path, 'w')
fcntl.lockf(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)