| 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 os |
| 16 |
|
| 17 |
try: |
| 18 |
import pty |
| 19 |
except ImportError: |
| 20 |
# to support --cover-inclusive on Windows |
| 21 |
if os.name not in ['nt']: |
| 22 |
raise |
| 23 |
|
| 24 |
from subprocess import Popen |
| 25 |
from subprocess import STDOUT |
| 26 |
|
| 27 |
import time |
| 28 |
|
| 29 |
from .execute_process_nopty import _close_fds |
| 30 |
from .execute_process_nopty import _yield_data |
| 31 |
|
| 32 |
|
| 33 |
def _execute_process_pty(cmd, cwd, env, shell, stderr_to_stdout=True): |
| 34 |
stdout_master, stdout_slave = None, None |
| 35 |
stderr_master, stderr_slave = None, None |
| 36 |
fds_to_close = [stdout_master, stdout_slave, stderr_master, stderr_slave] |
| 37 |
try: |
| 38 |
stdout_master, stdout_slave = pty.openpty() |
| 39 |
if stderr_to_stdout: |
| 40 |
stderr_master, stderr_slave = stdout_master, stdout_slave |
| 41 |
else: |
| 42 |
stderr_master, stderr_slave = pty.openpty() |
| 43 |
|
| 44 |
p = None |
| 45 |
while p is None: |
| 46 |
try: |
| 47 |
p = Popen( |
| 48 |
cmd, |
| 49 |
stdin=stdout_slave, stdout=stderr_slave, stderr=STDOUT, |
| 50 |
cwd=cwd, env=env, shell=shell, close_fds=False) |
| 51 |
except OSError as exc: |
| 52 |
# This can happen if a file you are trying to execute is being |
| 53 |
# written to simultaneously on Linux |
| 54 |
# (doesn't appear to happen on OS X) |
| 55 |
# It seems like the best strategy is to just try again later |
| 56 |
# Worst case is that the file eventually gets deleted, then a |
| 57 |
# different OSError would occur. |
| 58 |
if 'Text file busy' in '{0}'.format(exc): |
| 59 |
# This is a transient error, try again shortly |
| 60 |
time.sleep(0.01) |
| 61 |
continue |
| 62 |
raise |
| 63 |
# This causes the below select to exit when the subprocess closes. |
| 64 |
# On Linux, this sometimes causes Errno 5 OSError's when os.read |
| 65 |
# is called from within _yield_data, so on Linux _yield_data |
| 66 |
# catches and passes on that particular OSError. |
| 67 |
os.close(stdout_slave) |
| 68 |
if not stderr_to_stdout: |
| 69 |
os.close(stderr_slave) |
| 70 |
|
| 71 |
left_overs = {stdout_master: b'', stderr_master: b''} |
| 72 |
|
| 73 |
fds = [stdout_master] |
| 74 |
if stderr_master != stdout_master: |
| 75 |
fds.append(stderr_master) |
| 76 |
finally: |
| 77 |
# Make sure we don't leak file descriptors |
| 78 |
_close_fds(fds_to_close) |
| 79 |
|
| 80 |
# The linesep with pty's always seems to be "\r\n", even on OS X |
| 81 |
return _yield_data(p, fds, left_overs, "\r\n", fds_to_close) |