From eb6fa93845187d82a8120aeafa9f9aec9d48106d Mon Sep 17 00:00:00 2001 From: Douglas RAILLARD Date: Wed, 15 Jan 2020 16:06:20 +0000 Subject: [PATCH] utils/misc: Add redirect_streams() helper Update a command line to redirect standard streams as specified using the parameters. This helper allows honoring streams specified in the same way as subprocess.Popen, by doing it as much using shell redirections as possible. --- devlib/utils/misc.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/devlib/utils/misc.py b/devlib/utils/misc.py index 4f8beb1..2413cc7 100644 --- a/devlib/utils/misc.py +++ b/devlib/utils/misc.py @@ -46,6 +46,11 @@ try: except AttributeError: from contextlib2 import ExitStack +try: + from shlex import quote +except ImportError: + from pipes import quote + from past.builtins import basestring # pylint: disable=redefined-builtin @@ -232,6 +237,32 @@ def walk_modules(path): mods.append(submod) return mods +def redirect_streams(stdout, stderr, command): + """ + Update a command to redirect a given stream to /dev/null if it's + ``subprocess.DEVNULL``. + + :return: A tuple (stdout, stderr, command) with stream set to ``subprocess.PIPE`` + if the `stream` parameter was set to ``subprocess.DEVNULL``. + """ + def redirect(stream, redirection): + if stream == subprocess.DEVNULL: + suffix = '{}/dev/null'.format(redirection) + elif stream == subprocess.STDOUT: + suffix = '{}&1'.format(redirection) + # Indicate that there is nothing to monitor for stderr anymore + # since it's merged into stdout + stream = subprocess.DEVNULL + else: + suffix = '' + + return (stream, suffix) + + stdout, suffix1 = redirect(stdout, '>') + stderr, suffix2 = redirect(stderr, '2>') + + command = 'sh -c {} {} {}'.format(quote(command), suffix1, suffix2) + return (stdout, stderr, command) def ensure_directory_exists(dirpath): """A filter for directory paths to ensure they exist."""