Exemple #1
0
def restart_after_quit():
    e = sys.executable if getattr(sys, 'frozen', False) else sys.argv[0]
    is_calibre_debug_exe = os.path.splitext(e)[0].endswith('-debug')
    if iswindows and not is_calibre_debug_exe:
        # detach the stdout/stderr/stdin handles
        winutil.prepare_for_restart()
    if after_quit_actions['no_plugins_on_restart']:
        os.environ['CALIBRE_IGNORE_PLUGINS_ON_RESTART'] = '1'
    if after_quit_actions['debug_on_restart']:
        run_in_debug_mode()
        return
    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
        if hasattr(sys, 'run_local'):
            cmd = [sys.run_local]
            if DEBUG:
                cmd += ['calibre-debug', '-g']
            else:
                cmd.append('calibre')
        else:
            cmd = [e]
            if is_calibre_debug_exe:
                cmd.append('-g')
        prints('Restarting with:', ' '.join(cmd))
        subprocess.Popen(cmd)
Exemple #2
0
def windows_repair(library_path=None):
    import subprocess
    from calibre.utils.serialize import json_dumps, json_loads
    from polyglot.binary import as_hex_unicode, from_hex_bytes
    if library_path:
        library_path = as_hex_unicode(json_dumps(library_path))
        winutil.prepare_for_restart()
        os.environ['CALIBRE_REPAIR_CORRUPTED_DB'] = environ_item(library_path)
        subprocess.Popen([sys.executable])
    else:
        try:
            app = Application([])
            from calibre.gui2.dialogs.restore_library import repair_library_at
            library_path = json_loads(
                from_hex_bytes(os.environ.pop('CALIBRE_REPAIR_CORRUPTED_DB')))
            done = repair_library_at(library_path, wait_time=4)
        except Exception:
            done = False
            error_dialog(
                None,
                _('Failed to repair library'),
                _('Could not repair library. Click "Show details" for more information.'
                  ),
                det_msg=traceback.format_exc(),
                show=True)
        if done:
            subprocess.Popen([sys.executable])
        app.quit()