source: branches/testScripts/NBosCommand.py @ 631

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

userConfig now has svn path for math01 machine
osCommand now has alternate implementation for older versions of python

  • Property svn:executable set to *
File size: 2.0 KB
Line 
1#! /usr/bin/env python
2
3from socket import gethostname
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
12  if gethostname()=='math01.watson.ibm.com' :
13
14    # this machine has a back level of python, so must use an older
15    # techniques to implement this function.  This implementation
16    # runs the command in the same process as the script.
17    # This has the problem that if the command crashes, it will bring
18    # down the script. Another problem is that stderr and stdout are
19    # mingled together
20
21    import commands
22    result = commands.getstatusoutput(cmd)
23    retVal = { 'returnCode':result[0], 'stdout':result[1], 'stderr':'' }
24    return retVal
25
26  else :
27
28    import subprocess
29 
30    p=subprocess.Popen(cmd,shell=True,\
31                       stdout=subprocess.PIPE,\
32                       stderr=subprocess.PIPE)
33    cmdStdout,cmdStderr=p.communicate()
34    cmdRc=p.returncode
35    retVal = { 'returnCode':cmdRc, 'stdout':cmdStdout, 'stderr':cmdStderr }
36    return retVal
37
38
39#  run = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
40#
41#  # Wait for the process to return
42#  import thread, threading
43#  out, err = [''], ['']
44#  #out, err = "", ""
45#  out_ended, err_ended = threading.Event(), threading.Event()
46#
47#  def getOutput(output, lines, ended_event) :
48#    #for i in output.readlines() : lines.append(i)
49#    for i in output.readlines() :
50#      lines[0] = lines[0]+i
51#    ended_event.set()
52#
53#  out_thread = thread.start_new_thread(getOutput, (run.stdout, out, out_ended))
54#  err_thread = thread.start_new_thread(getOutput, (run.stderr, err, err_ended))
55#
56#  out_ended.wait()
57#  err_ended.wait()
58#
59#  returncode = run.wait()
60#
61#  retVal = { 'returnCode':returncode, 'stdout':out[0], 'stderr':err[0] }
62#  return retVal
63
Note: See TracBrowser for help on using the repository browser.