[oe-commits] Chris Larson : oe.process: subclass Popen

git version control git at git.openembedded.org
Thu Oct 14 00:24:26 UTC 2010


Module: openembedded.git
Branch: master
Commit: a016eb41a3714ca781ddb21ba351a8b872034f18
URL:    http://gitweb.openembedded.net/?p=openembedded.git&a=commit;h=a016eb41a3714ca781ddb21ba351a8b872034f18

Author: Chris Larson <chris_larson at mentor.com>
Date:   Wed Oct 13 17:18:51 2010 -0700

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 at mentor.com>

---

 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 179e0d4..63b1457 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)





More information about the Openembedded-commits mailing list