toaster-eventreplay 3.4 KB

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