base.bbclass: add popen/system convenience functions
authorChristopher Larson <clarson@mvista.com>
Tue, 19 Jan 2010 10:00:41 +0000 (10:00 +0000)
committerChris Larson <chris_larson@mentor.com>
Mon, 22 Mar 2010 01:31:40 +0000 (18:31 -0700)
Provides oe_popen, which is a subprocess.Popen wrapper that automatically
provides our exported variables in the environment, including the PATH, and
oe_system, which is just a wrapper that acts like system.

Signed-off-by: Chris Larson <clarson@mvista.com>
Acked-by: Tom Rini <tom_rini@mentor.com>
classes/base.bbclass
classes/utils.bbclass

index ae8b74e..182ce62 100644 (file)
@@ -154,12 +154,6 @@ python base_do_fetch() {
                        raise bb.build.FuncFailed("Checksum of '%s' failed" % uri)
 }
 
-def subprocess_setup():
-       import signal
-       # Python installs a SIGPIPE handler by default. This is usually not what
-       # non-Python subprocesses expect.
-       signal.signal(signal.SIGPIPE, signal.SIG_DFL)
-
 def oe_unpack_file(file, data, url = None):
        import subprocess
        if not url:
index ecb00ec..6ff11dd 100644 (file)
@@ -1,3 +1,33 @@
+def subprocess_setup():
+   import signal
+   # Python installs a SIGPIPE handler by default. This is usually not what
+   # non-Python subprocesses expect.
+   signal.signal(signal.SIGPIPE, signal.SIG_DFL)
+
+def oe_popen(d, cmd, **kwargs):
+    """ Convenience function to call out processes with our exported
+    variables in the environment.
+    """
+    from subprocess import Popen
+
+    if kwargs.get("env") is None:
+        env = d.getVar("__oe_popen_env", False)
+        if env is None:
+            env = {}
+            for v in d.keys():
+                if d.getVarFlag(v, "export"):
+                    env[v] = d.getVar(v, True) or ""
+            d.setVar("__oe_popen_env", env)
+        kwargs["env"] = env
+
+    kwargs["preexec_fn"] = subprocess_setup
+
+    return Popen(cmd, **kwargs)
+
+def oe_system(d, cmd):
+    """ Popen based version of os.system. """
+    return oe_popen(d, cmd, shell=True).wait()
+
 # like os.path.join but doesn't treat absolute RHS specially
 def base_path_join(a, *p):
     path = a