toaster-eventreplay 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python
  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. from __future__ import print_function
  27. import os
  28. import sys, logging
  29. # mangle syspath to allow easy import of modules
  30. sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
  31. 'lib'))
  32. import bb.cooker
  33. from bb.ui import toasterui
  34. import sys
  35. import logging
  36. import json, pickle
  37. class FileReadEventsServerConnection():
  38. """ Emulates a connection to a bitbake server that feeds
  39. events coming actually read from a saved log file.
  40. """
  41. class MockConnection():
  42. """ fill-in for the proxy to the server. we just return generic data
  43. """
  44. def __init__(self, sc):
  45. self._sc = sc
  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. else:
  70. raise Exception("Command %s not implemented" % commandArray[0])
  71. def terminateServer(self):
  72. """ do not do anything """
  73. pass
  74. class EventReader():
  75. def __init__(self, sc):
  76. self._sc = sc
  77. self.firstraise = 0
  78. def _create_event(self, line):
  79. def _import_class(name):
  80. assert len(name) > 0
  81. assert "." in name, name
  82. components = name.strip().split(".")
  83. modulename = ".".join(components[:-1])
  84. moduleklass = components[-1]
  85. module = __import__(modulename, fromlist=[str(moduleklass)])
  86. return getattr(module, moduleklass)
  87. # we build a toaster event out of current event log line
  88. try:
  89. event_data = json.loads(line.strip())
  90. event_class = _import_class(event_data['class'])
  91. event_object = pickle.loads(json.loads(event_data['vars']))
  92. except ValueError as e:
  93. print("Failed loading ", line)
  94. raise e
  95. if not isinstance(event_object, event_class):
  96. raise Exception("Error loading objects %s class %s ", event_object, event_class)
  97. return event_object
  98. def waitEvent(self, timeout):
  99. nextline = self._sc._eventfile.readline()
  100. if len(nextline) == 0:
  101. # the build data ended, while toasterui still waits for events.
  102. # this happens when the server was abruptly stopped, so we simulate this
  103. self.firstraise += 1
  104. if self.firstraise == 1:
  105. raise KeyboardInterrupt()
  106. else:
  107. return None
  108. else:
  109. self._sc.lineno += 1
  110. return self._create_event(nextline)
  111. def _readVariables(self, variableline):
  112. self._variables = json.loads(variableline.strip())['allvariables']
  113. def __init__(self, file_name):
  114. self.connection = FileReadEventsServerConnection.MockConnection(self)
  115. self._eventfile = open(file_name, "r")
  116. # we expect to have the variable dump at the start of the file
  117. self.lineno = 1
  118. self._readVariables(self._eventfile.readline())
  119. self.events = FileReadEventsServerConnection.EventReader(self)
  120. class MockConfigParameters():
  121. """ stand-in for cookerdata.ConfigParameters; as we don't really config a cooker, this
  122. serves just to supply needed interfaces for the toaster ui to work """
  123. def __init__(self):
  124. self.observe_only = True # we can only read files
  125. # run toaster ui on our mock bitbake class
  126. if __name__ == "__main__":
  127. if len(sys.argv) < 2:
  128. print("Usage: %s event.log " % sys.argv[0])
  129. sys.exit(1)
  130. file_name = sys.argv[-1]
  131. mock_connection = FileReadEventsServerConnection(file_name)
  132. configParams = MockConfigParameters()
  133. # run the main program and set exit code to the returned value
  134. sys.exit(toasterui.main(mock_connection.connection, mock_connection.events, configParams))