Changeset 832 for trunk


Ignore:
Timestamp:
Dec 26, 2007 1:05:06 PM (17 years ago)
Author:
dkearney
Message:

added steve's getCommandOutput() python function which allows the output to be scrolled to the rappture screen.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/Rappture/tools.py

    r511 r832  
    22#
    33# ======================================================================
    4 #  AUTHOR:  Derrick Kearney, Purdue University
    5 #  Copyright (c) 2005  Purdue Research Foundation, West Lafayette, IN
     4#  AUTHOR:  Derrick S. Kearney, Purdue University
     5#  AUTHOR:  Steve Clark, Purdue University
     6#  Copyright (c) 2005-2007  Purdue Research Foundation, West Lafayette, IN
    67# ======================================================================
    78
    8 import sys, os, re
     9import sys, os, re, popen2
    910
    10 def getCommandOutput(command):
    11     childin,childout,childerr = os.popen3(command)
    12     data = childout.read()
    13     err = childerr.read()
    14     errcode = childout.close()
    15     if err:
    16         error = '%s->%s' % (command, err)
    17         raise RuntimeError, error
    18     return data
     11# getCommandOutput function written by Steve Clark
     12
     13def getCommandOutput(command,
     14                     streamOutput=False):
     15    global commandPid
     16
     17    child = popen2.Popen3(command,1)
     18    commandPid = child.pid
     19    child.tochild.close() # don't need to talk to child
     20    childout = child.fromchild
     21    childoutFd = childout.fileno()
     22    childerr = child.childerr
     23    childerrFd = childerr.fileno()
     24
     25    outEOF = errEOF = 0
     26    BUFSIZ = 4096
     27
     28    outData = []
     29    errData = []
     30
     31    while 1:
     32        toCheck = []
     33        if not outEOF:
     34            toCheck.append(childoutFd)
     35        if not errEOF:
     36            toCheck.append(childerrFd)
     37        ready = select.select(toCheck,[],[]) # wait for input
     38        if childoutFd in ready[0]:
     39            outChunk = os.read(childoutFd,BUFSIZ)
     40            if outChunk == '':
     41                outEOF = 1
     42            outData.append(outChunk)
     43            if streamOutput:
     44                sys.stdout.write(outChunk)
     45                sys.stdout.flush()
     46
     47        if childerrFd in ready[0]:
     48            errChunk = os.read(childerrFd,BUFSIZ)
     49            if errChunk == '':
     50                errEOF = 1
     51            errData.append(errChunk)
     52            if streamOutput:
     53                sys.stderr.write(errChunk)
     54                sys.stderr.flush()
     55
     56        if outEOF and errEOF:
     57            break
     58
     59    err = child.wait()
     60    commandPid = 0
     61    if err != 0:
     62        sys.stderr.write("%s failed w/ exit code %d\n" % (command,err))
     63        if not streamOutput:
     64            sys.stderr.write("%s\n" % ("".join(errData)))
     65
     66    return err,"".join(outData),"".join(errData)
     67
    1968
    2069def getDriverNumber(driverFileName):
Note: See TracChangeset for help on using the changeset viewer.