source: branches/testScripts/NBosCommand.py @ 628

Last change on this file since 628 was 628, checked in by jpfasano, 13 years ago

Modified functions called to implement running command in seperate process

  • Property svn:executable set to *
File size: 1.4 KB
Line 
1#! /usr/bin/env python
2
3import subprocess
4
5#------------------------------------------------------------------------
6# Run a an OS command in another process.
7# Examples might be 'svn checkout', 'make test'.
8# Return: command's return code, stdout messages, & stderr messages
9#------------------------------------------------------------------------
10def run(cmd) :
11  p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
12  cmdStdout,cmdStderr=p.communicate()
13  cmdRc=p.returncode
14  print "run rc="
15  print cmdRc
16  retVal = { 'returnCode':cmdRc, 'stdout':cmdStdout, 'stderr':cmdStderr }
17  return retVal
18
19
20#  run = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
21#
22#  # Wait for the process to return
23#  import thread, threading
24#  out, err = [''], ['']
25#  #out, err = "", ""
26#  out_ended, err_ended = threading.Event(), threading.Event()
27#
28#  def getOutput(output, lines, ended_event) :
29#    #for i in output.readlines() : lines.append(i)
30#    for i in output.readlines() :
31#      lines[0] = lines[0]+i
32#    ended_event.set()
33#
34#  out_thread = thread.start_new_thread(getOutput, (run.stdout, out, out_ended))
35#  err_thread = thread.start_new_thread(getOutput, (run.stderr, err, err_ended))
36#
37#  out_ended.wait()
38#  err_ended.wait()
39#
40#  returncode = run.wait()
41#
42#  retVal = { 'returnCode':returncode, 'stdout':out[0], 'stderr':err[0] }
43#  return retVal
44
Note: See TracBrowser for help on using the repository browser.