bitbake/lib/bb/utils.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
29 class Event:
30     """Base class for events"""
31     type = "Event"
32
33     def __init__(self, d = bb.data.init()):
34         self._data = d
35
36     def getData(self):
37         return self._data
38
39     def setData(self, data):
40         self._data = data
41
42     data = property(getData, setData, None, "data property")
43
44 NotHandled = 0
45 Handled = 1
46 handlers = []
47
48 def tmpHandler(event):
49     """Default handler for code events"""
50     return NotHandled
51
52 def defaultTmpHandler():
53     tmp = "def tmpHandler(e):\n\t\"\"\"heh\"\"\"\n\treturn 0"
54     comp = compile(tmp, "tmpHandler(e)", "exec")
55     return comp
56
57 def fire(event):
58     """Fire off an Event"""
59     for h in handlers:
60         if type(h).__name__ == "code":
61             exec(h)
62             if tmpHandler(event) == Handled:
63                 return Handled
64         else:
65             if h(event) == Handled:
66                 return Handled
67     return NotHandled
68
69 def register(handler):
70     """Register an Event handler"""
71     if handler is not None:
72 #       handle string containing python code
73         if type(handler).__name__ == "str":
74             return registerCode(handler)
75 #       prevent duplicate registration
76         if not handler in handlers:
77             handlers.append(handler)
78
79 def registerCode(handlerStr):
80     """Register a 'code' Event.
81        Deprecated interface; call register instead.
82
83        Expects to be passed python code as a string, which will
84        be passed in turn to compile() and then exec().  Note that
85        the code will be within a function, so should have had
86        appropriate tabbing put in place."""
87     tmp = "def tmpHandler(e):\n%s" % handlerStr
88     comp = compile(tmp, "tmpHandler(e)", "exec")
89 #   prevent duplicate registration
90     if not comp in handlers:
91         handlers.append(comp)
92
93 def remove(handler):
94     """Remove an Event handler"""
95     for h in handlers:
96         if type(handler).__name__ == "str":
97             return removeCode(handler)
98
99         if handler is h:
100             handlers.remove(handler)
101
102 def removeCode(handlerStr):
103     """Remove a 'code' Event handler
104        Deprecated interface; call remove instead."""
105     tmp = "def tmpHandler(e):\n%s" % handlerStr
106     comp = compile(tmp, "tmpHandler(e)", "exec")
107     handlers.remove(comp)
108
109 def getName(e):
110     """Returns the name of a class or class instance"""
111     if getattr(e, "__name__", None) == None:
112         return e.__class__.__name__
113     else:
114         return e.__name__
115
116
117 class PkgBase(Event):
118     """Base class for package events"""
119
120     def __init__(self, t, d = {}):
121         self._pkg = t
122         Event.__init__(self, d)
123
124     def getPkg(self):
125         return self._pkg
126
127     def setPkg(self, pkg):
128         self._pkg = pkg
129
130     pkg = property(getPkg, setPkg, None, "pkg property")
131
132
133 class BuildBase(Event):
134     """Base class for bbmake run events"""
135
136     def __init__(self, n, p, c):
137         self._name = n
138         self._pkgs = p
139         Event.__init__(self, c)
140
141     def getPkgs(self):
142         return self._pkgs
143
144     def setPkgs(self, pkgs):
145         self._pkgs = pkgs
146
147     def getName(self):
148         return self._name
149
150     def setName(self, name):
151         self._name = name
152
153     def getCfg(self):
154         return self.data
155
156     def setCfg(self, cfg):
157         self.data = cfg
158
159     pkgs = property(getPkgs, setPkgs, None, "pkgs property")
160     name = property(getName, setName, None, "name property")
161     cfg = property(getCfg, setCfg, None, "cfg property")
162
163
164 class DepBase(PkgBase):
165     """Base class for dependency events"""
166
167     def __init__(self, t, data, d):
168         self._dep = d
169         PkgBase.__init__(self, t, data)
170
171     def getDep(self):
172         return self._dep
173
174     def setDep(self, dep):
175         self._dep = dep
176
177     dep = property(getDep, setDep, None, "dep property")
178
179
180 class PkgStarted(PkgBase):
181     """Package build started"""
182
183
184 class PkgFailed(PkgBase):
185     """Package build failed"""
186
187
188 class PkgSucceeded(PkgBase):
189     """Package build completed"""
190
191
192 class BuildStarted(BuildBase):
193     """bbmake build run started"""
194
195
196 class BuildCompleted(BuildBase):
197     """bbmake build run completed"""
198
199
200 class UnsatisfiedDep(DepBase):
201     """Unsatisfied Dependency"""
202
203
204 class RecursiveDep(DepBase):
205     """Recursive Dependency"""
206
207
208 class MultipleProviders(PkgBase):
209     """Multiple Providers"""
210