bitbake-diffsigs 8.0 KB

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