toaster-eventreplay 3.5 KB

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