96 for arg
in parser.rargs:
97 if arg[:2].startswith(
'--'):
101 del parser.rargs[:len(value)]
102 setattr(parser.values, option.dest, value)
111 for arg
in parser.rargs:
112 if arg[:2].startswith(
'--')
or arg[0] !=
'-':
116 del parser.rargs[:len(value)]
117 setattr(parser.values, option.dest, value)
157 killOnEarlyReturn=True,
160 printErrorInfo=False):
162 Run a command, with optional input and polling function.
165 command: list of the command and its arguments.
166 input: optional string of input to feed to the command, it should be
167 short enough to fit in an i/o pipe buffer.
168 pollFn: if present will be called occasionally to check if the command
169 should be finished early. If pollFn() returns true then the command
171 outStream: if present, the stdout output of the command will be written to
173 errStream: if present, the stderr output of the command will be written to
175 killOnEarlyReturn: if true and pollFn returns true, then the subprocess will
176 be killed, otherwise the subprocess will be detached.
177 verbose: if true, the command is echoed to stderr.
178 debug: if true, prints debugging information to stderr.
179 printErrorInfo: if true, prints error information when the subprocess
180 returns a non-zero exit code.
181 Returns: the output of the subprocess.
184 Raises Error if the subprocess returns an error code.
185 Raises ValueError if called with invalid arguments.
188 sys.stderr.write(
"command %s\n" % command)
191 stdin = subprocess.PIPE
193 process = subprocess.Popen(
197 stdout=subprocess.PIPE,
198 stderr=subprocess.PIPE)
200 if not isinstance(command, str):
201 command =
' '.join(command)
203 sys.stderr.write(
"Command failed: '%s'\n" % command)
206 def StartThread(out):
207 queue = queue.Queue()
209 def EnqueueOutput(out, queue):
210 for line
in iter(out.readline, b
''):
214 thread = threading.Thread(target=EnqueueOutput, args=(out, queue))
219 outQueue = StartThread(process.stdout)
220 errQueue = StartThread(process.stderr)
222 def ReadQueue(queue, out, out2):
225 line = queue.get(
False)
232 outBuf = StringIO.StringIO()
233 errorBuf = StringIO.StringIO()
235 process.stdin.write(input)
237 returncode = process.poll()
238 if returncode !=
None:
240 ReadQueue(errQueue, errorBuf, errStream)
241 ReadQueue(outQueue, outBuf, outStream)
242 if pollFn !=
None and pollFn():
244 if killOnEarlyReturn:
249 ReadQueue(errQueue, errorBuf, errStream)
250 ReadQueue(outQueue, outBuf, outStream)
252 out = outBuf.getvalue()
253 error = errorBuf.getvalue()
255 if not isinstance(command, str):
256 command =
' '.join(command)
258 sys.stderr.write(
"Command failed: '%s'\n" % command)
259 sys.stderr.write(
" stdout: '%s'\n" % out)
260 sys.stderr.write(
" stderr: '%s'\n" % error)
261 sys.stderr.write(
" returncode: %d\n" % returncode)
262 raise Error(
"Command failed: %s" % command)
264 sys.stderr.write(
"output: %s\n" % out)
RunCommand(command, input=None, pollFn=None, outStream=None, errStream=None, killOnEarlyReturn=True, verbose=False, debug=False, printErrorInfo=False)