+ 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
+from select import select
+import subprocess
+import signal
+
+class Sopen(subprocess.Popen):
+ def kill(self, signal = signal.SIGTERM):
+ os.kill(self.pid, signal)
+
+def read_t(stream, count, timeout=COMMAND_TIMEOUT*2):
+ lin, lout, lerr = select([stream], [], [], timeout)
+ if len(lin) == 0:
+ raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
+
+ return stream.read(count)
+
+class CMD:
+ def __init__(self):
+ pass
+
+ def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
+
+ #print "CMD.run_noexcept(%s)" % cmd
+ try:
+ return CMD.run(self,cmd,timeout)
+ except ExceptionTimeout:
+ import traceback; print traceback.print_exc()
+ return ("", "SCRIPTTIMEOUT")
+
+ def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
+ (o,e) = self.run(cmd, timeout)
+ self.output = o
+ self.error = e
+ if self.s.returncode is None:
+ self.s.wait()
+ return self.s.returncode
+
+ def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
+
+ #print "CMD.run(%s)" % cmd
+ s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
+ self.s = s
+ (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
+ #print "calling select(%s)" % timeout
+ lout, lin, lerr = select([f_out], [], [f_err], timeout)
+ #print "TIMEOUT!!!!!!!!!!!!!!!!!!!"
+ if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
+ # Reached a timeout! Nuke process so it does not hang.
+ #print "KILLING"
+ s.kill(signal.SIGKILL)
+ raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
+ else:
+ #print "RETURNING"
+ #print len(lin), len(lout), len(lerr)
+ pass
+
+ o_value = ""
+ e_value = ""
+
+ #print "reading from f_out"
+ if len(lout) > 0: o_value = f_out.read()
+ #print "reading from f_err"
+ if len(lerr) > 0: e_value = f_err.read()
+
+ #print "striping output"
+ o_value = o_value.strip()
+ e_value = e_value.strip()
+
+ #print "OUTPUT", o_value, e_value
+
+ #print "closing files"
+ f_out.close()
+ f_in.close()
+ f_err.close()
+ try:
+ #print "s.kill()"
+ s.kill()
+ #print "after s.kill()"
+ except OSError:
+ # no such process, due to it already exiting...
+ pass
+
+ #print o_value, e_value
+ return (o_value, e_value)
+
+ def runargs(self, args, timeout=COMMAND_TIMEOUT*2):