Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 20 | if not isinstance(self.command, str): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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, extrafiles): |
| 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 | for fobj, _ in extrafiles: |
| 83 | bb.utils.nonblockingfd(fobj.fileno()) |
| 84 | rin.append(fobj) |
| 85 | |
| 86 | def readextras(selected): |
| 87 | for fobj, func in extrafiles: |
| 88 | if fobj in selected: |
| 89 | try: |
| 90 | data = fobj.read() |
| 91 | except IOError as err: |
| 92 | if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK: |
| 93 | data = None |
| 94 | if data is not None: |
| 95 | func(data) |
| 96 | |
| 97 | try: |
| 98 | while pipe.poll() is None: |
| 99 | rlist = rin |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 100 | stdoutbuf = b"" |
| 101 | stderrbuf = b"" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | try: |
| 103 | r,w,e = select.select (rlist, [], [], 1) |
| 104 | except OSError as e: |
| 105 | if e.errno != errno.EINTR: |
| 106 | raise |
| 107 | |
| 108 | if pipe.stdout in r: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 109 | data = stdoutbuf + pipe.stdout.read() |
| 110 | if data is not None and len(data) > 0: |
| 111 | try: |
| 112 | data = data.decode("utf-8") |
| 113 | outdata.append(data) |
| 114 | log.write(data) |
| 115 | stdoutbuf = b"" |
| 116 | except UnicodeDecodeError: |
| 117 | stdoutbuf = data |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 118 | |
| 119 | if pipe.stderr in r: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 120 | data = stderrbuf + pipe.stderr.read() |
| 121 | if data is not None and len(data) > 0: |
| 122 | try: |
| 123 | data = data.decode("utf-8") |
| 124 | errdata.append(data) |
| 125 | log.write(data) |
| 126 | stderrbuf = b"" |
| 127 | except UnicodeDecodeError: |
| 128 | stderrbuf = data |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | |
| 130 | readextras(r) |
| 131 | |
| 132 | finally: |
| 133 | log.flush() |
| 134 | |
| 135 | readextras([fobj for fobj, _ in extrafiles]) |
| 136 | |
| 137 | if pipe.stdout is not None: |
| 138 | pipe.stdout.close() |
| 139 | if pipe.stderr is not None: |
| 140 | pipe.stderr.close() |
| 141 | return ''.join(outdata), ''.join(errdata) |
| 142 | |
| 143 | def run(cmd, input=None, log=None, extrafiles=None, **options): |
| 144 | """Convenience function to run a command and return its output, raising an |
| 145 | exception when the command fails""" |
| 146 | |
| 147 | if not extrafiles: |
| 148 | extrafiles = [] |
| 149 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 150 | if isinstance(cmd, str) and not "shell" in options: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | options["shell"] = True |
| 152 | |
| 153 | try: |
| 154 | pipe = Popen(cmd, **options) |
| 155 | except OSError as exc: |
| 156 | if exc.errno == 2: |
| 157 | raise NotFoundError(cmd) |
| 158 | else: |
| 159 | raise CmdError(cmd, exc) |
| 160 | |
| 161 | if log: |
| 162 | stdout, stderr = _logged_communicate(pipe, log, input, extrafiles) |
| 163 | else: |
| 164 | stdout, stderr = pipe.communicate(input) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 165 | if stdout: |
| 166 | stdout = stdout.decode("utf-8") |
| 167 | if stderr: |
| 168 | stderr = stderr.decode("utf-8") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 169 | |
| 170 | if pipe.returncode != 0: |
| 171 | raise ExecutionError(cmd, pipe.returncode, stdout, stderr) |
| 172 | return stdout, stderr |