Changeset 3170 for trunk/lang


Ignore:
Timestamp:
Sep 18, 2012, 6:49:46 PM (12 years ago)
Author:
dkearney
Message:

updates from steve to Rappture's python language tools.py

File:
1 edited

Legend:

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

    r2926 r3170  
    44#  AUTHOR:  Derrick S. Kearney, Purdue University
    55#  AUTHOR:  Steve Clark, Purdue University
    6 #  Copyright (c) 2005-2007  Purdue Research Foundation, West Lafayette, IN
     6#  Copyright (c) 2005-2012  Purdue Research Foundation, West Lafayette, IN
    77# ======================================================================
    88
    9 import sys, os, re, subprocess, select
     9import sys
     10import os
     11import re
     12import subprocess
     13import select
     14import signal
    1015
    11 # getCommandOutput function written by Steve Clark
     16commandPid = 0
     17
     18def sig_handler(signalType, frame):
     19    global commandPid
     20    if commandPid:
     21        os.kill(commandPid,signal.SIGTERM)
     22
    1223
    1324def getCommandOutput(command,
     
    1526    global commandPid
    1627
     28    sig_INT_handler = signal.signal(signal.SIGINT,sig_handler)
     29    sig_HUP_handler = signal.signal(signal.SIGHUP,sig_handler)
     30    sig_TERM_handler = signal.signal(signal.SIGTERM,sig_handler)
     31
    1732    BUFSIZ = 4096
    18     child      = subprocess.Popen(command,shell=True,bufsize=BUFSIZ, \
    19                                   stdout=subprocess.PIPE, \
    20                                   stderr=subprocess.PIPE, \
    21                                   close_fds=True)
     33    if isinstance(command,list):
     34       child = subprocess.Popen(command,bufsize=BUFSIZ, \
     35                                stdout=subprocess.PIPE, \
     36                                stderr=subprocess.PIPE, \
     37                                close_fds=True)
     38    else:
     39       child = subprocess.Popen(command,shell=True,bufsize=BUFSIZ, \
     40                                stdout=subprocess.PIPE, \
     41                                stderr=subprocess.PIPE, \
     42                                close_fds=True)
    2243    commandPid = child.pid
    2344    childout   = child.stdout
     
    2647    childerrFd = childerr.fileno()
    2748
    28     outEOF = errEOF = 0
     49    outEOF = False
     50    errEOF = False
    2951
    3052    outData = []
    3153    errData = []
    3254
    33     while 1:
     55    while True:
    3456        toCheck = []
    3557        if not outEOF:
     
    3759        if not errEOF:
    3860            toCheck.append(childerrFd)
    39         ready = select.select(toCheck,[],[]) # wait for input
    40         if childoutFd in ready[0]:
     61        try:
     62            readyRead,readyWrite,readyException = select.select(toCheck,[],[]) # wait for input
     63        except select.error,err:
     64            readyRead = []
     65            readyWrite = []
     66            readyException = []
     67        if childoutFd in readyRead:
    4168            outChunk = os.read(childoutFd,BUFSIZ)
    4269            if outChunk == '':
    43                 outEOF = 1
     70                outEOF = True
    4471            outData.append(outChunk)
    4572            if streamOutput:
     
    4774                sys.stdout.flush()
    4875
    49         if childerrFd in ready[0]:
     76        if childerrFd in readyRead:
    5077            errChunk = os.read(childerrFd,BUFSIZ)
    5178            if errChunk == '':
    52                 errEOF = 1
     79                errEOF = True
    5380            errData.append(errChunk)
    5481            if streamOutput:
     
    5986            break
    6087
    61     err = child.wait()
     88    pid,err = os.waitpid(commandPid,0)
    6289    commandPid = 0
     90
     91    signal.signal(signal.SIGINT,sig_INT_handler)
     92    signal.signal(signal.SIGHUP,sig_HUP_handler)
     93    signal.signal(signal.SIGTERM,sig_TERM_handler)
     94
    6395    if err != 0:
    64         sys.stderr.write("%s failed w/ exit code %d\n" % (command,err))
     96        if   os.WIFSIGNALED(err):
     97           sys.stderr.write("%s failed w/ signal %d\n" % (command,os.WTERMSIG(err)))
     98        else:
     99           if os.WIFEXITED(err):
     100               err = os.WEXITSTATUS(err)
     101           sys.stderr.write("%s failed w/ exit code %d\n" % (command,err))
    65102        if not streamOutput:
    66             sys.stderr.write("%s\n" % ("".join(errData)))
     103           sys.stderr.write("%s\n" % ("".join(errData)))
    67104
    68105    return err,"".join(outData),"".join(errData)
Note: See TracChangeset for help on using the changeset viewer.