blob: fe0f33eea177980060ae8e9c8bf576e9d901c75a [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
Brad Bishopa5c52ff2018-11-23 10:55:50 +13003# bitbake-diffsigs / bitbake-dumpsig
4# BitBake task signature data dump and comparison utility
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006# Copyright (C) 2012-2013, 2017 Intel Corporation
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007#
Brad Bishopc342db32019-05-15 21:57:59 -04008# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010
11import os
12import sys
13import warnings
Patrick Williams03907ee2022-05-01 06:28:52 -050014
Andrew Geissler5199d832021-09-24 16:47:35 -050015warnings.simplefilter("default")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050016import argparse
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017import logging
Patrick Williamsc0f7c042017-02-23 20:41:17 -060018import pickle
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019
20sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
21
22import bb.tinfoil
23import bb.siggen
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024import bb.msg
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025
Brad Bishopa5c52ff2018-11-23 10:55:50 +130026myname = os.path.basename(sys.argv[0])
27logger = bb.msg.logger_create(myname)
28
29is_dump = myname == 'bitbake-dumpsig'
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030
Patrick Williams03907ee2022-05-01 06:28:52 -050031
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032def 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
Patrick Williams03907ee2022-05-01 06:28:52 -050057
Brad Bishopa5c52ff2018-11-23 10:55:50 +130058def find_siginfo_task(bbhandler, pn, taskname, sig1=None, sig2=None):
59 """ Find the most recent signature files for the specified PN/task """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 if not taskname.startswith('do_'):
62 taskname = 'do_%s' % taskname
63
Brad Bishop6e60e8b2018-02-01 10:27:11 -050064 if sig1 and sig2:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050065 sigfiles = find_siginfo(bbhandler, pn, taskname, [sig1, sig2])
Andrew Geissler595f6302022-01-24 19:11:47 +000066 if not sigfiles:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050067 logger.error('No sigdata files found matching %s %s matching either %s or %s' % (pn, taskname, sig1, sig2))
68 sys.exit(1)
Patrick Williams03907ee2022-05-01 06:28:52 -050069 elif sig1 not in sigfiles:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig1))
71 sys.exit(1)
Patrick Williams03907ee2022-05-01 06:28:52 -050072 elif sig2 not in sigfiles:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050073 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig2))
74 sys.exit(1)
75 latestfiles = [sigfiles[sig1], sigfiles[sig2]]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 filedates = find_siginfo(bbhandler, pn, taskname)
Brad Bishopa5c52ff2018-11-23 10:55:50 +130078 latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-2:]
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 if not latestfiles:
80 logger.error('No sigdata files found matching %s %s' % (pn, taskname))
81 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082
Brad Bishopa5c52ff2018-11-23 10:55:50 +130083 return latestfiles
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
Brad Bishopa5c52ff2018-11-23 10:55:50 +130086# Define recursion callback
87def recursecb(key, hash1, hash2):
88 hashes = [hash1, hash2]
89 hashfiles = find_siginfo(tinfoil, key, None, hashes)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090
Brad Bishopa5c52ff2018-11-23 10:55:50 +130091 recout = []
Andrew Geissler595f6302022-01-24 19:11:47 +000092 if not hashfiles:
Brad Bishopa5c52ff2018-11-23 10:55:50 +130093 recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2))
Patrick Williams03907ee2022-05-01 06:28:52 -050094 elif hash1 not in hashfiles:
Brad Bishopa5c52ff2018-11-23 10:55:50 +130095 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash1))
Patrick Williams03907ee2022-05-01 06:28:52 -050096 elif hash2 not in hashfiles:
Brad Bishopa5c52ff2018-11-23 10:55:50 +130097 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash2))
98 else:
99 out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb, color=color)
100 for change in out2:
101 for line in change.splitlines():
Brad Bishopc342db32019-05-15 21:57:59 -0400102 recout.append(' ' + line)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300104 return recout
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105
106
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107parser = argparse.ArgumentParser(
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300108 description=("Dumps" if is_dump else "Compares") + " siginfo/sigdata files written out by BitBake")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300110parser.add_argument('-D', '--debug',
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500111 help='Enable debug output',
112 action='store_true')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300114if is_dump:
115 parser.add_argument("-t", "--task",
Patrick Williams03907ee2022-05-01 06:28:52 -0500116 help="find the signature data file for the last run of the specified task",
117 action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500118
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300119 parser.add_argument("sigdatafile1",
Patrick Williams03907ee2022-05-01 06:28:52 -0500120 help="Signature file to dump. Not used when using -t/--task.",
121 action="store", nargs='?', metavar="sigdatafile")
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300122else:
123 parser.add_argument('-c', '--color',
Patrick Williams03907ee2022-05-01 06:28:52 -0500124 help='Colorize the output (where %(metavar)s is %(choices)s)',
125 choices=['auto', 'always', 'never'], default='auto', metavar='color')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500126
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300127 parser.add_argument('-d', '--dump',
Patrick Williams03907ee2022-05-01 06:28:52 -0500128 help='Dump the last signature data instead of comparing (equivalent to using bitbake-dumpsig)',
129 action='store_true')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500130
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300131 parser.add_argument("-t", "--task",
Patrick Williams03907ee2022-05-01 06:28:52 -0500132 help="find the signature data files for the last two runs of the specified task and compare them",
133 action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500134
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300135 parser.add_argument("-s", "--signature",
Patrick Williams03907ee2022-05-01 06:28:52 -0500136 help="With -t/--task, specify the signatures to look for instead of taking the last two",
137 action="store", dest="sigargs", nargs=2, metavar=('fromsig', 'tosig'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500138
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300139 parser.add_argument("sigdatafile1",
Patrick Williams03907ee2022-05-01 06:28:52 -0500140 help="First signature file to compare (or signature file to dump, if second not specified). Not used when using -t/--task.",
141 action="store", nargs='?')
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300142
143 parser.add_argument("sigdatafile2",
Patrick Williams03907ee2022-05-01 06:28:52 -0500144 help="Second signature file to compare",
145 action="store", nargs='?')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146
147options = parser.parse_args()
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300148if is_dump:
149 options.color = 'never'
150 options.dump = True
151 options.sigdatafile2 = None
152 options.sigargs = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153
154if options.debug:
155 logger.setLevel(logging.DEBUG)
156
157color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty()))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158
159if options.taskargs:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600160 with bb.tinfoil.Tinfoil() as tinfoil:
161 tinfoil.prepare(config_only=True)
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300162 if not options.dump and options.sigargs:
Patrick Williams03907ee2022-05-01 06:28:52 -0500163 files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1], options.sigargs[0],
164 options.sigargs[1])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500165 else:
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300166 files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1])
167
168 if options.dump:
169 logger.debug("Signature file: %s" % files[-1])
170 output = bb.siggen.dump_sigfile(files[-1])
171 else:
172 if len(files) < 2:
Patrick Williams03907ee2022-05-01 06:28:52 -0500173 logger.error('Only one matching sigdata file found for the specified task (%s %s)' % (
174 options.taskargs[0], options.taskargs[1]))
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300175 sys.exit(1)
176
177 # Recurse into signature comparison
178 logger.debug("Signature file (previous): %s" % files[-2])
179 logger.debug("Signature file (latest): %s" % files[-1])
180 output = bb.siggen.compare_sigfiles(files[-2], files[-1], recursecb, color=color)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182 if options.sigargs:
183 logger.error('-s/--signature can only be used together with -t/--task')
184 sys.exit(1)
185 try:
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300186 if not options.dump and options.sigdatafile1 and options.sigdatafile2:
187 with bb.tinfoil.Tinfoil() as tinfoil:
188 tinfoil.prepare(config_only=True)
189 output = bb.siggen.compare_sigfiles(options.sigdatafile1, options.sigdatafile2, recursecb, color=color)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500190 elif options.sigdatafile1:
191 output = bb.siggen.dump_sigfile(options.sigdatafile1)
192 else:
193 logger.error('Must specify signature file(s) or -t/--task')
194 parser.print_help()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 sys.exit(1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500196 except IOError as e:
197 logger.error(str(e))
198 sys.exit(1)
199 except (pickle.UnpicklingError, EOFError):
200 logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files')
201 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300203if output:
204 print('\n'.join(output))