summaryrefslogtreecommitdiffstats
path: root/meta
diff options
context:
space:
mode:
authorMartin Jansa <Martin.Jansa@gmail.com>2011-04-04 09:44:36 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2011-05-16 15:32:19 +0100
commit04b79d93957753b8c0671569afb0854cd5ca765c (patch)
treec5a9dd785231aa9402f30d1713ea3aaa0be22fe5 /meta
parentadd4df47eba51ce71324390c53ba17103df38f8a (diff)
downloadast2050-yocto-poky-04b79d93957753b8c0671569afb0854cd5ca765c.zip
ast2050-yocto-poky-04b79d93957753b8c0671569afb0854cd5ca765c.tar.gz
lib/oe/process.py: import from OE
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta')
-rw-r--r--meta/lib/oe/process.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/meta/lib/oe/process.py b/meta/lib/oe/process.py
new file mode 100644
index 0000000..26c3e65
--- /dev/null
+++ b/meta/lib/oe/process.py
@@ -0,0 +1,74 @@
+import subprocess
+import signal
+
+def subprocess_setup():
+ # Python installs a SIGPIPE handler by default. This is usually not what
+ # non-Python subprocesses expect.
+ signal.signal(signal.SIGPIPE, signal.SIG_DFL)
+
+class CmdError(RuntimeError):
+ def __init__(self, command):
+ self.command = command
+
+ def __str__(self):
+ if not isinstance(self.command, basestring):
+ cmd = subprocess.list2cmdline(self.command)
+ else:
+ cmd = self.command
+
+ return "Execution of '%s' failed" % cmd
+
+class NotFoundError(CmdError):
+ def __str__(self):
+ return CmdError.__str__(self) + ": command not found"
+
+class ExecutionError(CmdError):
+ def __init__(self, command, exitcode, stdout = None, stderr = None):
+ CmdError.__init__(self, command)
+ self.exitcode = exitcode
+ self.stdout = stdout
+ self.stderr = stderr
+
+ def __str__(self):
+ message = ""
+ if self.stderr:
+ message += self.stderr
+ if self.stdout:
+ message += self.stdout
+ if message:
+ message = ":\n" + message
+ return (CmdError.__str__(self) +
+ " with exit code %s" % self.exitcode + message)
+
+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, input=None, **options):
+ """Convenience function to run a command and return its output, raising an
+ exception when the command fails"""
+
+ if isinstance(cmd, basestring) and not "shell" in options:
+ options["shell"] = True
+ try:
+ pipe = Popen(cmd, **options)
+ except OSError, exc:
+ if exc.errno == 2:
+ raise NotFoundError(cmd)
+ else:
+ raise
+ stdout, stderr = pipe.communicate(input)
+ if pipe.returncode != 0:
+ raise ExecutionError(cmd, pipe.returncode, stdout, stderr)
+ return stdout
OpenPOWER on IntegriCloud