diff options
author | Chris Larson <chris_larson@mentor.com> | 2010-10-13 17:18:51 -0700 |
---|---|---|
committer | Chris Larson <chris_larson@mentor.com> | 2010-10-13 17:20:11 -0700 |
commit | a016eb41a3714ca781ddb21ba351a8b872034f18 (patch) | |
tree | 79aff3d86507cccd7e892b47ebe646ea46db041a /lib/oe/process.py | |
parent | 14b7ca062d96ab6b44f1b2bd192649db36ae6263 (diff) | |
download | openembedded-a016eb41a3714ca781ddb21ba351a8b872034f18.tar.gz |
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 <chris_larson@mentor.com>
Diffstat (limited to 'lib/oe/process.py')
-rw-r--r-- | lib/oe/process.py | 39 |
1 files 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) |