aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorChris Larson <chris_larson@mentor.com>2010-10-12 19:05:19 -0700
committerChris Larson <chris_larson@mentor.com>2010-10-13 17:20:10 -0700
commit14b7ca062d96ab6b44f1b2bd192649db36ae6263 (patch)
tree9e5e6886c85667b56760d9c091b4d00e572d4758 /lib
parent8b2cac31bec0b97c8bc66ff3e6d2c9903f2ae8dc (diff)
downloadopenembedded-14b7ca062d96ab6b44f1b2bd192649db36ae6263.tar.gz
oe.process: pull some common bits over
Also update gitver to use the subprocess wrappers Signed-off-by: Chris Larson <chris_larson@mentor.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/oe/process.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/oe/process.py b/lib/oe/process.py
new file mode 100644
index 0000000000..179e0d4548
--- /dev/null
+++ b/lib/oe/process.py
@@ -0,0 +1,77 @@
+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)
+
+def run(cmd, **kwargs):
+ """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):
+ options["shell"] = True
+ options.update(kwargs)
+ try:
+ pipe = popen(cmd, **options)
+ except OSError, exc:
+ if exc.errno == 2:
+ raise NotFoundError(cmd)
+ else:
+ raise
+ stdout, stderr = pipe.communicate()
+ 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)