def process_handler(cmd, callback, stderr=subprocess.PIPE):
 sys.stdout.flush()
 sys.stderr.flush()
 close_fds = (sys.platform != 'win32')
 shell = isinstance(cmd, str)
 executable = None
 if (shell and (os.name == 'posix') and ('SHELL' in os.environ)):
  executable = os.environ['SHELL']
 p = subprocess.Popen(cmd, shell=shell, executable=executable, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=stderr, close_fds=close_fds)
 try:
  out = callback(p)
 except KeyboardInterrupt:
  print '^C'
  sys.stdout.flush()
  sys.stderr.flush()
  out = None
 finally:
  if (p.returncode is None):
   try:
    p.terminate()
    p.poll()
   except OSError:
    pass
  if (p.returncode is None):
   try:
    p.kill()
   except OSError:
    pass
 return out

def process_handler ( cmd , callback , stderr = subprocess . PIPE ) : sys . stdout . flush ( ) sys . stderr . flush ( ) close_fds = ( sys . platform != 'win32' ) shell = isinstance ( cmd , str ) executable = None if ( shell and ( os . name == 'posix' ) and ( 'SHELL' in os . environ ) ) : executable = os . environ [ 'SHELL' ] p = subprocess . Popen ( cmd , shell = shell , executable = executable , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = stderr , close_fds = close_fds ) try : out = callback ( p ) except KeyboardInterrupt : print '^C' sys . stdout . flush ( ) sys . stderr . flush ( ) out = None finally : if ( p . returncode is None ) : try : p . terminate ( ) p . poll ( ) except OSError : pass if ( p . returncode is None ) : try : p . kill ( ) except OSError : pass return out
open a command in a shell subprocess and execute a callback .