summaryrefslogtreecommitdiffstats
path: root/meta/lib/oe
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oe')
-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 0000000000..26c3e65910
--- /dev/null
+++ b/meta/lib/oe/process.py
@@ -0,0 +1,74 @@
1import subprocess
2import signal
3
4def subprocess_setup():
5 # Python installs a SIGPIPE handler by default. This is usually not what
6 # non-Python subprocesses expect.
7 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
8
9class CmdError(RuntimeError):
10 def __init__(self, command):
11 self.command = command
12
13 def __str__(self):
14 if not isinstance(self.command, basestring):
15 cmd = subprocess.list2cmdline(self.command)
16 else:
17 cmd = self.command
18
19 return "Execution of '%s' failed" % cmd
20
21class NotFoundError(CmdError):
22 def __str__(self):
23 return CmdError.__str__(self) + ": command not found"
24
25class ExecutionError(CmdError):
26 def __init__(self, command, exitcode, stdout = None, stderr = None):
27 CmdError.__init__(self, command)
28 self.exitcode = exitcode
29 self.stdout = stdout
30 self.stderr = stderr
31
32 def __str__(self):
33 message = ""
34 if self.stderr:
35 message += self.stderr
36 if self.stdout:
37 message += self.stdout
38 if message:
39 message = ":\n" + message
40 return (CmdError.__str__(self) +
41 " with exit code %s" % self.exitcode + message)
42
43class Popen(subprocess.Popen):
44 defaults = {
45 "close_fds": True,
46 "preexec_fn": subprocess_setup,
47 "stdout": subprocess.PIPE,
48 "stderr": subprocess.STDOUT,
49 "stdin": subprocess.PIPE,
50 "shell": False,
51 }
52
53 def __init__(self, *args, **kwargs):
54 options = dict(self.defaults)
55 options.update(kwargs)
56 subprocess.Popen.__init__(self, *args, **options)
57
58def run(cmd, input=None, **options):
59 """Convenience function to run a command and return its output, raising an
60 exception when the command fails"""
61
62 if isinstance(cmd, basestring) and not "shell" in options:
63 options["shell"] = True
64 try:
65 pipe = Popen(cmd, **options)
66 except OSError, exc:
67 if exc.errno == 2:
68 raise NotFoundError(cmd)
69 else:
70 raise
71 stdout, stderr = pipe.communicate(input)
72 if pipe.returncode != 0:
73 raise ExecutionError(cmd, pipe.returncode, stdout, stderr)
74 return stdout