chiark / gitweb /
copyup copydown of files works
authorIan Jackson <ian@anarres>
Fri, 9 Dec 2005 19:21:36 +0000 (19:21 +0000)
committerIan Jackson <ian@anarres>
Fri, 9 Dec 2005 19:21:36 +0000 (19:21 +0000)
virt-chroot/adt-virt-chroot
virt-chroot/test-input

index 2161aec97e49a18674750b9a2e9030ade8611a2e..481811ce11bb43972bd386f1dce5dacb01cae02e 100755 (executable)
@@ -72,6 +72,7 @@ def cmd_quit(c, ce):
        raise Quit(0, '')
 
 def execute_raw(what, instr, *popenargs, **popenargsk):
+       debug(" ++ %s" % string.join(popenargs[0]))
        sp = subprocess.Popen(*popenargs, **popenargsk)
        if instr is None: popenargsk['stdin'] = devnull_read
        (out, err) = sp.communicate(instr)
@@ -90,12 +91,11 @@ def execute(cmd_string, cmd_list=[], downp=False, outp=False):
        else: stdout = None
 
        cmd = perhaps_down + cmdl + cmd_list
-       debug(" + %s" % string.join(cmd))
 
        (status, out) = execute_raw(cmdl[0], None, cmd, stdout=stdout)
 
        if status: bomb("%s%s failed (exit status %d)" %
-                       ((downp and "(down) " or ""), what, status))
+                       ((downp and "(down) " or ""), cmdl[0], status))
 
        if outp and out and out[-1]=='\n': out = out[:-1]
        return out
@@ -142,6 +142,8 @@ def down_python_script(gobody, functions=''):
        script += (     gobody +
                        "go()\n" )
 
+       debug("+P ...\n"+script)
+
        scripte = urllib.quote(script)
        cmdl = down + ['python','-c',
                "'import urllib; s = urllib.unquote(%s); exec s'" %
@@ -169,37 +171,39 @@ def cmd_execute(c, ce):
 
 def copyupdown(c, ce, up_p):
        cmdnumargs(c, ce, 2)
+       wh = ce[0]
        sd = c[1:]
        sde = ce[1:]
        if not sd[0] or not sd[1]:
-               bomb("%s paths must be nonempty" % ce[0])
+               bomb("%s paths must be nonempty" % wh)
        dirsp = sd[0][-1]=='/'
        functions = "import errno\n"
        if dirsp != (sd[1][-1]=='/'):
                bomb("% paths must agree about directoryness"
-                       " (presence or absence of trailing /)" % ce[0])
+                       " (presence or absence of trailing /)" % wh)
        localfd = None
        deststdout = devnull_read
        srcstdin = devnull_read
        preexecfns = [None, None]
        if not dirsp:
-               gobody = "      setfd(%s,'%s',%s)\n" % (up_p, sde[0], not up_p)
+               gobody = "      setfd(%s,'%s',%s)\n" % (
+                                       1-up_p, sde[0], not up_p)
                gobody += "     os.execvp('cat', ['cat'])\n"
-               if up_p: deststdout = os.open(ce[1], os.WRONLY|os.CREAT, 0666)
-               else: srcstdin = os.open(ce[0], os.RDONLY)
+               if up_p: deststdout = file(sd[1], 'w')
+               else: srcstdin = file(sd[0], 'r')
                localcmdl = ['cat']
        else:
                paxbase = 'pax -p p -x cpio'
-               gobody = "      dir = urllib.unquote('%s'))\n" % ce[0]
+               gobody = "      dir = urllib.unquote('%s'))\n" % sde[0]
                if up_p:
-                       try: os.mkdir(ce[1])
+                       try: os.mkdir(sd[1])
                        except OSError, oe:
                                if oe.errno != errno.EEXIST: raise
                else:
                        gobody += ("    try: os.mkdir(dir)\n"
                                "       except OSError, oe:\n"
                                "               if oe.errno != errno.EEXIST: raise\n")
-               preexecfns[not up_p] = lambda *any: os.chdir(ce[not up_p])
+               preexecfns[not up_p] = lambda *any: os.chdir(sd[not up_p])
                gobody += "     os.chdir(dir)\n"
                gobody += "     paxcmd = '%s'.split()\n" % paxbase
                localcmdl = ['sh','-ec',
@@ -214,18 +218,23 @@ def copyupdown(c, ce, up_p):
 
        downcmdl = down_python_script(gobody, functions)
 
-       if up_p: cmdls = (downcmdl, localcmdll)
-       else: cmdls = (localcmdll, downcmdl)
+       if up_p: cmdls = (downcmdl, localcmdl)
+       else: cmdls = (localcmdl, downcmdl)
+
+       debug(`["cmdls", `cmdls`]`)
+       debug(`["srcstdin", `srcstdin`, "deststdout", `deststdout`, "devnull_read", devnull_read]`)
 
        subprocs = [None,None]
+       debug(" +< %s" % string.join(cmdls[0]))
        subprocs[0] = subprocess.Popen(cmdls[0], stdin=srcstdin,
                        stdout=subprocess.PIPE, preexec_fn=preexecfns[0])
+       debug(" +> %s" % string.join(cmdls[1]))
        subprocs[1] = subprocess.Popen(cmdls[1], stdin=subprocs[0].stdout,
                        stdout=deststdout, preexec_fn=preexecfns[1])
-       for sd in [1,0]:
-               status = subprocs[sd].wait
+       for sdn in [1,0]:
+               status = subprocs[sdn].wait()
                if status: bomb("%s %s failed, status %d" %
-                       (ce[0], ['source','destination'][sd], status))
+                       (wh, ['source','destination'][sdn], status))
 
 def cmd_copydown(c, ce): copyupdown(c, ce, False)
 def cmd_copyup(c, ce): copyupdown(c, ce, True)
@@ -235,9 +244,9 @@ def command():
        ce = ce.rstrip().split()
        c = map(urllib.unquote, ce)
        if not c: bomb('empty commands are not permitted')
+       debug('executing '+string.join(ce))
        try: f = globals()['cmd_'+c[0]]
-       except KeyError: bomb("unknown command `%s'" % cu[0])
-       ce = map(urllib.quote, c) # sanitise the quoted args
+       except KeyError: bomb("unknown command `%s'" % ce[0])
        r = f(c, ce)
        if not r: r = []
        r.insert(0, 'ok')
index d8709567a95509484710efc4ad8ebba960754c3a..aa8b566051c3b134fd9963fbde0f03b2faf36276 100644 (file)
@@ -1,4 +1,7 @@
 open
-execute id /dev/null out err
+copydown test-input foo
+execute ls,-l /dev/null out err
 copyup out /dev/tty
+copyup out outh
+copyup err errh
 quit