226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 | |
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() | |