blob: fa430bb3b3de054d2f527d42563a07f076a1680f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#!/usr/bin/env python3
2
3# bitbake-diffsigs / bitbake-dumpsig
4# BitBake task signature data dump and comparison utility
5#
6# Copyright (C) 2012-2013, 2017 Intel Corporation
7#
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
24import argparse
25import logging
26import pickle
27
28sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
29
30import bb.tinfoil
31import bb.siggen
32import bb.msg
33
34myname = os.path.basename(sys.argv[0])
35logger = bb.msg.logger_create(myname)
36
37is_dump = myname == 'bitbake-dumpsig'
38
39def 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
64def find_siginfo_task(bbhandler, pn, taskname, sig1=None, sig2=None):
65 """ Find the most recent signature files for the specified PN/task """
66
67 if not taskname.startswith('do_'):
68 taskname = 'do_%s' % taskname
69
70 if sig1 and sig2:
71 sigfiles = find_siginfo(bbhandler, pn, taskname, [sig1, sig2])
72 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]]
82 else:
83 filedates = find_siginfo(bbhandler, pn, taskname)
84 latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-2:]
85 if not latestfiles:
86 logger.error('No sigdata files found matching %s %s' % (pn, taskname))
87 sys.exit(1)
88
89 return latestfiles
90
91
92# Define recursion callback
93def recursecb(key, hash1, hash2):
94 hashes = [hash1, hash2]
95 hashfiles = find_siginfo(tinfoil, key, None, hashes)
96
97 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)
109
110 return recout
111
112
113parser = argparse.ArgumentParser(
114 description=("Dumps" if is_dump else "Compares") + " siginfo/sigdata files written out by BitBake")
115
116parser.add_argument('-D', '--debug',
117 help='Enable debug output',
118 action='store_true')
119
120if 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'))
124
125 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')
132
133 parser.add_argument('-d', '--dump',
134 help='Dump the last signature data instead of comparing (equivalent to using bitbake-dumpsig)',
135 action='store_true')
136
137 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'))
140
141 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'))
144
145 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='?')
152
153options = parser.parse_args()
154if is_dump:
155 options.color = 'never'
156 options.dump = True
157 options.sigdatafile2 = None
158 options.sigargs = None
159
160if options.debug:
161 logger.setLevel(logging.DEBUG)
162
163color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty()))
164
165if options.taskargs:
166 with bb.tinfoil.Tinfoil() as tinfoil:
167 tinfoil.prepare(config_only=True)
168 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])
170 else:
171 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)
185else:
186 if options.sigargs:
187 logger.error('-s/--signature can only be used together with -t/--task')
188 sys.exit(1)
189 try:
190 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)
194 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()
199 sys.exit(1)
200 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)
206
207if output:
208 print('\n'.join(output))