bitbake/lib/bb/data_smart.py,event.py:
[vuplus_bitbake] / lib / bb / event.py
1 #!/usr/bin/env python
2 # ex:ts=4:sw=4:sts=4:et
3 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4 """
5 BitBake 'Event' implementation
6
7 Classes and functions for manipulating 'events' in the
8 BitBake build tools.
9
10 Copyright (C) 2003, 2004  Chris Larson
11
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free Software
14 Foundation; either version 2 of the License, or (at your option) any later
15 version.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 USA. 
24 """
25
26 import os, re
27 import bb.data
28 import bb.utils
29
30 class Event:
31     """Base class for events"""
32     type = "Event"
33
34     def __init__(self, d = bb.data.init()):
35         self._data = d
36
37     def getData(self):
38         return self._data
39
40     def setData(self, data):
41         self._data = data
42
43     data = property(getData, setData, None, "data property")
44
45 NotHandled = 0
46 Handled = 1
47 handlers = []
48
49 def tmpHandler(event):
50     """Default handler for code events"""
51     return NotHandled
52
53 def defaultTmpHandler():
54     tmp = "def tmpHandler(e):\n\t\"\"\"heh\"\"\"\n\treturn NotHandled"
55     comp = bb.utils.better_compile(tmp, "tmpHandler(e)", "bb.event.defaultTmpHandler")
56     return comp
57
58 def fire(event):
59     """Fire off an Event"""
60     for h in handlers:
61         if type(h).__name__ == "code":
62             exec(h)
63             if tmpHandler(event) == Handled:
64                 return Handled
65         else:
66             if h(event) == Handled:
67                 return Handled
68     return NotHandled
69
70 def register(handler):
71     """Register an Event handler"""
72     if handler is not None:
73 #       handle string containing python code
74         if type(handler).__name__ == "str":
75             return _registerCode(handler)
76 #       prevent duplicate registration
77         if not handler in handlers:
78             handlers.append(handler)
79
80 def _registerCode(handlerStr):
81     """Register a 'code' Event.
82        Deprecated interface; call register instead.
83
84        Expects to be passed python code as a string, which will
85        be passed in turn to compile() and then exec().  Note that
86        the code will be within a function, so should have had
87        appropriate tabbing put in place."""
88     tmp = "def tmpHandler(e):\n%s" % handlerStr
89     comp = bb.utils.better_compile(tmp, "tmpHandler(e)", "bb.event._registerCode")
90 #   prevent duplicate registration
91     if not comp in handlers:
92         handlers.append(comp)
93
94 def remove(handler):
95     """Remove an Event handler"""
96     for h in handlers:
97         if type(handler).__name__ == "str":
98             return _removeCode(handler)
99
100         if handler is h:
101             handlers.remove(handler)
102
103 def _removeCode(handlerStr):
104     """Remove a 'code' Event handler
105        Deprecated interface; call remove instead."""
106     tmp = "def tmpHandler(e):\n%s" % handlerStr
107     comp = bb.utils.better_compile(tmp, "tmpHandler(e)", "bb.event._removeCode")
108     handlers.remove(comp)
109
110 def getName(e):
111     """Returns the name of a class or class instance"""
112     if getattr(e, "__name__", None) == None:
113         return e.__class__.__name__
114     else:
115         return e.__name__
116
117
118 class PkgBase(Event):
119     """Base class for package events"""
120
121     def __init__(self, t, d = bb.data.init()):
122         self._pkg = t
123         Event.__init__(self, d)
124
125     def getPkg(self):
126         return self._pkg
127
128     def setPkg(self, pkg):
129         self._pkg = pkg
130
131     pkg = property(getPkg, setPkg, None, "pkg property")
132
133
134 class BuildBase(Event):
135     """Base class for bbmake run events"""
136
137     def __init__(self, n, p, c, failures = 0):
138         self._name = n
139         self._pkgs = p
140         Event.__init__(self, c)
141         self._failures = failures
142
143     def getPkgs(self):
144         return self._pkgs
145
146     def setPkgs(self, pkgs):
147         self._pkgs = pkgs
148
149     def getName(self):
150         return self._name
151
152     def setName(self, name):
153         self._name = name
154
155     def getCfg(self):
156         return self.data
157
158     def setCfg(self, cfg):
159         self.data = cfg
160
161     def getFailures(self):
162         """
163         Return the number of failed packages
164         """
165         return self._failures
166
167     pkgs = property(getPkgs, setPkgs, None, "pkgs property")
168     name = property(getName, setName, None, "name property")
169     cfg = property(getCfg, setCfg, None, "cfg property")
170
171
172 class DepBase(PkgBase):
173     """Base class for dependency events"""
174
175     def __init__(self, t, data, d):
176         self._dep = d
177         PkgBase.__init__(self, t, data)
178
179     def getDep(self):
180         return self._dep
181
182     def setDep(self, dep):
183         self._dep = dep
184
185     dep = property(getDep, setDep, None, "dep property")
186
187
188 class PkgStarted(PkgBase):
189     """Package build started"""
190
191
192 class PkgFailed(PkgBase):
193     """Package build failed"""
194
195
196 class PkgSucceeded(PkgBase):
197     """Package build completed"""
198
199
200 class BuildStarted(BuildBase):
201     """bbmake build run started"""
202
203
204 class BuildCompleted(BuildBase):
205     """bbmake build run completed"""
206
207
208 class UnsatisfiedDep(DepBase):
209     """Unsatisfied Dependency"""
210
211
212 class RecursiveDep(DepBase):
213     """Recursive Dependency"""
214
215
216 class MultipleProviders(Event):
217     """Multiple Providers"""
218
219     def  __init__(self, item, candidates, data, runtime = False):
220         Event.__init__(self, data)
221         self._item = item
222         self._candidates = candidates
223         self._is_runtime = runtime
224
225     def isRuntime(self):
226         """
227         Is this a runtime issue?
228         """
229         return self._is_runtime
230
231     def getItem(self):
232         """
233         The name for the to be build item
234         """
235         return self._item
236
237     def getCandidates(self):
238         """
239         Get the possible Candidates for a PROVIDER.
240         """
241         return self._candidates