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