home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / base / subproc.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  8.1 KB  |  345 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''subprocess - Subprocesses with accessible I/O streams
  5. This copy has been modified from the original in the Python std lib.
  6. Its been edited to be Linux-only. Used for compatibility with Python < v2.4'''
  7. import sys
  8. import os
  9. import types
  10. import traceback
  11. import select
  12. import errno
  13. import fcntl
  14. import pickle
  15. __all__ = [
  16.     'Popen',
  17.     'PIPE',
  18.     'STDOUT',
  19.     'call']
  20.  
  21. try:
  22.     MAXFD = os.sysconf('SC_OPEN_MAX')
  23. except:
  24.     MAXFD = 256
  25.  
  26.  
  27. try:
  28.     False
  29. except NameError:
  30.     False = 0
  31.     True = 1
  32.  
  33. _active = []
  34.  
  35. def _cleanup():
  36.     for inst in _active[:]:
  37.         inst.poll()
  38.     
  39.  
  40. PIPE = -1
  41. STDOUT = -2
  42.  
  43. def call(*args, **kwargs):
  44.     return Popen(*args, **kwargs).wait()
  45.  
  46.  
  47. class Popen(object):
  48.     
  49.     def __init__(self, args, bufsize = 0, executable = None, stdin = None, stdout = None, stderr = None, preexec_fn = None, close_fds = False, shell = False, cwd = None, env = None, universal_newlines = False, startupinfo = None, creationflags = 0):
  50.         '''Create new Popen instance.'''
  51.         _cleanup()
  52.         if not isinstance(bufsize, (int, long)):
  53.             raise TypeError('bufsize must be an integer')
  54.         
  55.         if startupinfo is not None:
  56.             raise ValueError('startupinfo is only supported on Windows platforms')
  57.         
  58.         if creationflags != 0:
  59.             raise ValueError('creationflags is only supported on Windows platforms')
  60.         
  61.         self.stdin = None
  62.         self.stdout = None
  63.         self.stderr = None
  64.         self.pid = None
  65.         self.returncode = None
  66.         self.universal_newlines = universal_newlines
  67.         (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  68.         self._execute_child(args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
  69.         if p2cwrite:
  70.             self.stdin = os.fdopen(p2cwrite, 'wb', bufsize)
  71.         
  72.         if c2pread:
  73.             if universal_newlines:
  74.                 self.stdout = os.fdopen(c2pread, 'rU', bufsize)
  75.             else:
  76.                 self.stdout = os.fdopen(c2pread, 'rb', bufsize)
  77.         
  78.         if errread:
  79.             if universal_newlines:
  80.                 self.stderr = os.fdopen(errread, 'rU', bufsize)
  81.             else:
  82.                 self.stderr = os.fdopen(errread, 'rb', bufsize)
  83.         
  84.         _active.append(self)
  85.  
  86.     
  87.     def _translate_newlines(self, data):
  88.         data = data.replace('\r\n', '\n')
  89.         data = data.replace('\r', '\n')
  90.         return data
  91.  
  92.     
  93.     def _get_handles(self, stdin, stdout, stderr):
  94.         '''Construct and return tupel with IO objects:
  95.         p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
  96.         '''
  97.         (p2cread, p2cwrite) = (None, None)
  98.         (c2pread, c2pwrite) = (None, None)
  99.         (errread, errwrite) = (None, None)
  100.         if stdin == None:
  101.             pass
  102.         elif stdin == PIPE:
  103.             (p2cread, p2cwrite) = os.pipe()
  104.         elif type(stdin) == types.IntType:
  105.             p2cread = stdin
  106.         else:
  107.             p2cread = stdin.fileno()
  108.         if stdout == None:
  109.             pass
  110.         elif stdout == PIPE:
  111.             (c2pread, c2pwrite) = os.pipe()
  112.         elif type(stdout) == types.IntType:
  113.             c2pwrite = stdout
  114.         else:
  115.             c2pwrite = stdout.fileno()
  116.         if stderr == None:
  117.             pass
  118.         elif stderr == PIPE:
  119.             (errread, errwrite) = os.pipe()
  120.         elif stderr == STDOUT:
  121.             errwrite = c2pwrite
  122.         elif type(stderr) == types.IntType:
  123.             errwrite = stderr
  124.         else:
  125.             errwrite = stderr.fileno()
  126.         return (p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
  127.  
  128.     
  129.     def _set_cloexec_flag(self, fd):
  130.         
  131.         try:
  132.             cloexec_flag = fcntl.FD_CLOEXEC
  133.         except AttributeError:
  134.             cloexec_flag = 1
  135.  
  136.         old = fcntl.fcntl(fd, fcntl.F_GETFD)
  137.         fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag)
  138.  
  139.     
  140.     def _close_fds(self, but):
  141.         for i in range(3, MAXFD):
  142.             if i == but:
  143.                 continue
  144.             
  145.             
  146.             try:
  147.                 os.close(i)
  148.             continue
  149.             continue
  150.  
  151.         
  152.  
  153.     
  154.     def _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite):
  155.         '''Execute program (POSIX version)'''
  156.         if isinstance(args, types.StringTypes):
  157.             args = [
  158.                 args]
  159.         
  160.         if shell:
  161.             args = [
  162.                 '/bin/sh',
  163.                 '-c'] + args
  164.         
  165.         if executable == None:
  166.             executable = args[0]
  167.         
  168.         (errpipe_read, errpipe_write) = os.pipe()
  169.         self._set_cloexec_flag(errpipe_write)
  170.         self.pid = os.fork()
  171.         if self.pid == 0:
  172.             
  173.             try:
  174.                 if p2cwrite:
  175.                     os.close(p2cwrite)
  176.                 
  177.                 if c2pread:
  178.                     os.close(c2pread)
  179.                 
  180.                 if errread:
  181.                     os.close(errread)
  182.                 
  183.                 os.close(errpipe_read)
  184.                 if p2cread:
  185.                     os.dup2(p2cread, 0)
  186.                 
  187.                 if c2pwrite:
  188.                     os.dup2(c2pwrite, 1)
  189.                 
  190.                 if errwrite:
  191.                     os.dup2(errwrite, 2)
  192.                 
  193.                 if p2cread:
  194.                     os.close(p2cread)
  195.                 
  196.                 if c2pwrite and c2pwrite not in (p2cread,):
  197.                     os.close(c2pwrite)
  198.                 
  199.                 if errwrite and errwrite not in (p2cread, c2pwrite):
  200.                     os.close(errwrite)
  201.                 
  202.                 if close_fds:
  203.                     self._close_fds(but = errpipe_write)
  204.                 
  205.                 if cwd != None:
  206.                     os.chdir(cwd)
  207.                 
  208.                 if preexec_fn:
  209.                     apply(preexec_fn)
  210.                 
  211.                 if env == None:
  212.                     os.execvp(executable, args)
  213.                 else:
  214.                     os.execvpe(executable, args, env)
  215.             except:
  216.                 (exc_type, exc_value, tb) = sys.exc_info()
  217.                 exc_lines = traceback.format_exception(exc_type, exc_value, tb)
  218.                 exc_value.child_traceback = ''.join(exc_lines)
  219.                 os.write(errpipe_write, pickle.dumps(exc_value))
  220.  
  221.             os._exit(255)
  222.         
  223.         os.close(errpipe_write)
  224.         if p2cread and p2cwrite:
  225.             os.close(p2cread)
  226.         
  227.         if c2pwrite and c2pread:
  228.             os.close(c2pwrite)
  229.         
  230.         if errwrite and errread:
  231.             os.close(errwrite)
  232.         
  233.         data = os.read(errpipe_read, 1048576)
  234.         os.close(errpipe_read)
  235.         if data != '':
  236.             os.waitpid(self.pid, 0)
  237.             child_exception = pickle.loads(data)
  238.             raise child_exception
  239.         
  240.  
  241.     
  242.     def _handle_exitstatus(self, sts):
  243.         if os.WIFSIGNALED(sts):
  244.             self.returncode = -os.WTERMSIG(sts)
  245.         elif os.WIFEXITED(sts):
  246.             self.returncode = os.WEXITSTATUS(sts)
  247.         else:
  248.             raise RuntimeError('Unknown child exit status!')
  249.         _active.remove(self)
  250.  
  251.     
  252.     def poll(self):
  253.         '''Check if child process has terminated.  Returns returncode
  254.         attribute.'''
  255.         if self.returncode == None:
  256.             
  257.             try:
  258.                 (pid, sts) = os.waitpid(self.pid, os.WNOHANG)
  259.                 if pid == self.pid:
  260.                     self._handle_exitstatus(sts)
  261.             except os.error:
  262.                 pass
  263.             except:
  264.                 None<EXCEPTION MATCH>os.error
  265.             
  266.  
  267.         None<EXCEPTION MATCH>os.error
  268.         return self.returncode
  269.  
  270.     
  271.     def wait(self):
  272.         '''Wait for child process to terminate.  Returns returncode
  273.         attribute.'''
  274.         if self.returncode == None:
  275.             (pid, sts) = os.waitpid(self.pid, 0)
  276.             self._handle_exitstatus(sts)
  277.         
  278.         return self.returncode
  279.  
  280.     
  281.     def communicate(self, input = None):
  282.         read_set = []
  283.         write_set = []
  284.         stdout = None
  285.         stderr = None
  286.         if self.stdin:
  287.             self.stdin.flush()
  288.             if input:
  289.                 write_set.append(self.stdin)
  290.             else:
  291.                 self.stdin.close()
  292.         
  293.         if self.stdout:
  294.             read_set.append(self.stdout)
  295.             stdout = []
  296.         
  297.         if self.stderr:
  298.             read_set.append(self.stderr)
  299.             stderr = []
  300.         
  301.         while read_set or write_set:
  302.             (rlist, wlist, xlist) = select.select(read_set, write_set, [])
  303.             if self.stdin in wlist:
  304.                 bytes_written = os.write(self.stdin.fileno(), input[:512])
  305.                 input = input[bytes_written:]
  306.                 if not input:
  307.                     self.stdin.close()
  308.                     write_set.remove(self.stdin)
  309.                 
  310.             
  311.             if self.stdout in rlist:
  312.                 data = os.read(self.stdout.fileno(), 1024)
  313.                 if data == '':
  314.                     self.stdout.close()
  315.                     read_set.remove(self.stdout)
  316.                 
  317.                 stdout.append(data)
  318.             
  319.             if self.stderr in rlist:
  320.                 data = os.read(self.stderr.fileno(), 1024)
  321.                 if data == '':
  322.                     self.stderr.close()
  323.                     read_set.remove(self.stderr)
  324.                 
  325.                 stderr.append(data)
  326.                 continue
  327.         if stdout != None:
  328.             stdout = ''.join(stdout)
  329.         
  330.         if stderr != None:
  331.             stderr = ''.join(stderr)
  332.         
  333.         if self.universal_newlines and hasattr(open, 'newlines'):
  334.             if stdout:
  335.                 stdout = self._translate_newlines(stdout)
  336.             
  337.             if stderr:
  338.                 stderr = self._translate_newlines(stderr)
  339.             
  340.         
  341.         self.wait()
  342.         return (stdout, stderr)
  343.  
  344.  
  345.