toaster-eventreplay 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License version 2 as
  14. # published by the Free Software Foundation.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  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. """
  29. import os
  30. import sys
  31. import json
  32. import pickle
  33. import codecs
  34. from collections import namedtuple
  35. # mangle syspath to allow easy import of modules
  36. from os.path import join, dirname, abspath
  37. sys.path.insert(0, join(dirname(dirname(abspath(__file__))), 'lib'))
  38. import bb.cooker
  39. from bb.ui import toasterui
  40. class EventPlayer:
  41. """Emulate a connection to a bitbake server."""
  42. def __init__(self, eventfile, variables):
  43. self.eventfile = eventfile
  44. self.variables = variables
  45. self.eventmask = []
  46. def waitEvent(self, _timeout):
  47. """Read event from the file."""
  48. line = self.eventfile.readline().strip()
  49. if not line:
  50. return
  51. try:
  52. event_str = json.loads(line)['vars'].encode('utf-8')
  53. event = pickle.loads(codecs.decode(event_str, 'base64'))
  54. event_name = "%s.%s" % (event.__module__, event.__class__.__name__)
  55. if event_name not in self.eventmask:
  56. return
  57. return event
  58. except ValueError as err:
  59. print("Failed loading ", line)
  60. raise err
  61. def runCommand(self, command_line):
  62. """Emulate running a command on the server."""
  63. name = command_line[0]
  64. if name == "getVariable":
  65. var_name = command_line[1]
  66. variable = self.variables.get(var_name)
  67. if variable:
  68. return variable['v'], None
  69. return None, "Missing variable %s" % var_name
  70. elif name == "getAllKeysWithFlags":
  71. dump = {}
  72. flaglist = command_line[1]
  73. for key, val in self.variables.items():
  74. try:
  75. if not key.startswith("__"):
  76. dump[key] = {
  77. 'v': val['v'],
  78. 'history' : val['history'],
  79. }
  80. for flag in flaglist:
  81. dump[key][flag] = val[flag]
  82. except Exception as err:
  83. print(err)
  84. return (dump, None)
  85. elif name == 'setEventMask':
  86. self.eventmask = command_line[-1]
  87. return True, None
  88. else:
  89. raise Exception("Command %s not implemented" % command_line[0])
  90. def getEventHandle(self):
  91. """
  92. This method is called by toasterui.
  93. The return value is passed to self.runCommand but not used there.
  94. """
  95. pass
  96. def main(argv):
  97. with open(argv[-1]) as eventfile:
  98. # load variables from the first line
  99. variables = json.loads(eventfile.readline().strip())['allvariables']
  100. params = namedtuple('ConfigParams', ['observe_only'])(True)
  101. player = EventPlayer(eventfile, variables)
  102. return toasterui.main(player, player, params)
  103. # run toaster ui on our mock bitbake class
  104. if __name__ == "__main__":
  105. if len(sys.argv) != 2:
  106. print("Usage: %s <event file>" % os.path.basename(sys.argv[0]))
  107. sys.exit(1)
  108. sys.exit(main(sys.argv))