292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 | |
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() | |