process.py 4.4 KB

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