Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 24 | __version__ = "1.27.1" |
| 25 | |
| 26 | import sys |
| 27 | if sys.version_info < (2, 7, 3): |
| 28 | raise RuntimeError("Sorry, python 2.7.3 or later is required for this version of bitbake") |
| 29 | |
| 30 | |
| 31 | class BBHandledException(Exception): |
| 32 | """ |
| 33 | The big dilemma for generic bitbake code is what information to give the user |
| 34 | when an exception occurs. Any exception inheriting this base exception class |
| 35 | has already provided information to the user via some 'fired' message type such as |
| 36 | an explicitly fired event using bb.fire, or a bb.error message. If bitbake |
| 37 | encounters an exception derived from this class, no backtrace or other information |
| 38 | will be given to the user, its assumed the earlier event provided the relevant information. |
| 39 | """ |
| 40 | pass |
| 41 | |
| 42 | import os |
| 43 | import logging |
| 44 | |
| 45 | |
| 46 | class NullHandler(logging.Handler): |
| 47 | def emit(self, record): |
| 48 | pass |
| 49 | |
| 50 | Logger = logging.getLoggerClass() |
| 51 | class BBLogger(Logger): |
| 52 | def __init__(self, name): |
| 53 | if name.split(".")[0] == "BitBake": |
| 54 | self.debug = self.bbdebug |
| 55 | Logger.__init__(self, name) |
| 56 | |
| 57 | def bbdebug(self, level, msg, *args, **kwargs): |
| 58 | return self.log(logging.DEBUG - level + 1, msg, *args, **kwargs) |
| 59 | |
| 60 | def plain(self, msg, *args, **kwargs): |
| 61 | return self.log(logging.INFO + 1, msg, *args, **kwargs) |
| 62 | |
| 63 | def verbose(self, msg, *args, **kwargs): |
| 64 | return self.log(logging.INFO - 1, msg, *args, **kwargs) |
| 65 | |
| 66 | logging.raiseExceptions = False |
| 67 | logging.setLoggerClass(BBLogger) |
| 68 | |
| 69 | logger = logging.getLogger("BitBake") |
| 70 | logger.addHandler(NullHandler()) |
| 71 | logger.setLevel(logging.DEBUG - 2) |
| 72 | |
| 73 | # This has to be imported after the setLoggerClass, as the import of bb.msg |
| 74 | # can result in construction of the various loggers. |
| 75 | import bb.msg |
| 76 | |
| 77 | from bb import fetch2 as fetch |
| 78 | sys.modules['bb.fetch'] = sys.modules['bb.fetch2'] |
| 79 | |
| 80 | # Messaging convenience functions |
| 81 | def plain(*args): |
| 82 | logger.plain(''.join(args)) |
| 83 | |
| 84 | def debug(lvl, *args): |
| 85 | if isinstance(lvl, basestring): |
| 86 | logger.warn("Passed invalid debug level '%s' to bb.debug", lvl) |
| 87 | args = (lvl,) + args |
| 88 | lvl = 1 |
| 89 | logger.debug(lvl, ''.join(args)) |
| 90 | |
| 91 | def note(*args): |
| 92 | logger.info(''.join(args)) |
| 93 | |
| 94 | def warn(*args): |
| 95 | logger.warn(''.join(args)) |
| 96 | |
| 97 | def error(*args, **kwargs): |
| 98 | logger.error(''.join(args), extra=kwargs) |
| 99 | |
| 100 | def fatal(*args, **kwargs): |
| 101 | logger.critical(''.join(args), extra=kwargs) |
| 102 | raise BBHandledException() |
| 103 | |
| 104 | def deprecated(func, name=None, advice=""): |
| 105 | """This is a decorator which can be used to mark functions |
| 106 | as deprecated. It will result in a warning being emitted |
| 107 | when the function is used.""" |
| 108 | import warnings |
| 109 | |
| 110 | if advice: |
| 111 | advice = ": %s" % advice |
| 112 | if name is None: |
| 113 | name = func.__name__ |
| 114 | |
| 115 | def newFunc(*args, **kwargs): |
| 116 | warnings.warn("Call to deprecated function %s%s." % (name, |
| 117 | advice), |
| 118 | category=DeprecationWarning, |
| 119 | stacklevel=2) |
| 120 | return func(*args, **kwargs) |
| 121 | newFunc.__name__ = func.__name__ |
| 122 | newFunc.__doc__ = func.__doc__ |
| 123 | newFunc.__dict__.update(func.__dict__) |
| 124 | return newFunc |
| 125 | |
| 126 | # For compatibility |
| 127 | def deprecate_import(current, modulename, fromlist, renames = None): |
| 128 | """Import objects from one module into another, wrapping them with a DeprecationWarning""" |
| 129 | import sys |
| 130 | |
| 131 | module = __import__(modulename, fromlist = fromlist) |
| 132 | for position, objname in enumerate(fromlist): |
| 133 | obj = getattr(module, objname) |
| 134 | newobj = deprecated(obj, "{0}.{1}".format(current, objname), |
| 135 | "Please use {0}.{1} instead".format(modulename, objname)) |
| 136 | if renames: |
| 137 | newname = renames[position] |
| 138 | else: |
| 139 | newname = objname |
| 140 | |
| 141 | setattr(sys.modules[current], newname, newobj) |
| 142 | |