From a016eb41a3714ca781ddb21ba351a8b872034f18 Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Wed, 13 Oct 2010 17:18:51 -0700 Subject: oe.process: subclass Popen Rather than providing a wrapper function, we can subclass Popen and provide our slightly different defaults that way. Signed-off-by: Chris Larson --- lib/oe/process.py | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/lib/oe/process.py b/lib/oe/process.py index 179e0d4548..63b14571af 100644 --- a/lib/oe/process.py +++ b/lib/oe/process.py @@ -40,21 +40,29 @@ class ExecutionError(CmdError): return (CmdError.__str__(self) + " with exit code %s" % self.exitcode + message) -def run(cmd, **kwargs): +class Popen(subprocess.Popen): + defaults = { + "close_fds": True, + "preexec_fn": subprocess_setup, + "stdout": subprocess.PIPE, + "stderr": subprocess.STDOUT, + "stdin": subprocess.PIPE, + "shell": False, + } + + def __init__(self, *args, **kwargs): + options = dict(self.defaults) + options.update(kwargs) + subprocess.Popen.__init__(self, *args, **options) + +def run(cmd, **options): """Convenience function to run a command and return its output, raising an exception when the command fails""" - from subprocess import PIPE, STDOUT - options = { - "stdout": PIPE, - "stderr": STDOUT, - "shell": False, - } - if isinstance(cmd, basestring): + if isinstance(cmd, basestring) and not "shell" in options: options["shell"] = True - options.update(kwargs) try: - pipe = popen(cmd, **options) + pipe = Popen(cmd, **options) except OSError, exc: if exc.errno == 2: raise NotFoundError(cmd) @@ -64,14 +72,3 @@ def run(cmd, **kwargs): if pipe.returncode != 0: raise ExecutionError(cmd, pipe.returncode, stdout, stderr) return stdout - -def popen(cmd, **kwargs): - """ Convenience function to call out processes with our exported - variables in the environment. - """ - from subprocess import Popen - - kwargs["close_fds"] = True - kwargs["preexec_fn"] = subprocess_setup - - return Popen(cmd, **kwargs) -- cgit 1.2.3-korg