blob: 4c7b6d39df1b5468417876ac98f9a93629fb2be5 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright BitBake Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
5#
6
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007import logging
8import signal
9import subprocess
10import errno
11import select
Andrew Geisslerc9f78652020-09-18 14:11:35 -050012import bb
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013
14logger = logging.getLogger('BitBake.Process')
15
16def subprocess_setup():
17 # Python installs a SIGPIPE handler by default. This is usually not what
18 # non-Python subprocesses expect.
19 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
20
21class CmdError(RuntimeError):
22 def __init__(self, command, msg=None):
23 self.command = command
24 self.msg = msg
25
26 def __str__(self):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060027 if not isinstance(self.command, str):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028 cmd = subprocess.list2cmdline(self.command)
29 else:
30 cmd = self.command
31
32 msg = "Execution of '%s' failed" % cmd
33 if self.msg:
34 msg += ': %s' % self.msg
35 return msg
36
37class NotFoundError(CmdError):
38 def __str__(self):
39 return CmdError.__str__(self) + ": command not found"
40
41class ExecutionError(CmdError):
42 def __init__(self, command, exitcode, stdout = None, stderr = None):
43 CmdError.__init__(self, command)
44 self.exitcode = exitcode
45 self.stdout = stdout
46 self.stderr = stderr
Andrew Geissler635e0e42020-08-21 15:58:33 -050047 self.extra_message = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048
49 def __str__(self):
50 message = ""
51 if self.stderr:
52 message += self.stderr
53 if self.stdout:
54 message += self.stdout
55 if message:
56 message = ":\n" + message
57 return (CmdError.__str__(self) +
Andrew Geissler635e0e42020-08-21 15:58:33 -050058 " with exit code %s" % self.exitcode + message + (self.extra_message or ""))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059
60class Popen(subprocess.Popen):
61 defaults = {
62 "close_fds": True,
63 "preexec_fn": subprocess_setup,
64 "stdout": subprocess.PIPE,
Andrew Geissler595f6302022-01-24 19:11:47 +000065 "stderr": subprocess.PIPE,
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066 "stdin": subprocess.PIPE,
67 "shell": False,
68 }
69
70 def __init__(self, *args, **kwargs):
71 options = dict(self.defaults)
72 options.update(kwargs)
73 subprocess.Popen.__init__(self, *args, **options)
74
75def _logged_communicate(pipe, log, input, extrafiles):
76 if pipe.stdin:
77 if input is not None:
78 pipe.stdin.write(input)
79 pipe.stdin.close()
80
81 outdata, errdata = [], []
82 rin = []
83
84 if pipe.stdout is not None:
85 bb.utils.nonblockingfd(pipe.stdout.fileno())
86 rin.append(pipe.stdout)
87 if pipe.stderr is not None:
88 bb.utils.nonblockingfd(pipe.stderr.fileno())
89 rin.append(pipe.stderr)
90 for fobj, _ in extrafiles:
91 bb.utils.nonblockingfd(fobj.fileno())
92 rin.append(fobj)
93
94 def readextras(selected):
95 for fobj, func in extrafiles:
96 if fobj in selected:
97 try:
98 data = fobj.read()
99 except IOError as err:
100 if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
101 data = None
102 if data is not None:
103 func(data)
104
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105 def read_all_pipes(log, rin, outdata, errdata):
106 rlist = rin
107 stdoutbuf = b""
108 stderrbuf = b""
109
110 try:
111 r,w,e = select.select (rlist, [], [], 1)
112 except OSError as e:
113 if e.errno != errno.EINTR:
114 raise
115
116 readextras(r)
117
118 if pipe.stdout in r:
119 data = stdoutbuf + pipe.stdout.read()
120 if data is not None and len(data) > 0:
121 try:
122 data = data.decode("utf-8")
123 outdata.append(data)
124 log.write(data)
125 log.flush()
126 stdoutbuf = b""
127 except UnicodeDecodeError:
128 stdoutbuf = data
129
130 if pipe.stderr in r:
131 data = stderrbuf + pipe.stderr.read()
132 if data is not None and len(data) > 0:
133 try:
134 data = data.decode("utf-8")
135 errdata.append(data)
136 log.write(data)
137 log.flush()
138 stderrbuf = b""
139 except UnicodeDecodeError:
140 stderrbuf = data
141
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143 # Read all pipes while the process is open
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 while pipe.poll() is None:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145 read_all_pipes(log, rin, outdata, errdata)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000147 # Process closed, drain all pipes...
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500148 read_all_pipes(log, rin, outdata, errdata)
149 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150 log.flush()
151
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152 if pipe.stdout is not None:
153 pipe.stdout.close()
154 if pipe.stderr is not None:
155 pipe.stderr.close()
156 return ''.join(outdata), ''.join(errdata)
157
158def run(cmd, input=None, log=None, extrafiles=None, **options):
159 """Convenience function to run a command and return its output, raising an
160 exception when the command fails"""
161
162 if not extrafiles:
163 extrafiles = []
164
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600165 if isinstance(cmd, str) and not "shell" in options:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 options["shell"] = True
167
168 try:
169 pipe = Popen(cmd, **options)
170 except OSError as exc:
171 if exc.errno == 2:
172 raise NotFoundError(cmd)
173 else:
174 raise CmdError(cmd, exc)
175
176 if log:
177 stdout, stderr = _logged_communicate(pipe, log, input, extrafiles)
178 else:
179 stdout, stderr = pipe.communicate(input)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500180 if not stdout is None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600181 stdout = stdout.decode("utf-8")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182 if not stderr is None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600183 stderr = stderr.decode("utf-8")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184
185 if pipe.returncode != 0:
Andrew Geissler5199d832021-09-24 16:47:35 -0500186 if log:
187 # Don't duplicate the output in the exception if logging it
188 raise ExecutionError(cmd, pipe.returncode, None, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 raise ExecutionError(cmd, pipe.returncode, stdout, stderr)
190 return stdout, stderr