Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 9 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 12 | __version__ = "1.49.0" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | |
| 14 | import sys |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 15 | if sys.version_info < (3, 5, 0): |
| 16 | raise RuntimeError("Sorry, python 3.5.0 or later is required for this version of bitbake") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | |
| 18 | |
| 19 | class BBHandledException(Exception): |
| 20 | """ |
| 21 | The big dilemma for generic bitbake code is what information to give the user |
| 22 | when an exception occurs. Any exception inheriting this base exception class |
| 23 | has already provided information to the user via some 'fired' message type such as |
| 24 | an explicitly fired event using bb.fire, or a bb.error message. If bitbake |
| 25 | encounters an exception derived from this class, no backtrace or other information |
| 26 | will be given to the user, its assumed the earlier event provided the relevant information. |
| 27 | """ |
| 28 | pass |
| 29 | |
| 30 | import os |
| 31 | import logging |
| 32 | |
| 33 | |
| 34 | class NullHandler(logging.Handler): |
| 35 | def emit(self, record): |
| 36 | pass |
| 37 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 38 | class BBLoggerMixin(object): |
| 39 | def __init__(self, *args, **kwargs): |
| 40 | # Does nothing to allow calling super() from derived classes |
| 41 | pass |
| 42 | |
| 43 | def setup_bblogger(self, name): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | if name.split(".")[0] == "BitBake": |
| 45 | self.debug = self.bbdebug |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | |
| 47 | def bbdebug(self, level, msg, *args, **kwargs): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 48 | loglevel = logging.DEBUG - level + 1 |
| 49 | if not bb.event.worker_pid: |
| 50 | if self.name in bb.msg.loggerDefaultDomains and loglevel > (bb.msg.loggerDefaultDomains[self.name]): |
| 51 | return |
| 52 | if loglevel > bb.msg.loggerDefaultLogLevel: |
| 53 | return |
| 54 | return self.log(loglevel, msg, *args, **kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | |
| 56 | def plain(self, msg, *args, **kwargs): |
| 57 | return self.log(logging.INFO + 1, msg, *args, **kwargs) |
| 58 | |
| 59 | def verbose(self, msg, *args, **kwargs): |
| 60 | return self.log(logging.INFO - 1, msg, *args, **kwargs) |
| 61 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 62 | def verbnote(self, msg, *args, **kwargs): |
| 63 | return self.log(logging.INFO + 2, msg, *args, **kwargs) |
| 64 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 65 | Logger = logging.getLoggerClass() |
| 66 | class BBLogger(Logger, BBLoggerMixin): |
| 67 | def __init__(self, name, *args, **kwargs): |
| 68 | self.setup_bblogger(name) |
| 69 | super().__init__(name, *args, **kwargs) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 70 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | logging.raiseExceptions = False |
| 72 | logging.setLoggerClass(BBLogger) |
| 73 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 74 | class BBLoggerAdapter(logging.LoggerAdapter, BBLoggerMixin): |
| 75 | def __init__(self, logger, *args, **kwargs): |
| 76 | self.setup_bblogger(logger.name) |
| 77 | super().__init__(logger, *args, **kwargs) |
| 78 | |
| 79 | if sys.version_info < (3, 6): |
| 80 | # These properties were added in Python 3.6. Add them in older versions |
| 81 | # for compatibility |
| 82 | @property |
| 83 | def manager(self): |
| 84 | return self.logger.manager |
| 85 | |
| 86 | @manager.setter |
| 87 | def manager(self, value): |
| 88 | self.logger.manager = value |
| 89 | |
| 90 | @property |
| 91 | def name(self): |
| 92 | return self.logger.name |
| 93 | |
| 94 | def __repr__(self): |
| 95 | logger = self.logger |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 96 | level = logger.getLevelName(logger.getEffectiveLevel()) |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 97 | return '<%s %s (%s)>' % (self.__class__.__name__, logger.name, level) |
| 98 | |
| 99 | logging.LoggerAdapter = BBLoggerAdapter |
| 100 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | logger = logging.getLogger("BitBake") |
| 102 | logger.addHandler(NullHandler()) |
| 103 | logger.setLevel(logging.DEBUG - 2) |
| 104 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 105 | mainlogger = logging.getLogger("BitBake.Main") |
| 106 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 107 | class PrefixLoggerAdapter(logging.LoggerAdapter): |
| 108 | def __init__(self, prefix, logger): |
| 109 | super().__init__(logger, {}) |
| 110 | self.__msg_prefix = prefix |
| 111 | |
| 112 | def process(self, msg, kwargs): |
| 113 | return "%s%s" %(self.__msg_prefix, msg), kwargs |
| 114 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | # This has to be imported after the setLoggerClass, as the import of bb.msg |
| 116 | # can result in construction of the various loggers. |
| 117 | import bb.msg |
| 118 | |
| 119 | from bb import fetch2 as fetch |
| 120 | sys.modules['bb.fetch'] = sys.modules['bb.fetch2'] |
| 121 | |
| 122 | # Messaging convenience functions |
| 123 | def plain(*args): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 124 | mainlogger.plain(''.join(args)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | |
| 126 | def debug(lvl, *args): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 127 | if isinstance(lvl, str): |
| 128 | mainlogger.warning("Passed invalid debug level '%s' to bb.debug", lvl) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | args = (lvl,) + args |
| 130 | lvl = 1 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 131 | mainlogger.debug(lvl, ''.join(args)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 | |
| 133 | def note(*args): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 134 | mainlogger.info(''.join(args)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 135 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 136 | # |
| 137 | # A higher prioity note which will show on the console but isn't a warning |
| 138 | # |
| 139 | # Something is happening the user should be aware of but they probably did |
| 140 | # something to make it happen |
| 141 | # |
| 142 | def verbnote(*args): |
| 143 | mainlogger.verbnote(''.join(args)) |
| 144 | |
| 145 | # |
| 146 | # Warnings - things the user likely needs to pay attention to and fix |
| 147 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | def warn(*args): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 149 | mainlogger.warning(''.join(args)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 150 | |
| 151 | def error(*args, **kwargs): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 152 | mainlogger.error(''.join(args), extra=kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 153 | |
| 154 | def fatal(*args, **kwargs): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 155 | mainlogger.critical(''.join(args), extra=kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 156 | raise BBHandledException() |
| 157 | |
| 158 | def deprecated(func, name=None, advice=""): |
| 159 | """This is a decorator which can be used to mark functions |
| 160 | as deprecated. It will result in a warning being emitted |
| 161 | when the function is used.""" |
| 162 | import warnings |
| 163 | |
| 164 | if advice: |
| 165 | advice = ": %s" % advice |
| 166 | if name is None: |
| 167 | name = func.__name__ |
| 168 | |
| 169 | def newFunc(*args, **kwargs): |
| 170 | warnings.warn("Call to deprecated function %s%s." % (name, |
| 171 | advice), |
| 172 | category=DeprecationWarning, |
| 173 | stacklevel=2) |
| 174 | return func(*args, **kwargs) |
| 175 | newFunc.__name__ = func.__name__ |
| 176 | newFunc.__doc__ = func.__doc__ |
| 177 | newFunc.__dict__.update(func.__dict__) |
| 178 | return newFunc |
| 179 | |
| 180 | # For compatibility |
| 181 | def deprecate_import(current, modulename, fromlist, renames = None): |
| 182 | """Import objects from one module into another, wrapping them with a DeprecationWarning""" |
| 183 | import sys |
| 184 | |
| 185 | module = __import__(modulename, fromlist = fromlist) |
| 186 | for position, objname in enumerate(fromlist): |
| 187 | obj = getattr(module, objname) |
| 188 | newobj = deprecated(obj, "{0}.{1}".format(current, objname), |
| 189 | "Please use {0}.{1} instead".format(modulename, objname)) |
| 190 | if renames: |
| 191 | newname = renames[position] |
| 192 | else: |
| 193 | newname = objname |
| 194 | |
| 195 | setattr(sys.modules[current], newname, newobj) |
| 196 | |