home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1400 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.3 KB  |  174 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. from calibre.constants import iswindows, isosx, isfrozen, isnewosx
  13. from calibre.utils.config import prefs
  14. from calibre.ptempfile import PersistentTemporaryFile
  15. if iswindows:
  16.     import win32process
  17.     _windows_null_file = open(os.devnull, 'wb')
  18.  
  19.  
  20. class Worker(object):
  21.     
  22.     def osx_interpreter(self):
  23.         exe = os.path.basename(sys.executable)
  24.         if 'python' in exe:
  25.             return exe
  26.         return 'python'
  27.  
  28.     osx_interpreter = property(osx_interpreter)
  29.     
  30.     def osx_contents_dir(self):
  31.         fd = os.path.realpath(getattr(sys, 'frameworks_dir'))
  32.         return os.path.dirname(fd)
  33.  
  34.     osx_contents_dir = property(osx_contents_dir)
  35.     
  36.     def executable(self):
  37.         if iswindows:
  38.             return None(os.path.join, os.path.dirname(sys.executable) if isfrozen else 'Scripts\\calibre-parallel.exe')
  39.         if isnewosx:
  40.             return os.path.join(sys.console_binaries_path, 'calibre-parallel')
  41.         if isosx:
  42.             if not isfrozen:
  43.                 return 'calibre-parallel'
  44.             contents = os.path.join(self.osx_contents_dir, 'console.app', 'Contents')
  45.             return os.path.join(contents, 'MacOS', self.osx_interpreter)
  46.         if isfrozen:
  47.             return os.path.join(getattr(sys, 'frozen_path'), 'calibre-parallel')
  48.         c = os.path.join(sys.executables_location, 'calibre-parallel')
  49.         if os.access(c, os.X_OK):
  50.             return c
  51.         return 'calibre-parallel'
  52.  
  53.     executable = property(executable)
  54.     
  55.     def gui_executable(self):
  56.         if isnewosx:
  57.             return os.path.join(sys.binaries_path, 'calibre-parallel')
  58.         if isfrozen and isosx:
  59.             return os.path.join(self.osx_contents_dir, 'MacOS', self.osx_interpreter)
  60.         return self.executable
  61.  
  62.     gui_executable = property(gui_executable)
  63.     
  64.     def env(self):
  65.         env = dict(os.environ)
  66.         env['CALIBRE_WORKER'] = '1'
  67.         env.update(self._env)
  68.         return env
  69.  
  70.     env = property(env)
  71.     
  72.     def is_alive(self):
  73.         if hasattr(self, 'child'):
  74.             pass
  75.         return self.child.poll() is None
  76.  
  77.     is_alive = property(is_alive)
  78.     
  79.     def returncode(self):
  80.         if not hasattr(self, 'child'):
  81.             return None
  82.         self.child.poll()
  83.         return self.child.returncode
  84.  
  85.     returncode = property(returncode)
  86.     
  87.     def kill(self):
  88.         
  89.         try:
  90.             if self.is_alive:
  91.                 if iswindows:
  92.                     return self.child.kill()
  93.                 
  94.                 try:
  95.                     self.child.terminate()
  96.                     st = time.time()
  97.                     while self.is_alive and time.time() - st < 2:
  98.                         time.sleep(0.2)
  99.                         continue
  100.                         iswindows
  101.                 finally:
  102.                     pass
  103.  
  104.         except:
  105.             pass
  106.  
  107.  
  108.     
  109.     def __init__(self, env, gui = False):
  110.         self._env = { }
  111.         self.gui = gui
  112.         if isfrozen:
  113.             if not iswindows:
  114.                 pass
  115.             if not isosx:
  116.                 self._env['LD_LIBRARY_PATH'] = getattr(sys, 'frozen_path') + ':' + os.environ.get('LD_LIBRARY_PATH', '')
  117.             
  118.         self._env.update(env)
  119.  
  120.     
  121.     def __call__(self, redirect_output = True, cwd = None, priority = None):
  122.         exe = None if self.gui else self.executable
  123.         env = self.env
  124.         if not cwd:
  125.             pass
  126.         env['ORIGWD'] = os.path.abspath(os.getcwd())
  127.         _cwd = cwd
  128.         if isfrozen and not iswindows and not isosx:
  129.             _cwd = getattr(sys, 'frozen_path', None)
  130.         
  131.         if priority is None:
  132.             priority = prefs['worker_process_priority']
  133.         
  134.         cmd = [
  135.             exe]
  136.         if isosx and not isnewosx:
  137.             cmd += [
  138.                 '-c',
  139.                 self.osx_prefix + 'from calibre.utils.ipc.worker import main; main()']
  140.         
  141.         args = {
  142.             'env': env,
  143.             'cwd': _cwd }
  144.         if iswindows:
  145.             priority = {
  146.                 'high': win32process.HIGH_PRIORITY_CLASS,
  147.                 'normal': win32process.NORMAL_PRIORITY_CLASS,
  148.                 'low': win32process.IDLE_PRIORITY_CLASS }[priority]
  149.             args['creationflags'] = win32process.CREATE_NO_WINDOW | priority
  150.         
  151.         ret = None
  152.         if redirect_output:
  153.             self._file = PersistentTemporaryFile('_worker_redirect.log')
  154.             args['stdout'] = self._file._fd
  155.             args['stderr'] = subprocess.STDOUT
  156.             if iswindows:
  157.                 args['stdin'] = subprocess.PIPE
  158.             
  159.             ret = self._file.name
  160.         
  161.         if iswindows and 'stdin' not in args:
  162.             args['stdin'] = subprocess.PIPE
  163.             args['stdout'] = _windows_null_file
  164.             args['stderr'] = subprocess.STDOUT
  165.         
  166.         self.child = subprocess.Popen(cmd, **args)
  167.         if 'stdin' in args:
  168.             self.child.stdin.close()
  169.         
  170.         self.log_path = ret
  171.         return ret
  172.  
  173.  
  174.