def ensure_single_instance(args, open_at): try: from calibre.utils.lock import singleinstance si = singleinstance(singleinstance_name) except Exception: import traceback error_dialog(None, _('Cannot start viewer'), _( 'Failed to start viewer, could not insure only a single instance of the viewer is running. Click "Show Details" for more information'), det_msg=traceback.format_exc(), show=True) raise SystemExit(1) if not si: if len(args) > 1: t = RC(print_error=True, socket_address=viewer_socket_address()) t.start() t.join(3.0) if t.is_alive() or t.conn is None: error_dialog(None, _('Connect to viewer failed'), _( 'Unable to connect to existing viewer window, try restarting the viewer.'), show=True) raise SystemExit(1) t.conn.send((os.path.abspath(args[1]), open_at)) t.conn.close() prints('Opened book in existing viewer instance') raise SystemExit(0) listener = create_listener() return listener
def ensure_single_instance(): if 'CALIBRE_NO_SI_DANGER_DANGER' not in os.environ and not singleinstance('db'): ext = '.exe' if iswindows else '' raise SystemExit( _( 'Another calibre program such as another instance of {} or the main' ' calibre program is running. Having multiple programs that can make' ' changes to a calibre library running at the same time is not supported.' ).format('calibre-server' + ext))
def ensure_single_instance(): if b'CALIBRE_NO_SI_DANGER_DANGER' not in os.environ and not singleinstance('db'): ext = '.exe' if iswindows else '' raise SystemExit( _( 'Another calibre program such as another instance of {} or the main' ' calibre program is running. Having multiple programs that can make' ' changes to a calibre library running at the same time is not supported.' ).format('calibre-server' + ext))
def main(args=sys.argv): gui_debug = None if args[0] == '__CALIBRE_GUI_DEBUG__': gui_debug = args[1] args = ['calibre'] try: app, opts, args = init_qt(args) except AbortInit: return 1 try: from calibre.utils.lock import singleinstance si = singleinstance(singleinstance_name) except Exception: error_dialog( None, _('Cannot start calibre'), _('Failed to start calibre, single instance locking failed. Click "Show Details" for more information' ), det_msg=traceback.format_exc(), show=True) return 1 if si and opts.shutdown_running_calibre: return 0 if si: try: listener = create_listener() except socket.error: if iswindows or islinux: 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(), listener_failed=True) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) otherinstance = False try: listener = create_listener() except socket.error: # Good singleinstance is correct (on UNIX) otherinstance = True else: # On windows only singleinstance can be trusted otherinstance = True if iswindows else False if not otherinstance and not opts.shutdown_running_calibre: return run_gui(opts, args, listener, app, gui_debug=gui_debug) communicate(opts, args) return 0
def main(args=sys.argv): gui_debug = None if args[0] == '__CALIBRE_GUI_DEBUG__': gui_debug = args[1] args = ['calibre'] try: app, opts, args, actions = init_qt(args) except AbortInit: return 1 from calibre.utils.lock import singleinstance from multiprocessing.connection import Listener si = singleinstance('calibre GUI') if si and opts.shutdown_running_calibre: return 0 if si: try: listener = Listener(address=gui_socket_address()) except socket.error: if iswindows: cant_start() if os.path.exists(gui_socket_address()): os.remove(gui_socket_address()) try: listener = Listener(address=gui_socket_address()) except socket.error: cant_start() else: return run_gui(opts, args, actions, listener, app, gui_debug=gui_debug) else: return run_gui(opts, args, actions, listener, app, gui_debug=gui_debug) otherinstance = False try: listener = Listener(address=gui_socket_address()) except socket.error: # Good si is correct (on UNIX) otherinstance = True else: # On windows only singleinstance can be trusted otherinstance = True if iswindows else False if not otherinstance and not opts.shutdown_running_calibre: return run_gui(opts, args, actions, listener, app, gui_debug=gui_debug) communicate(opts, args) return 0
def main(args=sys.argv): gui_debug = None if args[0] == '__CALIBRE_GUI_DEBUG__': gui_debug = args[1] args = ['calibre'] try: app, opts, args = init_qt(args) except AbortInit: return 1 try: from calibre.utils.lock import singleinstance si = singleinstance(singleinstance_name) except Exception: error_dialog(None, _('Cannot start calibre'), _( 'Failed to start calibre, single instance locking failed. Click "Show Details" for more information'), det_msg=traceback.format_exc(), show=True) return 1 if si and opts.shutdown_running_calibre: return 0 if si: try: listener = create_listener() except socket.error: if iswindows or islinux: 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(), listener_failed=True) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) otherinstance = False try: listener = create_listener() except socket.error: # Good singleinstance is correct (on UNIX) otherinstance = True else: # On windows only singleinstance can be trusted otherinstance = True if iswindows else False if not otherinstance and not opts.shutdown_running_calibre: return run_gui(opts, args, listener, app, gui_debug=gui_debug) communicate(opts, args) return 0
def shutdown_other(rc=None): if rc is None: rc = build_pipe(print_error=False) if rc.conn is None: prints(_('No running calibre found')) return # No running instance found rc.conn.send(b'shutdown:') prints(_('Shutdown command sent, waiting for shutdown...')) for i in range(50): if singleinstance(singleinstance_name): return time.sleep(0.1) prints(_('Failed to shutdown running calibre instance')) raise SystemExit(1)
def do_register(self): try: check_allowed() except NotAllowed: return if singleinstance('register_default_programs'): if self.prefs.get('windows_register_default_programs', None) != __version__: self.prefs['windows_register_default_programs'] = __version__ if DEBUG: st = time.monotonic() prints('Registering with default programs...') register() if DEBUG: prints('Registered with default programs in %.1f seconds' % (time.monotonic() - st))
def shutdown_other(rc=None): if rc is None: rc = build_pipe(print_error=False) if rc.conn is None: prints(_('No running calibre found')) return # No running instance found rc.conn.send('shutdown:') prints(_('Shutdown command sent, waiting for shutdown...')) for i in xrange(50): if singleinstance(singleinstance_name): return time.sleep(0.1) prints(_('Failed to shutdown running calibre instance')) raise SystemExit(1)
def do_register(self): from calibre.utils.lock import singleinstance try: check_allowed() except NotAllowed: return if singleinstance('register_default_programs'): if self.prefs.get('windows_register_default_programs', None) != __version__: self.prefs['windows_register_default_programs'] = __version__ if DEBUG: st = time.time() prints('Registering with default programs...') register() if DEBUG: prints('Registered with default programs in %.1f seconds' % (time.time() - st))
def shutdown_other(rc=None): if rc is None: rc = build_pipe(print_error=False) if rc.conn is None: prints(_("No running calibre found")) return # No running instance found from calibre.utils.lock import singleinstance rc.conn.send("shutdown:") prints(_("Shutdown command sent, waiting for shutdown...")) for i in xrange(50): if singleinstance("calibre GUI"): return time.sleep(0.1) prints(_("Failed to shutdown running calibre instance")) raise SystemExit(1)
def __init__(self, opts): self.library_path = opts.library_path or prefs['library_path'] self.timeout = opts.timeout self.url = None if self.library_path is None: raise SystemExit( 'No saved library path, either run the GUI or use the' ' --with-library option') if self.library_path.partition(':')[0] in ('http', 'https'): parts = urlparse(self.library_path) self.library_id = parts.fragment or None self.url = urlunparse(parts._replace(fragment='')).rstrip('/') self.br = browser(handle_refresh=False, user_agent='{} {}'.format( __appname__, __version__)) self.is_remote = True username, password = read_credentials(opts) self.has_credentials = False if username and password: self.br.add_password(self.url, username, password) self.has_credentials = True if self.library_id == '-': self.list_libraries() raise SystemExit() else: self.library_path = os.path.expanduser(self.library_path) if not singleinstance('db'): ext = '.exe' if iswindows else '' raise SystemExit( _('Another calibre program such as {} or the main calibre program is running.' ' Having multiple programs that can make changes to a calibre library' ' running at the same time is a bad idea. calibredb can connect directly' ' to a running calibre Content server, to make changes through it, instead.' ' See the documentation of the {} option for details.'). format('calibre-server' + ext, '--with-library')) self._db = None self.is_remote = False
def __init__(self, opts): self.library_path = opts.library_path or prefs['library_path'] self.url = None if self.library_path is None: raise SystemExit( 'No saved library path, either run the GUI or use the' ' --with-library option' ) if self.library_path.partition(':')[0] in ('http', 'https'): parts = urlparse(self.library_path) self.library_id = parts.fragment or None self.url = urlunparse(parts._replace(fragment='')).rstrip('/') self.br = browser(handle_refresh=False, user_agent='{} {}'.format(__appname__, __version__)) self.is_remote = True username, password = read_credentials(opts) self.has_credentials = False if username and password: self.br.add_password(self.url, username, password) self.has_credentials = True if self.library_id == '-': self.list_libraries() raise SystemExit() else: self.library_path = os.path.expanduser(self.library_path) if not singleinstance('db'): ext = '.exe' if iswindows else '' raise SystemExit(_( 'Another calibre program such as {} or the main calibre program is running.' ' Having multiple programs that can make changes to a calibre library' ' running at the same time is a bad idea. calibredb can connect directly' ' to a running calibre content server, to make changes through it, instead.' ' See the documentation of the {} option for details.' ).format('calibre-server' + ext, '--with-library') ) self._db = None self.is_remote = False
def main(args=sys.argv): if os.environ.pop(b'CALIBRE_RESTARTING_FROM_GUI', None) == b'1': time.sleep(2) # give the parent process time to cleanup and close if iswindows and 'CALIBRE_REPAIR_CORRUPTED_DB' in os.environ: windows_repair() return 0 gui_debug = None if args[0] == '__CALIBRE_GUI_DEBUG__': gui_debug = args[1] args = ['calibre'] if iswindows: # Ensure that all ebook editor instances are grouped together in the task # bar. This prevents them from being grouped with viewer process when # launched from within calibre, as both use calibre-parallel.exe set_app_uid(MAIN_APP_UID) try: app, opts, args = init_qt(args) except AbortInit: return 1 try: si = singleinstance(singleinstance_name) except Exception: error_dialog(None, _('Cannot start calibre'), _( 'Failed to start calibre, single instance locking failed. Click "Show Details" for more information'), det_msg=traceback.format_exc(), show=True) return 1 if si and opts.shutdown_running_calibre: return 0 if si: try: listener = create_listener() except socket.error: if iswindows or islinux: 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(), listener_failed=True) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) otherinstance = False try: listener = create_listener() except socket.error: # Good singleinstance is correct (on UNIX) otherinstance = True else: # On windows only singleinstance can be trusted otherinstance = True if iswindows else False if not otherinstance and not opts.shutdown_running_calibre: return run_gui(opts, args, listener, app, gui_debug=gui_debug) communicate(opts, args) return 0
def run_gui(opts, args, listener, app, gui_debug=None): si = singleinstance('db') if not si: ext = '.exe' if iswindows else '' error_dialog(None, _('Cannot start calibre'), _( 'Another calibre program that can modify calibre libraries, such as,' ' {} or {} is already running. You must first shut it down, before' ' starting the main calibre program. If you are sure no such' ' program is running, try restarting your computer.').format( 'calibre-server' + ext, 'calibredb' + ext), show=True) return 1 initialize_file_icon_provider() app.load_builtin_fonts(scan_for_fonts=True) if not dynamic.get('welcome_wizard_was_run', False): from calibre.gui2.wizard import wizard wizard().exec_() dynamic.set('welcome_wizard_was_run', True) from calibre.gui2.ui import Main if isosx: actions = tuple(Main.create_application_menubar()) else: actions = tuple(Main.get_menubar_actions()) runner = GuiRunner(opts, args, actions, listener, app, gui_debug=gui_debug) ret = app.exec_() if getattr(runner.main, 'run_wizard_b4_shutdown', False): from calibre.gui2.wizard import wizard wizard().exec_() if getattr(runner.main, 'restart_after_quit', False): e = sys.executable if getattr(sys, 'frozen', False) else sys.argv[0] if getattr(runner.main, 'debug_on_restart', False) or gui_debug is not None: run_in_debug_mode() else: import subprocess if hasattr(sys, 'frameworks_dir'): app = os.path.dirname(os.path.dirname(os.path.realpath(sys.frameworks_dir))) prints('Restarting with:', app) subprocess.Popen('sleep 3s; open ' + shellquote(app), shell=True) else: os.environ[b'CALIBRE_RESTARTING_FROM_GUI'] = b'1' if iswindows and hasattr(winutil, 'prepare_for_restart'): winutil.prepare_for_restart() args = ['-g'] if os.path.splitext(e)[0].endswith('-debug') else [] prints('Restarting with:', ' '.join([e] + args)) subprocess.Popen([e] + args) else: if iswindows: try: runner.main.system_tray_icon.hide() except: pass if getattr(runner.main, 'gui_debug', None) is not None: e = sys.executable if getattr(sys, 'frozen', False) else sys.argv[0] debugfile = runner.main.gui_debug from calibre.gui2 import open_local_file if iswindows: with open(debugfile, 'r+b') as f: raw = f.read() raw = re.sub(b'(?<!\r)\n', b'\r\n', raw) f.seek(0) f.truncate() f.write(raw) open_local_file(debugfile) return ret
def main(args=sys.argv): if os.environ.pop('CALIBRE_RESTARTING_FROM_GUI', None) == environ_item('1'): time.sleep(2) # give the parent process time to cleanup and close if iswindows and 'CALIBRE_REPAIR_CORRUPTED_DB' in os.environ: windows_repair() return 0 gui_debug = None if args[0] == '__CALIBRE_GUI_DEBUG__': gui_debug = args[1] args = ['calibre'] try: app, opts, args = init_qt(args) except AbortInit: return 1 try: si = singleinstance(singleinstance_name) except Exception: error_dialog( None, _('Cannot start calibre'), _('Failed to start calibre, single instance locking failed. Click "Show Details" for more information' ), det_msg=traceback.format_exc(), show=True) return 1 if si and opts.shutdown_running_calibre: return 0 if si: try: listener = create_listener() except socket.error: if iswindows or islinux: 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(), listener_failed=True) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) otherinstance = False try: listener = create_listener() except socket.error: # Good singleinstance is correct (on UNIX) otherinstance = True else: # On windows only singleinstance can be trusted otherinstance = True if iswindows else False if not otherinstance and not opts.shutdown_running_calibre: return run_gui(opts, args, listener, app, gui_debug=gui_debug) communicate(opts, args) return 0
def main(args=sys.argv): if iswindows and 'CALIBRE_REPAIR_CORRUPTED_DB' in os.environ: windows_repair() return 0 gui_debug = None if args[0] == '__CALIBRE_GUI_DEBUG__': gui_debug = args[1] args = ['calibre'] if iswindows: # Ensure that all ebook editor instances are grouped together in the task # bar. This prevents them from being grouped with viewer process when # launched from within calibre, as both use calibre-parallel.exe import ctypes try: ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('com.calibre-ebook.main-gui') except Exception: pass # Only available on windows 7 and newer try: app, opts, args = init_qt(args) except AbortInit: return 1 try: from calibre.utils.lock import singleinstance si = singleinstance(singleinstance_name) except Exception: error_dialog(None, _('Cannot start calibre'), _( 'Failed to start calibre, single instance locking failed. Click "Show Details" for more information'), det_msg=traceback.format_exc(), show=True) return 1 if si and opts.shutdown_running_calibre: return 0 if si: try: listener = create_listener() except socket.error: if iswindows or islinux: 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(), listener_failed=True) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) otherinstance = False try: listener = create_listener() except socket.error: # Good singleinstance is correct (on UNIX) otherinstance = True else: # On windows only singleinstance can be trusted otherinstance = True if iswindows else False if not otherinstance and not opts.shutdown_running_calibre: return run_gui(opts, args, listener, app, gui_debug=gui_debug) communicate(opts, args) return 0
def run_gui(opts, args, listener, app, gui_debug=None): si = singleinstance('db') if not si: ext = '.exe' if iswindows else '' error_dialog( None, _('Cannot start calibre'), _('Another calibre program that can modify calibre libraries, such as,' ' {} or {} is already running. You must first shut it down, before' ' starting the main calibre program. If you are sure no such' ' program is running, try restarting your computer.').format( 'calibre-server' + ext, 'calibredb' + ext), show=True) return 1 initialize_file_icon_provider() app.load_builtin_fonts(scan_for_fonts=True) if not dynamic.get('welcome_wizard_was_run', False): from calibre.gui2.wizard import wizard wizard().exec_() dynamic.set('welcome_wizard_was_run', True) from calibre.gui2.ui import Main if isosx: actions = tuple(Main.create_application_menubar()) else: actions = tuple(Main.get_menubar_actions()) runner = GuiRunner(opts, args, actions, listener, app, gui_debug=gui_debug) ret = app.exec_() if getattr(runner.main, 'run_wizard_b4_shutdown', False): from calibre.gui2.wizard import wizard wizard().exec_() if getattr(runner.main, 'restart_after_quit', False): e = sys.executable if getattr(sys, 'frozen', False) else sys.argv[0] if getattr(runner.main, 'debug_on_restart', False) or gui_debug is not None: run_in_debug_mode() else: if hasattr(sys, 'frameworks_dir'): app = os.path.dirname( os.path.dirname(os.path.realpath(sys.frameworks_dir))) from calibre.debug import run_calibre_debug prints('Restarting with:', app) run_calibre_debug( '-c', 'import sys, os, time; time.sleep(3); os.execlp("open", "open", sys.argv[-1])', app) else: import subprocess os.environ['CALIBRE_RESTARTING_FROM_GUI'] = environ_item('1') if iswindows and hasattr(winutil, 'prepare_for_restart'): winutil.prepare_for_restart() if hasattr(sys, 'run_local'): cmd = [sys.run_local] if DEBUG: cmd += ['calibre-debug', '-g'] else: cmd.append('calibre') else: args = [ '-g' ] if os.path.splitext(e)[0].endswith('-debug') else [] cmd = [e] + args prints('Restarting with:', ' '.join(cmd)) subprocess.Popen(cmd) else: if iswindows: try: runner.main.system_tray_icon.hide() except: pass if getattr(runner.main, 'gui_debug', None) is not None: e = sys.executable if getattr(sys, 'frozen', False) else sys.argv[0] debugfile = runner.main.gui_debug from calibre.gui2 import open_local_file if iswindows: with open(debugfile, 'r+b') as f: raw = f.read() raw = re.sub(b'(?<!\r)\n', b'\r\n', raw) f.seek(0) f.truncate() f.write(raw) open_local_file(debugfile) return ret
def main(args=sys.argv): if iswindows and 'CALIBRE_REPAIR_CORRUPTED_DB' in os.environ: windows_repair() return 0 gui_debug = None if args[0] == '__CALIBRE_GUI_DEBUG__': gui_debug = args[1] args = ['calibre'] if iswindows: # Ensure that all ebook editor instances are grouped together in the task # bar. This prevents them from being grouped with viewer process when # launched from within calibre, as both use calibre-parallel.exe import ctypes try: ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID( 'com.calibre-ebook.main-gui') except Exception: pass # Only available on windows 7 and newer try: app, opts, args = init_qt(args) except AbortInit: return 1 try: from calibre.utils.lock import singleinstance si = singleinstance(singleinstance_name) except Exception: error_dialog( None, _('Cannot start calibre'), _('Failed to start calibre, single instance locking failed. Click "Show Details" for more information' ), det_msg=traceback.format_exc(), show=True) return 1 if si and opts.shutdown_running_calibre: return 0 if si: try: listener = create_listener() except socket.error: if iswindows or islinux: 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(), listener_failed=True) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) else: return run_gui(opts, args, listener, app, gui_debug=gui_debug) otherinstance = False try: listener = create_listener() except socket.error: # Good singleinstance is correct (on UNIX) otherinstance = True else: # On windows only singleinstance can be trusted otherinstance = True if iswindows else False if not otherinstance and not opts.shutdown_running_calibre: return run_gui(opts, args, listener, app, gui_debug=gui_debug) communicate(opts, args) return 0