source: branches/testScripts/NBosCommand.py @ 634

Last change on this file since 634 was 632, checked in by jpfasano, 12 years ago

use the python version number to decide how to implement function

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