__init__.py 4.7 KB

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