]> code.ossystems Code Review - openembedded-core.git/commitdiff
meta/lib/oe/path: Use check_output for subprocess so we can see error info. Import...
authorRichard Purdie <richard.purdie@linuxfoundation.org>
Fri, 18 Feb 2011 18:14:50 +0000 (18:14 +0000)
committerRichard Purdie <richard.purdie@linuxfoundation.org>
Fri, 18 Feb 2011 18:14:50 +0000 (18:14 +0000)
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
meta/lib/oe/path.py

index da7811fac47a4124f8fc7c6c571cd4d961c28b0a..74674bfee8a7a1a7b21bf9696a42fcc26a0d14b8 100644 (file)
@@ -1,4 +1,5 @@
 import shutil
+import subprocess
 
 def join(*paths):
     """Like os.path.join but doesn't treat absolute RHS specially"""
@@ -45,24 +46,16 @@ def format_display(path, metadata):
     else:
         return rel
 
-
-class CopyFailed(Exception):
-    pass
-
 def copytree(src, dst):
     # We could use something like shutil.copytree here but it turns out to
     # to be slow. It takes twice as long copying to an empty directory. 
     # If dst already has contents performance can be 15 time slower
     # This way we also preserve hardlinks between files in the tree.
 
-    import subprocess
-
     bb.mkdirhier(dst)
     cmd = 'tar -cf - -C %s -ps . | tar -xf - -C %s' % (src, dst)
-    ret = subprocess.call(cmd, shell=True)
-    if ret != 0:
-        raise CopyFailed("Command %s failed with return value %s" % (cmd, ret))
-    return 
+    check_output(cmd, shell=True, stderr=subprocess.STDOUT)
+
 
 def remove(path):
     """Equivalent to rm -f or rm -rf"""
@@ -86,3 +79,38 @@ def symlink(source, destination, force=False):
     except OSError, e:
         if e.errno != errno.EEXIST or os.readlink(destination) != source:
             raise
+
+
+# Not needed when we move to python 2.7
+def check_output(*popenargs, **kwargs):
+    r"""Run command with arguments and return its output as a byte string.
+
+    If the exit code was non-zero it raises a CalledProcessError.  The
+    CalledProcessError object will have the return code in the returncode
+    attribute and output in the output attribute.
+
+    The arguments are the same as for the Popen constructor.  Example:
+
+    >>> check_output(["ls", "-l", "/dev/null"])
+    'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'
+
+    The stdout argument is not allowed as it is used internally.
+    To capture standard error in the result, use stderr=STDOUT.
+
+    >>> check_output(["/bin/sh", "-c",
+    ...               "ls -l non_existent_file ; exit 0"],
+    ...              stderr=STDOUT)
+    'ls: non_existent_file: No such file or directory\n'
+    """
+    if 'stdout' in kwargs:
+        raise ValueError('stdout argument not allowed, it will be overridden.')
+    process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
+    output, unused_err = process.communicate()
+    retcode = process.poll()
+    if retcode:
+        cmd = kwargs.get("args")
+        if cmd is None:
+            cmd = popenargs[0]
+        raise subprocess.CalledProcessError(retcode, cmd, output=output)
+    return output
+