Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2014 Alex Damian |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
| 6 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 11 | """ |
| 12 | This command takes a filename as a single parameter. The filename is read |
| 13 | as a build eventlog, and the ToasterUI is used to process events in the file |
| 14 | and log data in the database |
| 15 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | import os |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 18 | import sys |
| 19 | import json |
| 20 | import pickle |
| 21 | import codecs |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 22 | import warnings |
| 23 | warnings.simplefilter("default") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 24 | |
| 25 | from collections import namedtuple |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | |
| 27 | # mangle syspath to allow easy import of modules |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | from os.path import join, dirname, abspath |
| 29 | sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'lib')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | |
| 31 | import bb.cooker |
| 32 | from bb.ui import toasterui |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 34 | class EventPlayer: |
| 35 | """Emulate a connection to a bitbake server.""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 37 | def __init__(self, eventfile, variables): |
| 38 | self.eventfile = eventfile |
| 39 | self.variables = variables |
| 40 | self.eventmask = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 42 | def waitEvent(self, _timeout): |
| 43 | """Read event from the file.""" |
| 44 | line = self.eventfile.readline().strip() |
| 45 | if not line: |
| 46 | return |
| 47 | try: |
| 48 | event_str = json.loads(line)['vars'].encode('utf-8') |
| 49 | event = pickle.loads(codecs.decode(event_str, 'base64')) |
| 50 | event_name = "%s.%s" % (event.__module__, event.__class__.__name__) |
| 51 | if event_name not in self.eventmask: |
| 52 | return |
| 53 | return event |
| 54 | except ValueError as err: |
| 55 | print("Failed loading ", line) |
| 56 | raise err |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 58 | def runCommand(self, command_line): |
| 59 | """Emulate running a command on the server.""" |
| 60 | name = command_line[0] |
| 61 | |
| 62 | if name == "getVariable": |
| 63 | var_name = command_line[1] |
| 64 | variable = self.variables.get(var_name) |
| 65 | if variable: |
| 66 | return variable['v'], None |
| 67 | return None, "Missing variable %s" % var_name |
| 68 | |
| 69 | elif name == "getAllKeysWithFlags": |
| 70 | dump = {} |
| 71 | flaglist = command_line[1] |
| 72 | for key, val in self.variables.items(): |
| 73 | try: |
| 74 | if not key.startswith("__"): |
| 75 | dump[key] = { |
| 76 | 'v': val['v'], |
| 77 | 'history' : val['history'], |
| 78 | } |
| 79 | for flag in flaglist: |
| 80 | dump[key][flag] = val[flag] |
| 81 | except Exception as err: |
| 82 | print(err) |
| 83 | return (dump, None) |
| 84 | |
| 85 | elif name == 'setEventMask': |
| 86 | self.eventmask = command_line[-1] |
| 87 | return True, None |
| 88 | |
| 89 | else: |
| 90 | raise Exception("Command %s not implemented" % command_line[0]) |
| 91 | |
| 92 | def getEventHandle(self): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 94 | This method is called by toasterui. |
| 95 | The return value is passed to self.runCommand but not used there. |
| 96 | """ |
| 97 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 99 | def main(argv): |
| 100 | with open(argv[-1]) as eventfile: |
| 101 | # load variables from the first line |
| 102 | variables = json.loads(eventfile.readline().strip())['allvariables'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 104 | params = namedtuple('ConfigParams', ['observe_only'])(True) |
| 105 | player = EventPlayer(eventfile, variables) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 107 | return toasterui.main(player, player, params) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | |
| 109 | # run toaster ui on our mock bitbake class |
| 110 | if __name__ == "__main__": |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | if len(sys.argv) != 2: |
| 112 | print("Usage: %s <event file>" % os.path.basename(sys.argv[0])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | sys.exit(1) |
| 114 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 115 | sys.exit(main(sys.argv)) |