Commits

Ronny Pfannschmidt committed 80e0e40

bootstrap split

  • Participants
  • Parent commits 8b0da77

Comments (0)

Files changed (1)

File bootstrap-split

 # HG changeset patch
-# Parent 2215b64e94d04807b74648343fb052abef1b888e
+# Parent e90f819f04d0127f4d7a0a242423c0be4491f033
+
+diff --git a/execnet/gateway.py b/execnet/gateway.py
+--- a/execnet/gateway.py
++++ b/execnet/gateway.py
+@@ -16,7 +16,6 @@ class Gateway(gateway_base.BaseGateway):
+ 
+     def __init__(self, io, id):
+         super(Gateway, self).__init__(io=io, id=id, _startcount=1)
+-        self._remote_bootstrap_gateway(io)
+         self._initreceive()
+ 
+     @property
+@@ -63,18 +62,6 @@ class Gateway(gateway_base.BaseGateway):
+         data = serialize(self._strconfig)
+         self._send(Message.RECONFIGURE, data=data)
+ 
+-    def _remote_bootstrap_gateway(self, io):
+-        """ send gateway bootstrap code to a remote Python interpreter
+-            endpoint, which reads from io for a string to execute.
+-        """
+-        sendexec(io,
+-            inspect.getsource(gateway_base),
+-            self._remotesetup,
+-            "io.write('1'.encode('ascii'))",
+-            "serve(io, id='%s-slave')" % self.id,
+-        )
+-        s = io.read(1)
+-        assert s == "1".encode('ascii')
+ 
+     def _rinfo(self, update=False):
+         """ return some sys/env information from remote. """
+@@ -231,26 +218,6 @@ class PopenGatewayBase(Gateway):
+             io.popen.pid = self.remote_exec(
+                 "import os; channel.send(os.getpid())").receive()
+ 
+-class PopenGateway(PopenGatewayBase):
+-    """ This Gateway provides interaction with a newly started
+-        python subprocess.
+-    """
+-
+-    def _remote_bootstrap_gateway(self, io):
+-        sendexec(io,
+-            "import sys",
+-            "sys.path.insert(0, %r)" % importdir,
+-            "from execnet.gateway_base import serve, init_popen_io",
+-            "sys.stdout.write('1')",
+-            "sys.stdout.flush()",
+-            "serve(init_popen_io(), id='%s-slave')" % self.id,
+-        )
+-        s = io.read(1)
+-        assert s == "1".encode('ascii')
+-
+-def sendexec(io, *sources):
+-    source = "\n".join(sources)
+-    io.write((repr(source)+ "\n").encode('ascii'))
+ 
+ class HostNotFound(Exception):
+     pass
+diff --git a/execnet/gateway_bootstrap.py b/execnet/gateway_bootstrap.py
+new file mode 100644
+--- /dev/null
++++ b/execnet/gateway_bootstrap.py
+@@ -0,0 +1,54 @@
++"""
++code to initialize the remote side of a gateway once the io is created
++"""
++import os
++import inspect
++import execnet
++from execnet import gateway_base
++importdir = os.path.dirname(os.path.dirname(execnet.__file__))
++
++def bootstrap_popen(io, spec):
++    sendexec(io,
++        "import sys",
++        "sys.path.insert(0, %r)" % importdir,
++        "from execnet.gateway_base import serve, init_popen_io",
++        "sys.stdout.write('1')",
++        "sys.stdout.flush()",
++        "serve(init_popen_io(), id='%s-slave')" % spec.id,
++    )
++    s = io.read(1)
++    assert s == "1".encode('ascii')
++
++
++def bootstrap_ssh(io, spec):
++    sendexec(io,
++        inspect.getsource(gateway_base),
++        'io = init_popen_io()',
++        "io.write('1'.encode('ascii'))",
++        "serve(io, id='%s-slave')" % spec.id,
++    )
++    s = io.read(1)
++    assert s == "1".encode('ascii')
++    
++def bootstrap_socket(io, id):
++    #XXX: switch to spec
++    from execnet.gateway_socket import SocketIO
++
++    sendexec(io,
++        inspect.getsource(gateway_base),
++        'import socket',
++        inspect.getsource(SocketIO),
++        "io = SocketIO(clientsock)",
++        "io.write('1'.encode('ascii'))",
++        "serve(io, id='%s-slave')" % id,
++    )
++    s = io.read(1)
++    assert s == "1".encode('ascii')
++
++
++
++
++def sendexec(io, *sources):
++    source = "\n".join(sources)
++    io.write((repr(source)+ "\n").encode('ascii'))
++
+diff --git a/execnet/gateway_socket.py b/execnet/gateway_socket.py
+--- a/execnet/gateway_socket.py
++++ b/execnet/gateway_socket.py
+@@ -2,10 +2,12 @@ import socket
+ from execnet.gateway import Gateway, HostNotFound
+ import os, sys, inspect
+ 
++
+ try: bytes
+ except NameError: bytes = str
+ 
+ class SocketIO:
++
+     error = (socket.error, EOFError)
+     def __init__(self, sock):
+         self.sock = sock
+@@ -53,7 +55,6 @@ class SocketGateway(Gateway):
+         SocketGateway connections.
+     """
+ 
+-    _remotesetup = "import socket\n%s\nio = SocketIO(clientsock)" % inspect.getsource(SocketIO)
+ 
+     def __init__(self, host, port, id):
+         """ instantiate a gateway to a process accessed
+@@ -68,6 +69,9 @@ class SocketGateway(Gateway):
+             sock.connect((host, port))
+         except socket.gaierror:
+             raise HostNotFound(str(sys.exc_info()[1]))
++        #XXX: temporary
++        from execnet.gateway_bootstrap import bootstrap_socket
++        bootstrap_socket(io, id)
+         super(SocketGateway, self).__init__(io=io, id=id)
+ 
+     def new_remote(cls, gateway, id, hostport=None):
+diff --git a/execnet/multi.py b/execnet/multi.py
+--- a/execnet/multi.py
++++ b/execnet/multi.py
+@@ -7,7 +7,7 @@ Managing Gateway Groups and interactions
+ import os, sys, atexit
+ import execnet
+ from execnet import XSpec
+-from execnet import gateway, gateway_io
++from execnet import gateway, gateway_io, gateway_bootstrap
+ from execnet.gateway_base import queue, reraise, trace, TimeoutError
+ 
+ NO_ENDMARKER_WANTED = object()
+@@ -76,10 +76,13 @@ class Group:
+         self.allocate_id(spec)
+         if spec.popen:
+             io = gateway_io.create_io(spec)
+-            gw = gateway.PopenGateway(io, spec.id)
++            gateway_bootstrap.bootstrap_popen(io, spec)
++            gw = gateway.Gateway(io, spec.id)
++            #XXX jython pid fix
+         elif spec.ssh:
+             io = gateway_io.create_io(spec)
+-            gw = gateway.SshGateway(io, spec.id)
++            gateway_bootstrap.bootstrap_ssh(io, spec)
++            gw = gateway.Gateway(io, spec.id)
+         elif spec.socket:
+             assert not spec.python, (
+                 "socket: specifying python executables not yet supported")