import py import py.__impl__.initpkg def installremotepy(gw): if hasattr(gw, '__remotepyinstalled'): return channel = gw.remote_exec(""" import new, sys subchannel = channel.gateway.remote_exec('''if 1: #print 'hi there' import py rootpath = py.path.local(py.__file__).dirpath() for shortname in channel: #print 'got', shortname path = rootpath.join(*shortname.split('.')) #print 'checking', path if path.check(dir=1): path = path.join('__init__.py') else: path = path.new(ext='.py') if not path.check(file=1): channel.send(None) else: channel.send(path.read()) #print 'waiting again' ''') def pyimplimport(name, globals=None, locals=None, fromlist=None): # no relative imports from the py lib! #print 'importing', name, fromlist #r = sys.modules.keys() #r.sort() #print 'by the way sys.modules.keys()=', r #print 'globals __name__ is', (globals or {}).get('__name__') if name.startswith('py.__impl__'): if name in sys.modules: mod = sys.modules[name] else: shorter_name = name[len('py.__impl__'):] #print repr(subchannel) #print '- sending ->', shorter_name subchannel.send(shorter_name) #print 'waiting for an answer' modsource = subchannel.receive() #print '<- got -', repr(modsource)[:30] if modsource is None: raise ImportError, name mod = new.module(name) sys.modules[name] = mod mod.__path__ = [''] try: co = compile(modsource, name, 'exec') exec co in mod.__dict__ except Exception, e: if name in sys.modules: del sys.modules[name] raise if fromlist: for x in fromlist: if hasattr(mod, x): continue try: submod = __import__(name+'.'+x, globals, locals, ['__doc__']) except ImportError: pass else: setattr(mod, x, submod) return mod else: return py else: return original_import(name, globals, locals, fromlist) py = new.module('py') py.__file__ = '' py.__path__ = [] initpkg = new.module('py.initpkg') sys.modules['py.initpkg'] = initpkg sys.modules['py'] = py #print 'receiving 0' recv1 = channel.receive() #print 'receiving 1' recv2 = channel.receive() #print 'receiving 2' import __builtin__ original_import = __import__ __builtin__.__import__ = pyimplimport #print 'waiting 1' co = compile(recv1, 'py.initpkg', 'exec') #print 'step 2' exec co in initpkg.__dict__ #print 'waiting 3' co = compile(recv2, 'py', 'exec') #print 'step 4' exec co in py.__dict__ del co #print 'left 5' waittag = channel.receive() # shut down subchannel.close() __builtin__.__import__ = original_import """) channel.send(str(py.code.Source(py.__impl__.initpkg))) channel.send(str(py.code.Source(py))) def shutdown(): channel.send(None) channel.waitclose(5.0) previous_exit() previous_exit = gw.exit gw.exit = shutdown setattr(gw, '__remotepyinstalled', True) def test_magma(): gw = py.execnet.SshGateway('magma') installremotepy(gw) channel = gw.remote_exec(''' import py channel.send(str(py.path.local().listdir())) ''') print channel.receive() channel.waitclose(2.0) ##def test_1(): ## gw = py.execnet.SocketGateway('central', 8888) ## installremotepy(gw) ## channel = gw.remote_exec(''' ## import py ## channel.send(str(py.path.local().listdir())) ## ''') ## print channel.receive() ## channel.waitclose(2.0)