Example #1
0
def _init_connection(remote_cmd):
    """Run remote_cmd, register connection, and then return it

    If remote_cmd is None, then the local connection will be
    returned.  This also updates some settings on the remote side,
    like global settings, its connection number, and verbosity.

    """
    global _processes
    if not remote_cmd:
        return Globals.local_connection

    log.Log("Executing remote command {rc}".format(rc=_safe_str(remote_cmd)),
            log.INFO)
    try:
        # we need buffered read on SSH communications, hence using
        # default value for bufsize parameter
        if os.name == 'nt':
            # FIXME workaround because python 3.7 doesn't yet accept bytes
            process = subprocess.Popen(os.fsdecode(remote_cmd),
                                       shell=True,
                                       stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE)
        else:
            process = subprocess.Popen(remote_cmd,
                                       shell=True,
                                       stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE)
        (stdin, stdout) = (process.stdin, process.stdout)
        # only to avoid resource warnings about subprocess still running
        _processes.append(process)
    except OSError:
        (stdin, stdout) = (None, None)
    conn_number = len(Globals.connections)
    conn = connection.PipeConnection(stdout, stdin, conn_number)

    if not _validate_connection_version(conn, remote_cmd):
        return None
    log.Log("Registering connection {co}".format(co=conn_number), log.DEBUG)
    _init_connection_routing(conn, conn_number, remote_cmd)
    _init_connection_settings(conn)
    return conn
Example #2
0
 def run(self):
     return connection.PipeConnection(sys.stdin.buffer,
                                      sys.stdout.buffer).Server()