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 run_in_debug_mode():
    from calibre.debug import run_calibre_debug
    import tempfile, subprocess
    fd, logpath = tempfile.mkstemp('.txt')
    os.close(fd)
    run_calibre_debug(
        '--gui-debug', logpath, stdout=lopen(logpath, 'wb'),
        stderr=subprocess.STDOUT, stdin=lopen(os.devnull, 'rb'))
Exemple #3
0
def run_in_debug_mode():
    from calibre.debug import run_calibre_debug
    import tempfile, subprocess
    fd, logpath = tempfile.mkstemp('.txt')
    os.close(fd)
    run_calibre_debug(
        '--gui-debug', logpath, stdout=lopen(logpath, 'w'),
        stderr=subprocess.STDOUT, stdin=lopen(os.devnull, 'r'))
Exemple #4
0
def run_in_debug_mode():
    from calibre.debug import run_calibre_debug
    import tempfile, subprocess
    fd, logpath = tempfile.mkstemp('.txt')
    os.close(fd)
    os.environ[b'CALIBRE_RESTARTING_FROM_GUI'] = b'1'
    run_calibre_debug(
        '--gui-debug', logpath, stdout=lopen(logpath, 'w'),
        stderr=subprocess.STDOUT, stdin=lopen(os.devnull, 'r'))
Exemple #5
0
def run_in_debug_mode():
    from calibre.debug import run_calibre_debug
    import tempfile, subprocess
    fd, logpath = tempfile.mkstemp('.txt')
    os.close(fd)
    os.environ[b'CALIBRE_RESTARTING_FROM_GUI'] = b'1'
    run_calibre_debug(
        '--gui-debug', logpath, stdout=lopen(logpath, 'w'),
        stderr=subprocess.STDOUT, stdin=lopen(os.devnull, 'r'))
Exemple #6
0
def compile_pyj(data,
                filename='<stdin>',
                beautify=True,
                private_scope=True,
                libdir=None,
                omit_baselib=False,
                js_version=5,
                add_call_site_to_functions=''):
    if isinstance(data, bytes):
        data = data.decode('utf-8')
    options = {
        'beautify': beautify,
        'private_scope': private_scope,
        'keep_baselib': not omit_baselib,
        'filename': filename,
        'js_version': js_version,
        'add_call_site_to_functions': '',
    }
    if not ok_to_import_webengine():
        from calibre.debug import run_calibre_debug
        p = run_calibre_debug(
            '-c',
            'from calibre.utils.rapydscript import *; forked_compile()',
            json.dumps(options),
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE)
        stdout = p.communicate(as_bytes(data))[0]
        if p.wait() != 0:
            raise SystemExit(p.returncode)
        idx = stdout.find(OUTPUT_SENTINEL)
        result = as_unicode(stdout[idx + len(OUTPUT_SENTINEL):])
    else:
        c = compiler()
        result = c(data, options)
    return result
Exemple #7
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