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 | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 12 | __version__ = "2.0.1" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | |
| 14 | import sys |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 15 | if sys.version_info < (3, 6, 0): |
| 16 | raise RuntimeError("Sorry, python 3.6.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 |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 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 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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": |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 45 | self.debug = self._debug_helper |
| 46 | |
| 47 | def _debug_helper(self, *args, **kwargs): |
| 48 | return self.bbdebug(1, *args, **kwargs) |
| 49 | |
| 50 | def debug2(self, *args, **kwargs): |
| 51 | return self.bbdebug(2, *args, **kwargs) |
| 52 | |
| 53 | def debug3(self, *args, **kwargs): |
| 54 | return self.bbdebug(3, *args, **kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | |
| 56 | def bbdebug(self, level, msg, *args, **kwargs): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 57 | loglevel = logging.DEBUG - level + 1 |
| 58 | if not bb.event.worker_pid: |
| 59 | if self.name in bb.msg.loggerDefaultDomains and loglevel > (bb.msg.loggerDefaultDomains[self.name]): |
| 60 | return |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 61 | if loglevel < bb.msg.loggerDefaultLogLevel: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 62 | return |
| 63 | return self.log(loglevel, msg, *args, **kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | |
| 65 | def plain(self, msg, *args, **kwargs): |
| 66 | return self.log(logging.INFO + 1, msg, *args, **kwargs) |
| 67 | |
| 68 | def verbose(self, msg, *args, **kwargs): |
| 69 | return self.log(logging.INFO - 1, msg, *args, **kwargs) |
| 70 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 71 | def verbnote(self, msg, *args, **kwargs): |
| 72 | return self.log(logging.INFO + 2, msg, *args, **kwargs) |
| 73 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 74 | def warnonce(self, msg, *args, **kwargs): |
| 75 | return self.log(logging.WARNING - 1, msg, *args, **kwargs) |
| 76 | |
| 77 | def erroronce(self, msg, *args, **kwargs): |
| 78 | return self.log(logging.ERROR - 1, msg, *args, **kwargs) |
| 79 | |
| 80 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 81 | Logger = logging.getLoggerClass() |
| 82 | class BBLogger(Logger, BBLoggerMixin): |
| 83 | def __init__(self, name, *args, **kwargs): |
| 84 | self.setup_bblogger(name) |
| 85 | super().__init__(name, *args, **kwargs) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 86 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | logging.raiseExceptions = False |
| 88 | logging.setLoggerClass(BBLogger) |
| 89 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 90 | class BBLoggerAdapter(logging.LoggerAdapter, BBLoggerMixin): |
| 91 | def __init__(self, logger, *args, **kwargs): |
| 92 | self.setup_bblogger(logger.name) |
| 93 | super().__init__(logger, *args, **kwargs) |
| 94 | |
| 95 | if sys.version_info < (3, 6): |
| 96 | # These properties were added in Python 3.6. Add them in older versions |
| 97 | # for compatibility |
| 98 | @property |
| 99 | def manager(self): |
| 100 | return self.logger.manager |
| 101 | |
| 102 | @manager.setter |
| 103 | def manager(self, value): |
| 104 | self.logger.manager = value |
| 105 | |
| 106 | @property |
| 107 | def name(self): |
| 108 | return self.logger.name |
| 109 | |
| 110 | def __repr__(self): |
| 111 | logger = self.logger |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 112 | level = logger.getLevelName(logger.getEffectiveLevel()) |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 113 | return '<%s %s (%s)>' % (self.__class__.__name__, logger.name, level) |
| 114 | |
| 115 | logging.LoggerAdapter = BBLoggerAdapter |
| 116 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | logger = logging.getLogger("BitBake") |
| 118 | logger.addHandler(NullHandler()) |
| 119 | logger.setLevel(logging.DEBUG - 2) |
| 120 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 121 | mainlogger = logging.getLogger("BitBake.Main") |
| 122 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 123 | class PrefixLoggerAdapter(logging.LoggerAdapter): |
| 124 | def __init__(self, prefix, logger): |
| 125 | super().__init__(logger, {}) |
| 126 | self.__msg_prefix = prefix |
| 127 | |
| 128 | def process(self, msg, kwargs): |
| 129 | return "%s%s" %(self.__msg_prefix, msg), kwargs |
| 130 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | # This has to be imported after the setLoggerClass, as the import of bb.msg |
| 132 | # can result in construction of the various loggers. |
| 133 | import bb.msg |
| 134 | |
| 135 | from bb import fetch2 as fetch |
| 136 | sys.modules['bb.fetch'] = sys.modules['bb.fetch2'] |
| 137 | |
| 138 | # Messaging convenience functions |
| 139 | def plain(*args): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 140 | mainlogger.plain(''.join(args)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | |
| 142 | def debug(lvl, *args): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 143 | if isinstance(lvl, str): |
| 144 | mainlogger.warning("Passed invalid debug level '%s' to bb.debug", lvl) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 145 | args = (lvl,) + args |
| 146 | lvl = 1 |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 147 | mainlogger.bbdebug(lvl, ''.join(args)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | |
| 149 | def note(*args): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 150 | mainlogger.info(''.join(args)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 152 | # |
| 153 | # A higher prioity note which will show on the console but isn't a warning |
| 154 | # |
| 155 | # Something is happening the user should be aware of but they probably did |
| 156 | # something to make it happen |
| 157 | # |
| 158 | def verbnote(*args): |
| 159 | mainlogger.verbnote(''.join(args)) |
| 160 | |
| 161 | # |
| 162 | # Warnings - things the user likely needs to pay attention to and fix |
| 163 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 164 | def warn(*args): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 165 | mainlogger.warning(''.join(args)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 167 | def warnonce(*args): |
| 168 | mainlogger.warnonce(''.join(args)) |
| 169 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 170 | def error(*args, **kwargs): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 171 | mainlogger.error(''.join(args), extra=kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 172 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 173 | def erroronce(*args): |
| 174 | mainlogger.erroronce(''.join(args)) |
| 175 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 176 | def fatal(*args, **kwargs): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 177 | mainlogger.critical(''.join(args), extra=kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 178 | raise BBHandledException() |
| 179 | |
| 180 | def deprecated(func, name=None, advice=""): |
| 181 | """This is a decorator which can be used to mark functions |
| 182 | as deprecated. It will result in a warning being emitted |
| 183 | when the function is used.""" |
| 184 | import warnings |
| 185 | |
| 186 | if advice: |
| 187 | advice = ": %s" % advice |
| 188 | if name is None: |
| 189 | name = func.__name__ |
| 190 | |
| 191 | def newFunc(*args, **kwargs): |
| 192 | warnings.warn("Call to deprecated function %s%s." % (name, |
| 193 | advice), |
| 194 | category=DeprecationWarning, |
| 195 | stacklevel=2) |
| 196 | return func(*args, **kwargs) |
| 197 | newFunc.__name__ = func.__name__ |
| 198 | newFunc.__doc__ = func.__doc__ |
| 199 | newFunc.__dict__.update(func.__dict__) |
| 200 | return newFunc |
| 201 | |
| 202 | # For compatibility |
| 203 | def deprecate_import(current, modulename, fromlist, renames = None): |
| 204 | """Import objects from one module into another, wrapping them with a DeprecationWarning""" |
| 205 | import sys |
| 206 | |
| 207 | module = __import__(modulename, fromlist = fromlist) |
| 208 | for position, objname in enumerate(fromlist): |
| 209 | obj = getattr(module, objname) |
| 210 | newobj = deprecated(obj, "{0}.{1}".format(current, objname), |
| 211 | "Please use {0}.{1} instead".format(modulename, objname)) |
| 212 | if renames: |
| 213 | newname = renames[position] |
| 214 | else: |
| 215 | newname = objname |
| 216 | |
| 217 | setattr(sys.modules[current], newname, newobj) |
| 218 | |