2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
import os, inspect, socket
|
|
|
|
import sys
|
|
|
|
from py.magic import autopath ; mypath = autopath()
|
|
|
|
|
|
|
|
import py
|
|
|
|
|
|
|
|
# the list of modules that must be send to the other side
|
|
|
|
# for bootstrapping gateways
|
2007-02-03 03:57:47 +08:00
|
|
|
# XXX we'd like to have a leaner and meaner bootstrap mechanism
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
startup_modules = [
|
|
|
|
'py.__.thread.io',
|
|
|
|
'py.__.execnet.inputoutput',
|
|
|
|
'py.__.execnet.gateway',
|
|
|
|
'py.__.execnet.message',
|
|
|
|
'py.__.execnet.channel',
|
|
|
|
]
|
|
|
|
|
|
|
|
def getsource(dottedname):
|
|
|
|
mod = __import__(dottedname, None, None, ['__doc__'])
|
|
|
|
return inspect.getsource(mod)
|
|
|
|
|
|
|
|
from py.__.execnet import inputoutput, gateway
|
|
|
|
|
|
|
|
class InstallableGateway(gateway.Gateway):
|
|
|
|
""" initialize gateways on both sides of a inputoutput object. """
|
|
|
|
def __init__(self, io):
|
2007-02-02 09:02:55 +08:00
|
|
|
self._remote_bootstrap_gateway(io)
|
2007-02-03 03:57:47 +08:00
|
|
|
super(InstallableGateway, self).__init__(io=io, _startcount=1)
|
2007-08-08 01:34:59 +08:00
|
|
|
# XXX we dissallow execution form the other side
|
|
|
|
self._initreceive(requestqueue=False)
|
2007-01-24 22:24:01 +08:00
|
|
|
|
2007-02-02 09:02:55 +08:00
|
|
|
def _remote_bootstrap_gateway(self, io, extra=''):
|
2007-01-24 22:24:01 +08:00
|
|
|
""" return Gateway with a asynchronously remotely
|
|
|
|
initialized counterpart Gateway (which may or may not succeed).
|
|
|
|
Note that the other sides gateways starts enumerating
|
|
|
|
its channels with even numbers while the sender
|
|
|
|
gateway starts with odd numbers. This allows to
|
|
|
|
uniquely identify channels across both sides.
|
|
|
|
"""
|
2007-01-28 16:48:59 +08:00
|
|
|
bootstrap = [extra]
|
2007-01-24 22:24:01 +08:00
|
|
|
bootstrap += [getsource(x) for x in startup_modules]
|
2007-02-03 03:57:47 +08:00
|
|
|
bootstrap += [io.server_stmt,
|
2007-08-08 01:34:59 +08:00
|
|
|
"Gateway(io=io, _startcount=2)._servemain()",
|
2007-02-03 03:57:47 +08:00
|
|
|
]
|
2007-01-24 22:24:01 +08:00
|
|
|
source = "\n".join(bootstrap)
|
|
|
|
self._trace("sending gateway bootstrap code")
|
|
|
|
io.write('%r\n' % source)
|
|
|
|
|
|
|
|
class PopenCmdGateway(InstallableGateway):
|
|
|
|
def __init__(self, cmd):
|
|
|
|
infile, outfile = os.popen2(cmd)
|
|
|
|
io = inputoutput.Popen2IO(infile, outfile)
|
|
|
|
super(PopenCmdGateway, self).__init__(io=io)
|
|
|
|
|
|
|
|
class PopenGateway(PopenCmdGateway):
|
2007-02-03 03:57:47 +08:00
|
|
|
""" This Gateway provides interaction with a newly started
|
|
|
|
python subprocess.
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
def __init__(self, python=sys.executable):
|
2007-02-03 03:57:47 +08:00
|
|
|
""" instantiate a gateway to a subprocess
|
|
|
|
started with the given 'python' executable.
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
cmd = '%s -u -c "exec input()"' % python
|
|
|
|
super(PopenGateway, self).__init__(cmd)
|
|
|
|
|
2007-02-02 09:02:55 +08:00
|
|
|
def _remote_bootstrap_gateway(self, io, extra=''):
|
2007-02-03 03:57:47 +08:00
|
|
|
# have the subprocess use the same PYTHONPATH and py lib
|
2007-01-24 22:24:01 +08:00
|
|
|
x = py.path.local(py.__file__).dirpath().dirpath()
|
|
|
|
ppath = os.environ.get('PYTHONPATH', '')
|
|
|
|
plist = [str(x)] + ppath.split(':')
|
|
|
|
s = "\n".join([extra,
|
|
|
|
"import sys ; sys.path[:0] = %r" % (plist,),
|
|
|
|
"import os ; os.environ['PYTHONPATH'] = %r" % ppath,
|
2007-02-02 09:44:31 +08:00
|
|
|
str(py.code.Source(stdouterrin_setnull)),
|
2007-02-03 03:57:47 +08:00
|
|
|
"stdouterrin_setnull()",
|
2007-01-24 22:24:01 +08:00
|
|
|
""
|
|
|
|
])
|
2007-02-02 09:02:55 +08:00
|
|
|
super(PopenGateway, self)._remote_bootstrap_gateway(io, s)
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
class SocketGateway(InstallableGateway):
|
2007-02-03 03:57:47 +08:00
|
|
|
""" This Gateway provides interaction with a remote process
|
|
|
|
by connecting to a specified socket. On the remote
|
|
|
|
side you need to manually start a small script
|
|
|
|
(py/execnet/script/socketserver.py) that accepts
|
|
|
|
SocketGateway connections.
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
def __init__(self, host, port):
|
2007-02-03 03:59:10 +08:00
|
|
|
""" instantiate a gateway to a process accessed
|
2007-02-03 03:57:47 +08:00
|
|
|
via a host/port specified socket.
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
self.host = host = str(host)
|
|
|
|
self.port = port = int(port)
|
2007-02-03 03:57:47 +08:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2007-01-24 22:24:01 +08:00
|
|
|
sock.connect((host, port))
|
|
|
|
io = inputoutput.SocketIO(sock)
|
2007-01-28 16:48:59 +08:00
|
|
|
super(SocketGateway, self).__init__(io=io)
|
2007-02-02 07:57:55 +08:00
|
|
|
self.remoteaddress = '%s:%d' % (self.host, self.port)
|
2007-01-24 22:24:01 +08:00
|
|
|
|
2007-02-03 03:57:47 +08:00
|
|
|
def new_remote(cls, gateway, hostport=None):
|
|
|
|
""" return a new (connected) socket gateway, instatiated
|
|
|
|
indirectly through the given 'gateway'.
|
2007-01-24 22:24:01 +08:00
|
|
|
"""
|
|
|
|
if hostport is None:
|
2007-01-28 18:14:22 +08:00
|
|
|
host, port = ('', 0) # XXX works on all platforms?
|
2007-01-24 22:24:01 +08:00
|
|
|
else:
|
|
|
|
host, port = hostport
|
|
|
|
socketserverbootstrap = py.code.Source(
|
2007-02-03 03:57:47 +08:00
|
|
|
mypath.dirpath('script', 'socketserver.py').read('rU'), """
|
2007-01-24 22:24:01 +08:00
|
|
|
import socket
|
|
|
|
sock = bind_and_listen((%r, %r))
|
2007-01-28 18:14:22 +08:00
|
|
|
port = sock.getsockname()
|
|
|
|
channel.send(port)
|
2007-01-24 22:24:01 +08:00
|
|
|
startserver(sock)
|
2007-02-03 03:57:47 +08:00
|
|
|
""" % (host, port)
|
|
|
|
)
|
2007-01-24 22:24:01 +08:00
|
|
|
# execute the above socketserverbootstrap on the other side
|
|
|
|
channel = gateway.remote_exec(socketserverbootstrap)
|
2007-01-28 18:14:22 +08:00
|
|
|
(realhost, realport) = channel.receive()
|
2007-02-03 03:57:47 +08:00
|
|
|
#gateway._trace("new_remote received"
|
|
|
|
# "port=%r, hostname = %r" %(realport, hostname))
|
2007-01-28 18:14:22 +08:00
|
|
|
return py.execnet.SocketGateway(host, realport)
|
2007-02-03 03:57:47 +08:00
|
|
|
new_remote = classmethod(new_remote)
|
|
|
|
|
2007-01-24 22:24:01 +08:00
|
|
|
|
|
|
|
class SshGateway(PopenCmdGateway):
|
2007-02-03 03:57:47 +08:00
|
|
|
""" This Gateway provides interaction with a remote process,
|
|
|
|
established via the 'ssh' command line binary.
|
|
|
|
The remote side needs to have a Python interpreter executable.
|
|
|
|
"""
|
2007-01-24 22:24:01 +08:00
|
|
|
def __init__(self, sshaddress, remotepython='python', identity=None):
|
2007-02-03 03:57:47 +08:00
|
|
|
""" instantiate a remote ssh process with the
|
|
|
|
given 'sshaddress' and remotepython version.
|
|
|
|
you may specify an 'identity' filepath.
|
|
|
|
"""
|
2007-02-02 07:57:55 +08:00
|
|
|
self.remoteaddress = sshaddress
|
2007-01-24 22:24:01 +08:00
|
|
|
remotecmd = '%s -u -c "exec input()"' % (remotepython,)
|
|
|
|
cmdline = [sshaddress, remotecmd]
|
|
|
|
# XXX Unix style quoting
|
|
|
|
for i in range(len(cmdline)):
|
|
|
|
cmdline[i] = "'" + cmdline[i].replace("'", "'\\''") + "'"
|
|
|
|
cmd = 'ssh -C'
|
|
|
|
if identity is not None:
|
|
|
|
cmd += ' -i %s' % (identity,)
|
|
|
|
cmdline.insert(0, cmd)
|
|
|
|
super(SshGateway, self).__init__(' '.join(cmdline))
|
2007-02-02 09:44:31 +08:00
|
|
|
|
|
|
|
def _remote_bootstrap_gateway(self, io, s=""):
|
|
|
|
extra = "\n".join([
|
|
|
|
str(py.code.Source(stdouterrin_setnull)),
|
|
|
|
"stdouterrin_setnull()",
|
|
|
|
s,
|
|
|
|
])
|
|
|
|
super(SshGateway, self)._remote_bootstrap_gateway(io, extra)
|
|
|
|
|
2007-02-03 03:57:47 +08:00
|
|
|
|
2007-02-02 09:44:31 +08:00
|
|
|
def stdouterrin_setnull():
|
2007-02-03 03:57:47 +08:00
|
|
|
""" redirect file descriptors 0 and 1 (and possibly 2) to /dev/null.
|
|
|
|
note that this function may run remotely without py lib support.
|
|
|
|
"""
|
2007-02-02 09:44:31 +08:00
|
|
|
# complete confusion (this is independent from the sys.stdout
|
|
|
|
# and sys.stderr redirection that gateway.remote_exec() can do)
|
|
|
|
# note that we redirect fd 2 on win too, since for some reason that
|
|
|
|
# blocks there, while it works (sending to stderr if possible else
|
|
|
|
# ignoring) on *nix
|
|
|
|
import sys, os
|
|
|
|
try:
|
|
|
|
devnull = os.devnull
|
|
|
|
except AttributeError:
|
|
|
|
if os.name == 'nt':
|
|
|
|
devnull = 'NUL'
|
|
|
|
else:
|
|
|
|
devnull = '/dev/null'
|
2007-02-03 20:31:47 +08:00
|
|
|
# stdin
|
2007-02-02 09:44:31 +08:00
|
|
|
sys.stdin = os.fdopen(os.dup(0), 'rb', 0)
|
|
|
|
fd = os.open(devnull, os.O_RDONLY)
|
|
|
|
os.dup2(fd, 0)
|
|
|
|
os.close(fd)
|
2007-02-03 20:31:47 +08:00
|
|
|
|
|
|
|
# stdout
|
|
|
|
sys.stdout = os.fdopen(os.dup(1), 'wb', 0)
|
2007-02-02 09:44:31 +08:00
|
|
|
fd = os.open(devnull, os.O_WRONLY)
|
|
|
|
os.dup2(fd, 1)
|
2007-02-03 20:31:47 +08:00
|
|
|
|
|
|
|
# stderr for win32
|
2007-02-02 09:44:31 +08:00
|
|
|
if os.name == 'nt':
|
2007-02-03 20:31:47 +08:00
|
|
|
sys.stderr = os.fdopen(os.dup(2), 'wb', 0)
|
2007-02-02 09:44:31 +08:00
|
|
|
os.dup2(fd, 2)
|
|
|
|
os.close(fd)
|