1
0
mirror of https://git.yoctoproject.org/poky synced 2026-05-09 05:29:32 +00:00

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>
This commit is contained in:
Ed Bartosh
2016-06-20 14:00:51 +03:00
committed by Richard Purdie
parent a158388a51
commit f6fc25d1df
+40 -44
View File
@@ -33,7 +33,7 @@ import threading
from io import StringIO from io import StringIO
from contextlib import closing from contextlib import closing
from functools import wraps from functools import wraps
from collections import defaultdict from collections import defaultdict, namedtuple
import bb, bb.exceptions, bb.command import bb, bb.exceptions, bb.command
from bb import utils, data, parse, event, cache, providers, taskdata, runqueue, build from bb import utils, data, parse, event, cache, providers, taskdata, runqueue, build
import queue import queue
@@ -117,58 +117,52 @@ class CookerFeatures(object):
return next(self._features) return next(self._features)
class EventLogWriteHandler: class EventWriter:
def __init__(self, cooker, eventfile): def __init__(self, cooker, eventfile):
# set our handler's event processor self.file_inited = None
self.event = self.EventWriter(cooker, eventfile) self.cooker = cooker
self.eventfile = eventfile
self.event_queue = []
class EventWriter: def init_file(self):
def __init__(self, cooker, eventfile): # write current configuration data
self.file_inited = None with open(eventfile, "w") as f:
self.cooker = cooker f.write("%s\n" % json.dumps({ "allvariables" : self.cooker.getAllKeysWithFlags(["doc", "func"])}))
self.eventfile = eventfile
self.event_queue = []
def init_file(self): def write_event(self, event):
# write current configuration data with open(self.eventfile, "a") as f:
with open(eventfile, "w") as f: try:
f.write("%s\n" % json.dumps({ "allvariables" : self.cooker.getAllKeysWithFlags(["doc", "func"])})) str_event = codecs.encode(pickle.dumps(event), 'base64').decode('utf-8')
f.write("%s\n" % json.dumps({"class": event.__module__ + "." + event.__class__.__name__,
def write_event(self, event): "vars": str_event}))
with open(self.eventfile, "a") as f: except Exception as e:
try: import traceback
str_event = codecs.encode(pickle.dumps(event), 'base64').decode('utf-8') print(e, traceback.format_exc())
f.write("%s\n" % json.dumps({"class": event.__module__ + "." + event.__class__.__name__,
"vars": str_event}))
except Exception as e:
import traceback
print(e, traceback.format_exc())
def send(self, event): def send(self, event):
event_class = event.__module__ + "." + event.__class__.__name__ event_class = event.__module__ + "." + event.__class__.__name__
# init on bb.event.BuildStarted # init on bb.event.BuildStarted
if self.file_inited is None: if self.file_inited is None:
if event_class == "bb.event.BuildStarted": if event_class == "bb.event.BuildStarted":
self.init_file() self.init_file()
self.file_inited = True self.file_inited = True
# write pending events # write pending events
for e in self.event_queue: for e in self.event_queue:
self.write_event(e) self.write_event(e)
# also write the current event # also write the current event
self.write_event(event) self.write_event(event)
else:
# queue all events until the file is inited
self.event_queue.append(event)
else: else:
# we have the file, just write the event # queue all events until the file is inited
self.write_event(event) self.event_queue.append(event)
else:
# we have the file, just write the event
self.write_event(event)
#============================================================================# #============================================================================#
# BBCooker # BBCooker
@@ -210,7 +204,9 @@ class BBCooker:
# we log all events to a file if so directed # we log all events to a file if so directed
if self.configuration.writeeventlog: if self.configuration.writeeventlog:
# register the log file writer as UI Handler # register the log file writer as UI Handler
bb.event.register_UIHhandler(EventLogWriteHandler(self, self.configuration.writeeventlog)) writer = EventWriter(self, self.configuration.writeeventlog)
EventLogWriteHandler = namedtuple('EventLogWriteHandler', ['event'])
bb.event.register_UIHhandler(EventLogWriteHandler(writer))
self.inotify_modified_files = [] self.inotify_modified_files = []