2005-08-31 10:47:56 +00:00
|
|
|
# ex:ts=4:sw=4:sts=4:et
|
|
|
|
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
"""
|
|
|
|
BitBake 'Event' implementation
|
|
|
|
|
|
|
|
Classes and functions for manipulating 'events' in the
|
|
|
|
BitBake build tools.
|
|
|
|
"""
|
|
|
|
|
2007-01-08 23:53:01 +00:00
|
|
|
# Copyright (C) 2003, 2004 Chris Larson
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2010-04-08 17:22:29 +00:00
|
|
|
import os, sys
|
2010-04-08 23:30:52 +00:00
|
|
|
import warnings
|
2010-12-10 21:38:48 +00:00
|
|
|
try:
|
|
|
|
import cPickle as pickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle
|
2010-06-09 23:17:29 +00:00
|
|
|
import logging
|
2010-09-28 15:24:55 +00:00
|
|
|
import atexit
|
2010-06-09 23:17:29 +00:00
|
|
|
import bb.utils
|
2010-01-20 18:46:02 +00:00
|
|
|
|
|
|
|
# This is the pid for which we should generate the event. This is set when
|
|
|
|
# the runqueue forks off.
|
|
|
|
worker_pid = 0
|
|
|
|
worker_pipe = None
|
2010-08-16 15:37:29 +00:00
|
|
|
useStdout = True
|
2005-08-31 10:47:56 +00:00
|
|
|
|
2010-09-10 00:57:51 +00:00
|
|
|
class Event(object):
|
2005-08-31 10:47:56 +00:00
|
|
|
"""Base class for events"""
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.pid = worker_pid
|
2005-08-31 10:47:56 +00:00
|
|
|
|
|
|
|
NotHandled = 0
|
2010-04-08 23:30:52 +00:00
|
|
|
Handled = 1
|
2006-05-09 15:44:08 +00:00
|
|
|
|
|
|
|
Registered = 10
|
|
|
|
AlreadyRegistered = 14
|
|
|
|
|
|
|
|
# Internal
|
2010-01-20 18:46:02 +00:00
|
|
|
_handlers = {}
|
|
|
|
_ui_handlers = {}
|
|
|
|
_ui_handler_seq = 0
|
2005-08-31 10:47:56 +00:00
|
|
|
|
2010-03-31 03:06:07 +00:00
|
|
|
# For compatibility
|
|
|
|
bb.utils._context["NotHandled"] = NotHandled
|
|
|
|
bb.utils._context["Handled"] = Handled
|
|
|
|
|
2010-03-25 17:33:41 +00:00
|
|
|
def fire_class_handlers(event, d):
|
2010-12-10 16:04:08 +00:00
|
|
|
if isinstance(event, logging.LogRecord):
|
2010-04-09 22:56:14 +00:00
|
|
|
return
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
for handler in _handlers:
|
|
|
|
h = _handlers[handler]
|
|
|
|
event.data = d
|
2005-08-31 10:47:56 +00:00
|
|
|
if type(h).__name__ == "code":
|
2010-03-31 03:06:07 +00:00
|
|
|
locals = {"e": event}
|
2010-03-31 15:52:41 +00:00
|
|
|
bb.utils.simple_exec(h, locals)
|
2010-04-08 23:30:52 +00:00
|
|
|
ret = bb.utils.better_eval("tmpHandler(e)", locals)
|
|
|
|
if ret is not None:
|
|
|
|
warnings.warn("Using Handled/NotHandled in event handlers is deprecated",
|
|
|
|
DeprecationWarning, stacklevel = 2)
|
2005-08-31 10:47:56 +00:00
|
|
|
else:
|
2010-01-20 18:46:02 +00:00
|
|
|
h(event)
|
|
|
|
del event.data
|
|
|
|
|
2010-09-28 15:24:55 +00:00
|
|
|
ui_queue = []
|
|
|
|
@atexit.register
|
|
|
|
def print_ui_queue():
|
|
|
|
"""If we're exiting before a UI has been spawned, display any queued
|
|
|
|
LogRecords to the console."""
|
|
|
|
logger = logging.getLogger("BitBake")
|
|
|
|
if not _ui_handlers:
|
|
|
|
console = logging.StreamHandler(sys.stdout)
|
|
|
|
console.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
|
|
|
|
logger.handlers = [console]
|
|
|
|
while ui_queue:
|
2010-09-28 15:52:32 +00:00
|
|
|
event = ui_queue.pop()
|
2010-09-28 15:24:55 +00:00
|
|
|
if isinstance(event, logging.LogRecord):
|
|
|
|
logger.handle(event)
|
|
|
|
|
2010-03-25 17:33:41 +00:00
|
|
|
def fire_ui_handlers(event, d):
|
2010-09-28 15:24:55 +00:00
|
|
|
if not _ui_handlers:
|
|
|
|
# No UI handlers registered yet, queue up the messages
|
2010-09-28 15:52:32 +00:00
|
|
|
ui_queue.append(event)
|
2010-09-28 15:24:55 +00:00
|
|
|
return
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
errors = []
|
|
|
|
for h in _ui_handlers:
|
|
|
|
#print "Sending event %s" % event
|
|
|
|
try:
|
|
|
|
# We use pickle here since it better handles object instances
|
|
|
|
# which xmlrpc's marshaller does not. Events *must* be serializable
|
|
|
|
# by pickle.
|
|
|
|
_ui_handlers[h].event.send((pickle.dumps(event)))
|
|
|
|
except:
|
|
|
|
errors.append(h)
|
|
|
|
for h in errors:
|
|
|
|
del _ui_handlers[h]
|
|
|
|
|
2010-03-25 17:33:41 +00:00
|
|
|
def fire(event, d):
|
|
|
|
"""Fire off an Event"""
|
|
|
|
|
2010-03-24 23:56:12 +00:00
|
|
|
# We can fire class handlers in the worker process context and this is
|
2010-03-25 17:33:41 +00:00
|
|
|
# desired so they get the task based datastore.
|
2010-03-24 23:56:12 +00:00
|
|
|
# UI handlers need to be fired in the server context so we defer this. They
|
2010-03-25 17:33:41 +00:00
|
|
|
# don't have a datastore so the datastore context isn't a problem.
|
|
|
|
|
|
|
|
fire_class_handlers(event, d)
|
|
|
|
if worker_pid != 0:
|
|
|
|
worker_fire(event, d)
|
|
|
|
else:
|
|
|
|
fire_ui_handlers(event, d)
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
def worker_fire(event, d):
|
|
|
|
data = "<event>" + pickle.dumps(event) + "</event>"
|
2010-08-16 15:37:29 +00:00
|
|
|
worker_pipe.write(data)
|
|
|
|
worker_pipe.flush()
|
2010-01-20 18:46:02 +00:00
|
|
|
|
|
|
|
def fire_from_worker(event, d):
|
|
|
|
if not event.startswith("<event>") or not event.endswith("</event>"):
|
2010-08-16 15:37:29 +00:00
|
|
|
print("Error, not an event %s" % event)
|
2010-01-20 18:46:02 +00:00
|
|
|
return
|
2010-08-20 11:25:19 +00:00
|
|
|
#print "Got event %s" % event
|
2010-01-20 18:46:02 +00:00
|
|
|
event = pickle.loads(event[7:-8])
|
2010-03-25 17:33:41 +00:00
|
|
|
fire_ui_handlers(event, d)
|
2005-08-31 10:47:56 +00:00
|
|
|
|
2006-05-09 15:44:08 +00:00
|
|
|
def register(name, handler):
|
2005-08-31 10:47:56 +00:00
|
|
|
"""Register an Event handler"""
|
2006-05-09 15:44:08 +00:00
|
|
|
|
|
|
|
# already registered
|
2010-01-20 18:46:02 +00:00
|
|
|
if name in _handlers:
|
2006-05-09 15:44:08 +00:00
|
|
|
return AlreadyRegistered
|
|
|
|
|
2005-08-31 10:47:56 +00:00
|
|
|
if handler is not None:
|
2010-01-20 18:46:02 +00:00
|
|
|
# handle string containing python code
|
2005-08-31 10:47:56 +00:00
|
|
|
if type(handler).__name__ == "str":
|
2010-01-20 18:46:02 +00:00
|
|
|
tmp = "def tmpHandler(e):\n%s" % handler
|
|
|
|
comp = bb.utils.better_compile(tmp, "tmpHandler(e)", "bb.event._registerCode")
|
|
|
|
_handlers[name] = comp
|
2006-05-09 15:44:08 +00:00
|
|
|
else:
|
2010-01-20 18:46:02 +00:00
|
|
|
_handlers[name] = handler
|
2006-05-09 15:44:08 +00:00
|
|
|
|
|
|
|
return Registered
|
2005-08-31 10:47:56 +00:00
|
|
|
|
2006-05-09 15:44:08 +00:00
|
|
|
def remove(name, handler):
|
2005-08-31 10:47:56 +00:00
|
|
|
"""Remove an Event handler"""
|
2010-01-20 18:46:02 +00:00
|
|
|
_handlers.pop(name)
|
2005-08-31 10:47:56 +00:00
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
def register_UIHhandler(handler):
|
|
|
|
bb.event._ui_handler_seq = bb.event._ui_handler_seq + 1
|
|
|
|
_ui_handlers[_ui_handler_seq] = handler
|
2010-08-16 15:37:29 +00:00
|
|
|
bb.event.useStdout = False
|
2010-01-20 18:46:02 +00:00
|
|
|
return _ui_handler_seq
|
2005-08-31 10:47:56 +00:00
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
def unregister_UIHhandler(handlerNum):
|
|
|
|
if handlerNum in _ui_handlers:
|
|
|
|
del _ui_handlers[handlerNum]
|
|
|
|
return
|
2005-08-31 10:47:56 +00:00
|
|
|
|
|
|
|
def getName(e):
|
|
|
|
"""Returns the name of a class or class instance"""
|
|
|
|
if getattr(e, "__name__", None) == None:
|
|
|
|
return e.__class__.__name__
|
|
|
|
else:
|
|
|
|
return e.__name__
|
|
|
|
|
2007-08-03 13:40:52 +00:00
|
|
|
class ConfigParsed(Event):
|
|
|
|
"""Configuration Parsing Complete"""
|
2005-08-31 10:47:56 +00:00
|
|
|
|
2009-11-03 23:20:15 +00:00
|
|
|
class RecipeParsed(Event):
|
|
|
|
""" Recipe Parsing Complete """
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
def __init__(self, fn):
|
2009-11-03 23:20:15 +00:00
|
|
|
self.fn = fn
|
2010-01-20 18:46:02 +00:00
|
|
|
Event.__init__(self)
|
2009-11-03 23:20:15 +00:00
|
|
|
|
2008-03-03 22:01:45 +00:00
|
|
|
class StampUpdate(Event):
|
|
|
|
"""Trigger for any adjustment of the stamp files to happen"""
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
def __init__(self, targets, stampfns):
|
2008-03-03 22:01:45 +00:00
|
|
|
self._targets = targets
|
|
|
|
self._stampfns = stampfns
|
2010-01-20 18:46:02 +00:00
|
|
|
Event.__init__(self)
|
2008-03-03 22:01:45 +00:00
|
|
|
|
|
|
|
def getStampPrefix(self):
|
|
|
|
return self._stampfns
|
|
|
|
|
|
|
|
def getTargets(self):
|
|
|
|
return self._targets
|
|
|
|
|
|
|
|
stampPrefix = property(getStampPrefix)
|
|
|
|
targets = property(getTargets)
|
|
|
|
|
2005-08-31 10:47:56 +00:00
|
|
|
class BuildBase(Event):
|
|
|
|
"""Base class for bbmake run events"""
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
def __init__(self, n, p, failures = 0):
|
2005-08-31 10:47:56 +00:00
|
|
|
self._name = n
|
|
|
|
self._pkgs = p
|
2010-01-20 18:46:02 +00:00
|
|
|
Event.__init__(self)
|
2006-03-20 17:45:11 +00:00
|
|
|
self._failures = failures
|
2005-08-31 10:47:56 +00:00
|
|
|
|
|
|
|
def getPkgs(self):
|
|
|
|
return self._pkgs
|
|
|
|
|
|
|
|
def setPkgs(self, pkgs):
|
|
|
|
self._pkgs = pkgs
|
|
|
|
|
|
|
|
def getName(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
def setName(self, name):
|
|
|
|
self._name = name
|
|
|
|
|
|
|
|
def getCfg(self):
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
def setCfg(self, cfg):
|
|
|
|
self.data = cfg
|
|
|
|
|
2006-03-20 17:45:11 +00:00
|
|
|
def getFailures(self):
|
|
|
|
"""
|
|
|
|
Return the number of failed packages
|
|
|
|
"""
|
|
|
|
return self._failures
|
|
|
|
|
2005-08-31 10:47:56 +00:00
|
|
|
pkgs = property(getPkgs, setPkgs, None, "pkgs property")
|
|
|
|
name = property(getName, setName, None, "name property")
|
|
|
|
cfg = property(getCfg, setCfg, None, "cfg property")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BuildStarted(BuildBase):
|
|
|
|
"""bbmake build run started"""
|
|
|
|
|
|
|
|
|
|
|
|
class BuildCompleted(BuildBase):
|
|
|
|
"""bbmake build run completed"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-03-20 17:45:11 +00:00
|
|
|
class NoProvider(Event):
|
|
|
|
"""No Provider for an Event"""
|
|
|
|
|
2010-06-08 20:20:35 +00:00
|
|
|
def __init__(self, item, runtime=False, dependees=None):
|
2010-01-20 18:46:02 +00:00
|
|
|
Event.__init__(self)
|
2006-03-20 17:45:11 +00:00
|
|
|
self._item = item
|
|
|
|
self._runtime = runtime
|
2010-06-08 20:20:35 +00:00
|
|
|
self._dependees = dependees
|
2006-03-20 17:45:11 +00:00
|
|
|
|
|
|
|
def getItem(self):
|
|
|
|
return self._item
|
|
|
|
|
|
|
|
def isRuntime(self):
|
|
|
|
return self._runtime
|
2005-08-31 10:47:56 +00:00
|
|
|
|
2006-03-20 17:45:11 +00:00
|
|
|
class MultipleProviders(Event):
|
2005-08-31 10:47:56 +00:00
|
|
|
"""Multiple Providers"""
|
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
def __init__(self, item, candidates, runtime = False):
|
|
|
|
Event.__init__(self)
|
2006-03-20 17:45:11 +00:00
|
|
|
self._item = item
|
|
|
|
self._candidates = candidates
|
|
|
|
self._is_runtime = runtime
|
|
|
|
|
|
|
|
def isRuntime(self):
|
|
|
|
"""
|
|
|
|
Is this a runtime issue?
|
|
|
|
"""
|
|
|
|
return self._is_runtime
|
|
|
|
|
|
|
|
def getItem(self):
|
|
|
|
"""
|
|
|
|
The name for the to be build item
|
|
|
|
"""
|
|
|
|
return self._item
|
|
|
|
|
|
|
|
def getCandidates(self):
|
|
|
|
"""
|
|
|
|
Get the possible Candidates for a PROVIDER.
|
|
|
|
"""
|
|
|
|
return self._candidates
|
2010-01-20 18:46:02 +00:00
|
|
|
|
2010-11-19 05:47:36 +00:00
|
|
|
class ParseStarted(Event):
|
|
|
|
"""Recipe parsing for the runqueue has begun"""
|
2010-11-30 15:25:13 +00:00
|
|
|
def __init__(self, total):
|
2010-11-19 05:47:36 +00:00
|
|
|
Event.__init__(self)
|
|
|
|
self.total = total
|
|
|
|
|
|
|
|
class ParseCompleted(Event):
|
|
|
|
"""Recipe parsing for the runqueue has completed"""
|
2010-01-20 18:46:02 +00:00
|
|
|
|
|
|
|
def __init__(self, cached, parsed, skipped, masked, virtuals, errors, total):
|
|
|
|
Event.__init__(self)
|
|
|
|
self.cached = cached
|
|
|
|
self.parsed = parsed
|
|
|
|
self.skipped = skipped
|
|
|
|
self.virtuals = virtuals
|
|
|
|
self.masked = masked
|
|
|
|
self.errors = errors
|
|
|
|
self.sofar = cached + parsed
|
|
|
|
self.total = total
|
|
|
|
|
2010-11-19 05:47:36 +00:00
|
|
|
class ParseProgress(Event):
|
|
|
|
"""Recipe parsing progress"""
|
|
|
|
|
|
|
|
def __init__(self, current):
|
|
|
|
self.current = current
|
|
|
|
|
2010-11-22 15:13:56 +00:00
|
|
|
class CacheLoadStarted(Event):
|
|
|
|
"""Loading of the dependency cache has begun"""
|
|
|
|
def __init__(self, total):
|
|
|
|
Event.__init__(self)
|
|
|
|
self.total = total
|
|
|
|
|
|
|
|
class CacheLoadProgress(Event):
|
|
|
|
"""Cache loading progress"""
|
|
|
|
def __init__(self, current):
|
|
|
|
Event.__init__(self)
|
|
|
|
self.current = current
|
|
|
|
|
|
|
|
class CacheLoadCompleted(Event):
|
|
|
|
"""Cache loading is complete"""
|
|
|
|
def __init__(self, total, num_entries):
|
|
|
|
Event.__init__(self)
|
|
|
|
self.total = total
|
|
|
|
self.num_entries = num_entries
|
|
|
|
|
2011-01-01 23:55:54 +00:00
|
|
|
|
2010-01-20 18:46:02 +00:00
|
|
|
class DepTreeGenerated(Event):
|
|
|
|
"""
|
|
|
|
Event when a dependency tree has been generated
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, depgraph):
|
|
|
|
Event.__init__(self)
|
|
|
|
self._depgraph = depgraph
|
2010-06-09 23:17:29 +00:00
|
|
|
|
|
|
|
class MsgBase(Event):
|
|
|
|
"""Base class for messages"""
|
|
|
|
|
|
|
|
def __init__(self, msg):
|
|
|
|
self._message = msg
|
|
|
|
Event.__init__(self)
|
|
|
|
|
|
|
|
class MsgDebug(MsgBase):
|
|
|
|
"""Debug Message"""
|
|
|
|
|
|
|
|
class MsgNote(MsgBase):
|
|
|
|
"""Note Message"""
|
|
|
|
|
|
|
|
class MsgWarn(MsgBase):
|
|
|
|
"""Warning Message"""
|
|
|
|
|
|
|
|
class MsgError(MsgBase):
|
|
|
|
"""Error Message"""
|
|
|
|
|
|
|
|
class MsgFatal(MsgBase):
|
|
|
|
"""Fatal Message"""
|
|
|
|
|
|
|
|
class MsgPlain(MsgBase):
|
|
|
|
"""General output"""
|
|
|
|
|
|
|
|
class LogHandler(logging.Handler):
|
|
|
|
"""Dispatch logging messages as bitbake events"""
|
|
|
|
|
|
|
|
def emit(self, record):
|
2010-06-10 15:05:52 +00:00
|
|
|
fire(record, None)
|
|
|
|
if bb.event.useStdout:
|
|
|
|
print(self.format(record))
|