Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 81 lines (68 sloc) 2.116 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
"""
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
from execnet.gateway import Gateway
importdir = os.path.dirname(os.path.dirname(execnet.__file__))


class HostNotFound(Exception):
    pass


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):
    try:
        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')
    except EOFError:
        ret = io.wait()
        if ret == 255:
            raise HostNotFound(io.remoteaddress)


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'))


def bootstrap(io, spec):
    if spec.popen:
        bootstrap_popen(io, spec)
    elif spec.ssh:
        bootstrap_ssh(io, spec)
    elif spec.socket:
        bootstrap_socket(io, spec)
    else:
        raise ValueError('unknown gateway type, cant bootstrap')
    gw = Gateway(io, spec.id)
    if hasattr(io, 'popen'):
        # fix for jython 2.5.1
        if io.popen.pid is None:
            io.popen.pid = gw.remote_exec(
                "import os; channel.send(os.getpid())").receive()
    return gw
Something went wrong with that request. Please try again.