blob: fa430bb3b3de054d2f527d42563a07f076a1680f [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#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License version 2 as
10# published by the Free Software Foundation.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21import os
22import sys
23import warnings
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024import argparse
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025import logging
Patrick Williamsc0f7c042017-02-23 20:41:17 -060026import pickle
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
28sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
29
30import bb.tinfoil
31import bb.siggen
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032import bb.msg
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
Brad Bishopa5c52ff2018-11-23 10:55:50 +130034myname = os.path.basename(sys.argv[0])
35logger = bb.msg.logger_create(myname)
36
37is_dump = myname == 'bitbake-dumpsig'
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038
Brad Bishopd7bf8c12018-02-25 22:55:05 -050039def find_siginfo(tinfoil, pn, taskname, sigs=None):
40 result = None
41 tinfoil.set_event_mask(['bb.event.FindSigInfoResult',
42 'logging.LogRecord',
43 'bb.command.CommandCompleted',
44 'bb.command.CommandFailed'])
45 ret = tinfoil.run_command('findSigInfo', pn, taskname, sigs)
46 if ret:
47 while True:
48 event = tinfoil.wait_event(1)
49 if event:
50 if isinstance(event, bb.command.CommandCompleted):
51 break
52 elif isinstance(event, bb.command.CommandFailed):
53 logger.error(str(event))
54 sys.exit(2)
55 elif isinstance(event, bb.event.FindSigInfoResult):
56 result = event.result
57 elif isinstance(event, logging.LogRecord):
58 logger.handle(event)
59 else:
60 logger.error('No result returned from findSigInfo command')
61 sys.exit(2)
62 return result
63
Brad Bishopa5c52ff2018-11-23 10:55:50 +130064def find_siginfo_task(bbhandler, pn, taskname, sig1=None, sig2=None):
65 """ Find the most recent signature files for the specified PN/task """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067 if not taskname.startswith('do_'):
68 taskname = 'do_%s' % taskname
69
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070 if sig1 and sig2:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050071 sigfiles = find_siginfo(bbhandler, pn, taskname, [sig1, sig2])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050072 if len(sigfiles) == 0:
73 logger.error('No sigdata files found matching %s %s matching either %s or %s' % (pn, taskname, sig1, sig2))
74 sys.exit(1)
75 elif not sig1 in sigfiles:
76 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig1))
77 sys.exit(1)
78 elif not sig2 in sigfiles:
79 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig2))
80 sys.exit(1)
81 latestfiles = [sigfiles[sig1], sigfiles[sig2]]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050083 filedates = find_siginfo(bbhandler, pn, taskname)
Brad Bishopa5c52ff2018-11-23 10:55:50 +130084 latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-2:]
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 if not latestfiles:
86 logger.error('No sigdata files found matching %s %s' % (pn, taskname))
87 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088
Brad Bishopa5c52ff2018-11-23 10:55:50 +130089 return latestfiles
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091
Brad Bishopa5c52ff2018-11-23 10:55:50 +130092# Define recursion callback
93def recursecb(key, hash1, hash2):
94 hashes = [hash1, hash2]
95 hashfiles = find_siginfo(tinfoil, key, None, hashes)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
Brad Bishopa5c52ff2018-11-23 10:55:50 +130097 recout = []
98 if len(hashfiles) == 0:
99 recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2))
100 elif not hash1 in hashfiles:
101 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash1))
102 elif not hash2 in hashfiles:
103 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash2))
104 else:
105 out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb, color=color)
106 for change in out2:
107 for line in change.splitlines():
108 recout.append(' ' + line)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300110 return recout
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111
112
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113parser = argparse.ArgumentParser(
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300114 description=("Dumps" if is_dump else "Compares") + " siginfo/sigdata files written out by BitBake")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300116parser.add_argument('-D', '--debug',
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 help='Enable debug output',
118 action='store_true')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300120if is_dump:
121 parser.add_argument("-t", "--task",
122 help="find the signature data file for the last run of the specified task",
123 action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300125 parser.add_argument("sigdatafile1",
126 help="Signature file to dump. Not used when using -t/--task.",
127 action="store", nargs='?', metavar="sigdatafile")
128else:
129 parser.add_argument('-c', '--color',
130 help='Colorize the output (where %(metavar)s is %(choices)s)',
131 choices=['auto', 'always', 'never'], default='auto', metavar='color')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500132
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300133 parser.add_argument('-d', '--dump',
134 help='Dump the last signature data instead of comparing (equivalent to using bitbake-dumpsig)',
135 action='store_true')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500136
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300137 parser.add_argument("-t", "--task",
138 help="find the signature data files for the last two runs of the specified task and compare them",
139 action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300141 parser.add_argument("-s", "--signature",
142 help="With -t/--task, specify the signatures to look for instead of taking the last two",
143 action="store", dest="sigargs", nargs=2, metavar=('fromsig', 'tosig'))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500144
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300145 parser.add_argument("sigdatafile1",
146 help="First signature file to compare (or signature file to dump, if second not specified). Not used when using -t/--task.",
147 action="store", nargs='?')
148
149 parser.add_argument("sigdatafile2",
150 help="Second signature file to compare",
151 action="store", nargs='?')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500152
153options = parser.parse_args()
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300154if is_dump:
155 options.color = 'never'
156 options.dump = True
157 options.sigdatafile2 = None
158 options.sigargs = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500159
160if options.debug:
161 logger.setLevel(logging.DEBUG)
162
163color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty()))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164
165if options.taskargs:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600166 with bb.tinfoil.Tinfoil() as tinfoil:
167 tinfoil.prepare(config_only=True)
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300168 if not options.dump and options.sigargs:
169 files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1], options.sigargs[0], options.sigargs[1])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 else:
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300171 files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1])
172
173 if options.dump:
174 logger.debug("Signature file: %s" % files[-1])
175 output = bb.siggen.dump_sigfile(files[-1])
176 else:
177 if len(files) < 2:
178 logger.error('Only one matching sigdata file found for the specified task (%s %s)' % (options.taskargs[0], options.taskargs[1]))
179 sys.exit(1)
180
181 # Recurse into signature comparison
182 logger.debug("Signature file (previous): %s" % files[-2])
183 logger.debug("Signature file (latest): %s" % files[-1])
184 output = bb.siggen.compare_sigfiles(files[-2], files[-1], recursecb, color=color)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500186 if options.sigargs:
187 logger.error('-s/--signature can only be used together with -t/--task')
188 sys.exit(1)
189 try:
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300190 if not options.dump and options.sigdatafile1 and options.sigdatafile2:
191 with bb.tinfoil.Tinfoil() as tinfoil:
192 tinfoil.prepare(config_only=True)
193 output = bb.siggen.compare_sigfiles(options.sigdatafile1, options.sigdatafile2, recursecb, color=color)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194 elif options.sigdatafile1:
195 output = bb.siggen.dump_sigfile(options.sigdatafile1)
196 else:
197 logger.error('Must specify signature file(s) or -t/--task')
198 parser.print_help()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 sys.exit(1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500200 except IOError as e:
201 logger.error(str(e))
202 sys.exit(1)
203 except (pickle.UnpicklingError, EOFError):
204 logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files')
205 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206
Brad Bishopa5c52ff2018-11-23 10:55:50 +1300207if output:
208 print('\n'.join(output))