Changeset 6034
- Timestamp:
- Feb 22, 2016, 10:34:37 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1.4/lang/python/Rappture/tools.py
r3844 r6034 15 15 import signal 16 16 import traceback 17 import time 17 18 18 19 commandPid = 0 … … 24 25 25 26 26 def getCommandOutput(command, 27 streamOutput=False): 27 def executeCommand(command, 28 stdin=None, 29 streamOutput=False): 28 30 global commandPid 31 32 exitStatus = 0 33 fpStdin = None 34 outData = [] 35 errData = [] 29 36 30 37 try: … … 38 45 print traceback.format_exc() 39 46 40 BUFSIZ = 4096 41 if isinstance(command,list): 42 child = subprocess.Popen(command,bufsize=BUFSIZ, \ 43 stdout=subprocess.PIPE, \ 44 stderr=subprocess.PIPE, \ 45 close_fds=True) 47 if stdin == None: 48 commandStdin = None 46 49 else: 50 if isinstance(stdin,file): 51 commandStdin = stdin 52 elif isinstance(stdin,int): 53 commandStdin = stdin 54 else: 55 try: 56 stdinPath = str(stdin) 57 except UnicodeEncodeError: 58 try: 59 stdinPath = unicode(stdin).encode('unicode_escape') 60 except: 61 stdinPath = None 62 except: 63 stdinPath = None 64 if stdinPath: 65 if os.path.isfile(stdinPath): 66 try: 67 fpStdin = open(stdinPath,'r') 68 except (IOError,OSError): 69 exitStatus = 1 70 errData.append("File %s could not be opened.\n" % (stdinPath)) 71 if streamOutput: 72 sys.stderr.write("File %s could not be opened.\n" % (stdinPath)) 73 sys.stderr.flush() 74 else: 75 commandStdin = fpStdin 76 else: 77 exitStatus = 1 78 errData.append("File %s does not exist.\n" % (stdinPath)) 79 if streamOutput: 80 sys.stderr.write("File %s does not exist.\n" % (stdinPath)) 81 sys.stderr.flush() 82 else: 83 exitStatus = 1 84 errData.append("Bad argument type specified for stdin.\n") 85 if streamOutput: 86 sys.stderr.write("Bad argument type specified for stdin.\n") 87 sys.stderr.flush() 88 89 if not exitStatus: 90 BUFSIZ = 4096 91 if isinstance(command,list): 92 child = subprocess.Popen(command,bufsize=BUFSIZ, \ 93 stdin=commandStdin, \ 94 stdout=subprocess.PIPE, \ 95 stderr=subprocess.PIPE, \ 96 close_fds=True) 97 else: 98 try: 99 commandStr = str(command) 100 except UnicodeEncodeError: 101 commandStr = unicode(command).encode('unicode_escape') 102 commandArgs = shlex.split(commandStr) 103 child = subprocess.Popen(commandArgs,bufsize=BUFSIZ, \ 104 stdin=commandStdin, \ 105 stdout=subprocess.PIPE, \ 106 stderr=subprocess.PIPE, \ 107 close_fds=True) 108 commandPid = child.pid 109 childout = child.stdout 110 childoutFd = childout.fileno() 111 childerr = child.stderr 112 childerrFd = childerr.fileno() 113 114 outEOF = False 115 errEOF = False 116 117 while True: 118 toCheck = [] 119 if not outEOF: 120 toCheck.append(childoutFd) 121 if not errEOF: 122 toCheck.append(childerrFd) 123 try: 124 readyRead,readyWrite,readyException = select.select(toCheck,[],[]) # wait for input 125 except select.error,err: 126 readyRead = [] 127 readyWrite = [] 128 readyException = [] 129 if childoutFd in readyRead: 130 outChunk = os.read(childoutFd,BUFSIZ) 131 if outChunk == '': 132 outEOF = True 133 outData.append(outChunk) 134 if streamOutput: 135 sys.stdout.write(outChunk) 136 sys.stdout.flush() 137 138 if childerrFd in readyRead: 139 errChunk = os.read(childerrFd,BUFSIZ) 140 if errChunk == '': 141 errEOF = True 142 errData.append(errChunk) 143 if streamOutput: 144 sys.stderr.write(errChunk) 145 sys.stderr.flush() 146 147 if outEOF and errEOF: 148 break 149 150 pid,exitStatus = os.waitpid(commandPid,0) 151 commandPid = 0 152 if fpStdin: 153 try: 154 fpStdin.close() 155 except: 156 pass 157 47 158 try: 48 commandStr = str(command) 49 except UnicodeEncodeError: 50 commandStr = unicode(command).encode('unicode_escape') 51 commandArgs = shlex.split(commandStr) 52 child = subprocess.Popen(commandArgs,bufsize=BUFSIZ, \ 53 stdout=subprocess.PIPE, \ 54 stderr=subprocess.PIPE, \ 55 close_fds=True) 56 commandPid = child.pid 57 childout = child.stdout 58 childoutFd = childout.fileno() 59 childerr = child.stderr 60 childerrFd = childerr.fileno() 61 62 outEOF = False 63 errEOF = False 64 65 outData = [] 66 errData = [] 67 68 while True: 69 toCheck = [] 70 if not outEOF: 71 toCheck.append(childoutFd) 72 if not errEOF: 73 toCheck.append(childerrFd) 74 try: 75 readyRead,readyWrite,readyException = select.select(toCheck,[],[]) # wait for input 76 except select.error,err: 77 readyRead = [] 78 readyWrite = [] 79 readyException = [] 80 if childoutFd in readyRead: 81 outChunk = os.read(childoutFd,BUFSIZ) 82 if outChunk == '': 83 outEOF = True 84 outData.append(outChunk) 85 if streamOutput: 86 sys.stdout.write(outChunk) 87 sys.stdout.flush() 88 89 if childerrFd in readyRead: 90 errChunk = os.read(childerrFd,BUFSIZ) 91 if errChunk == '': 92 errEOF = True 93 errData.append(errChunk) 94 if streamOutput: 95 sys.stderr.write(errChunk) 96 sys.stderr.flush() 97 98 if outEOF and errEOF: 99 break 100 101 pid,err = os.waitpid(commandPid,0) 102 commandPid = 0 103 104 try: 105 signal.signal(signal.SIGINT,sig_INT_handler) 106 signal.signal(signal.SIGHUP,sig_HUP_handler) 107 signal.signal(signal.SIGTERM,sig_TERM_handler) 108 except UnboundLocalError: 109 # happens when used in a thread 110 pass 111 except: 112 print traceback.format_exc() 113 114 if err != 0: 115 if os.WIFSIGNALED(err): 116 sys.stderr.write("%s failed w/ signal %d\n" % (command,os.WTERMSIG(err))) 117 else: 118 if os.WIFEXITED(err): 119 err = os.WEXITSTATUS(err) 120 sys.stderr.write("%s failed w/ exit code %d\n" % (command,err)) 121 if not streamOutput: 122 sys.stderr.write("%s\n" % ("".join(errData))) 123 124 return err,"".join(outData),"".join(errData) 159 signal.signal(signal.SIGINT,sig_INT_handler) 160 signal.signal(signal.SIGHUP,sig_HUP_handler) 161 signal.signal(signal.SIGTERM,sig_TERM_handler) 162 except UnboundLocalError: 163 # happens when used in a thread 164 pass 165 except: 166 print traceback.format_exc() 167 168 if exitStatus != 0: 169 if os.WIFSIGNALED(exitStatus): 170 sys.stderr.write("%s failed w/ signal %d\n" % (command,os.WTERMSIG(exitStatus))) 171 else: 172 if os.WIFEXITED(exitStatus): 173 exitStatus = os.WEXITSTATUS(exitStatus) 174 sys.stderr.write("%s failed w/ exit code %d\n" % (command,exitStatus)) 175 if not streamOutput: 176 sys.stderr.write("%s\n" % ("".join(errData))) 177 178 return exitStatus,"".join(outData),"".join(errData) 179 180 181 def getCommandOutput(command, 182 streamOutput=False): 183 exitStatus,stdOut,stdErr = executeCommand(command, 184 streamOutput=streamOutput) 185 186 return exitStatus,stdOut,stdErr 125 187 126 188
Note: See TracChangeset
for help on using the changeset viewer.