diff options
Diffstat (limited to 'bitbake/lib/bb/process.py')
-rw-r--r-- | bitbake/lib/bb/process.py | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/bitbake/lib/bb/process.py b/bitbake/lib/bb/process.py new file mode 100644 index 0000000000..8b1aea9a10 --- /dev/null +++ b/bitbake/lib/bb/process.py | |||
@@ -0,0 +1,133 @@ | |||
1 | import logging | ||
2 | import signal | ||
3 | import subprocess | ||
4 | import errno | ||
5 | import select | ||
6 | |||
7 | logger = logging.getLogger('BitBake.Process') | ||
8 | |||
9 | def subprocess_setup(): | ||
10 | # Python installs a SIGPIPE handler by default. This is usually not what | ||
11 | # non-Python subprocesses expect. | ||
12 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) | ||
13 | |||
14 | class CmdError(RuntimeError): | ||
15 | def __init__(self, command, msg=None): | ||
16 | self.command = command | ||
17 | self.msg = msg | ||
18 | |||
19 | def __str__(self): | ||
20 | if not isinstance(self.command, basestring): | ||
21 | cmd = subprocess.list2cmdline(self.command) | ||
22 | else: | ||
23 | cmd = self.command | ||
24 | |||
25 | msg = "Execution of '%s' failed" % cmd | ||
26 | if self.msg: | ||
27 | msg += ': %s' % self.msg | ||
28 | return msg | ||
29 | |||
30 | class NotFoundError(CmdError): | ||
31 | def __str__(self): | ||
32 | return CmdError.__str__(self) + ": command not found" | ||
33 | |||
34 | class ExecutionError(CmdError): | ||
35 | def __init__(self, command, exitcode, stdout = None, stderr = None): | ||
36 | CmdError.__init__(self, command) | ||
37 | self.exitcode = exitcode | ||
38 | self.stdout = stdout | ||
39 | self.stderr = stderr | ||
40 | |||
41 | def __str__(self): | ||
42 | message = "" | ||
43 | if self.stderr: | ||
44 | message += self.stderr | ||
45 | if self.stdout: | ||
46 | message += self.stdout | ||
47 | if message: | ||
48 | message = ":\n" + message | ||
49 | return (CmdError.__str__(self) + | ||
50 | " with exit code %s" % self.exitcode + message) | ||
51 | |||
52 | class Popen(subprocess.Popen): | ||
53 | defaults = { | ||
54 | "close_fds": True, | ||
55 | "preexec_fn": subprocess_setup, | ||
56 | "stdout": subprocess.PIPE, | ||
57 | "stderr": subprocess.STDOUT, | ||
58 | "stdin": subprocess.PIPE, | ||
59 | "shell": False, | ||
60 | } | ||
61 | |||
62 | def __init__(self, *args, **kwargs): | ||
63 | options = dict(self.defaults) | ||
64 | options.update(kwargs) | ||
65 | subprocess.Popen.__init__(self, *args, **options) | ||
66 | |||
67 | def _logged_communicate(pipe, log, input): | ||
68 | if pipe.stdin: | ||
69 | if input is not None: | ||
70 | pipe.stdin.write(input) | ||
71 | pipe.stdin.close() | ||
72 | |||
73 | outdata, errdata = [], [] | ||
74 | rin = [] | ||
75 | |||
76 | if pipe.stdout is not None: | ||
77 | bb.utils.nonblockingfd(pipe.stdout.fileno()) | ||
78 | rin.append(pipe.stdout) | ||
79 | if pipe.stderr is not None: | ||
80 | bb.utils.nonblockingfd(pipe.stderr.fileno()) | ||
81 | rin.append(pipe.stderr) | ||
82 | |||
83 | try: | ||
84 | while pipe.poll() is None: | ||
85 | rlist = rin | ||
86 | try: | ||
87 | r,w,e = select.select (rlist, [], [], 1) | ||
88 | except OSError as e: | ||
89 | if e.errno != errno.EINTR: | ||
90 | raise | ||
91 | |||
92 | if pipe.stdout in r: | ||
93 | data = pipe.stdout.read() | ||
94 | if data is not None: | ||
95 | outdata.append(data) | ||
96 | log.write(data) | ||
97 | |||
98 | if pipe.stderr in r: | ||
99 | data = pipe.stderr.read() | ||
100 | if data is not None: | ||
101 | errdata.append(data) | ||
102 | log.write(data) | ||
103 | finally: | ||
104 | log.flush() | ||
105 | if pipe.stdout is not None: | ||
106 | pipe.stdout.close() | ||
107 | if pipe.stderr is not None: | ||
108 | pipe.stderr.close() | ||
109 | return ''.join(outdata), ''.join(errdata) | ||
110 | |||
111 | def run(cmd, input=None, log=None, **options): | ||
112 | """Convenience function to run a command and return its output, raising an | ||
113 | exception when the command fails""" | ||
114 | |||
115 | if isinstance(cmd, basestring) and not "shell" in options: | ||
116 | options["shell"] = True | ||
117 | |||
118 | try: | ||
119 | pipe = Popen(cmd, **options) | ||
120 | except OSError as exc: | ||
121 | if exc.errno == 2: | ||
122 | raise NotFoundError(cmd) | ||
123 | else: | ||
124 | raise CmdError(cmd, exc) | ||
125 | |||
126 | if log: | ||
127 | stdout, stderr = _logged_communicate(pipe, log, input) | ||
128 | else: | ||
129 | stdout, stderr = pipe.communicate(input) | ||
130 | |||
131 | if pipe.returncode != 0: | ||
132 | raise ExecutionError(cmd, pipe.returncode, stdout, stderr) | ||
133 | return stdout, stderr | ||