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 | # 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 24 | """ |
| 25 | This command takes a filename as a single parameter. The filename is read |
| 26 | as a build eventlog, and the ToasterUI is used to process events in the file |
| 27 | and log data in the database |
| 28 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | import os |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 31 | import sys |
| 32 | import json |
| 33 | import pickle |
| 34 | import codecs |
| 35 | |
| 36 | from collections import namedtuple |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | |
| 38 | # mangle syspath to allow easy import of modules |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 39 | from os.path import join, dirname, abspath |
| 40 | sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'lib')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | |
| 42 | import bb.cooker |
| 43 | from bb.ui import toasterui |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 45 | class EventPlayer: |
| 46 | """Emulate a connection to a bitbake server.""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 48 | def __init__(self, eventfile, variables): |
| 49 | self.eventfile = eventfile |
| 50 | self.variables = variables |
| 51 | self.eventmask = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 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 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 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): |
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 | This method is called by toasterui. |
| 106 | The return value is passed to self.runCommand but not used there. |
| 107 | """ |
| 108 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 110 | def 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 115 | params = namedtuple('ConfigParams', ['observe_only'])(True) |
| 116 | player = EventPlayer(eventfile, variables) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 118 | return toasterui.main(player, player, params) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 119 | |
| 120 | # run toaster ui on our mock bitbake class |
| 121 | if __name__ == "__main__": |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 122 | if len(sys.argv) != 2: |
| 123 | print("Usage: %s <event file>" % os.path.basename(sys.argv[0])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | sys.exit(1) |
| 125 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 126 | sys.exit(main(sys.argv)) |