# Path: ros2_batch_job/vendor/osrf_pycommon/osrf_pycommon/process_utils/execute_process_nopty.py
# def _close_fds(fds_to_close):
#     # This function is used to close (if not already closed) any fds used
#     for s in fds_to_close:
#         if s is None:
#             continue
#         try:
#             os.close(s)
#         except OSError as exc:
#             # This could raise "OSError: [Errno 9] Bad file descriptor"
#             # If it has already been closed, but that's ok
#             if "Bad file descriptor" not in "{0}".format(exc):
#                 raise
# 
# Path: ros2_batch_job/vendor/osrf_pycommon/osrf_pycommon/process_utils/execute_process_nopty.py
# def _yield_data(p, fds, left_overs, linesep, fds_to_close=None):
#     # This function uses select and subprocess.Popen.poll to collect out
#     # from a subprocess until it has finished, yielding it as it goes
#     fds_to_close = [] if fds_to_close is None else fds_to_close
# 
#     def yield_to_stream(data, stream):
#         if stream == fds[0]:
#             return data, None, None
#         else:
#             return None, data, None
# 
#     try:
#         while p.poll() is None:
#             # If Windows
#             if _is_windows:
#                 for stream in fds:
#                     # This will not produce the best results, but at least
#                     # it will function on Windows. A True IOCP implementation
#                     # would be required to get streaming from Windows streams.
#                     data = stream.readline()
#                     if data:
#                         yield yield_to_stream(data, stream)
#                 continue
#             # Otherwise Unix
#             try:
#                 rlist, wlist, xlist = select.select(fds, [], [])
#             except select.error as exc:
#                 # Ignore EINTR
#                 try:
#                     errnum = exc.errno
#                 except AttributeError:
#                     errnum = exc[0]
#                 if errnum == errno.EINTR:
#                     continue
#                 raise
#             for stream in rlist:
#                 left_over = left_overs[stream]
#                 fileno = getattr(stream, 'fileno', lambda: stream)()
#                 try:
#                     incoming = os.read(fileno, 1024)
#                 except OSError as exc:
#                     # On Linux, when using a pty, in order to get select
#                     # to return when the subprocess finishes, os.close
#                     # must be called on the slave pty fd after forking
#                     # the subprocess with popen. On some versions of
#                     # the Linux kernel this causes an Errno 5 OSError,
#                     # "Input/output error". Therefore, I am explicitly
#                     # catching and passing on this error. In my testing
#                     # this error does not occur repeatedly (it does not
#                     # become a busy wait). See:
#                     #   http://stackoverflow.com/a/12207447/671658
#                     if _is_linux and "Input/output error" in "{0}".format(exc):
#                         continue
#                     raise
#                 if not incoming:
#                     # In this case, EOF has been reached, see docs for os.read
#                     if left_over:
#                         yield yield_to_stream(left_over, stream)
#                     continue
#                 data, left_over = _process_incoming_lines(incoming, left_over)
#                 left_overs[stream] = left_over
#                 yield yield_to_stream(data, stream)
#         # Done
#         yield None, None, p.returncode
#     finally:
#         # Make sure we don't leak file descriptors
#         _close_fds(fds_to_close)


import os
    import pty
import time
from subprocess import Popen
from subprocess import STDOUT
from .execute_process_nopty import _close_fds
from .execute_process_nopty import _yield_data
            except OSError as exc:
                # This can happen if a file you are trying to execute is being
                # written to simultaneously on Linux
                # (doesn't appear to happen on OS X)
                # It seems like the best strategy is to just try again later
                # Worst case is that the file eventually gets deleted, then a
                # different OSError would occur.
                if 'Text file busy' in '{0}'.format(exc):
                    # This is a transient error, try again shortly
                    time.sleep(0.01)
                    continue
                raise
        # This causes the below select to exit when the subprocess closes.
        # On Linux, this sometimes causes Errno 5 OSError's when os.read
        # is called from within _yield_data, so on Linux _yield_data
        # catches and passes on that particular OSError.
        os.close(stdout_slave)
        if not stderr_to_stdout:
            os.close(stderr_slave)

        left_overs = {stdout_master: b'', stderr_master: b''}

        fds = [stdout_master]
        if stderr_master != stdout_master:
            fds.append(stderr_master)
    finally:
        # Make sure we don't leak file descriptors
        _close_fds(fds_to_close)

    # The linesep with pty's always seems to be "\r\n", even on OS X

# Path: ros2_batch_job/vendor/osrf_pycommon/osrf_pycommon/process_utils/execute_process_nopty.py
# def _close_fds(fds_to_close):
#     # This function is used to close (if not already closed) any fds used
#     for s in fds_to_close:
#         if s is None:
#             continue
#         try:
#             os.close(s)
#         except OSError as exc:
#             # This could raise "OSError: [Errno 9] Bad file descriptor"
#             # If it has already been closed, but that's ok
#             if "Bad file descriptor" not in "{0}".format(exc):
#                 raise
# 
# Path: ros2_batch_job/vendor/osrf_pycommon/osrf_pycommon/process_utils/execute_process_nopty.py
# def _yield_data(p, fds, left_overs, linesep, fds_to_close=None):
#     # This function uses select and subprocess.Popen.poll to collect out
#     # from a subprocess until it has finished, yielding it as it goes
#     fds_to_close = [] if fds_to_close is None else fds_to_close
# 
#     def yield_to_stream(data, stream):
#         if stream == fds[0]:
#             return data, None, None
#         else:
#             return None, data, None
# 
#     try:
#         while p.poll() is None:
#             # If Windows
#             if _is_windows:
#                 for stream in fds:
#                     # This will not produce the best results, but at least
#                     # it will function on Windows. A True IOCP implementation
#                     # would be required to get streaming from Windows streams.
#                     data = stream.readline()
#                     if data:
#                         yield yield_to_stream(data, stream)
#                 continue
#             # Otherwise Unix
#             try:
#                 rlist, wlist, xlist = select.select(fds, [], [])
#             except select.error as exc:
#                 # Ignore EINTR
#                 try:
#                     errnum = exc.errno
#                 except AttributeError:
#                     errnum = exc[0]
#                 if errnum == errno.EINTR:
#                     continue
#                 raise
#             for stream in rlist:
#                 left_over = left_overs[stream]
#                 fileno = getattr(stream, 'fileno', lambda: stream)()
#                 try:
#                     incoming = os.read(fileno, 1024)
#                 except OSError as exc:
#                     # On Linux, when using a pty, in order to get select
#                     # to return when the subprocess finishes, os.close
#                     # must be called on the slave pty fd after forking
#                     # the subprocess with popen. On some versions of
#                     # the Linux kernel this causes an Errno 5 OSError,
#                     # "Input/output error". Therefore, I am explicitly
#                     # catching and passing on this error. In my testing
#                     # this error does not occur repeatedly (it does not
#                     # become a busy wait). See:
#                     #   http://stackoverflow.com/a/12207447/671658
#                     if _is_linux and "Input/output error" in "{0}".format(exc):
#                         continue
#                     raise
#                 if not incoming:
#                     # In this case, EOF has been reached, see docs for os.read
#                     if left_over:
#                         yield yield_to_stream(left_over, stream)
#                     continue
#                 data, left_over = _process_incoming_lines(incoming, left_over)
#                 left_overs[stream] = left_over
#                 yield yield_to_stream(data, stream)
#         # Done
#         yield None, None, p.returncode
#     finally:
#         # Make sure we don't leak file descriptors
#         _close_fds(fds_to_close)


# Path: ros2_batch_job/vendor/osrf_pycommon/osrf_pycommon/process_utils/execute_process_pty.py
import os
    import pty
import time
from subprocess import Popen
from subprocess import STDOUT
from .execute_process_nopty import _close_fds
from .execute_process_nopty import _yield_data

            except OSError as exc:
                # This can happen if a file you are trying to execute is being
                # written to simultaneously on Linux
                # (doesn't appear to happen on OS X)
                # It seems like the best strategy is to just try again later
                # Worst case is that the file eventually gets deleted, then a
                # different OSError would occur.
                if 'Text file busy' in '{0}'.format(exc):
                    # This is a transient error, try again shortly
                    time.sleep(0.01)
                    continue
                raise
        # This causes the below select to exit when the subprocess closes.
        # On Linux, this sometimes causes Errno 5 OSError's when os.read
        # is called from within _yield_data, so on Linux _yield_data
        # catches and passes on that particular OSError.
        os.close(stdout_slave)
        if not stderr_to_stdout:
            os.close(stderr_slave)

        left_overs = {stdout_master: b'', stderr_master: b''}

        fds = [stdout_master]
        if stderr_master != stdout_master:
            fds.append(stderr_master)
    finally:
        # Make sure we don't leak file descriptors
        _close_fds(fds_to_close)

    # The linesep with pty's always seems to be "\r\n", even on OS X

    return _yield_data(p, fds, left_overs, "\r\n", fds_to_close)