bitbake-diffsigs 8.2 KB

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