toaster-eventreplay 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/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. # This command takes a filename as a single parameter. The filename is read
  24. # as a build eventlog, and the ToasterUI is used to process events in the file
  25. # and log data in the database
  26. import sys
  27. import json
  28. import pickle
  29. import codecs
  30. from collections import namedtuple
  31. # mangle syspath to allow easy import of modules
  32. from os.path import join, dirname, abspath
  33. sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'lib'))
  34. import bb.cooker
  35. from bb.ui import toasterui
  36. class FileReadEventsServerConnection():
  37. """ Emulates a connection to a bitbake server that feeds
  38. events coming actually read from a saved log file.
  39. """
  40. class MockConnection():
  41. """ fill-in for the proxy to the server. we just return generic data
  42. """
  43. def __init__(self, sc):
  44. self._sc = sc
  45. self.eventmask = []
  46. def runCommand(self, commandArray):
  47. """ emulates running a command on the server; only read-only commands are accepted """
  48. command_name = commandArray[0]
  49. if command_name == "getVariable":
  50. if commandArray[1] in self._sc._variables:
  51. return (self._sc._variables[commandArray[1]]['v'], None)
  52. return (None, "Missing variable")
  53. elif command_name == "getAllKeysWithFlags":
  54. dump = {}
  55. flaglist = commandArray[1]
  56. for k in self._sc._variables.keys():
  57. try:
  58. if not k.startswith("__"):
  59. v = self._sc._variables[k]['v']
  60. dump[k] = {
  61. 'v' : v ,
  62. 'history' : self._sc._variables[k]['history'],
  63. }
  64. for d in flaglist:
  65. dump[k][d] = self._sc._variables[k][d]
  66. except Exception as e:
  67. print(e)
  68. return (dump, None)
  69. elif command_name == 'setEventMask':
  70. self.eventmask = commandArray[-1]
  71. return True, None
  72. else:
  73. raise Exception("Command %s not implemented" % commandArray[0])
  74. def terminateServer(self):
  75. """ do not do anything """
  76. pass
  77. def getEventHandle(self):
  78. pass
  79. class EventReader():
  80. def __init__(self, sc):
  81. self._sc = sc
  82. self.firstraise = 0
  83. def _create_event(self, line):
  84. def _import_class(name):
  85. assert len(name) > 0
  86. assert "." in name, name
  87. components = name.strip().split(".")
  88. modulename = ".".join(components[:-1])
  89. moduleklass = components[-1]
  90. module = __import__(modulename, fromlist=[str(moduleklass)])
  91. return getattr(module, moduleklass)
  92. # we build a toaster event out of current event log line
  93. try:
  94. event_data = json.loads(line.strip())
  95. event_class = _import_class(event_data['class'])
  96. event_str = event_data['vars'].encode('utf-8')
  97. event_object = pickle.loads(codecs.decode(event_str, 'base64'))
  98. except ValueError as e:
  99. print("Failed loading ", line)
  100. raise e
  101. if not isinstance(event_object, event_class):
  102. raise Exception("Error loading objects %s class %s ", event_object, event_class)
  103. return event_object
  104. def waitEvent(self, timeout):
  105. nextline = self._sc._eventfile.readline()
  106. if len(nextline) == 0:
  107. # the build data ended, while toasterui still waits for events.
  108. # this happens when the server was abruptly stopped, so we simulate this
  109. self.firstraise += 1
  110. if self.firstraise == 1:
  111. raise KeyboardInterrupt()
  112. else:
  113. return None
  114. else:
  115. self._sc.lineno += 1
  116. return self._create_event(nextline)
  117. def _readVariables(self, variableline):
  118. self._variables = json.loads(variableline.strip())['allvariables']
  119. def __init__(self, file_name):
  120. self.connection = FileReadEventsServerConnection.MockConnection(self)
  121. self._eventfile = open(file_name, "r")
  122. # we expect to have the variable dump at the start of the file
  123. self.lineno = 1
  124. self._readVariables(self._eventfile.readline())
  125. self.events = FileReadEventsServerConnection.EventReader(self)
  126. # run toaster ui on our mock bitbake class
  127. if __name__ == "__main__":
  128. if len(sys.argv) < 2:
  129. print("Usage: %s event.log " % sys.argv[0])
  130. sys.exit(1)
  131. file_name = sys.argv[-1]
  132. mock_connection = FileReadEventsServerConnection(file_name)
  133. configParams = namedtuple('ConfigParams', ['observe_only'])(True)
  134. # run the main program and set exit code to the returned value
  135. sys.exit(toasterui.main(mock_connection.connection, mock_connection.events, configParams))