From ee3fdcba3d3b247c7bedfbc7e7054e6e19a5133e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 22 Jun 2016 07:17:52 +0530 Subject: [PATCH] Allow launching of calibre worker process in-tree --- setup/__init__.py | 4 +++- setup/run-calibre-worker.py | 17 +++++++++++++++++ src/calibre/utils/ipc/launch.py | 6 ++++-- src/calibre/utils/ipc/simple_worker.py | 14 +++++++------- 4 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 setup/run-calibre-worker.py diff --git a/setup/__init__.py b/setup/__init__.py index ce06aab9a1..b54c09efed 100644 --- a/setup/__init__.py +++ b/setup/__init__.py @@ -16,10 +16,12 @@ isnetbsd = 'netbsd' in sys.platform isdragonflybsd = 'dragonfly' in sys.platform isbsd = isnetbsd or isfreebsd or isdragonflybsd islinux = not isosx and not iswindows and not isbsd -SRC = os.path.abspath('src') +sys.setup_dir = os.path.dirname(os.path.abspath(__file__)) +SRC = os.path.abspath(os.path.join(os.path.dirname(sys.setup_dir), 'src')) sys.path.insert(0, SRC) sys.resources_location = os.path.join(os.path.dirname(SRC), 'resources') sys.extensions_location = os.path.join(SRC, 'calibre', 'plugins') +sys.running_from_setup = True __version__ = __appname__ = modules = functions = basenames = scripts = None diff --git a/setup/run-calibre-worker.py b/setup/run-calibre-worker.py new file mode 100644 index 0000000000..dfb6dcf1fd --- /dev/null +++ b/setup/run-calibre-worker.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +# License: GPLv3 Copyright: 2016, Kovid Goyal + +from __future__ import (unicode_literals, division, absolute_import, + print_function) +import sys, os +sys.setup_dir = os.path.dirname(os.path.abspath(__file__)) +SRC = os.path.abspath(os.path.join(os.path.dirname(sys.setup_dir), 'src')) +sys.path.insert(0, SRC) +sys.resources_location = os.path.join(os.path.dirname(SRC), 'resources') +sys.extensions_location = os.path.join(SRC, 'calibre', 'plugins') +sys.running_from_setup = True + +from calibre.utils.ipc.worker import main +print(111111111111) +sys.exit(main()) diff --git a/src/calibre/utils/ipc/launch.py b/src/calibre/utils/ipc/launch.py index 2c3352d15d..2cb4ee3300 100644 --- a/src/calibre/utils/ipc/launch.py +++ b/src/calibre/utils/ipc/launch.py @@ -55,6 +55,8 @@ class Worker(object): @property def executable(self): + if hasattr(sys, 'running_from_setup'): + return [sys.executable, os.path.join(sys.setup_dir, 'run-calibre-worker.py')] e = self.exe_name if iswindows: return os.path.join(os.path.dirname(sys.executable), @@ -73,7 +75,7 @@ class Worker(object): @property def gui_executable(self): - if isosx: + if isosx and not hasattr(sys, 'running_from_setup'): if self.job_name in {'ebook-viewer', 'ebook-edit'}: return self.executable.replace('/console.app/', '/%s.app/' % self.job_name) return os.path.join(sys.binaries_path, self.exe_name) @@ -181,7 +183,7 @@ class Worker(object): _cwd = cwd if priority is None: priority = prefs['worker_process_priority'] - cmd = [exe] + cmd = [exe] if isinstance(exe, basestring) else exe args = { 'env' : env, 'cwd' : _cwd, diff --git a/src/calibre/utils/ipc/simple_worker.py b/src/calibre/utils/ipc/simple_worker.py index 5d23118731..72cb23fcbb 100644 --- a/src/calibre/utils/ipc/simple_worker.py +++ b/src/calibre/utils/ipc/simple_worker.py @@ -123,12 +123,10 @@ def create_worker(env, priority='normal', cwd=None, func='main'): env = dict(env) env.update({ - 'CALIBRE_WORKER_ADDRESS' : - hexlify(cPickle.dumps(listener.address, -1)), - 'CALIBRE_WORKER_KEY' : hexlify(auth_key), - 'CALIBRE_SIMPLE_WORKER': - 'calibre.utils.ipc.simple_worker:%s' % func, - }) + 'CALIBRE_WORKER_ADDRESS': hexlify(cPickle.dumps(listener.address, -1)), + 'CALIBRE_WORKER_KEY': hexlify(auth_key), + 'CALIBRE_SIMPLE_WORKER': 'calibre.utils.ipc.simple_worker:%s' % func, + }) w = Worker(env) w(cwd=cwd, priority=priority) @@ -157,7 +155,9 @@ def start_pipe_worker(command, env=None, priority='normal', **process_args): args['preexec_fn'] = partial(renice, niceness) args['close_fds'] = True - p = subprocess.Popen([w.executable, '--pipe-worker', command], **args) + exe = w.executable + cmd = [exe] if isinstance(exe, basestring) else exe + p = subprocess.Popen(cmd + ['--pipe-worker', command], **args) return p def fork_job(mod_name, func_name, args=(), kwargs={}, timeout=300, # seconds