__init__.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. #
  4. # BitBake Build System Python Library
  5. #
  6. # Copyright (C) 2003 Holger Schurig
  7. # Copyright (C) 2003, 2004 Chris Larson
  8. #
  9. # Based on Gentoo's portage.py.
  10. #
  11. # SPDX-License-Identifier: GPL-2.0-only
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License version 2 as
  15. # published by the Free Software Foundation.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License along
  23. # with this program; if not, write to the Free Software Foundation, Inc.,
  24. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  25. __version__ = "1.43.0"
  26. import sys
  27. if sys.version_info < (3, 4, 0):
  28. raise RuntimeError("Sorry, python 3.4.0 or later is required for this version of bitbake")
  29. class BBHandledException(Exception):
  30. """
  31. The big dilemma for generic bitbake code is what information to give the user
  32. when an exception occurs. Any exception inheriting this base exception class
  33. has already provided information to the user via some 'fired' message type such as
  34. an explicitly fired event using bb.fire, or a bb.error message. If bitbake
  35. encounters an exception derived from this class, no backtrace or other information
  36. will be given to the user, its assumed the earlier event provided the relevant information.
  37. """
  38. pass
  39. import os
  40. import logging
  41. class NullHandler(logging.Handler):
  42. def emit(self, record):
  43. pass
  44. Logger = logging.getLoggerClass()
  45. class BBLogger(Logger):
  46. def __init__(self, name):
  47. if name.split(".")[0] == "BitBake":
  48. self.debug = self.bbdebug
  49. Logger.__init__(self, name)
  50. def bbdebug(self, level, msg, *args, **kwargs):
  51. return self.log(logging.DEBUG - level + 1, msg, *args, **kwargs)
  52. def plain(self, msg, *args, **kwargs):
  53. return self.log(logging.INFO + 1, msg, *args, **kwargs)
  54. def verbose(self, msg, *args, **kwargs):
  55. return self.log(logging.INFO - 1, msg, *args, **kwargs)
  56. def verbnote(self, msg, *args, **kwargs):
  57. return self.log(logging.INFO + 2, msg, *args, **kwargs)
  58. logging.raiseExceptions = False
  59. logging.setLoggerClass(BBLogger)
  60. logger = logging.getLogger("BitBake")
  61. logger.addHandler(NullHandler())
  62. logger.setLevel(logging.DEBUG - 2)
  63. mainlogger = logging.getLogger("BitBake.Main")
  64. # This has to be imported after the setLoggerClass, as the import of bb.msg
  65. # can result in construction of the various loggers.
  66. import bb.msg
  67. from bb import fetch2 as fetch
  68. sys.modules['bb.fetch'] = sys.modules['bb.fetch2']
  69. # Messaging convenience functions
  70. def plain(*args):
  71. mainlogger.plain(''.join(args))
  72. def debug(lvl, *args):
  73. if isinstance(lvl, str):
  74. mainlogger.warning("Passed invalid debug level '%s' to bb.debug", lvl)
  75. args = (lvl,) + args
  76. lvl = 1
  77. mainlogger.debug(lvl, ''.join(args))
  78. def note(*args):
  79. mainlogger.info(''.join(args))
  80. #
  81. # A higher prioity note which will show on the console but isn't a warning
  82. #
  83. # Something is happening the user should be aware of but they probably did
  84. # something to make it happen
  85. #
  86. def verbnote(*args):
  87. mainlogger.verbnote(''.join(args))
  88. #
  89. # Warnings - things the user likely needs to pay attention to and fix
  90. #
  91. def warn(*args):
  92. mainlogger.warning(''.join(args))
  93. def error(*args, **kwargs):
  94. mainlogger.error(''.join(args), extra=kwargs)
  95. def fatal(*args, **kwargs):
  96. mainlogger.critical(''.join(args), extra=kwargs)
  97. raise BBHandledException()
  98. def deprecated(func, name=None, advice=""):
  99. """This is a decorator which can be used to mark functions
  100. as deprecated. It will result in a warning being emitted
  101. when the function is used."""
  102. import warnings
  103. if advice:
  104. advice = ": %s" % advice
  105. if name is None:
  106. name = func.__name__
  107. def newFunc(*args, **kwargs):
  108. warnings.warn("Call to deprecated function %s%s." % (name,
  109. advice),
  110. category=DeprecationWarning,
  111. stacklevel=2)
  112. return func(*args, **kwargs)
  113. newFunc.__name__ = func.__name__
  114. newFunc.__doc__ = func.__doc__
  115. newFunc.__dict__.update(func.__dict__)
  116. return newFunc
  117. # For compatibility
  118. def deprecate_import(current, modulename, fromlist, renames = None):
  119. """Import objects from one module into another, wrapping them with a DeprecationWarning"""
  120. import sys
  121. module = __import__(modulename, fromlist = fromlist)
  122. for position, objname in enumerate(fromlist):
  123. obj = getattr(module, objname)
  124. newobj = deprecated(obj, "{0}.{1}".format(current, objname),
  125. "Please use {0}.{1} instead".format(modulename, objname))
  126. if renames:
  127. newname = renames[position]
  128. else:
  129. newname = objname
  130. setattr(sys.modules[current], newname, newobj)