Changeset 3170 for trunk/lang/python/Rappture/tools.py
- Timestamp:
- Sep 18, 2012, 6:49:46 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lang/python/Rappture/tools.py
r2926 r3170 4 4 # AUTHOR: Derrick S. Kearney, Purdue University 5 5 # AUTHOR: Steve Clark, Purdue University 6 # Copyright (c) 2005-20 07Purdue Research Foundation, West Lafayette, IN6 # Copyright (c) 2005-2012 Purdue Research Foundation, West Lafayette, IN 7 7 # ====================================================================== 8 8 9 import sys, os, re, subprocess, select 9 import sys 10 import os 11 import re 12 import subprocess 13 import select 14 import signal 10 15 11 # getCommandOutput function written by Steve Clark 16 commandPid = 0 17 18 def sig_handler(signalType, frame): 19 global commandPid 20 if commandPid: 21 os.kill(commandPid,signal.SIGTERM) 22 12 23 13 24 def getCommandOutput(command, … … 15 26 global commandPid 16 27 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 17 32 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) 22 43 commandPid = child.pid 23 44 childout = child.stdout … … 26 47 childerrFd = childerr.fileno() 27 48 28 outEOF = errEOF = 0 49 outEOF = False 50 errEOF = False 29 51 30 52 outData = [] 31 53 errData = [] 32 54 33 while 1:55 while True: 34 56 toCheck = [] 35 57 if not outEOF: … … 37 59 if not errEOF: 38 60 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: 41 68 outChunk = os.read(childoutFd,BUFSIZ) 42 69 if outChunk == '': 43 outEOF = 170 outEOF = True 44 71 outData.append(outChunk) 45 72 if streamOutput: … … 47 74 sys.stdout.flush() 48 75 49 if childerrFd in ready [0]:76 if childerrFd in readyRead: 50 77 errChunk = os.read(childerrFd,BUFSIZ) 51 78 if errChunk == '': 52 errEOF = 179 errEOF = True 53 80 errData.append(errChunk) 54 81 if streamOutput: … … 59 86 break 60 87 61 err = child.wait()88 pid,err = os.waitpid(commandPid,0) 62 89 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 63 95 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)) 65 102 if not streamOutput: 66 103 sys.stderr.write("%s\n" % ("".join(errData))) 67 104 68 105 return err,"".join(outData),"".join(errData)
Note: See TracChangeset
for help on using the changeset viewer.