| 1 |
# Copyright 2014 Open Source Robotics Foundation, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import asyncio |
| 16 |
import os |
| 17 |
|
| 18 |
try: |
| 19 |
import pty |
| 20 |
has_pty = True |
| 21 |
except ImportError: |
| 22 |
has_pty = False |
| 23 |
|
| 24 |
from ..get_loop_impl import get_loop_impl |
| 25 |
|
| 26 |
|
| 27 |
def get_loop(): |
| 28 |
return get_loop_impl(asyncio) |
| 29 |
|
| 30 |
|
| 31 |
async def _async_execute_process_nopty( |
| 32 |
protocol_class, cmd, cwd, env, shell, |
| 33 |
stderr_to_stdout=True |
| 34 |
): |
| 35 |
loop = get_loop() |
| 36 |
stderr = asyncio.subprocess.PIPE |
| 37 |
if stderr_to_stdout is True: |
| 38 |
stderr = asyncio.subprocess.STDOUT |
| 39 |
# Start the subprocess |
| 40 |
if shell is True: |
| 41 |
transport, protocol = await loop.subprocess_shell( |
| 42 |
protocol_class, " ".join(cmd), cwd=cwd, env=env, |
| 43 |
stderr=stderr, close_fds=False) |
| 44 |
else: |
| 45 |
transport, protocol = await loop.subprocess_exec( |
| 46 |
protocol_class, *cmd, cwd=cwd, env=env, |
| 47 |
stderr=stderr, close_fds=False) |
| 48 |
return transport, protocol |
| 49 |
|
| 50 |
|
| 51 |
if has_pty: |
| 52 |
# If pty is availabe, use them to emulate the tty |
| 53 |
async def _async_execute_process_pty( |
| 54 |
protocol_class, cmd, cwd, env, shell, |
| 55 |
stderr_to_stdout=True |
| 56 |
): |
| 57 |
loop = get_loop() |
| 58 |
# Create the PTY's |
| 59 |
stdout_master, stdout_slave = pty.openpty() |
| 60 |
if stderr_to_stdout: |
| 61 |
stderr_master, stderr_slave = stdout_master, stdout_slave |
| 62 |
else: |
| 63 |
stderr_master, stderr_slave = pty.openpty() |
| 64 |
|
| 65 |
def protocol_factory(): |
| 66 |
return protocol_class( |
| 67 |
stdin=None, |
| 68 |
stdout=stdout_master, |
| 69 |
stderr=stderr_master |
| 70 |
) |
| 71 |
|
| 72 |
# Start the subprocess |
| 73 |
if shell is True: |
| 74 |
transport, protocol = await loop.subprocess_shell( |
| 75 |
protocol_factory, " ".join(cmd), cwd=cwd, env=env, |
| 76 |
stdout=stdout_slave, stderr=stderr_slave, close_fds=False) |
| 77 |
else: |
| 78 |
transport, protocol = await loop.subprocess_exec( |
| 79 |
protocol_factory, *cmd, cwd=cwd, env=env, |
| 80 |
stdout=stdout_slave, stderr=stderr_slave, close_fds=False) |
| 81 |
|
| 82 |
# Close our copies of the slaves, |
| 83 |
# the child's copy of the slave remain open until it terminates |
| 84 |
os.close(stdout_slave) |
| 85 |
if not stderr_to_stdout: |
| 86 |
os.close(stderr_slave) |
| 87 |
|
| 88 |
# Create Protocol classes |
| 89 |
class PtyStdoutProtocol(asyncio.Protocol): |
| 90 |
def connection_made(self, transport): |
| 91 |
if hasattr(protocol, 'on_stdout_open'): |
| 92 |
protocol.on_stdout_open() |
| 93 |
|
| 94 |
def data_received(self, data): |
| 95 |
if hasattr(protocol, 'on_stdout_received'): |
| 96 |
protocol.on_stdout_received(data) |
| 97 |
|
| 98 |
def connection_lost(self, exc): |
| 99 |
if hasattr(protocol, 'on_stdout_close'): |
| 100 |
protocol.on_stdout_close(exc) |
| 101 |
|
| 102 |
class PtyStderrProtocol(asyncio.Protocol): |
| 103 |
def connection_made(self, transport): |
| 104 |
if hasattr(protocol, 'on_stderr_open'): |
| 105 |
protocol.on_stderr_open() |
| 106 |
|
| 107 |
def data_received(self, data): |
| 108 |
if hasattr(protocol, 'on_stderr_received'): |
| 109 |
protocol.on_stderr_received(data) |
| 110 |
|
| 111 |
def connection_lost(self, exc): |
| 112 |
if hasattr(protocol, 'on_stderr_close'): |
| 113 |
protocol.on_stderr_close(exc) |
| 114 |
|
| 115 |
# Add the pty's to the read loop |
| 116 |
# Also store the transport, protocol tuple for each call to |
| 117 |
# connect_read_pipe, to prevent the destruction of the protocol |
| 118 |
# class instance, otherwise no data is received. |
| 119 |
protocol.stdout_tuple = await loop.connect_read_pipe( |
| 120 |
PtyStdoutProtocol, os.fdopen(stdout_master, 'rb', 0)) |
| 121 |
if not stderr_to_stdout: |
| 122 |
protocol.stderr_tuple = await loop.connect_read_pipe( |
| 123 |
PtyStderrProtocol, os.fdopen(stderr_master, 'rb', 0)) |
| 124 |
# Return the protocol and transport |
| 125 |
return transport, protocol |
| 126 |
else: |
| 127 |
_async_execute_process_pty = _async_execute_process_nopty |
| 128 |
|
| 129 |
|
| 130 |
async def async_execute_process( |
| 131 |
protocol_class, cmd=None, cwd=None, env=None, shell=False, |
| 132 |
emulate_tty=False, stderr_to_stdout=True |
| 133 |
): |
| 134 |
if emulate_tty: |
| 135 |
transport, protocol = await _async_execute_process_pty( |
| 136 |
protocol_class, cmd, cwd, env, shell, |
| 137 |
stderr_to_stdout) |
| 138 |
else: |
| 139 |
transport, protocol = await _async_execute_process_nopty( |
| 140 |
protocol_class, cmd, cwd, env, shell, |
| 141 |
stderr_to_stdout) |
| 142 |
return transport, protocol |