Welcome, guest | Sign In | My Account | Store | Cart
import os, popen2, fcntl, FCNTL, select

def makeNonBlocking(fd):
    fl
= fcntl.fcntl(fd, FCNTL.F_GETFL)
   
try:
        fcntl
.fcntl(fd, FCNTL.F_SETFL, fl | FCNTL.O_NDELAY)
   
except AttributeError:
        fcntl
.fcntl(fd, FCNTL.F_SETFL, fl | FCNTL.FNDELAY)
   

def getCommandOutput(command):
    child
= popen2.Popen3(command, 1) # capture stdout and stderr from command
    child
.tochild.close()             # don't need to talk to child
    outfile
= child.fromchild
    outfd
= outfile.fileno()
    errfile
= child.childerr
    errfd
= errfile.fileno()
    makeNonBlocking
(outfd)            # don't deadlock!
    makeNonBlocking
(errfd)
    outdata
= errdata = ''
    outeof
= erreof = 0
   
while 1:
        ready
= select.select([outfd,errfd],[],[]) # wait for input
       
if outfd in ready[0]:
            outchunk
= outfile.read()
           
if outchunk == '': outeof = 1
            outdata
= outdata + outchunk
       
if errfd in ready[0]:
            errchunk
= errfile.read()
           
if errchunk == '': erreof = 1
            errdata
= errdata + errchunk
       
if outeof and erreof: break
       
select.select([],[],[],.1) # give a little time for buffers to fill
    err
= child.wait()
   
if err != 0:
       
raise RuntimeError, '%s failed w/ exit code %d\n%s' % (command, err, errdata)
   
return outdata

def getCommandOutput2(command):
    child
= os.popen(command)
    data
= child.read()
    err
= child.close()
   
if err:
       
raise RuntimeError, '%s failed w/ exit code %d' % (command, err)
   
return data

History

  • revision 2 (22 years ago)
  • previous revisions are not available