source: branches/testScripts/NBosCommand.py @ 627

Last change on this file since 627 was 626, checked in by jpfasano, 12 years ago

Added NBosCommand (nightly build operating system command).
This is an attempt to run commands and exe's in a subprocess, so if they
crash they don't bring down the script

  • 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#  cmdRc=p.wait
13#  cmdRc=p.returncode
14#  print "run rc="
15#  print cmdRc
16#  cmdStdout=p.stdout.read()
17#  cmdStderr=p.stderr.read()
18#  retVal = { 'returnCode':cmdRc, 'stdout':cmdStdout, 'stderr':cmdStderr }
19#  return retVal
20
21
22  run = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
23
24  # Wait for the process to return
25  import thread, threading
26  out, err = [''], ['']
27  #out, err = "", ""
28  out_ended, err_ended = threading.Event(), threading.Event()
29
30  def getOutput(output, lines, ended_event) :
31    #for i in output.readlines() : lines.append(i)
32    for i in output.readlines() : 
33      lines[0] = lines[0]+i
34    ended_event.set()
35
36  out_thread = thread.start_new_thread(getOutput, (run.stdout, out, out_ended))
37  err_thread = thread.start_new_thread(getOutput, (run.stderr, err, err_ended))
38
39  out_ended.wait()
40  err_ended.wait()
41
42  returncode = run.wait()
43
44  retVal = { 'returnCode':returncode, 'stdout':out[0], 'stderr':err[0] }
45  return retVal
46
Note: See TracBrowser for help on using the repository browser.