llsubprocess.py

Go to the documentation of this file.
00001 """\
00002 @file llsubprocess.py
00003 @author Phoenix
00004 @date 2008-01-18
00005 @brief The simplest possible wrapper for a common sub-process paradigm.
00006 
00007 $LicenseInfo:firstyear=2007&license=mit$
00008 
00009 Copyright (c) 2007-2008, Linden Research, Inc.
00010 
00011 Permission is hereby granted, free of charge, to any person obtaining a copy
00012 of this software and associated documentation files (the "Software"), to deal
00013 in the Software without restriction, including without limitation the rights
00014 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015 copies of the Software, and to permit persons to whom the Software is
00016 furnished to do so, subject to the following conditions:
00017 
00018 The above copyright notice and this permission notice shall be included in
00019 all copies or substantial portions of the Software.
00020 
00021 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027 THE SOFTWARE.
00028 $/LicenseInfo$
00029 """
00030 
00031 import os
00032 import popen2
00033 import time
00034 import select
00035 
00036 class Timeout(RuntimeError):
00037     "Exception raised when a subprocess times out."
00038     pass
00039 
00040 def run(command, args=None, data=None, timeout=None):
00041     """\
00042 @brief Run command with arguments
00043 
00044 This is it. This is the function I want to run all the time when doing
00045 subprocces, but end up copying the code everywhere. none of the
00046 standard commands are secure and provide a way to specify input, get
00047 all the output, and get the result.
00048 @param command A string specifying a process to launch.
00049 @param args Arguments to be passed to command. Must be list, tuple or None.
00050 @param data input to feed to the command.
00051 @param timeout Maximum number of many seconds to run.
00052 @return Returns (result, stdout, stderr) from process.
00053 """
00054     cmd = [command]
00055     if args:
00056         cmd.extend([str(arg) for arg in args])
00057     #print  "cmd: ","' '".join(cmd)
00058     child = popen2.Popen3(cmd, True)
00059     #print child.pid
00060     out = []
00061     err = []
00062     result = -1
00063     time_left = timeout
00064     tochild = [child.tochild.fileno()]
00065     while True:
00066         time_start = time.time()
00067         #print "time:",time_left
00068         p_in, p_out, p_err = select.select(
00069             [child.fromchild.fileno(), child.childerr.fileno()],
00070             tochild,
00071             [],
00072             time_left)
00073         if p_in:
00074             new_line = os.read(child.fromchild.fileno(), 32 * 1024)
00075             if new_line:
00076                 #print "line:",new_line
00077                 out.append(new_line)
00078             new_line = os.read(child.childerr.fileno(), 32 * 1024)
00079             if new_line:
00080                 #print "error:", new_line
00081                 err.append(new_line)
00082         if p_out:
00083             if data:
00084                 #print "p_out"
00085                 bytes = os.write(child.tochild.fileno(), data)
00086                 data = data[bytes:]
00087                 if len(data) == 0:
00088                     data = None
00089                     tochild = []
00090                     child.tochild.close()
00091         result = child.poll()
00092         if result != -1:
00093             child.tochild.close()
00094             child.fromchild.close()
00095             child.childerr.close()
00096             break
00097         if time_left is not None:
00098             time_left -= (time.time() - time_start)
00099             if time_left < 0:
00100                 raise Timeout
00101     #print "result:",result
00102     out = ''.join(out)
00103     #print "stdout:", out
00104     err = ''.join(err)
00105     #print "stderr:", err
00106     return result, out, err

Generated on Fri May 16 08:31:53 2008 for SecondLife by  doxygen 1.5.5