home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_646 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  7.1 KB  |  245 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'Process',
  6.     'current_process',
  7.     'active_children']
  8. import os
  9. import sys
  10. import signal
  11. import itertools
  12.  
  13. try:
  14.     ORIGINAL_DIR = os.path.abspath(os.getcwd())
  15. except OSError:
  16.     ORIGINAL_DIR = None
  17.  
  18.  
  19. def current_process():
  20.     return _current_process
  21.  
  22.  
  23. def active_children():
  24.     _cleanup()
  25.     return list(_current_process._children)
  26.  
  27.  
  28. def _cleanup():
  29.     for p in list(_current_process._children):
  30.         if p._popen.poll() is not None:
  31.             _current_process._children.discard(p)
  32.             continue
  33.     
  34.  
  35.  
  36. class Process(object):
  37.     _Popen = None
  38.     
  39.     def __init__(self, group = None, target = None, name = None, args = (), kwargs = { }):
  40.         count = _current_process._counter.next()
  41.         self._identity = _current_process._identity + (count,)
  42.         self._authkey = _current_process._authkey
  43.         self._daemonic = _current_process._daemonic
  44.         self._tempdir = _current_process._tempdir
  45.         self._parent_pid = os.getpid()
  46.         self._popen = None
  47.         self._target = target
  48.         self._args = tuple(args)
  49.         self._kwargs = dict(kwargs)
  50.         if not name:
  51.             pass
  52.         self._name = type(self).__name__ + '-' + ':'.join((lambda .0: for i in .0:
  53. str(i))(self._identity))
  54.  
  55.     
  56.     def run(self):
  57.         if self._target:
  58.             self._target(*self._args, **self._kwargs)
  59.         
  60.  
  61.     
  62.     def start(self):
  63.         _cleanup()
  64.         if self._Popen is not None:
  65.             Popen = self._Popen
  66.         else:
  67.             Popen = Popen
  68.             import forking
  69.         self._popen = Popen(self)
  70.         _current_process._children.add(self)
  71.  
  72.     
  73.     def terminate(self):
  74.         self._popen.terminate()
  75.  
  76.     
  77.     def join(self, timeout = None):
  78.         res = self._popen.wait(timeout)
  79.         if res is not None:
  80.             _current_process._children.discard(self)
  81.         
  82.  
  83.     
  84.     def is_alive(self):
  85.         if self is _current_process:
  86.             return True
  87.         if self._popen is None:
  88.             return False
  89.         self._popen.poll()
  90.         return self._popen.returncode is None
  91.  
  92.     
  93.     def name(self):
  94.         return self._name
  95.  
  96.     name = property(name)
  97.     
  98.     def name(self, name):
  99.         self._name = name
  100.  
  101.     name = name.setter(name)
  102.     
  103.     def daemon(self):
  104.         return self._daemonic
  105.  
  106.     daemon = property(daemon)
  107.     
  108.     def daemon(self, daemonic):
  109.         self._daemonic = daemonic
  110.  
  111.     daemon = daemon.setter(daemon)
  112.     
  113.     def authkey(self):
  114.         return self._authkey
  115.  
  116.     authkey = property(authkey)
  117.     
  118.     def authkey(self, authkey):
  119.         self._authkey = AuthenticationString(authkey)
  120.  
  121.     authkey = authkey.setter(authkey)
  122.     
  123.     def exitcode(self):
  124.         if self._popen is None:
  125.             return self._popen
  126.         return self._popen.poll()
  127.  
  128.     exitcode = property(exitcode)
  129.     
  130.     def ident(self):
  131.         if self is _current_process:
  132.             return os.getpid()
  133.         if self._popen:
  134.             pass
  135.         return self._popen.pid
  136.  
  137.     ident = property(ident)
  138.     pid = ident
  139.     
  140.     def __repr__(self):
  141.         if self is _current_process:
  142.             status = 'started'
  143.         elif self._parent_pid != os.getpid():
  144.             status = 'unknown'
  145.         elif self._popen is None:
  146.             status = 'initial'
  147.         elif self._popen.poll() is not None:
  148.             status = self.exitcode
  149.         else:
  150.             status = 'started'
  151.         if type(status) is int:
  152.             if status == 0:
  153.                 status = 'stopped'
  154.             else:
  155.                 status = 'stopped[%s]' % _exitcode_to_name.get(status, status)
  156.         
  157.         if not self._daemonic or ' daemon':
  158.             pass
  159.         return '<%s(%s, %s%s)>' % (type(self).__name__, self._name, status, '')
  160.  
  161.     
  162.     def _bootstrap(self):
  163.         global _current_process
  164.         util = util
  165.         import 
  166.         
  167.         try:
  168.             self._children = set()
  169.             self._counter = itertools.count(1)
  170.             
  171.             try:
  172.                 sys.stdin.close()
  173.                 sys.stdin = open(os.devnull)
  174.             except (OSError, ValueError):
  175.                 pass
  176.  
  177.             _current_process = self
  178.             util._finalizer_registry.clear()
  179.             util._run_after_forkers()
  180.             util.info('child process calling self.run()')
  181.             
  182.             try:
  183.                 self.run()
  184.                 exitcode = 0
  185.             finally:
  186.                 util._exit_function()
  187.  
  188.         except SystemExit:
  189.             e = None
  190.             if not e.args:
  191.                 exitcode = 1
  192.             elif type(e.args[0]) is int:
  193.                 exitcode = e.args[0]
  194.             else:
  195.                 sys.stderr.write(e.args[0] + '\n')
  196.                 sys.stderr.flush()
  197.                 exitcode = 1
  198.         except:
  199.             e.args
  200.             exitcode = 1
  201.             import traceback
  202.             sys.stderr.write('Process %s:\n' % self.name)
  203.             sys.stderr.flush()
  204.             traceback.print_exc()
  205.  
  206.         util.info('process exiting with exitcode %d' % exitcode)
  207.         return exitcode
  208.  
  209.  
  210.  
  211. class AuthenticationString(bytes):
  212.     
  213.     def __reduce__(self):
  214.         Popen = Popen
  215.         import forking
  216.         if not Popen.thread_is_spawning():
  217.             raise TypeError('Pickling an AuthenticationString object is disallowed for security reasons')
  218.         Popen.thread_is_spawning()
  219.         return (AuthenticationString, (bytes(self),))
  220.  
  221.  
  222.  
  223. class _MainProcess(Process):
  224.     
  225.     def __init__(self):
  226.         self._identity = ()
  227.         self._daemonic = False
  228.         self._name = 'MainProcess'
  229.         self._parent_pid = None
  230.         self._popen = None
  231.         self._counter = itertools.count(1)
  232.         self._children = set()
  233.         self._authkey = AuthenticationString(os.urandom(32))
  234.         self._tempdir = None
  235.  
  236.  
  237. _current_process = _MainProcess()
  238. del _MainProcess
  239. _exitcode_to_name = { }
  240. for name, signum in signal.__dict__.items():
  241.     if name[:3] == 'SIG' and '_' not in name:
  242.         _exitcode_to_name[-signum] = name
  243.         continue
  244.  
  245.