@contextmanager
def run_interactive_shell_command(command, **kwargs):
    '\n    Runs a single command in shell and provides stdout, stderr and stdin\n    streams.\n\n    This function creates a context manager that sets up the process (using\n    ``subprocess.Popen()``), returns to caller and waits for process to exit on\n    leaving.\n\n    By default the process is opened in ``universal_newlines`` mode and creates\n    pipes for all streams (stdout, stderr and stdin) using ``subprocess.PIPE``\n    special value. These pipes are closed automatically, so if you want to get\n    the contents of the streams you should retrieve them before the context\n    manager exits.\n\n    >>> with run_interactive_shell_command(["echo", "TEXT"]) as p:\n    ...     stdout = p.stdout\n    ...     stdout_text = stdout.read()\n    >>> stdout_text\n    \'TEXT\\n\'\n    >>> stdout.closed\n    True\n\n    Custom streams provided are not closed except of ``subprocess.PIPE``.\n\n    >>> from tempfile import TemporaryFile\n    >>> stream = TemporaryFile()\n    >>> with run_interactive_shell_command(["echo", "TEXT"],\n    ...                                    stdout=stream) as p:\n    ...     stderr = p.stderr\n    >>> stderr.closed\n    True\n    >>> stream.closed\n    False\n\n    :param command: The command to run on shell. This parameter can either\n                    be a sequence of arguments that are directly passed to\n                    the process or a string. A string gets splitted beforehand\n                    using ``shlex.split()``. If providing ``shell=True`` as a\n                    keyword-argument, no ``shlex.split()`` is performed and the\n                    command string goes directly to ``subprocess.Popen()``.\n    :param kwargs:  Additional keyword arguments to pass to\n                    ``subprocess.Popen`` that are used to spawn the process.\n    :return:        A context manager yielding the process started from the\n                    command.\n    '
    if ((not kwargs.get('shell', False)) and isinstance(command, str)):
        command = shlex.split(command)
    else:
        command = list(command)
    if (platform.system() == 'Windows'):
        command[0] = which(command[0])
    args = {'stdout': PIPE, 'stderr': PIPE, 'stdin': PIPE, 'universal_newlines': True}
    args.update(kwargs)
    process = Popen(command, **args)
    try:
        (yield process)
    finally:
        if (args['stdout'] is PIPE):
            process.stdout.close()
        if (args['stderr'] is PIPE):
            process.stderr.close()
        if (args['stdin'] is PIPE):
            process.stdin.close()
        process.wait()
Runs a single command in shell and provides stdout, stderr and stdin
streams.

This function creates a context manager that sets up the process (using
``subprocess.Popen()``), returns to caller and waits for process to exit on
leaving.

By default the process is opened in ``universal_newlines`` mode and creates
pipes for all streams (stdout, stderr and stdin) using ``subprocess.PIPE``
special value. These pipes are closed automatically, so if you want to get
the contents of the streams you should retrieve them before the context
manager exits.

>>> with run_interactive_shell_command(["echo", "TEXT"]) as p:
...     stdout = p.stdout
...     stdout_text = stdout.read()
>>> stdout_text
'TEXT\n'
>>> stdout.closed
True

Custom streams provided are not closed except of ``subprocess.PIPE``.

>>> from tempfile import TemporaryFile
>>> stream = TemporaryFile()
>>> with run_interactive_shell_command(["echo", "TEXT"],
...                                    stdout=stream) as p:
...     stderr = p.stderr
>>> stderr.closed
True
>>> stream.closed
False

:param command: The command to run on shell. This parameter can either
                be a sequence of arguments that are directly passed to
                the process or a string. A string gets splitted beforehand
                using ``shlex.split()``. If providing ``shell=True`` as a
                keyword-argument, no ``shlex.split()`` is performed and the
                command string goes directly to ``subprocess.Popen()``.
:param kwargs:  Additional keyword arguments to pass to
                ``subprocess.Popen`` that are used to spawn the process.
:return:        A context manager yielding the process started from the
                command.
@contextmanager
def run_interactive_shell_command(command, **kwargs):
    '\n    Runs a single command in shell and provides stdout, stderr and stdin\n    streams.\n\n    This function creates a context manager that sets up the process (using\n    ``subprocess.Popen()``), returns to caller and waits for process to exit on\n    leaving.\n\n    By default the process is opened in ``universal_newlines`` mode and creates\n    pipes for all streams (stdout, stderr and stdin) using ``subprocess.PIPE``\n    special value. These pipes are closed automatically, so if you want to get\n    the contents of the streams you should retrieve them before the context\n    manager exits.\n\n    >>> with run_interactive_shell_command(["echo", "TEXT"]) as p:\n    ...     stdout = p.stdout\n    ...     stdout_text = stdout.read()\n    >>> stdout_text\n    \'TEXT\\n\'\n    >>> stdout.closed\n    True\n\n    Custom streams provided are not closed except of ``subprocess.PIPE``.\n\n    >>> from tempfile import TemporaryFile\n    >>> stream = TemporaryFile()\n    >>> with run_interactive_shell_command(["echo", "TEXT"],\n    ...                                    stdout=stream) as p:\n    ...     stderr = p.stderr\n    >>> stderr.closed\n    True\n    >>> stream.closed\n    False\n\n    :param command: The command to run on shell. This parameter can either\n                    be a sequence of arguments that are directly passed to\n                    the process or a string. A string gets splitted beforehand\n                    using ``shlex.split()``. If providing ``shell=True`` as a\n                    keyword-argument, no ``shlex.split()`` is performed and the\n                    command string goes directly to ``subprocess.Popen()``.\n    :param kwargs:  Additional keyword arguments to pass to\n                    ``subprocess.Popen`` that are used to spawn the process.\n    :return:        A context manager yielding the process started from the\n                    command.\n    '
    if ((not kwargs.get('shell', False)) and isinstance(command, str)):
        command = shlex.split(command)
    else:
        command = list(command)
    if (platform.system() == 'Windows'):
        command[0] = which(command[0])
    args = {'stdout': PIPE, 'stderr': PIPE, 'stdin': PIPE, 'universal_newlines': True}
    args.update(kwargs)
    process = Popen(command, **args)
    try:
        (yield process)
    finally:
        if (args['stdout'] is PIPE):
            process.stdout.close()
        if (args['stderr'] is PIPE):
            process.stderr.close()
        if (args['stdin'] is PIPE):
            process.stdin.close()
        process.wait()