diff options
author | Ed Bartosh <ed.bartosh@linux.intel.com> | 2016-06-20 14:00:51 +0300 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2016-06-20 17:23:57 +0100 |
commit | f6fc25d1df2b77cb1d6534b302812c3af97dfd28 (patch) | |
tree | ee93854c3b6fcf6c3e6763123875a0b5cb98dcc8 /bitbake/lib/bb/cooker.py | |
parent | a158388a51147320bd599299081f1ecfe1152a6a (diff) | |
download | poky-f6fc25d1df2b77cb1d6534b302812c3af97dfd28.tar.gz |
bitbake: cooker: replace EventLogWriteHandler with namedtuple
class EventLogWriteHandler is a simple wrapper class with only one
class member. Replacing it with namedtuple makes code less nested and more
readable.
(Bitbake rev: 7c5b6812d32d173df36e7f9fc1d877329e79f994)
Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/cooker.py')
-rw-r--r-- | bitbake/lib/bb/cooker.py | 86 |
1 files changed, 41 insertions, 45 deletions
diff --git a/bitbake/lib/bb/cooker.py b/bitbake/lib/bb/cooker.py index ca53ff4eaa..3e4f8d8e5e 100644 --- a/bitbake/lib/bb/cooker.py +++ b/bitbake/lib/bb/cooker.py | |||
@@ -33,7 +33,7 @@ import threading | |||
33 | from io import StringIO | 33 | from io import StringIO |
34 | from contextlib import closing | 34 | from contextlib import closing |
35 | from functools import wraps | 35 | from functools import wraps |
36 | from collections import defaultdict | 36 | from collections import defaultdict, namedtuple |
37 | import bb, bb.exceptions, bb.command | 37 | import bb, bb.exceptions, bb.command |
38 | from bb import utils, data, parse, event, cache, providers, taskdata, runqueue, build | 38 | from bb import utils, data, parse, event, cache, providers, taskdata, runqueue, build |
39 | import queue | 39 | import queue |
@@ -117,58 +117,52 @@ class CookerFeatures(object): | |||
117 | return next(self._features) | 117 | return next(self._features) |
118 | 118 | ||
119 | 119 | ||
120 | class EventLogWriteHandler: | 120 | class EventWriter: |
121 | |||
122 | def __init__(self, cooker, eventfile): | 121 | def __init__(self, cooker, eventfile): |
123 | # set our handler's event processor | 122 | self.file_inited = None |
124 | self.event = self.EventWriter(cooker, eventfile) | 123 | self.cooker = cooker |
125 | 124 | self.eventfile = eventfile | |
126 | class EventWriter: | 125 | self.event_queue = [] |
127 | def __init__(self, cooker, eventfile): | ||
128 | self.file_inited = None | ||
129 | self.cooker = cooker | ||
130 | self.eventfile = eventfile | ||
131 | self.event_queue = [] | ||
132 | |||
133 | def init_file(self): | ||
134 | # write current configuration data | ||
135 | with open(eventfile, "w") as f: | ||
136 | f.write("%s\n" % json.dumps({ "allvariables" : self.cooker.getAllKeysWithFlags(["doc", "func"])})) | ||
137 | |||
138 | def write_event(self, event): | ||
139 | with open(self.eventfile, "a") as f: | ||
140 | try: | ||
141 | str_event = codecs.encode(pickle.dumps(event), 'base64').decode('utf-8') | ||
142 | f.write("%s\n" % json.dumps({"class": event.__module__ + "." + event.__class__.__name__, | ||
143 | "vars": str_event})) | ||
144 | except Exception as e: | ||
145 | import traceback | ||
146 | print(e, traceback.format_exc()) | ||
147 | 126 | ||
127 | def init_file(self): | ||
128 | # write current configuration data | ||
129 | with open(eventfile, "w") as f: | ||
130 | f.write("%s\n" % json.dumps({ "allvariables" : self.cooker.getAllKeysWithFlags(["doc", "func"])})) | ||
131 | |||
132 | def write_event(self, event): | ||
133 | with open(self.eventfile, "a") as f: | ||
134 | try: | ||
135 | str_event = codecs.encode(pickle.dumps(event), 'base64').decode('utf-8') | ||
136 | f.write("%s\n" % json.dumps({"class": event.__module__ + "." + event.__class__.__name__, | ||
137 | "vars": str_event})) | ||
138 | except Exception as e: | ||
139 | import traceback | ||
140 | print(e, traceback.format_exc()) | ||
148 | 141 | ||
149 | def send(self, event): | ||
150 | event_class = event.__module__ + "." + event.__class__.__name__ | ||
151 | 142 | ||
152 | # init on bb.event.BuildStarted | 143 | def send(self, event): |
153 | if self.file_inited is None: | 144 | event_class = event.__module__ + "." + event.__class__.__name__ |
154 | if event_class == "bb.event.BuildStarted": | ||
155 | self.init_file() | ||
156 | self.file_inited = True | ||
157 | 145 | ||
158 | # write pending events | 146 | # init on bb.event.BuildStarted |
159 | for e in self.event_queue: | 147 | if self.file_inited is None: |
160 | self.write_event(e) | 148 | if event_class == "bb.event.BuildStarted": |
149 | self.init_file() | ||
150 | self.file_inited = True | ||
161 | 151 | ||
162 | # also write the current event | 152 | # write pending events |
163 | self.write_event(event) | 153 | for e in self.event_queue: |
154 | self.write_event(e) | ||
164 | 155 | ||
165 | else: | 156 | # also write the current event |
166 | # queue all events until the file is inited | 157 | self.write_event(event) |
167 | self.event_queue.append(event) | ||
168 | 158 | ||
169 | else: | 159 | else: |
170 | # we have the file, just write the event | 160 | # queue all events until the file is inited |
171 | self.write_event(event) | 161 | self.event_queue.append(event) |
162 | |||
163 | else: | ||
164 | # we have the file, just write the event | ||
165 | self.write_event(event) | ||
172 | 166 | ||
173 | #============================================================================# | 167 | #============================================================================# |
174 | # BBCooker | 168 | # BBCooker |
@@ -210,7 +204,9 @@ class BBCooker: | |||
210 | # we log all events to a file if so directed | 204 | # we log all events to a file if so directed |
211 | if self.configuration.writeeventlog: | 205 | if self.configuration.writeeventlog: |
212 | # register the log file writer as UI Handler | 206 | # register the log file writer as UI Handler |
213 | bb.event.register_UIHhandler(EventLogWriteHandler(self, self.configuration.writeeventlog)) | 207 | writer = EventWriter(self, self.configuration.writeeventlog) |
208 | EventLogWriteHandler = namedtuple('EventLogWriteHandler', ['event']) | ||
209 | bb.event.register_UIHhandler(EventLogWriteHandler(writer)) | ||
214 | 210 | ||
215 | self.inotify_modified_files = [] | 211 | self.inotify_modified_files = [] |
216 | 212 | ||