250 |
250 |
251 # optional channels |
251 # optional channels |
252 else: |
252 else: |
253 pass |
253 pass |
254 |
254 |
255 def runcommand(self, args, prompt=None, input=None): |
255 def runcommand(self, args, prompt=None, input=None, output=None, error=None): |
256 """ |
256 """ |
257 Public method to execute a command via the command server. |
257 Public method to execute a command via the command server. |
258 |
258 |
259 @param args list of arguments for the command (list of string) |
259 @param args list of arguments for the command (list of string) |
260 @param prompt function to reply to prompts by the server. It |
260 @keyparam prompt function to reply to prompts by the server. It |
261 receives the max number of bytes to return and the contents |
261 receives the max number of bytes to return and the contents |
262 of the output channel received so far. |
262 of the output channel received so far. |
263 @param input function to reply to bulk data requests by the server. |
263 @keyparam input function to reply to bulk data requests by the server. |
264 It receives the max number of bytes to return. |
264 It receives the max number of bytes to return. |
265 @return output and errors of the command server (string) |
265 @keyparam output function receiving the data from the server (string). |
|
266 If a prompt function is given, this parameter will be ignored. |
|
267 @keyparam error function receiving error messages from the server (string) |
|
268 @return output and errors of the command server (string). In case output |
|
269 and/or error functions were given, the respective return value will |
|
270 be an empty string. |
266 """ |
271 """ |
267 self.__commandRunning = True |
272 self.__commandRunning = True |
268 |
273 outputChannels = {} |
269 output = io.StringIO() |
274 outputBuffer = None |
270 error = io.StringIO() |
275 errorBuffer = None |
271 outputChannels = { |
276 |
272 "o": output.write, |
277 if prompt is not None or output is None: |
273 "e": error.write |
278 outputBuffer = io.StringIO() |
274 } |
279 outputChannels["o"] = outputBuffer.write |
|
280 else: |
|
281 outputChannels["o"] = output |
|
282 if error: |
|
283 outputChannels["e"] = error |
|
284 else: |
|
285 errorBuffer = io.StringIO() |
|
286 outputChannels["e"] = errorBuffer.write |
275 |
287 |
276 inputChannels = {} |
288 inputChannels = {} |
277 if prompt is not None: |
289 if prompt is not None: |
278 def func(size): |
290 def func(size): |
279 reply = prompt(size, output.getvalue()) |
291 reply = prompt(size, outputBuffer.getvalue()) |
280 return reply |
292 return reply |
281 inputChannels["L"] = func |
293 inputChannels["L"] = func |
282 if input is not None: |
294 if input is not None: |
283 inputChannels["I"] = input |
295 inputChannels["I"] = input |
284 |
296 |
285 self.__cancel = False |
297 self.__cancel = False |
286 self.__runcommand(args, inputChannels, outputChannels) |
298 self.__runcommand(args, inputChannels, outputChannels) |
287 out = output.getvalue() |
299 if outputBuffer: |
288 err = error.getvalue() |
300 out = outputBuffer.getvalue() |
|
301 else: |
|
302 out = "" |
|
303 if errorBuffer: |
|
304 err = errorBuffer.getvalue() |
|
305 else: |
|
306 err = "" |
289 |
307 |
290 self.__commandRunning = False |
308 self.__commandRunning = False |
291 |
309 |
292 return out, err |
310 return out, err |
293 |
311 |