2 from select import select
10 class ExceptionTimeout(Exception): pass
12 ssh_options = { 'StrictHostKeyChecking':'no',
14 'PasswordAuthentication':'no',
15 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
17 class Sopen(subprocess.Popen):
18 def kill(self, signal = signal.SIGTERM):
19 os.kill(self.pid, signal)
21 def read_t(stream, count, timeout=COMMAND_TIMEOUT*2):
22 lin, lout, lerr = select([stream], [], [], timeout)
24 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
26 return stream.read(count)
32 def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
34 #print "CMD.run_noexcept(%s)" % cmd
36 return CMD.run(self,cmd,timeout)
37 except ExceptionTimeout:
38 print traceback.print_exc()
39 return ("", "SCRIPTTIMEOUT")
41 def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
42 (o,e) = self.run(cmd, timeout)
45 if self.s.returncode is None:
47 return self.s.returncode
49 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
51 #print "CMD.run(%s)" % cmd
52 s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
54 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
55 #print "calling select(%s)" % timeout
56 lout, lin, lerr = select([f_out], [], [f_err], timeout)
57 #print "TIMEOUT!!!!!!!!!!!!!!!!!!!"
58 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
59 # Reached a timeout! Nuke process so it does not hang.
61 s.kill(signal.SIGKILL)
62 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
65 #print len(lin), len(lout), len(lerr)
71 o_value = f_out.read()
72 e_value = f_err.read()
74 #print "striping output"
75 o_value = o_value.strip()
76 e_value = e_value.strip()
78 #print "OUTPUT -%s-%s-" % (o_value, e_value)
80 #print "closing files"
87 #print "after s.kill()"
89 # no such process, due to it already exiting...
92 #print o_value, e_value
93 return (o_value, e_value)
95 def runargs(self, args, timeout=COMMAND_TIMEOUT*2):
97 #print "CMD.run(%s)" % " ".join(args)
98 s = Sopen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
100 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
101 lout, lin, lerr = select([f_out], [], [f_err], timeout)
102 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
103 # Reached a timeout! Nuke process so it does not hang.
104 s.kill(signal.SIGKILL)
105 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
106 o_value = f_out.read()
108 if o_value == "": # An error has occured
109 e_value = f_err.read()
111 o_value = o_value.strip()
112 e_value = e_value.strip()
120 # no such process, due to it already exiting...
123 return (o_value, e_value)
127 def __init__(self, user, host, port=22, options = ssh_options):
128 self.options = options
134 def __options_to_str(self):
136 for o,v in self.options.iteritems():
137 options = options + "-o %s=%s " % (o,v)
140 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
141 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
142 self.user, self.host, cmd)
143 #print "SSH.run(%s)" % cmd
144 return CMD.run(self, cmd, timeout)
146 def get_file(self, rmt_filename, local_filename=None):
147 if local_filename == None:
148 local_filename = "./"
149 cmd = "scp -P %s -B %s %s@%s:%s %s" % (self.port, self.__options_to_str(),
150 self.user, self.host,
151 rmt_filename, local_filename)
153 # errors will be on stderr,
154 # success will have a blank stderr...
155 return CMD.run_noexcept(self, cmd)
157 def run_noexcept(self, cmd):
158 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
159 self.user, self.host, cmd)
160 #print "SSH.run_noexcept(%s)" % cmd
161 return CMD.run_noexcept(self, cmd)
163 def run_noexcept2(self, cmd, timeout=COMMAND_TIMEOUT*2):
164 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
165 self.user, self.host, cmd)
166 #print "SSH.run_noexcept2(%s)" % cmd
167 r = CMD.run_noexcept(self, cmd, timeout)
169 # XXX: this may be resulting in deadlocks... not sure.
170 #if self.s.returncode is None:
172 # self.s.kill(signal.SIGKILL)
174 # self.ret = self.s.returncode
179 def system2(self, cmd, timeout=COMMAND_TIMEOUT*2):
180 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
181 self.user, self.host, cmd)
182 #print "SSH.system2(%s)" % cmd
183 return CMD.system(self, cmd, timeout)
186 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
187 self.user, self.host, cmd)
190 (f_in, f_out, f_err) = os.popen3(cmd)
193 if value == "": # An error has occured
195 value = value.strip()
198 print " == %s" % value
206 self.start = time.time()
209 self.end = time.time()
210 t = self.end-self.start
214 self.end = time.time()
215 t = self.end-self.start
216 self.start = self.end