toaster-eventreplay 4.1 KB

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