def _remote_redirect(self, stdout=None, stderr=None): |
""" return a handle representing a redirection of a remote |
end's stdout to a local file object. with handle.close() |
the redirection will be reverted. |
""" |
clist = [] |
for name, out in ('stdout', stdout), ('stderr', stderr): |
if out: |
outchannel = self.newchannel() |
outchannel.setcallback(getattr(out, 'write', out)) |
channel = self.remote_exec(""" |
import sys |
outchannel = channel.receive() |
outchannel.gateway._ThreadOut(sys, %r).setdefaultwriter(outchannel.send) |
""" % name) |
-> channel.send(outchannel) |
clist.append(channel) |
for c in clist: |
c.waitclose() |
class Handle: |
def close(_): |
for name, out in ('stdout', stdout), ('stderr', stderr): |
if out: |
c = self.remote_exec(""" |
import sys |
channel.gateway._ThreadOut(sys, %r).resetdefault() |
""" % name) |
c.waitclose() |
return Handle() |