process.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. import logging
  5. import signal
  6. import subprocess
  7. import errno
  8. import select
  9. logger = logging.getLogger('BitBake.Process')
  10. def subprocess_setup():
  11. # Python installs a SIGPIPE handler by default. This is usually not what
  12. # non-Python subprocesses expect.
  13. signal.signal(signal.SIGPIPE, signal.SIG_DFL)
  14. class CmdError(RuntimeError):
  15. def __init__(self, command, msg=None):
  16. self.command = command
  17. self.msg = msg
  18. def __str__(self):
  19. if not isinstance(self.command, str):
  20. cmd = subprocess.list2cmdline(self.command)
  21. else:
  22. cmd = self.command
  23. msg = "Execution of '%s' failed" % cmd
  24. if self.msg:
  25. msg += ': %s' % self.msg
  26. return msg
  27. class NotFoundError(CmdError):
  28. def __str__(self):
  29. return CmdError.__str__(self) + ": command not found"
  30. class ExecutionError(CmdError):
  31. def __init__(self, command, exitcode, stdout = None, stderr = None):
  32. CmdError.__init__(self, command)
  33. self.exitcode = exitcode
  34. self.stdout = stdout
  35. self.stderr = stderr
  36. def __str__(self):
  37. message = ""
  38. if self.stderr:
  39. message += self.stderr
  40. if self.stdout:
  41. message += self.stdout
  42. if message:
  43. message = ":\n" + message
  44. return (CmdError.__str__(self) +
  45. " with exit code %s" % self.exitcode + message)
  46. class Popen(subprocess.Popen):
  47. defaults = {
  48. "close_fds": True,
  49. "preexec_fn": subprocess_setup,
  50. "stdout": subprocess.PIPE,
  51. "stderr": subprocess.STDOUT,
  52. "stdin": subprocess.PIPE,
  53. "shell": False,
  54. }
  55. def __init__(self, *args, **kwargs):
  56. options = dict(self.defaults)
  57. options.update(kwargs)
  58. subprocess.Popen.__init__(self, *args, **options)
  59. def _logged_communicate(pipe, log, input, extrafiles):
  60. if pipe.stdin:
  61. if input is not None:
  62. pipe.stdin.write(input)
  63. pipe.stdin.close()
  64. outdata, errdata = [], []
  65. rin = []
  66. if pipe.stdout is not None:
  67. bb.utils.nonblockingfd(pipe.stdout.fileno())
  68. rin.append(pipe.stdout)
  69. if pipe.stderr is not None:
  70. bb.utils.nonblockingfd(pipe.stderr.fileno())
  71. rin.append(pipe.stderr)
  72. for fobj, _ in extrafiles:
  73. bb.utils.nonblockingfd(fobj.fileno())
  74. rin.append(fobj)
  75. def readextras(selected):
  76. for fobj, func in extrafiles:
  77. if fobj in selected:
  78. try:
  79. data = fobj.read()
  80. except IOError as err:
  81. if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
  82. data = None
  83. if data is not None:
  84. func(data)
  85. def read_all_pipes(log, rin, outdata, errdata):
  86. rlist = rin
  87. stdoutbuf = b""
  88. stderrbuf = b""
  89. try:
  90. r,w,e = select.select (rlist, [], [], 1)
  91. except OSError as e:
  92. if e.errno != errno.EINTR:
  93. raise
  94. readextras(r)
  95. if pipe.stdout in r:
  96. data = stdoutbuf + pipe.stdout.read()
  97. if data is not None and len(data) > 0:
  98. try:
  99. data = data.decode("utf-8")
  100. outdata.append(data)
  101. log.write(data)
  102. log.flush()
  103. stdoutbuf = b""
  104. except UnicodeDecodeError:
  105. stdoutbuf = data
  106. if pipe.stderr in r:
  107. data = stderrbuf + pipe.stderr.read()
  108. if data is not None and len(data) > 0:
  109. try:
  110. data = data.decode("utf-8")
  111. errdata.append(data)
  112. log.write(data)
  113. log.flush()
  114. stderrbuf = b""
  115. except UnicodeDecodeError:
  116. stderrbuf = data
  117. try:
  118. # Read all pipes while the process is open
  119. while pipe.poll() is None:
  120. read_all_pipes(log, rin, outdata, errdata)
  121. # Pocess closed, drain all pipes...
  122. read_all_pipes(log, rin, outdata, errdata)
  123. finally:
  124. log.flush()
  125. if pipe.stdout is not None:
  126. pipe.stdout.close()
  127. if pipe.stderr is not None:
  128. pipe.stderr.close()
  129. return ''.join(outdata), ''.join(errdata)
  130. def run(cmd, input=None, log=None, extrafiles=None, **options):
  131. """Convenience function to run a command and return its output, raising an
  132. exception when the command fails"""
  133. if not extrafiles:
  134. extrafiles = []
  135. if isinstance(cmd, str) and not "shell" in options:
  136. options["shell"] = True
  137. try:
  138. pipe = Popen(cmd, **options)
  139. except OSError as exc:
  140. if exc.errno == 2:
  141. raise NotFoundError(cmd)
  142. else:
  143. raise CmdError(cmd, exc)
  144. if log:
  145. stdout, stderr = _logged_communicate(pipe, log, input, extrafiles)
  146. else:
  147. stdout, stderr = pipe.communicate(input)
  148. if not stdout is None:
  149. stdout = stdout.decode("utf-8")
  150. if not stderr is None:
  151. stderr = stderr.decode("utf-8")
  152. if pipe.returncode != 0:
  153. raise ExecutionError(cmd, pipe.returncode, stdout, stderr)
  154. return stdout, stderr