PageRenderTime 30ms CodeModel.GetById 17ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/lib/python/indra/util/llsubprocess.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 117 lines | 106 code | 0 blank | 11 comment | 0 complexity | 3f9afb2397a3860098a9a583947329bd MD5 | raw file
  1"""\
  2@file llsubprocess.py
  3@author Phoenix
  4@date 2008-01-18
  5@brief The simplest possible wrapper for a common sub-process paradigm.
  6
  7$LicenseInfo:firstyear=2007&license=mit$
  8
  9Copyright (c) 2007-2009, Linden Research, Inc.
 10
 11Permission is hereby granted, free of charge, to any person obtaining a copy
 12of this software and associated documentation files (the "Software"), to deal
 13in the Software without restriction, including without limitation the rights
 14to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 15copies of the Software, and to permit persons to whom the Software is
 16furnished to do so, subject to the following conditions:
 17
 18The above copyright notice and this permission notice shall be included in
 19all copies or substantial portions of the Software.
 20
 21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 27THE SOFTWARE.
 28$/LicenseInfo$
 29"""
 30
 31import os
 32import popen2
 33import time
 34import select
 35
 36class Timeout(RuntimeError):
 37    "Exception raised when a subprocess times out."
 38    pass
 39
 40def run(command, args=None, data=None, timeout=None):
 41    """\
 42@brief Run command with arguments
 43
 44This is it. This is the function I want to run all the time when doing
 45subprocces, but end up copying the code everywhere. none of the
 46standard commands are secure and provide a way to specify input, get
 47all the output, and get the result.
 48@param command A string specifying a process to launch.
 49@param args Arguments to be passed to command. Must be list, tuple or None.
 50@param data input to feed to the command.
 51@param timeout Maximum number of many seconds to run.
 52@return Returns (result, stdout, stderr) from process.
 53"""
 54    cmd = [command]
 55    if args:
 56        cmd.extend([str(arg) for arg in args])
 57    #print  "cmd: ","' '".join(cmd)
 58    child = popen2.Popen3(cmd, True)
 59    #print child.pid
 60    out = []
 61    err = []
 62    result = -1
 63    time_left = timeout
 64    tochild = [child.tochild.fileno()]
 65    while True:
 66        time_start = time.time()
 67        #print "time:",time_left
 68        p_in, p_out, p_err = select.select(
 69            [child.fromchild.fileno(), child.childerr.fileno()],
 70            tochild,
 71            [],
 72            time_left)
 73        if p_in:
 74            new_line = os.read(child.fromchild.fileno(), 32 * 1024)
 75            if new_line:
 76                #print "line:",new_line
 77                out.append(new_line)
 78            new_line = os.read(child.childerr.fileno(), 32 * 1024)
 79            if new_line:
 80                #print "error:", new_line
 81                err.append(new_line)
 82        if p_out:
 83            if data:
 84                #print "p_out"
 85                bytes = os.write(child.tochild.fileno(), data)
 86                data = data[bytes:]
 87                if len(data) == 0:
 88                    data = None
 89                    tochild = []
 90                    child.tochild.close()
 91        result = child.poll()
 92        if result != -1:
 93            # At this point, the child process has exited and result
 94            # is the return value from the process. Between the time
 95            # we called select() and poll() the process may have
 96            # exited so read all the data left on the child process
 97            # stdout and stderr.
 98            last = child.fromchild.read()
 99            if last:
100                out.append(last)
101            last = child.childerr.read()
102            if last:
103                err.append(last)
104            child.tochild.close()
105            child.fromchild.close()
106            child.childerr.close()
107            break
108        if time_left is not None:
109            time_left -= (time.time() - time_start)
110            if time_left < 0:
111                raise Timeout
112    #print "result:",result
113    out = ''.join(out)
114    #print "stdout:", out
115    err = ''.join(err)
116    #print "stderr:", err
117    return result, out, err