__init__.py 4.5 KB

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