home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from __future__ import with_statement
- __license__ = 'GPL v3'
- __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
- __docformat__ = 'restructuredtext en'
- import subprocess
- import os
- import sys
- import time
- from calibre.constants import iswindows, isosx, isfrozen, isnewosx
- from calibre.utils.config import prefs
- from calibre.ptempfile import PersistentTemporaryFile
- if iswindows:
- import win32process
- _windows_null_file = open(os.devnull, 'wb')
-
-
- class Worker(object):
-
- def osx_interpreter(self):
- exe = os.path.basename(sys.executable)
- if 'python' in exe:
- return exe
- return 'python'
-
- osx_interpreter = property(osx_interpreter)
-
- def osx_contents_dir(self):
- fd = os.path.realpath(getattr(sys, 'frameworks_dir'))
- return os.path.dirname(fd)
-
- osx_contents_dir = property(osx_contents_dir)
-
- def executable(self):
- if iswindows:
- return None(os.path.join, os.path.dirname(sys.executable) if isfrozen else 'Scripts\\calibre-parallel.exe')
- if isnewosx:
- return os.path.join(sys.console_binaries_path, 'calibre-parallel')
- if isosx:
- if not isfrozen:
- return 'calibre-parallel'
- contents = os.path.join(self.osx_contents_dir, 'console.app', 'Contents')
- return os.path.join(contents, 'MacOS', self.osx_interpreter)
- if isfrozen:
- return os.path.join(getattr(sys, 'frozen_path'), 'calibre-parallel')
- c = os.path.join(sys.executables_location, 'calibre-parallel')
- if os.access(c, os.X_OK):
- return c
- return 'calibre-parallel'
-
- executable = property(executable)
-
- def gui_executable(self):
- if isnewosx:
- return os.path.join(sys.binaries_path, 'calibre-parallel')
- if isfrozen and isosx:
- return os.path.join(self.osx_contents_dir, 'MacOS', self.osx_interpreter)
- return self.executable
-
- gui_executable = property(gui_executable)
-
- def env(self):
- env = dict(os.environ)
- env['CALIBRE_WORKER'] = '1'
- env.update(self._env)
- return env
-
- env = property(env)
-
- def is_alive(self):
- if hasattr(self, 'child'):
- pass
- return self.child.poll() is None
-
- is_alive = property(is_alive)
-
- def returncode(self):
- if not hasattr(self, 'child'):
- return None
- self.child.poll()
- return self.child.returncode
-
- returncode = property(returncode)
-
- def kill(self):
-
- try:
- if self.is_alive:
- if iswindows:
- return self.child.kill()
-
- try:
- self.child.terminate()
- st = time.time()
- while self.is_alive and time.time() - st < 2:
- time.sleep(0.2)
- continue
- iswindows
- finally:
- pass
-
- except:
- pass
-
-
-
- def __init__(self, env, gui = False):
- self._env = { }
- self.gui = gui
- if isfrozen:
- if not iswindows:
- pass
- if not isosx:
- self._env['LD_LIBRARY_PATH'] = getattr(sys, 'frozen_path') + ':' + os.environ.get('LD_LIBRARY_PATH', '')
-
- self._env.update(env)
-
-
- def __call__(self, redirect_output = True, cwd = None, priority = None):
- exe = None if self.gui else self.executable
- env = self.env
- if not cwd:
- pass
- env['ORIGWD'] = os.path.abspath(os.getcwd())
- _cwd = cwd
- if isfrozen and not iswindows and not isosx:
- _cwd = getattr(sys, 'frozen_path', None)
-
- if priority is None:
- priority = prefs['worker_process_priority']
-
- cmd = [
- exe]
- if isosx and not isnewosx:
- cmd += [
- '-c',
- self.osx_prefix + 'from calibre.utils.ipc.worker import main; main()']
-
- args = {
- 'env': env,
- 'cwd': _cwd }
- if iswindows:
- priority = {
- 'high': win32process.HIGH_PRIORITY_CLASS,
- 'normal': win32process.NORMAL_PRIORITY_CLASS,
- 'low': win32process.IDLE_PRIORITY_CLASS }[priority]
- args['creationflags'] = win32process.CREATE_NO_WINDOW | priority
-
- ret = None
- if redirect_output:
- self._file = PersistentTemporaryFile('_worker_redirect.log')
- args['stdout'] = self._file._fd
- args['stderr'] = subprocess.STDOUT
- if iswindows:
- args['stdin'] = subprocess.PIPE
-
- ret = self._file.name
-
- if iswindows and 'stdin' not in args:
- args['stdin'] = subprocess.PIPE
- args['stdout'] = _windows_null_file
- args['stderr'] = subprocess.STDOUT
-
- self.child = subprocess.Popen(cmd, **args)
- if 'stdin' in args:
- self.child.stdin.close()
-
- self.log_path = ret
- return ret
-
-
-