1 # VirtSubproc is part of autopkgtest
2 # autopkgtest is a tool for testing Debian binary packages
4 # autopkgtest is Copyright (C) 2006-2007 Canonical Ltd.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 # See the file CREDITS for a full list of credits information (often
21 # installed as /usr/share/doc/autopkgtest/CREDITS).
35 progname = "<VirtSubproc>"
36 devnull_read = file('/dev/null','r')
41 def __init__(q,ec,m): q.ec = ec; q.m = m
44 def alarm_handler(*a): raise Timeout()
45 def timeout_start(to): signal.alarm(to)
46 def timeout_stop(): signal.alarm(0)
49 def __init__(fc,e): fc.e = e
52 if not debuglevel: return
53 print >> sys.stderr, progname+": debug:", m
56 raise Quit(12, progname+": failure: %s" % m)
60 def cmdnumargs(c, ce, nargs=0, noptargs=0):
62 bomb("too few arguments to command `%s'" % ce[0])
63 if noptargs is not None and len(c) > 1+nargs+noptargs:
64 bomb("too many arguments to command `%s'" % ce[0])
66 def cmd_capabilities(c, ce):
68 return caller.hook_capabilities() + ['execute-debug']
76 if not downtmp: bomb("`close' when not open")
80 caller.hook_forked_inchild()
82 def execute_raw(what, instr, timeout, *popenargs, **popenargsk):
83 debug(" ++ %s" % string.join(popenargs[0]))
84 sp = subprocess.Popen(preexec_fn=preexecfn, *popenargs, **popenargsk)
85 if instr is None: popenargsk['stdin'] = devnull_read
86 timeout_start(timeout)
87 (out, err) = sp.communicate(instr)
89 if err: bomb("%s unexpectedly produced stderr output `%s'" %
94 def execute(cmd_string, cmd_list=[], downp=False, outp=False, timeout=0):
95 cmdl = cmd_string.split()
97 if downp: perhaps_down = down
100 if outp: stdout = subprocess.PIPE
103 cmd = cmdl + cmd_list
104 if len(perhaps_down): cmd = perhaps_down + [' '.join(cmd)]
106 (status, out) = execute_raw(cmdl[0], None, timeout,
109 if status: bomb("%s%s failed (exit status %d)" %
110 ((downp and "(down) " or ""), cmdl[0], status))
112 if outp and out and out[-1]=='\n': out = out[:-1]
118 if downtmp: bomb("`open' when already open")
119 downtmp = caller.hook_open()
122 def cmd_revert(c, ce):
124 if not downtmp: bomb("`revert' when not open")
125 if not 'revert' in caller.hook_capabilities():
126 bomb("`revert' when `revert' not advertised")
129 def down_python_script(gobody, functions=''):
130 # Many things are made much harder by the inability of
131 # dchroot, ssh, et al, to cope without mangling the arguments.
132 # So we run a sub-python on the testbed and feed it a script
133 # on stdin. The sub-python decodes the arguments.
135 script = ( "import urllib\n"
137 "def setfd(fd,fnamee,write,mode=0666):\n"
138 " fname = urllib.unquote(fnamee)\n"
139 " if write: rw = os.O_WRONLY|os.O_CREAT\n"
140 " else: rw = os.O_RDONLY\n"
141 " nfd = os.open(fname, rw, mode)\n"
142 " if fd >= 0: os.dup2(nfd,fd)\n"
145 script += ( " os.environ['TMPDIR']= urllib.unquote('%s')\n" %
146 urllib.quote(downtmp) )
147 script += ( " os.chdir(os.environ['TMPDIR'])\n" )
151 debug("+P ...\n"+script)
153 scripte = urllib.quote(script)
154 cmdl = down + ['python','-c',
155 "'import urllib; s = urllib.unquote(%s); exec s'" %
159 def cmd_execute(c, ce):
160 cmdnumargs(c, ce, 5, None)
161 debug_re = regexp.compile('debug=(\d+)\-(\d+)$')
166 if kw.startswith('debug='):
167 if debug_g: bomb("multiple debug= in execute")
168 m = debug_re.match(kw)
169 if not m: bomb("invalid execute debug arg `%s'" % kw)
171 elif kw.startswith('timeout='):
172 try: timeout = int(kw[8:],0)
173 except ValueError: bomb("invalid timeout arg `%s'" %kw)
174 elif kw.startswith('env='):
175 es = kw[4:]; eq = es.find('=')
176 if eq <= 0: bomb("invalid env arg `%s'" % kw)
177 envs.append((es[:eq], es[eq+1:]))
178 else: bomb("invalid execute kw arg `%s'" % kw)
180 gobody = " import sys\n"
184 (tfd,hfd) = m.groups()
186 gobody += " os.dup2(1,%d)\n" % tfd
190 if ioe == tfd: ioe_tfd = -1
191 gobody += " setfd(%d,'%s',%d)\n" % (
192 ioe_tfd, ce[ioe+2], ioe>0 )
194 gobody += (" os.environ[urllib.unquote('%s')]"
195 " = urllib.unquote('%s')\n"
196 % tuple(map(urllib.quote, e)))
197 gobody += " os.chdir(urllib.unquote('" + ce[5] +"'))\n"
198 gobody += " cmd = '%s'\n" % ce[1]
199 gobody += (" cmd = cmd.split(',')\n"
200 " cmd = map(urllib.unquote, cmd)\n"
203 " if not os.access(c0, os.X_OK):\n"
204 " status = os.stat(c0)\n"
205 " mode = status.st_mode | 0111\n"
206 " os.chmod(c0, mode)\n"
207 " try: os.execvp(c0, cmd)\n"
208 " except OSError, e:\n"
209 " print >>sys.stderr, \"%s: %s\" % (\n"
210 " (c0, os.strerror(e.errno)))\n"
212 cmdl = down_python_script(gobody)
215 (status, out) = execute_raw('sub-python', None, timeout,
216 cmdl, stdout=stdout, stdin=devnull_read,
217 stderr=subprocess.PIPE)
219 raise FailedCmd(['timeout'])
221 if out: bomb("sub-python unexpected produced stdout"
222 " visible to us `%s'" % out)
225 def copyupdown(c, ce, upp):
234 if not sd[0] or not sd[1]:
235 bomb("%s paths must be nonempty" % wh)
236 dirsp = sd[0][-1]=='/'
237 functions = "import errno\n"
238 if dirsp != (sd[1][-1]=='/'):
239 bomb("% paths must agree about directoryness"
240 " (presence or absence of trailing /)" % wh)
242 deststdout = devnull_read
243 srcstdin = devnull_read
247 deststdout = file(sd[idst], 'w')
249 srcstdin = file(sd[isrc], 'r')
250 status = os.fstat(srcstdin.fileno())
251 if status.st_mode & 0111: modestr = ',0777'
252 gobody = " setfd(%s,'%s',%s%s)\n" % (
253 1-upp, sde[iremote], not upp, modestr)
254 gobody += " os.execvp('cat', ['cat'])\n"
257 gobody = " dir = urllib.unquote('%s')\n" % sde[iremote]
259 try: os.mkdir(sd[ilocal])
261 if oe.errno != errno.EEXIST: raise
263 gobody += (" try: os.mkdir(dir)\n"
264 " except OSError, oe:\n"
265 " if oe.errno != errno.EEXIST: raise\n")
266 gobody +=( " os.chdir(dir)\n"
267 " tarcmd = 'tar -f -'.split()\n")
268 localcmdl = 'tar -f -'.split()
269 taropts = [None, None]
270 taropts[isrc] = '-c .'
271 taropts[idst] = '-p -x --no-same-owner'
272 gobody += " tarcmd += '%s'.split()\n" % taropts[iremote]
273 localcmdl += ['-C',sd[ilocal]]
274 localcmdl += taropts[ilocal].split()
275 gobody += " os.execvp('tar', tarcmd)\n";
277 downcmdl = down_python_script(gobody, functions)
279 if upp: cmdls = (downcmdl, localcmdl)
280 else: cmdls = (localcmdl, downcmdl)
282 debug(`["cmdls", `cmdls`]`)
283 debug(`["srcstdin", `srcstdin`, "deststdout", `deststdout`, "devnull_read", devnull_read]`)
285 subprocs = [None,None]
286 debug(" +< %s" % string.join(cmdls[0]))
287 subprocs[0] = subprocess.Popen(cmdls[0], stdin=srcstdin,
288 stdout=subprocess.PIPE, preexec_fn=preexecfn)
289 debug(" +> %s" % string.join(cmdls[1]))
290 subprocs[1] = subprocess.Popen(cmdls[1], stdin=subprocs[0].stdout,
291 stdout=deststdout, preexec_fn=preexecfn)
292 timeout_start(copy_timeout)
294 debug(" +"+"<>"[sdn]+"?");
295 status = subprocs[sdn].wait()
298 bomb("%s %s failed, status %d" %
299 (wh, ['source','destination'][sdn], status))
302 def cmd_copydown(c, ce): copyupdown(c, ce, False)
303 def cmd_copyup(c, ce): copyupdown(c, ce, True)
307 ce = sys.stdin.readline()
308 if not ce: bomb('end of file - caller quit?')
309 ce = ce.rstrip().split()
310 c = map(urllib.unquote, ce)
311 if not c: bomb('empty commands are not permitted')
312 debug('executing '+string.join(ce))
313 try: f = globals()['cmd_'+c[0]]
314 except KeyError: bomb("unknown command `%s'" % ce[0])
319 except FailedCmd, fc:
324 global downtmp, cleaning
327 if downtmp: caller.hook_cleanup()
338 print >> sys.stderr, q.m
340 print >> sys.stderr, "Unexpected cleanup error:"
341 traceback.print_exc()
342 print >> sys.stderr, ''
344 print >> sys.stderr, ("while cleaning up"
345 " because of another error:")
350 global downtmp, cleaning
352 signal_list = [ signal.SIGHUP, signal.SIGTERM,
353 signal.SIGINT, signal.SIGPIPE ]
355 for signum in signal_list: signal.signal(signum, f)
356 def handler(sig, *any):
357 sethandlers(signal.SIG_DFL)
359 os.kill(os.getpid(), sig)
364 while True: command()
367 if q.m: print >> sys.stderr, q.m
371 print >> sys.stderr, "Unexpected error:"
372 traceback.print_exc()
376 signal.signal(signal.SIGALRM, alarm_handler)
377 debug("down = %s" % string.join(down))