diff options
author | Martin Jansa <Martin.Jansa@gmail.com> | 2011-04-04 09:44:36 +0200 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2011-05-16 15:32:19 +0100 |
commit | 04b79d93957753b8c0671569afb0854cd5ca765c (patch) | |
tree | c5a9dd785231aa9402f30d1713ea3aaa0be22fe5 /meta/lib/oe | |
parent | add4df47eba51ce71324390c53ba17103df38f8a (diff) | |
download | poky-04b79d93957753b8c0671569afb0854cd5ca765c.tar.gz |
lib/oe/process.py: import from OE
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib/oe')
-rw-r--r-- | meta/lib/oe/process.py | 74 |
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 @@ | |||
1 | import subprocess | ||
2 | import signal | ||
3 | |||
4 | def 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 | |||
9 | class 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 | |||
21 | class NotFoundError(CmdError): | ||
22 | def __str__(self): | ||
23 | return CmdError.__str__(self) + ": command not found" | ||
24 | |||
25 | class 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 | |||
43 | class 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 | |||
58 | def 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 | ||