You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
7.5 KiB

6 years ago
  1. #
  2. # Copyright (c) 2001 - 2017 The SCons Foundation
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining
  5. # a copy of this software and associated documentation files (the
  6. # "Software"), to deal in the Software without restriction, including
  7. # without limitation the rights to use, copy, modify, merge, publish,
  8. # distribute, sublicense, and/or sell copies of the Software, and to
  9. # permit persons to whom the Software is furnished to do so, subject to
  10. # the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included
  13. # in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  16. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #
  23. """SCons.Errors
  24. This file contains the exception classes used to handle internal
  25. and user errors in SCons.
  26. """
  27. __revision__ = "src/engine/SCons/Errors.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  28. import shutil
  29. import SCons.Util
  30. class BuildError(Exception):
  31. """ Errors occurring while building.
  32. BuildError have the following attributes:
  33. =========================================
  34. Information about the cause of the build error:
  35. -----------------------------------------------
  36. errstr : a description of the error message
  37. status : the return code of the action that caused the build error.
  38. Must be set to a non-zero value even if the build error is not due
  39. to an action returning a non-zero returned code.
  40. exitstatus : SCons exit status due to this build error.
  41. Must be nonzero unless due to an explicit Exit()
  42. call. Not always the same as status, since
  43. actions return a status code that should be
  44. respected, but SCons typically exits with 2
  45. irrespective of the return value of the failed
  46. action.
  47. filename : The name of the file or directory that caused the
  48. build error. Set to None if no files are associated with
  49. this error. This might be different from the target
  50. being built. For example, failure to create the
  51. directory in which the target file will appear. It
  52. can be None if the error is not due to a particular
  53. filename.
  54. exc_info : Info about exception that caused the build
  55. error. Set to (None, None, None) if this build
  56. error is not due to an exception.
  57. Information about the cause of the location of the error:
  58. ---------------------------------------------------------
  59. node : the error occured while building this target node(s)
  60. executor : the executor that caused the build to fail (might
  61. be None if the build failures is not due to the
  62. executor failing)
  63. action : the action that caused the build to fail (might be
  64. None if the build failures is not due to the an
  65. action failure)
  66. command : the command line for the action that caused the
  67. build to fail (might be None if the build failures
  68. is not due to the an action failure)
  69. """
  70. def __init__(self,
  71. node=None, errstr="Unknown error", status=2, exitstatus=2,
  72. filename=None, executor=None, action=None, command=None,
  73. exc_info=(None, None, None)):
  74. # py3: errstr should be string and not bytes.
  75. self.errstr = SCons.Util.to_str(errstr)
  76. self.status = status
  77. self.exitstatus = exitstatus
  78. self.filename = filename
  79. self.exc_info = exc_info
  80. self.node = node
  81. self.executor = executor
  82. self.action = action
  83. self.command = command
  84. Exception.__init__(self, node, errstr, status, exitstatus, filename,
  85. executor, action, command, exc_info)
  86. def __str__(self):
  87. if self.filename:
  88. return self.filename + ': ' + self.errstr
  89. else:
  90. return self.errstr
  91. class InternalError(Exception):
  92. pass
  93. class UserError(Exception):
  94. pass
  95. class StopError(Exception):
  96. pass
  97. class EnvironmentError(Exception):
  98. pass
  99. class MSVCError(IOError):
  100. pass
  101. class ExplicitExit(Exception):
  102. def __init__(self, node=None, status=None, *args):
  103. self.node = node
  104. self.status = status
  105. self.exitstatus = status
  106. Exception.__init__(self, *args)
  107. def convert_to_BuildError(status, exc_info=None):
  108. """
  109. Convert any return code a BuildError Exception.
  110. :Parameters:
  111. - `status`: can either be a return code or an Exception.
  112. The buildError.status we set here will normally be
  113. used as the exit status of the "scons" process.
  114. """
  115. if not exc_info and isinstance(status, Exception):
  116. exc_info = (status.__class__, status, None)
  117. if isinstance(status, BuildError):
  118. buildError = status
  119. buildError.exitstatus = 2 # always exit with 2 on build errors
  120. elif isinstance(status, ExplicitExit):
  121. status = status.status
  122. errstr = 'Explicit exit, status %s' % status
  123. buildError = BuildError(
  124. errstr=errstr,
  125. status=status, # might be 0, OK here
  126. exitstatus=status, # might be 0, OK here
  127. exc_info=exc_info)
  128. elif isinstance(status, (StopError, UserError)):
  129. buildError = BuildError(
  130. errstr=str(status),
  131. status=2,
  132. exitstatus=2,
  133. exc_info=exc_info)
  134. elif isinstance(status, shutil.SameFileError):
  135. # PY3 has a exception for when copying file to itself
  136. # It's object provides info differently than below
  137. try:
  138. filename = status.filename
  139. except AttributeError:
  140. filename = None
  141. buildError = BuildError(
  142. errstr=status.args[0],
  143. status=status.errno,
  144. exitstatus=2,
  145. filename=filename,
  146. exc_info=exc_info)
  147. elif isinstance(status, (EnvironmentError, OSError, IOError)):
  148. # If an IOError/OSError happens, raise a BuildError.
  149. # Report the name of the file or directory that caused the
  150. # error, which might be different from the target being built
  151. # (for example, failure to create the directory in which the
  152. # target file will appear).
  153. try:
  154. filename = status.filename
  155. except AttributeError:
  156. filename = None
  157. buildError = BuildError(
  158. errstr=status.strerror,
  159. status=status.errno,
  160. exitstatus=2,
  161. filename=filename,
  162. exc_info=exc_info)
  163. elif isinstance(status, Exception):
  164. buildError = BuildError(
  165. errstr='%s : %s' % (status.__class__.__name__, status),
  166. status=2,
  167. exitstatus=2,
  168. exc_info=exc_info)
  169. elif SCons.Util.is_String(status):
  170. buildError = BuildError(
  171. errstr=status,
  172. status=2,
  173. exitstatus=2)
  174. else:
  175. buildError = BuildError(
  176. errstr="Error %s" % status,
  177. status=status,
  178. exitstatus=2)
  179. #import sys
  180. #sys.stderr.write("convert_to_BuildError: status %s => (errstr %s, status %s)\n"%(status,buildError.errstr, buildError.status))
  181. return buildError
  182. # Local Variables:
  183. # tab-width:4
  184. # indent-tabs-mode:nil
  185. # End:
  186. # vim: set expandtab tabstop=4 shiftwidth=4: