blob: 80967a093406122ba1ebe2bb383ba135b137989f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#!/usr/bin/env python3
2# ex:ts=4:sw=4:sts=4:et
3# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4#
5# Copyright (C) 2014 Alex Damian
6#
7# This file re-uses code spread throughout other Bitbake source files.
8# As such, all other copyrights belong to their own right holders.
9#
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License version 2 as
13# published by the Free Software Foundation.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License along
21# with this program; if not, write to the Free Software Foundation, Inc.,
22# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24"""
25This command takes a filename as a single parameter. The filename is read
26as a build eventlog, and the ToasterUI is used to process events in the file
27and log data in the database
28"""
29
30import os
31import sys
32import json
33import pickle
34import codecs
35
36from collections import namedtuple
37
38# mangle syspath to allow easy import of modules
39from os.path import join, dirname, abspath
40sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'lib'))
41
42import bb.cooker
43from bb.ui import toasterui
44
45class EventPlayer:
46 """Emulate a connection to a bitbake server."""
47
48 def __init__(self, eventfile, variables):
49 self.eventfile = eventfile
50 self.variables = variables
51 self.eventmask = []
52
53 def waitEvent(self, _timeout):
54 """Read event from the file."""
55 line = self.eventfile.readline().strip()
56 if not line:
57 return
58 try:
59 event_str = json.loads(line)['vars'].encode('utf-8')
60 event = pickle.loads(codecs.decode(event_str, 'base64'))
61 event_name = "%s.%s" % (event.__module__, event.__class__.__name__)
62 if event_name not in self.eventmask:
63 return
64 return event
65 except ValueError as err:
66 print("Failed loading ", line)
67 raise err
68
69 def runCommand(self, command_line):
70 """Emulate running a command on the server."""
71 name = command_line[0]
72
73 if name == "getVariable":
74 var_name = command_line[1]
75 variable = self.variables.get(var_name)
76 if variable:
77 return variable['v'], None
78 return None, "Missing variable %s" % var_name
79
80 elif name == "getAllKeysWithFlags":
81 dump = {}
82 flaglist = command_line[1]
83 for key, val in self.variables.items():
84 try:
85 if not key.startswith("__"):
86 dump[key] = {
87 'v': val['v'],
88 'history' : val['history'],
89 }
90 for flag in flaglist:
91 dump[key][flag] = val[flag]
92 except Exception as err:
93 print(err)
94 return (dump, None)
95
96 elif name == 'setEventMask':
97 self.eventmask = command_line[-1]
98 return True, None
99
100 else:
101 raise Exception("Command %s not implemented" % command_line[0])
102
103 def getEventHandle(self):
104 """
105 This method is called by toasterui.
106 The return value is passed to self.runCommand but not used there.
107 """
108 pass
109
110def main(argv):
111 with open(argv[-1]) as eventfile:
112 # load variables from the first line
113 variables = json.loads(eventfile.readline().strip())['allvariables']
114
115 params = namedtuple('ConfigParams', ['observe_only'])(True)
116 player = EventPlayer(eventfile, variables)
117
118 return toasterui.main(player, player, params)
119
120# run toaster ui on our mock bitbake class
121if __name__ == "__main__":
122 if len(sys.argv) != 2:
123 print("Usage: %s <event file>" % os.path.basename(sys.argv[0]))
124 sys.exit(1)
125
126 sys.exit(main(sys.argv))