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')
40 def __init__(q,ec,m): q.ec = ec; q.m = m
43 if not debuglevel: return
44 print >> sys.stderr, progname+": debug:", m
47 raise Quit(12, progname+": failure: %s" % m)
51 def cmdnumargs(c, ce, nargs=0, noptargs=0):
53 bomb("too few arguments to command `%s'" % ce[0])
54 if noptargs is not None and len(c) > 1+nargs+noptargs:
55 bomb("too many arguments to command `%s'" % ce[0])
57 def cmd_capabilities(c, ce):
59 return caller.hook_capabilities() + ['execute-debug']
67 if not downtmp: bomb("`close' when not open")
71 caller.hook_forked_inchild()
73 def execute_raw(what, instr, *popenargs, **popenargsk):
74 debug(" ++ %s" % string.join(popenargs[0]))
75 sp = subprocess.Popen(preexec_fn=preexecfn, *popenargs, **popenargsk)
76 if instr is None: popenargsk['stdin'] = devnull_read
77 (out, err) = sp.communicate(instr)
78 if err: bomb("%s unexpectedly produced stderr output `%s'" %
83 def execute(cmd_string, cmd_list=[], downp=False, outp=False):
84 cmdl = cmd_string.split()
86 if downp: perhaps_down = down
89 if outp: stdout = subprocess.PIPE
93 if len(perhaps_down): cmd = perhaps_down + [' '.join(cmd)]
95 (status, out) = execute_raw(cmdl[0], None, cmd, stdout=stdout)
97 if status: bomb("%s%s failed (exit status %d)" %
98 ((downp and "(down) " or ""), cmdl[0], status))
100 if outp and out and out[-1]=='\n': out = out[:-1]
106 if downtmp: bomb("`open' when already open")
107 downtmp = caller.hook_open()
110 def cmd_revert(c, ce):
112 if not downtmp: bomb("`revert' when not open")
113 if not 'revert' in caller.hook_capabilities():
114 bomb("`revert' when `revert' not advertised")
117 def down_python_script(gobody, functions=''):
118 # Many things are made much harder by the inability of
119 # dchroot, ssh, et al, to cope without mangling the arguments.
120 # So we run a sub-python on the testbed and feed it a script
121 # on stdin. The sub-python decodes the arguments.
123 script = ( "import urllib\n"
125 "def setfd(fd,fnamee,write,mode=0666):\n"
126 " fname = urllib.unquote(fnamee)\n"
127 " if write: rw = os.O_WRONLY|os.O_CREAT\n"
128 " else: rw = os.O_RDONLY\n"
129 " nfd = os.open(fname, rw, mode)\n"
130 " if fd >= 0: os.dup2(nfd,fd)\n"
133 script += ( " os.environ['TMPDIR']= urllib.unquote('%s')\n" %
134 urllib.quote(downtmp) )
135 script += ( " os.chdir(os.environ['TMPDIR'])\n" )
139 debug("+P ...\n"+script)
141 scripte = urllib.quote(script)
142 cmdl = down + ['python','-c',
143 "'import urllib; s = urllib.unquote(%s); exec s'" %
147 def cmd_execute(c, ce):
148 cmdnumargs(c, ce, 5, None)
149 debug_re = regexp.compile('debug=(\d+)\-(\d+)$')
153 if kw.startswith('debug='):
154 if debug_g: bomb("multiple debug= in execute")
155 m = debug_re.match(kw)
156 if not m: bomb("invalid execute debug arg `%s'" % kw)
158 elif kw.startswith('env='):
159 es = kw[4:]; eq = es.find('=')
160 if eq <= 0: bomb("invalid env arg `%s'" % kw)
161 envs.append((es[:eq], es[eq+1:]))
162 else: bomb("invalid execute kw arg `%s'" % kw)
164 gobody = " import sys\n"
168 (tfd,hfd) = m.groups()
170 gobody += " os.dup2(1,%d)\n" % tfd
174 if ioe == tfd: ioe_tfd = -1
175 gobody += " setfd(%d,'%s',%d)\n" % (
176 ioe_tfd, ce[ioe+2], ioe>0 )
178 gobody += (" os.environ[urllib.unquote('%s')]"
179 " = urllib.unquote('%s')\n"
180 % tuple(map(urllib.quote, e)))
181 gobody += " os.chdir(urllib.unquote('" + ce[5] +"'))\n"
182 gobody += " cmd = '%s'\n" % ce[1]
183 gobody += (" cmd = cmd.split(',')\n"
184 " cmd = map(urllib.unquote, cmd)\n"
187 " if not os.access(c0, os.X_OK):\n"
188 " status = os.stat(c0)\n"
189 " mode = status.st_mode | 0111\n"
190 " os.chmod(c0, mode)\n"
191 " try: os.execvp(c0, cmd)\n"
192 " except OSError, e:\n"
193 " print >>sys.stderr, \"%s: %s\" % (\n"
194 " (c0, os.strerror(e.errno)))\n"
196 cmdl = down_python_script(gobody)
198 (status, out) = execute_raw('sub-python', None, cmdl, stdout=stdout,
199 stdin=devnull_read, stderr=subprocess.PIPE)
200 if out: bomb("sub-python unexpected produced stdout"
201 " visible to us `%s'" % out)
204 def copyupdown(c, ce, upp):
213 if not sd[0] or not sd[1]:
214 bomb("%s paths must be nonempty" % wh)
215 dirsp = sd[0][-1]=='/'
216 functions = "import errno\n"
217 if dirsp != (sd[1][-1]=='/'):
218 bomb("% paths must agree about directoryness"
219 " (presence or absence of trailing /)" % wh)
221 deststdout = devnull_read
222 srcstdin = devnull_read
226 deststdout = file(sd[idst], 'w')
228 srcstdin = file(sd[isrc], 'r')
229 status = os.fstat(srcstdin.fileno())
230 if status.st_mode & 0111: modestr = ',0777'
231 gobody = " setfd(%s,'%s',%s%s)\n" % (
232 1-upp, sde[iremote], not upp, modestr)
233 gobody += " os.execvp('cat', ['cat'])\n"
236 gobody = " dir = urllib.unquote('%s')\n" % sde[iremote]
238 try: os.mkdir(sd[ilocal])
240 if oe.errno != errno.EEXIST: raise
242 gobody += (" try: os.mkdir(dir)\n"
243 " except OSError, oe:\n"
244 " if oe.errno != errno.EEXIST: raise\n")
245 gobody +=( " os.chdir(dir)\n"
246 " tarcmd = 'tar -f -'.split()\n")
247 localcmdl = 'tar -f -'.split()
248 taropts = [None, None]
249 taropts[isrc] = '-c .'
250 taropts[idst] = '-p -x --no-same-owner'
251 gobody += " tarcmd += '%s'.split()\n" % taropts[iremote]
252 localcmdl += ['-C',sd[ilocal]]
253 localcmdl += taropts[ilocal].split()
254 gobody += " os.execvp('tar', tarcmd)\n";
256 downcmdl = down_python_script(gobody, functions)
258 if upp: cmdls = (downcmdl, localcmdl)
259 else: cmdls = (localcmdl, downcmdl)
261 debug(`["cmdls", `cmdls`]`)
262 debug(`["srcstdin", `srcstdin`, "deststdout", `deststdout`, "devnull_read", devnull_read]`)
264 subprocs = [None,None]
265 debug(" +< %s" % string.join(cmdls[0]))
266 subprocs[0] = subprocess.Popen(cmdls[0], stdin=srcstdin,
267 stdout=subprocess.PIPE, preexec_fn=preexecfn)
268 debug(" +> %s" % string.join(cmdls[1]))
269 subprocs[1] = subprocess.Popen(cmdls[1], stdin=subprocs[0].stdout,
270 stdout=deststdout, preexec_fn=preexecfn)
272 debug(" +"+"<>"[sdn]+"?");
273 status = subprocs[sdn].wait()
274 if status: bomb("%s %s failed, status %d" %
275 (wh, ['source','destination'][sdn], status))
277 def cmd_copydown(c, ce): copyupdown(c, ce, False)
278 def cmd_copyup(c, ce): copyupdown(c, ce, True)
282 ce = sys.stdin.readline()
283 if not ce: bomb('end of file - caller quit?')
284 ce = ce.rstrip().split()
285 c = map(urllib.unquote, ce)
286 if not c: bomb('empty commands are not permitted')
287 debug('executing '+string.join(ce))
288 try: f = globals()['cmd_'+c[0]]
289 except KeyError: bomb("unknown command `%s'" % ce[0])
296 global downtmp, cleaning
299 if downtmp: caller.hook_cleanup()
310 print >> sys.stderr, q.m
312 print >> sys.stderr, "Unexpected cleanup error:"
313 traceback.print_exc()
314 print >> sys.stderr, ''
316 print >> sys.stderr, ("while cleaning up"
317 " because of another error:")
322 global downtmp, cleaning
324 signal_list = [ signal.SIGHUP, signal.SIGTERM,
325 signal.SIGINT, signal.SIGPIPE ]
327 for signum in signal_list: signal.signal(signum, f)
328 def handler(sig, *any):
329 sethandlers(signal.SIG_DFL)
331 os.kill(os.getpid(), sig)
336 while True: command()
339 if q.m: print >> sys.stderr, q.m
343 print >> sys.stderr, "Unexpected error:"
344 traceback.print_exc()
348 debug("down = %s" % string.join(down))