blob: 80967a093406122ba1ebe2bb383ba135b137989f [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002# 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
Patrick Williamsc0f7c042017-02-23 20:41:17 -060024"""
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"""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030import os
Patrick Williamsc0f7c042017-02-23 20:41:17 -060031import sys
32import json
33import pickle
34import codecs
35
36from collections import namedtuple
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
38# mangle syspath to allow easy import of modules
Patrick Williamsc0f7c042017-02-23 20:41:17 -060039from os.path import join, dirname, abspath
40sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'lib'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041
42import bb.cooker
43from bb.ui import toasterui
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044
Patrick Williamsc0f7c042017-02-23 20:41:17 -060045class EventPlayer:
46 """Emulate a connection to a bitbake server."""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047
Patrick Williamsc0f7c042017-02-23 20:41:17 -060048 def __init__(self, eventfile, variables):
49 self.eventfile = eventfile
50 self.variables = variables
51 self.eventmask = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068
Patrick Williamsc0f7c042017-02-23 20:41:17 -060069 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):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 """
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600105 This method is called by toasterui.
106 The return value is passed to self.runCommand but not used there.
107 """
108 pass
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600110def main(argv):
111 with open(argv[-1]) as eventfile:
112 # load variables from the first line
113 variables = json.loads(eventfile.readline().strip())['allvariables']
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600115 params = namedtuple('ConfigParams', ['observe_only'])(True)
116 player = EventPlayer(eventfile, variables)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600118 return toasterui.main(player, player, params)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119
120# run toaster ui on our mock bitbake class
121if __name__ == "__main__":
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600122 if len(sys.argv) != 2:
123 print("Usage: %s <event file>" % os.path.basename(sys.argv[0]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124 sys.exit(1)
125
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600126 sys.exit(main(sys.argv))