From c168c529367e123b557e915f5e5e49018c6de572 Mon Sep 17 00:00:00 2001 From: Richard Purdie Date: Fri, 18 Feb 2011 18:14:50 +0000 Subject: meta/lib/oe/path: Use check_output for subprocess so we can see error info. Import code to be python 2.6 compatible. Signed-off-by: Richard Purdie --- meta/lib/oe/path.py | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'meta/lib/oe/path.py') diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py index da7811fac4..74674bfee8 100644 --- a/meta/lib/oe/path.py +++ b/meta/lib/oe/path.py @@ -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 + -- cgit 1.2.3-korg