process.py 5.3 KB

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