bitbake-diffsigs 7.5 KB

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