blob: 366bc418845088468cdf55a229a49d8858a437a6 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3"""
4BitBake 'Event' implementation
5
6Classes and functions for manipulating 'events' in the
7BitBake build tools.
8"""
9
10# Copyright (C) 2003, 2004 Chris Larson
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License version 2 as
14# published by the Free Software Foundation.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License along
22# with this program; if not, write to the Free Software Foundation, Inc.,
23# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25import os, sys
26import warnings
27try:
28 import cPickle as pickle
29except ImportError:
30 import pickle
31import logging
32import atexit
33import traceback
34import bb.utils
35import bb.compat
36import bb.exceptions
37
38# This is the pid for which we should generate the event. This is set when
39# the runqueue forks off.
40worker_pid = 0
41worker_fire = None
42
43logger = logging.getLogger('BitBake.Event')
44
45class Event(object):
46 """Base class for events"""
47
48 def __init__(self):
49 self.pid = worker_pid
50
51Registered = 10
52AlreadyRegistered = 14
53
54def get_class_handlers():
55 return _handlers
56
57def set_class_handlers(h):
58 global _handlers
59 _handlers = h
60
61def clean_class_handlers():
62 return bb.compat.OrderedDict()
63
64# Internal
65_handlers = clean_class_handlers()
66_ui_handlers = {}
67_ui_logfilters = {}
68_ui_handler_seq = 0
69_event_handler_map = {}
70_catchall_handlers = {}
71_eventfilter = None
72_uiready = False
73
74def execute_handler(name, handler, event, d):
75 event.data = d
76 addedd = False
77 if 'd' not in __builtins__:
78 __builtins__['d'] = d
79 addedd = True
80 try:
81 ret = handler(event)
82 except (bb.parse.SkipRecipe, bb.BBHandledException):
83 raise
84 except Exception:
85 etype, value, tb = sys.exc_info()
86 logger.error("Execution of event handler '%s' failed" % name,
87 exc_info=(etype, value, tb.tb_next))
88 raise
89 except SystemExit as exc:
90 if exc.code != 0:
91 logger.error("Execution of event handler '%s' failed" % name)
92 raise
93 finally:
94 del event.data
95 if addedd:
96 del __builtins__['d']
97
98def fire_class_handlers(event, d):
99 if isinstance(event, logging.LogRecord):
100 return
101
102 eid = str(event.__class__)[8:-2]
103 evt_hmap = _event_handler_map.get(eid, {})
104 for name, handler in _handlers.iteritems():
105 if name in _catchall_handlers or name in evt_hmap:
106 if _eventfilter:
107 if not _eventfilter(name, handler, event, d):
108 continue
109 execute_handler(name, handler, event, d)
110
111ui_queue = []
112@atexit.register
113def print_ui_queue():
114 """If we're exiting before a UI has been spawned, display any queued
115 LogRecords to the console."""
116 logger = logging.getLogger("BitBake")
117 if not _uiready:
118 from bb.msg import BBLogFormatter
119 console = logging.StreamHandler(sys.stdout)
120 console.setFormatter(BBLogFormatter("%(levelname)s: %(message)s"))
121 logger.handlers = [console]
122
123 # First check to see if we have any proper messages
124 msgprint = False
125 for event in ui_queue:
126 if isinstance(event, logging.LogRecord):
127 if event.levelno > logging.DEBUG:
128 logger.handle(event)
129 msgprint = True
130 if msgprint:
131 return
132
133 # Nope, so just print all of the messages we have (including debug messages)
134 for event in ui_queue:
135 if isinstance(event, logging.LogRecord):
136 logger.handle(event)
137
138def fire_ui_handlers(event, d):
139 if not _uiready:
140 # No UI handlers registered yet, queue up the messages
141 ui_queue.append(event)
142 return
143
144 errors = []
145 for h in _ui_handlers:
146 #print "Sending event %s" % event
147 try:
148 if not _ui_logfilters[h].filter(event):
149 continue
150 # We use pickle here since it better handles object instances
151 # which xmlrpc's marshaller does not. Events *must* be serializable
152 # by pickle.
153 if hasattr(_ui_handlers[h].event, "sendpickle"):
154 _ui_handlers[h].event.sendpickle((pickle.dumps(event)))
155 else:
156 _ui_handlers[h].event.send(event)
157 except:
158 errors.append(h)
159 for h in errors:
160 del _ui_handlers[h]
161
162def fire(event, d):
163 """Fire off an Event"""
164
165 # We can fire class handlers in the worker process context and this is
166 # desired so they get the task based datastore.
167 # UI handlers need to be fired in the server context so we defer this. They
168 # don't have a datastore so the datastore context isn't a problem.
169
170 fire_class_handlers(event, d)
171 if worker_fire:
172 worker_fire(event, d)
173 else:
174 fire_ui_handlers(event, d)
175
176def fire_from_worker(event, d):
177 fire_ui_handlers(event, d)
178
179noop = lambda _: None
180def register(name, handler, mask=None):
181 """Register an Event handler"""
182
183 # already registered
184 if name in _handlers:
185 return AlreadyRegistered
186
187 if handler is not None:
188 # handle string containing python code
189 if isinstance(handler, basestring):
190 tmp = "def %s(e):\n%s" % (name, handler)
191 try:
192 code = compile(tmp, "%s(e)" % name, "exec")
193 except SyntaxError:
194 logger.error("Unable to register event handler '%s':\n%s", name,
195 ''.join(traceback.format_exc(limit=0)))
196 _handlers[name] = noop
197 return
198 env = {}
199 bb.utils.better_exec(code, env)
200 func = bb.utils.better_eval(name, env)
201 _handlers[name] = func
202 else:
203 _handlers[name] = handler
204
205 if not mask or '*' in mask:
206 _catchall_handlers[name] = True
207 else:
208 for m in mask:
209 if _event_handler_map.get(m, None) is None:
210 _event_handler_map[m] = {}
211 _event_handler_map[m][name] = True
212
213 return Registered
214
215def remove(name, handler):
216 """Remove an Event handler"""
217 _handlers.pop(name)
218
219def set_eventfilter(func):
220 global _eventfilter
221 _eventfilter = func
222
223def register_UIHhandler(handler, mainui=False):
224 if mainui:
225 global _uiready
226 _uiready = True
227 bb.event._ui_handler_seq = bb.event._ui_handler_seq + 1
228 _ui_handlers[_ui_handler_seq] = handler
229 level, debug_domains = bb.msg.constructLogOptions()
230 _ui_logfilters[_ui_handler_seq] = UIEventFilter(level, debug_domains)
231 return _ui_handler_seq
232
233def unregister_UIHhandler(handlerNum):
234 if handlerNum in _ui_handlers:
235 del _ui_handlers[handlerNum]
236 return
237
238# Class to allow filtering of events and specific filtering of LogRecords *before* we put them over the IPC
239class UIEventFilter(object):
240 def __init__(self, level, debug_domains):
241 self.update(None, level, debug_domains)
242
243 def update(self, eventmask, level, debug_domains):
244 self.eventmask = eventmask
245 self.stdlevel = level
246 self.debug_domains = debug_domains
247
248 def filter(self, event):
249 if isinstance(event, logging.LogRecord):
250 if event.levelno >= self.stdlevel:
251 return True
252 if event.name in self.debug_domains and event.levelno >= self.debug_domains[event.name]:
253 return True
254 return False
255 eid = str(event.__class__)[8:-2]
256 if self.eventmask and eid not in self.eventmask:
257 return False
258 return True
259
260def set_UIHmask(handlerNum, level, debug_domains, mask):
261 if not handlerNum in _ui_handlers:
262 return False
263 if '*' in mask:
264 _ui_logfilters[handlerNum].update(None, level, debug_domains)
265 else:
266 _ui_logfilters[handlerNum].update(mask, level, debug_domains)
267 return True
268
269def getName(e):
270 """Returns the name of a class or class instance"""
271 if getattr(e, "__name__", None) == None:
272 return e.__class__.__name__
273 else:
274 return e.__name__
275
276class OperationStarted(Event):
277 """An operation has begun"""
278 def __init__(self, msg = "Operation Started"):
279 Event.__init__(self)
280 self.msg = msg
281
282class OperationCompleted(Event):
283 """An operation has completed"""
284 def __init__(self, total, msg = "Operation Completed"):
285 Event.__init__(self)
286 self.total = total
287 self.msg = msg
288
289class OperationProgress(Event):
290 """An operation is in progress"""
291 def __init__(self, current, total, msg = "Operation in Progress"):
292 Event.__init__(self)
293 self.current = current
294 self.total = total
295 self.msg = msg + ": %s/%s" % (current, total);
296
297class ConfigParsed(Event):
298 """Configuration Parsing Complete"""
299
300class RecipeEvent(Event):
301 def __init__(self, fn):
302 self.fn = fn
303 Event.__init__(self)
304
305class RecipePreFinalise(RecipeEvent):
306 """ Recipe Parsing Complete but not yet finialised"""
307
308class RecipeParsed(RecipeEvent):
309 """ Recipe Parsing Complete """
310
311class StampUpdate(Event):
312 """Trigger for any adjustment of the stamp files to happen"""
313
314 def __init__(self, targets, stampfns):
315 self._targets = targets
316 self._stampfns = stampfns
317 Event.__init__(self)
318
319 def getStampPrefix(self):
320 return self._stampfns
321
322 def getTargets(self):
323 return self._targets
324
325 stampPrefix = property(getStampPrefix)
326 targets = property(getTargets)
327
328class BuildBase(Event):
329 """Base class for bbmake run events"""
330
331 def __init__(self, n, p, failures = 0):
332 self._name = n
333 self._pkgs = p
334 Event.__init__(self)
335 self._failures = failures
336
337 def getPkgs(self):
338 return self._pkgs
339
340 def setPkgs(self, pkgs):
341 self._pkgs = pkgs
342
343 def getName(self):
344 return self._name
345
346 def setName(self, name):
347 self._name = name
348
349 def getCfg(self):
350 return self.data
351
352 def setCfg(self, cfg):
353 self.data = cfg
354
355 def getFailures(self):
356 """
357 Return the number of failed packages
358 """
359 return self._failures
360
361 pkgs = property(getPkgs, setPkgs, None, "pkgs property")
362 name = property(getName, setName, None, "name property")
363 cfg = property(getCfg, setCfg, None, "cfg property")
364
365
366
367
368
369class BuildStarted(BuildBase, OperationStarted):
370 """bbmake build run started"""
371 def __init__(self, n, p, failures = 0):
372 OperationStarted.__init__(self, "Building Started")
373 BuildBase.__init__(self, n, p, failures)
374
375class BuildCompleted(BuildBase, OperationCompleted):
376 """bbmake build run completed"""
377 def __init__(self, total, n, p, failures=0, interrupted=0):
378 if not failures:
379 OperationCompleted.__init__(self, total, "Building Succeeded")
380 else:
381 OperationCompleted.__init__(self, total, "Building Failed")
382 self._interrupted = interrupted
383 BuildBase.__init__(self, n, p, failures)
384
385class DiskFull(Event):
386 """Disk full case build aborted"""
387 def __init__(self, dev, type, freespace, mountpoint):
388 Event.__init__(self)
389 self._dev = dev
390 self._type = type
391 self._free = freespace
392 self._mountpoint = mountpoint
393
394class NoProvider(Event):
395 """No Provider for an Event"""
396
397 def __init__(self, item, runtime=False, dependees=None, reasons=None, close_matches=None):
398 Event.__init__(self)
399 self._item = item
400 self._runtime = runtime
401 self._dependees = dependees
402 self._reasons = reasons
403 self._close_matches = close_matches
404
405 def getItem(self):
406 return self._item
407
408 def isRuntime(self):
409 return self._runtime
410
411class MultipleProviders(Event):
412 """Multiple Providers"""
413
414 def __init__(self, item, candidates, runtime = False):
415 Event.__init__(self)
416 self._item = item
417 self._candidates = candidates
418 self._is_runtime = runtime
419
420 def isRuntime(self):
421 """
422 Is this a runtime issue?
423 """
424 return self._is_runtime
425
426 def getItem(self):
427 """
428 The name for the to be build item
429 """
430 return self._item
431
432 def getCandidates(self):
433 """
434 Get the possible Candidates for a PROVIDER.
435 """
436 return self._candidates
437
438class ParseStarted(OperationStarted):
439 """Recipe parsing for the runqueue has begun"""
440 def __init__(self, total):
441 OperationStarted.__init__(self, "Recipe parsing Started")
442 self.total = total
443
444class ParseCompleted(OperationCompleted):
445 """Recipe parsing for the runqueue has completed"""
446 def __init__(self, cached, parsed, skipped, masked, virtuals, errors, total):
447 OperationCompleted.__init__(self, total, "Recipe parsing Completed")
448 self.cached = cached
449 self.parsed = parsed
450 self.skipped = skipped
451 self.virtuals = virtuals
452 self.masked = masked
453 self.errors = errors
454 self.sofar = cached + parsed
455
456class ParseProgress(OperationProgress):
457 """Recipe parsing progress"""
458 def __init__(self, current, total):
459 OperationProgress.__init__(self, current, total, "Recipe parsing")
460
461
462class CacheLoadStarted(OperationStarted):
463 """Loading of the dependency cache has begun"""
464 def __init__(self, total):
465 OperationStarted.__init__(self, "Loading cache Started")
466 self.total = total
467
468class CacheLoadProgress(OperationProgress):
469 """Cache loading progress"""
470 def __init__(self, current, total):
471 OperationProgress.__init__(self, current, total, "Loading cache")
472
473class CacheLoadCompleted(OperationCompleted):
474 """Cache loading is complete"""
475 def __init__(self, total, num_entries):
476 OperationCompleted.__init__(self, total, "Loading cache Completed")
477 self.num_entries = num_entries
478
479class TreeDataPreparationStarted(OperationStarted):
480 """Tree data preparation started"""
481 def __init__(self):
482 OperationStarted.__init__(self, "Preparing tree data Started")
483
484class TreeDataPreparationProgress(OperationProgress):
485 """Tree data preparation is in progress"""
486 def __init__(self, current, total):
487 OperationProgress.__init__(self, current, total, "Preparing tree data")
488
489class TreeDataPreparationCompleted(OperationCompleted):
490 """Tree data preparation completed"""
491 def __init__(self, total):
492 OperationCompleted.__init__(self, total, "Preparing tree data Completed")
493
494class DepTreeGenerated(Event):
495 """
496 Event when a dependency tree has been generated
497 """
498
499 def __init__(self, depgraph):
500 Event.__init__(self)
501 self._depgraph = depgraph
502
503class TargetsTreeGenerated(Event):
504 """
505 Event when a set of buildable targets has been generated
506 """
507 def __init__(self, model):
508 Event.__init__(self)
509 self._model = model
510
511class ReachableStamps(Event):
512 """
513 An event listing all stamps reachable after parsing
514 which the metadata may use to clean up stale data
515 """
516
517 def __init__(self, stamps):
518 Event.__init__(self)
519 self.stamps = stamps
520
521class FilesMatchingFound(Event):
522 """
523 Event when a list of files matching the supplied pattern has
524 been generated
525 """
526 def __init__(self, pattern, matches):
527 Event.__init__(self)
528 self._pattern = pattern
529 self._matches = matches
530
531class CoreBaseFilesFound(Event):
532 """
533 Event when a list of appropriate config files has been generated
534 """
535 def __init__(self, paths):
536 Event.__init__(self)
537 self._paths = paths
538
539class ConfigFilesFound(Event):
540 """
541 Event when a list of appropriate config files has been generated
542 """
543 def __init__(self, variable, values):
544 Event.__init__(self)
545 self._variable = variable
546 self._values = values
547
548class ConfigFilePathFound(Event):
549 """
550 Event when a path for a config file has been found
551 """
552 def __init__(self, path):
553 Event.__init__(self)
554 self._path = path
555
556class MsgBase(Event):
557 """Base class for messages"""
558
559 def __init__(self, msg):
560 self._message = msg
561 Event.__init__(self)
562
563class MsgDebug(MsgBase):
564 """Debug Message"""
565
566class MsgNote(MsgBase):
567 """Note Message"""
568
569class MsgWarn(MsgBase):
570 """Warning Message"""
571
572class MsgError(MsgBase):
573 """Error Message"""
574
575class MsgFatal(MsgBase):
576 """Fatal Message"""
577
578class MsgPlain(MsgBase):
579 """General output"""
580
581class LogExecTTY(Event):
582 """Send event containing program to spawn on tty of the logger"""
583 def __init__(self, msg, prog, sleep_delay, retries):
584 Event.__init__(self)
585 self.msg = msg
586 self.prog = prog
587 self.sleep_delay = sleep_delay
588 self.retries = retries
589
590class LogHandler(logging.Handler):
591 """Dispatch logging messages as bitbake events"""
592
593 def emit(self, record):
594 if record.exc_info:
595 etype, value, tb = record.exc_info
596 if hasattr(tb, 'tb_next'):
597 tb = list(bb.exceptions.extract_traceback(tb, context=3))
598 record.bb_exc_info = (etype, value, tb)
599 record.exc_info = None
600 fire(record, None)
601
602 def filter(self, record):
603 record.taskpid = worker_pid
604 return True
605
606class RequestPackageInfo(Event):
607 """
608 Event to request package information
609 """
610
611class PackageInfo(Event):
612 """
613 Package information for GUI
614 """
615 def __init__(self, pkginfolist):
616 Event.__init__(self)
617 self._pkginfolist = pkginfolist
618
619class MetadataEvent(Event):
620 """
621 Generic event that target for OE-Core classes
622 to report information during asynchrous execution
623 """
624 def __init__(self, eventtype, eventdata):
625 Event.__init__(self)
626 self.type = eventtype
627 self._localdata = eventdata
628
629class SanityCheck(Event):
630 """
631 Event to run sanity checks, either raise errors or generate events as return status.
632 """
633 def __init__(self, generateevents = True):
634 Event.__init__(self)
635 self.generateevents = generateevents
636
637class SanityCheckPassed(Event):
638 """
639 Event to indicate sanity check has passed
640 """
641
642class SanityCheckFailed(Event):
643 """
644 Event to indicate sanity check has failed
645 """
646 def __init__(self, msg, network_error=False):
647 Event.__init__(self)
648 self._msg = msg
649 self._network_error = network_error
650
651class NetworkTest(Event):
652 """
653 Event to run network connectivity tests, either raise errors or generate events as return status.
654 """
655 def __init__(self, generateevents = True):
656 Event.__init__(self)
657 self.generateevents = generateevents
658
659class NetworkTestPassed(Event):
660 """
661 Event to indicate network test has passed
662 """
663
664class NetworkTestFailed(Event):
665 """
666 Event to indicate network test has failed
667 """
668