blob: 6646dccdfa2a7df9d21108f70972b060b0d2e4bc [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
Andrew Geissler5199d832021-09-24 16:47:35 -050014warnings.simplefilter("default")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050015import argparse
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016import logging
Patrick Williamsc0f7c042017-02-23 20:41:17 -060017import pickle
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018
19sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
20
21import bb.tinfoil
22import bb.siggen
Brad Bishop6e60e8b2018-02-01 10:27:11 -050023import bb.msg
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024
Brad Bishopa5c52ff2018-11-23 10:55:50 +130025myname = os.path.basename(sys.argv[0])
26logger = bb.msg.logger_create(myname)
27
28is_dump = myname == 'bitbake-dumpsig'
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029
Brad Bishopd7bf8c12018-02-25 22:55:05 -050030def find_siginfo(tinfoil, pn, taskname, sigs=None):
31 result = None
32 tinfoil.set_event_mask(['bb.event.FindSigInfoResult',
33 'logging.LogRecord',
34 'bb.command.CommandCompleted',
35 'bb.command.CommandFailed'])
36 ret = tinfoil.run_command('findSigInfo', pn, taskname, sigs)
37 if ret:
38 while True:
39 event = tinfoil.wait_event(1)
40 if event:
41 if isinstance(event, bb.command.CommandCompleted):
42 break
43 elif isinstance(event, bb.command.CommandFailed):
44 logger.error(str(event))
45 sys.exit(2)
46 elif isinstance(event, bb.event.FindSigInfoResult):
47 result = event.result
48 elif isinstance(event, logging.LogRecord):
49 logger.handle(event)
50 else:
51 logger.error('No result returned from findSigInfo command')
52 sys.exit(2)
53 return result
54
Brad Bishopa5c52ff2018-11-23 10:55:50 +130055def find_siginfo_task(bbhandler, pn, taskname, sig1=None, sig2=None):
56 """ Find the most recent signature files for the specified PN/task """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 if not taskname.startswith('do_'):
59 taskname = 'do_%s' % taskname
60
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061 if sig1 and sig2:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050062 sigfiles = find_siginfo(bbhandler, pn, taskname, [sig1, sig2])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050063 if len(sigfiles) == 0:
64 logger.error('No sigdata files found matching %s %s matching either %s or %s' % (pn, taskname, sig1, sig2))
65 sys.exit(1)
66 elif not sig1 in sigfiles:
67 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig1))
68 sys.exit(1)
69 elif not sig2 in sigfiles:
70 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig2))
71 sys.exit(1)
72 latestfiles = [sigfiles[sig1], sigfiles[sig2]]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074 filedates = find_siginfo(bbhandler, pn, taskname)
Brad Bishopa5c52ff2018-11-23 10:55:50 +130075 latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-2:]
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076 if not latestfiles:
77 logger.error('No sigdata files found matching %s %s' % (pn, taskname))
78 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079
Brad Bishopa5c52ff2018-11-23 10:55:50 +130080 return latestfiles
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082
Brad Bishopa5c52ff2018-11-23 10:55:50 +130083# Define recursion callback
84def recursecb(key, hash1, hash2):
85 hashes = [hash1, hash2]
86 hashfiles = find_siginfo(tinfoil, key, None, hashes)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
Brad Bishopa5c52ff2018-11-23 10:55:50 +130088 recout = []
89 if len(hashfiles) == 0:
90 recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2))
91 elif not hash1 in hashfiles:
92 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash1))
93 elif not hash2 in hashfiles:
94 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash2))
95 else:
96 out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb, color=color)
97 for change in out2:
98 for line in change.splitlines():
Brad Bishopc342db32019-05-15 21:57:59 -040099 recout.append(' ' + line)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300101 return recout
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
103
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500104parser = argparse.ArgumentParser(
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300105 description=("Dumps" if is_dump else "Compares") + " siginfo/sigdata files written out by BitBake")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300107parser.add_argument('-D', '--debug',
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500108 help='Enable debug output',
109 action='store_true')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300111if is_dump:
112 parser.add_argument("-t", "--task",
113 help="find the signature data file for the last run of the specified task",
114 action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300116 parser.add_argument("sigdatafile1",
117 help="Signature file to dump. Not used when using -t/--task.",
118 action="store", nargs='?', metavar="sigdatafile")
119else:
120 parser.add_argument('-c', '--color',
121 help='Colorize the output (where %(metavar)s is %(choices)s)',
122 choices=['auto', 'always', 'never'], default='auto', metavar='color')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300124 parser.add_argument('-d', '--dump',
125 help='Dump the last signature data instead of comparing (equivalent to using bitbake-dumpsig)',
126 action='store_true')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500127
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300128 parser.add_argument("-t", "--task",
129 help="find the signature data files for the last two runs of the specified task and compare them",
130 action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300132 parser.add_argument("-s", "--signature",
133 help="With -t/--task, specify the signatures to look for instead of taking the last two",
134 action="store", dest="sigargs", nargs=2, metavar=('fromsig', 'tosig'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500135
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300136 parser.add_argument("sigdatafile1",
137 help="First signature file to compare (or signature file to dump, if second not specified). Not used when using -t/--task.",
138 action="store", nargs='?')
139
140 parser.add_argument("sigdatafile2",
141 help="Second signature file to compare",
142 action="store", nargs='?')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500143
144options = parser.parse_args()
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300145if is_dump:
146 options.color = 'never'
147 options.dump = True
148 options.sigdatafile2 = None
149 options.sigargs = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150
151if options.debug:
152 logger.setLevel(logging.DEBUG)
153
154color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty()))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155
156if options.taskargs:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600157 with bb.tinfoil.Tinfoil() as tinfoil:
158 tinfoil.prepare(config_only=True)
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300159 if not options.dump and options.sigargs:
160 files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1], options.sigargs[0], options.sigargs[1])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 else:
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300162 files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1])
163
164 if options.dump:
165 logger.debug("Signature file: %s" % files[-1])
166 output = bb.siggen.dump_sigfile(files[-1])
167 else:
168 if len(files) < 2:
169 logger.error('Only one matching sigdata file found for the specified task (%s %s)' % (options.taskargs[0], options.taskargs[1]))
170 sys.exit(1)
171
172 # Recurse into signature comparison
173 logger.debug("Signature file (previous): %s" % files[-2])
174 logger.debug("Signature file (latest): %s" % files[-1])
175 output = bb.siggen.compare_sigfiles(files[-2], files[-1], recursecb, color=color)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177 if options.sigargs:
178 logger.error('-s/--signature can only be used together with -t/--task')
179 sys.exit(1)
180 try:
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300181 if not options.dump and options.sigdatafile1 and options.sigdatafile2:
182 with bb.tinfoil.Tinfoil() as tinfoil:
183 tinfoil.prepare(config_only=True)
184 output = bb.siggen.compare_sigfiles(options.sigdatafile1, options.sigdatafile2, recursecb, color=color)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500185 elif options.sigdatafile1:
186 output = bb.siggen.dump_sigfile(options.sigdatafile1)
187 else:
188 logger.error('Must specify signature file(s) or -t/--task')
189 parser.print_help()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500190 sys.exit(1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500191 except IOError as e:
192 logger.error(str(e))
193 sys.exit(1)
194 except (pickle.UnpicklingError, EOFError):
195 logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files')
196 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300198if output:
199 print('\n'.join(output))