__init__.py 4.6 KB

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