bitbake-diffsigs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env python3
  2. # bitbake-diffsigs / bitbake-dumpsig
  3. # BitBake task signature data dump and comparison utility
  4. #
  5. # Copyright (C) 2012-2013, 2017 Intel Corporation
  6. #
  7. # SPDX-License-Identifier: GPL-2.0-only
  8. #
  9. import os
  10. import sys
  11. import warnings
  12. warnings.simplefilter("default")
  13. import argparse
  14. import logging
  15. import pickle
  16. sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
  17. import bb.tinfoil
  18. import bb.siggen
  19. import bb.msg
  20. myname = os.path.basename(sys.argv[0])
  21. logger = bb.msg.logger_create(myname)
  22. is_dump = myname == 'bitbake-dumpsig'
  23. def find_siginfo(tinfoil, pn, taskname, sigs=None):
  24. result = None
  25. tinfoil.set_event_mask(['bb.event.FindSigInfoResult',
  26. 'logging.LogRecord',
  27. 'bb.command.CommandCompleted',
  28. 'bb.command.CommandFailed'])
  29. ret = tinfoil.run_command('findSigInfo', pn, taskname, sigs)
  30. if ret:
  31. while True:
  32. event = tinfoil.wait_event(1)
  33. if event:
  34. if isinstance(event, bb.command.CommandCompleted):
  35. break
  36. elif isinstance(event, bb.command.CommandFailed):
  37. logger.error(str(event))
  38. sys.exit(2)
  39. elif isinstance(event, bb.event.FindSigInfoResult):
  40. result = event.result
  41. elif isinstance(event, logging.LogRecord):
  42. logger.handle(event)
  43. else:
  44. logger.error('No result returned from findSigInfo command')
  45. sys.exit(2)
  46. return result
  47. def find_siginfo_task(bbhandler, pn, taskname, sig1=None, sig2=None):
  48. """ Find the most recent signature files for the specified PN/task """
  49. if not taskname.startswith('do_'):
  50. taskname = 'do_%s' % taskname
  51. if sig1 and sig2:
  52. sigfiles = find_siginfo(bbhandler, pn, taskname, [sig1, sig2])
  53. if not sigfiles:
  54. logger.error('No sigdata files found matching %s %s matching either %s or %s' % (pn, taskname, sig1, sig2))
  55. sys.exit(1)
  56. elif sig1 not in sigfiles:
  57. logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig1))
  58. sys.exit(1)
  59. elif sig2 not in sigfiles:
  60. logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig2))
  61. sys.exit(1)
  62. latestfiles = [sigfiles[sig1]['path'], sigfiles[sig2]['path']]
  63. else:
  64. sigfiles = find_siginfo(bbhandler, pn, taskname)
  65. latestsigs = sorted(sigfiles.keys(), key=lambda h: sigfiles[h]['time'])[-2:]
  66. if not latestsigs:
  67. logger.error('No sigdata files found matching %s %s' % (pn, taskname))
  68. sys.exit(1)
  69. latestfiles = [sigfiles[latestsigs[0]]['path']]
  70. if len(latestsigs) > 1:
  71. latestfiles.append(sigfiles[latestsigs[1]]['path'])
  72. return latestfiles
  73. # Define recursion callback
  74. def recursecb(key, hash1, hash2):
  75. hashes = [hash1, hash2]
  76. hashfiles = find_siginfo(tinfoil, key, None, hashes)
  77. recout = []
  78. if not hashfiles:
  79. recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2))
  80. elif hash1 not in hashfiles:
  81. recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash1))
  82. elif hash2 not in hashfiles:
  83. recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash2))
  84. else:
  85. out2 = bb.siggen.compare_sigfiles(hashfiles[hash1]['path'], hashfiles[hash2]['path'], recursecb, color=color)
  86. for change in out2:
  87. for line in change.splitlines():
  88. recout.append(' ' + line)
  89. return recout
  90. parser = argparse.ArgumentParser(
  91. description=("Dumps" if is_dump else "Compares") + " siginfo/sigdata files written out by BitBake")
  92. parser.add_argument('-D', '--debug',
  93. help='Enable debug output',
  94. action='store_true')
  95. if is_dump:
  96. parser.add_argument("-t", "--task",
  97. help="find the signature data file for the last run of the specified task",
  98. action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
  99. parser.add_argument("sigdatafile1",
  100. help="Signature file to dump. Not used when using -t/--task.",
  101. action="store", nargs='?', metavar="sigdatafile")
  102. else:
  103. parser.add_argument('-c', '--color',
  104. help='Colorize the output (where %(metavar)s is %(choices)s)',
  105. choices=['auto', 'always', 'never'], default='auto', metavar='color')
  106. parser.add_argument('-d', '--dump',
  107. help='Dump the last signature data instead of comparing (equivalent to using bitbake-dumpsig)',
  108. action='store_true')
  109. parser.add_argument("-t", "--task",
  110. help="find the signature data files for the last two runs of the specified task and compare them",
  111. action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
  112. parser.add_argument("-s", "--signature",
  113. help="With -t/--task, specify the signatures to look for instead of taking the last two",
  114. action="store", dest="sigargs", nargs=2, metavar=('fromsig', 'tosig'))
  115. parser.add_argument("sigdatafile1",
  116. help="First signature file to compare (or signature file to dump, if second not specified). Not used when using -t/--task.",
  117. action="store", nargs='?')
  118. parser.add_argument("sigdatafile2",
  119. help="Second signature file to compare",
  120. action="store", nargs='?')
  121. options = parser.parse_args()
  122. if is_dump:
  123. options.color = 'never'
  124. options.dump = True
  125. options.sigdatafile2 = None
  126. options.sigargs = None
  127. if options.debug:
  128. logger.setLevel(logging.DEBUG)
  129. color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty()))
  130. if options.taskargs:
  131. with bb.tinfoil.Tinfoil() as tinfoil:
  132. tinfoil.prepare(config_only=True)
  133. if not options.dump and options.sigargs:
  134. files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1], options.sigargs[0],
  135. options.sigargs[1])
  136. else:
  137. files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1])
  138. if options.dump:
  139. logger.debug("Signature file: %s" % files[-1])
  140. output = bb.siggen.dump_sigfile(files[-1])
  141. else:
  142. if len(files) < 2:
  143. logger.error('Only one matching sigdata file found for the specified task (%s %s)' % (
  144. options.taskargs[0], options.taskargs[1]))
  145. sys.exit(1)
  146. # Recurse into signature comparison
  147. logger.debug("Signature file (previous): %s" % files[-2])
  148. logger.debug("Signature file (latest): %s" % files[-1])
  149. output = bb.siggen.compare_sigfiles(files[-2], files[-1], recursecb, color=color)
  150. else:
  151. if options.sigargs:
  152. logger.error('-s/--signature can only be used together with -t/--task')
  153. sys.exit(1)
  154. try:
  155. if not options.dump and options.sigdatafile1 and options.sigdatafile2:
  156. with bb.tinfoil.Tinfoil() as tinfoil:
  157. tinfoil.prepare(config_only=True)
  158. output = bb.siggen.compare_sigfiles(options.sigdatafile1, options.sigdatafile2, recursecb, color=color)
  159. elif options.sigdatafile1:
  160. output = bb.siggen.dump_sigfile(options.sigdatafile1)
  161. else:
  162. logger.error('Must specify signature file(s) or -t/--task')
  163. parser.print_help()
  164. sys.exit(1)
  165. except IOError as e:
  166. logger.error(str(e))
  167. sys.exit(1)
  168. except (pickle.UnpicklingError, EOFError):
  169. logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files')
  170. sys.exit(1)
  171. if output:
  172. print('\n'.join(output))