home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1464 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.8 KB  |  194 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import subprocess
  9. import os
  10. import sys
  11. import time
  12. import binascii
  13. import cPickle
  14. from calibre.constants import iswindows, isosx, isfrozen, isnewosx
  15. from calibre.utils.config import prefs
  16. from calibre.ptempfile import PersistentTemporaryFile, base_dir
  17. if iswindows:
  18.     import win32process
  19.     
  20.     try:
  21.         _windows_null_file = open(os.devnull, 'wb')
  22.     raise RuntimeError('NUL %r file missing in windows' % os.devnull)
  23.  
  24.  
  25.  
  26. class Worker(object):
  27.     exe_name = 'calibre-parallel'
  28.     
  29.     def osx_interpreter(self):
  30.         exe = os.path.basename(sys.executable)
  31.         if 'python' in exe:
  32.             return exe
  33.         return 'python'
  34.  
  35.     osx_interpreter = property(osx_interpreter)
  36.     
  37.     def osx_contents_dir(self):
  38.         fd = os.path.realpath(getattr(sys, 'frameworks_dir'))
  39.         return os.path.dirname(fd)
  40.  
  41.     osx_contents_dir = property(osx_contents_dir)
  42.     
  43.     def executable(self):
  44.         e = self.exe_name
  45.         if iswindows:
  46.             return None(os.path.join, os.path.dirname(sys.executable) if isfrozen else 'Scripts\\%s.exe' % e)
  47.         if isnewosx:
  48.             return os.path.join(sys.console_binaries_path, e)
  49.         if isosx:
  50.             if not isfrozen:
  51.                 return e
  52.             contents = os.path.join(self.osx_contents_dir, 'console.app', 'Contents')
  53.             return os.path.join(contents, 'MacOS', self.osx_interpreter)
  54.         if isfrozen:
  55.             return os.path.join(getattr(sys, 'frozen_path'), e)
  56.         c = os.path.join(sys.executables_location, e)
  57.         if os.access(c, os.X_OK):
  58.             return c
  59.         return e
  60.  
  61.     executable = property(executable)
  62.     
  63.     def gui_executable(self):
  64.         if isnewosx:
  65.             return os.path.join(sys.binaries_path, self.exe_name)
  66.         if isfrozen and isosx:
  67.             return os.path.join(self.osx_contents_dir, 'MacOS', self.osx_interpreter)
  68.         return self.executable
  69.  
  70.     gui_executable = property(gui_executable)
  71.     
  72.     def env(self):
  73.         env = dict(os.environ)
  74.         env['CALIBRE_WORKER'] = '1'
  75.         td = binascii.hexlify(cPickle.dumps(base_dir()))
  76.         env['CALIBRE_WORKER_TEMP_DIR'] = td
  77.         env.update(self._env)
  78.         return env
  79.  
  80.     env = property(env)
  81.     
  82.     def is_alive(self):
  83.         if hasattr(self, 'child'):
  84.             pass
  85.         return self.child.poll() is None
  86.  
  87.     is_alive = property(is_alive)
  88.     
  89.     def returncode(self):
  90.         if not hasattr(self, 'child'):
  91.             return None
  92.         self.child.poll()
  93.         return self.child.returncode
  94.  
  95.     returncode = property(returncode)
  96.     
  97.     def pid(self):
  98.         if not hasattr(self, 'child'):
  99.             return None
  100.         return getattr(self.child, 'pid', None)
  101.  
  102.     pid = property(pid)
  103.     
  104.     def kill(self):
  105.         
  106.         try:
  107.             if self.is_alive:
  108.                 if iswindows:
  109.                     return self.child.kill()
  110.                 
  111.                 try:
  112.                     self.child.terminate()
  113.                     st = time.time()
  114.                     while self.is_alive and time.time() - st < 2:
  115.                         time.sleep(0.2)
  116.                         continue
  117.                         iswindows
  118.                 finally:
  119.                     pass
  120.  
  121.         except:
  122.             pass
  123.  
  124.  
  125.     
  126.     def __init__(self, env, gui = False):
  127.         self._env = { }
  128.         self.gui = gui
  129.         if isfrozen:
  130.             if not iswindows:
  131.                 pass
  132.             if not isosx:
  133.                 self._env['LD_LIBRARY_PATH'] = getattr(sys, 'frozen_path') + ':' + os.environ.get('LD_LIBRARY_PATH', '')
  134.             
  135.         self._env.update(env)
  136.  
  137.     
  138.     def __call__(self, redirect_output = True, cwd = None, priority = None):
  139.         exe = None if self.gui else self.executable
  140.         env = self.env
  141.         if not cwd:
  142.             pass
  143.         env['ORIGWD'] = os.path.abspath(os.getcwd())
  144.         _cwd = cwd
  145.         if isfrozen and not iswindows and not isosx:
  146.             _cwd = getattr(sys, 'frozen_path', None)
  147.         
  148.         if priority is None:
  149.             priority = prefs['worker_process_priority']
  150.         
  151.         cmd = [
  152.             exe]
  153.         if isosx and not isnewosx:
  154.             cmd += [
  155.                 '-c',
  156.                 self.osx_prefix + 'from calibre.utils.ipc.worker import main; main()']
  157.         
  158.         args = {
  159.             'env': env,
  160.             'cwd': _cwd }
  161.         if iswindows:
  162.             priority = {
  163.                 'high': win32process.HIGH_PRIORITY_CLASS,
  164.                 'normal': win32process.NORMAL_PRIORITY_CLASS,
  165.                 'low': win32process.IDLE_PRIORITY_CLASS }[priority]
  166.             args['creationflags'] = win32process.CREATE_NO_WINDOW | priority
  167.         
  168.         ret = None
  169.         if redirect_output:
  170.             self._file = PersistentTemporaryFile('_worker_redirect.log')
  171.             args['stdout'] = self._file._fd
  172.             args['stderr'] = subprocess.STDOUT
  173.             if iswindows:
  174.                 args['stdin'] = subprocess.PIPE
  175.             
  176.             ret = self._file.name
  177.         
  178.         if iswindows and 'stdin' not in args:
  179.             args['stdin'] = subprocess.PIPE
  180.             args['stdout'] = _windows_null_file
  181.             args['stderr'] = subprocess.STDOUT
  182.         
  183.         if not iswindows:
  184.             args['close_fds'] = True
  185.         
  186.         self.child = subprocess.Popen(cmd, **args)
  187.         if 'stdin' in args:
  188.             self.child.stdin.close()
  189.         
  190.         self.log_path = ret
  191.         return ret
  192.  
  193.  
  194.