blob: f36c929d25bb3856eb4cd2a96776f393b823aed6 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: GPL-2.0-only
3#
4
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005import logging
6import signal
7import subprocess
8import errno
9import select
10
11logger = logging.getLogger('BitBake.Process')
12
13def subprocess_setup():
14 # Python installs a SIGPIPE handler by default. This is usually not what
15 # non-Python subprocesses expect.
16 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
17
18class CmdError(RuntimeError):
19 def __init__(self, command, msg=None):
20 self.command = command
21 self.msg = msg
22
23 def __str__(self):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060024 if not isinstance(self.command, str):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025 cmd = subprocess.list2cmdline(self.command)
26 else:
27 cmd = self.command
28
29 msg = "Execution of '%s' failed" % cmd
30 if self.msg:
31 msg += ': %s' % self.msg
32 return msg
33
34class NotFoundError(CmdError):
35 def __str__(self):
36 return CmdError.__str__(self) + ": command not found"
37
38class ExecutionError(CmdError):
39 def __init__(self, command, exitcode, stdout = None, stderr = None):
40 CmdError.__init__(self, command)
41 self.exitcode = exitcode
42 self.stdout = stdout
43 self.stderr = stderr
Andrew Geissler635e0e42020-08-21 15:58:33 -050044 self.extra_message = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045
46 def __str__(self):
47 message = ""
48 if self.stderr:
49 message += self.stderr
50 if self.stdout:
51 message += self.stdout
52 if message:
53 message = ":\n" + message
54 return (CmdError.__str__(self) +
Andrew Geissler635e0e42020-08-21 15:58:33 -050055 " with exit code %s" % self.exitcode + message + (self.extra_message or ""))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056
57class Popen(subprocess.Popen):
58 defaults = {
59 "close_fds": True,
60 "preexec_fn": subprocess_setup,
61 "stdout": subprocess.PIPE,
62 "stderr": subprocess.STDOUT,
63 "stdin": subprocess.PIPE,
64 "shell": False,
65 }
66
67 def __init__(self, *args, **kwargs):
68 options = dict(self.defaults)
69 options.update(kwargs)
70 subprocess.Popen.__init__(self, *args, **options)
71
72def _logged_communicate(pipe, log, input, extrafiles):
73 if pipe.stdin:
74 if input is not None:
75 pipe.stdin.write(input)
76 pipe.stdin.close()
77
78 outdata, errdata = [], []
79 rin = []
80
81 if pipe.stdout is not None:
82 bb.utils.nonblockingfd(pipe.stdout.fileno())
83 rin.append(pipe.stdout)
84 if pipe.stderr is not None:
85 bb.utils.nonblockingfd(pipe.stderr.fileno())
86 rin.append(pipe.stderr)
87 for fobj, _ in extrafiles:
88 bb.utils.nonblockingfd(fobj.fileno())
89 rin.append(fobj)
90
91 def readextras(selected):
92 for fobj, func in extrafiles:
93 if fobj in selected:
94 try:
95 data = fobj.read()
96 except IOError as err:
97 if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
98 data = None
99 if data is not None:
100 func(data)
101
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102 def read_all_pipes(log, rin, outdata, errdata):
103 rlist = rin
104 stdoutbuf = b""
105 stderrbuf = b""
106
107 try:
108 r,w,e = select.select (rlist, [], [], 1)
109 except OSError as e:
110 if e.errno != errno.EINTR:
111 raise
112
113 readextras(r)
114
115 if pipe.stdout in r:
116 data = stdoutbuf + pipe.stdout.read()
117 if data is not None and len(data) > 0:
118 try:
119 data = data.decode("utf-8")
120 outdata.append(data)
121 log.write(data)
122 log.flush()
123 stdoutbuf = b""
124 except UnicodeDecodeError:
125 stdoutbuf = data
126
127 if pipe.stderr in r:
128 data = stderrbuf + pipe.stderr.read()
129 if data is not None and len(data) > 0:
130 try:
131 data = data.decode("utf-8")
132 errdata.append(data)
133 log.write(data)
134 log.flush()
135 stderrbuf = b""
136 except UnicodeDecodeError:
137 stderrbuf = data
138
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500139 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140 # Read all pipes while the process is open
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 while pipe.poll() is None:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500142 read_all_pipes(log, rin, outdata, errdata)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500143
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500144 # Pocess closed, drain all pipes...
145 read_all_pipes(log, rin, outdata, errdata)
146 finally:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147 log.flush()
148
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 if pipe.stdout is not None:
150 pipe.stdout.close()
151 if pipe.stderr is not None:
152 pipe.stderr.close()
153 return ''.join(outdata), ''.join(errdata)
154
155def run(cmd, input=None, log=None, extrafiles=None, **options):
156 """Convenience function to run a command and return its output, raising an
157 exception when the command fails"""
158
159 if not extrafiles:
160 extrafiles = []
161
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600162 if isinstance(cmd, str) and not "shell" in options:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163 options["shell"] = True
164
165 try:
166 pipe = Popen(cmd, **options)
167 except OSError as exc:
168 if exc.errno == 2:
169 raise NotFoundError(cmd)
170 else:
171 raise CmdError(cmd, exc)
172
173 if log:
174 stdout, stderr = _logged_communicate(pipe, log, input, extrafiles)
175 else:
176 stdout, stderr = pipe.communicate(input)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177 if not stdout is None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600178 stdout = stdout.decode("utf-8")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500179 if not stderr is None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600180 stderr = stderr.decode("utf-8")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181
182 if pipe.returncode != 0:
183 raise ExecutionError(cmd, pipe.returncode, stdout, stderr)
184 return stdout, stderr