llsubprocess.py 3.2 KB

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