blob: 4e6bbddcd8d93e2cc4dea473ad85fd637f6574af [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
3# bitbake-diffsigs
4# BitBake task signature data comparison utility
5#
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
24import fnmatch
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025import argparse
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026import logging
Patrick Williamsc0f7c042017-02-23 20:41:17 -060027import pickle
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028
29sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
30
31import bb.tinfoil
32import bb.siggen
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033import bb.msg
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034
Brad Bishop6e60e8b2018-02-01 10:27:11 -050035logger = bb.msg.logger_create('bitbake-diffsigs')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036
Brad Bishopd7bf8c12018-02-25 22:55:05 -050037def find_siginfo(tinfoil, pn, taskname, sigs=None):
38 result = None
39 tinfoil.set_event_mask(['bb.event.FindSigInfoResult',
40 'logging.LogRecord',
41 'bb.command.CommandCompleted',
42 'bb.command.CommandFailed'])
43 ret = tinfoil.run_command('findSigInfo', pn, taskname, sigs)
44 if ret:
45 while True:
46 event = tinfoil.wait_event(1)
47 if event:
48 if isinstance(event, bb.command.CommandCompleted):
49 break
50 elif isinstance(event, bb.command.CommandFailed):
51 logger.error(str(event))
52 sys.exit(2)
53 elif isinstance(event, bb.event.FindSigInfoResult):
54 result = event.result
55 elif isinstance(event, logging.LogRecord):
56 logger.handle(event)
57 else:
58 logger.error('No result returned from findSigInfo command')
59 sys.exit(2)
60 return result
61
Brad Bishop6e60e8b2018-02-01 10:27:11 -050062def find_compare_task(bbhandler, pn, taskname, sig1=None, sig2=None, color=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 """ Find the most recent signature files for the specified PN/task and compare them """
64
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 if not taskname.startswith('do_'):
66 taskname = 'do_%s' % taskname
67
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 if sig1 and sig2:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069 sigfiles = find_siginfo(bbhandler, pn, taskname, [sig1, sig2])
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070 if len(sigfiles) == 0:
71 logger.error('No sigdata files found matching %s %s matching either %s or %s' % (pn, taskname, sig1, sig2))
72 sys.exit(1)
73 elif not sig1 in sigfiles:
74 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig1))
75 sys.exit(1)
76 elif not sig2 in sigfiles:
77 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig2))
78 sys.exit(1)
79 latestfiles = [sigfiles[sig1], sigfiles[sig2]]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050081 filedates = find_siginfo(bbhandler, pn, taskname)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050082 latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-3:]
83 if not latestfiles:
84 logger.error('No sigdata files found matching %s %s' % (pn, taskname))
85 sys.exit(1)
86 elif len(latestfiles) < 2:
87 logger.error('Only one matching sigdata file found for the specified task (%s %s)' % (pn, taskname))
88 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 # Define recursion callback
91 def recursecb(key, hash1, hash2):
92 hashes = [hash1, hash2]
Brad Bishopd7bf8c12018-02-25 22:55:05 -050093 hashfiles = find_siginfo(bbhandler, key, None, hashes)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094
Brad Bishop6e60e8b2018-02-01 10:27:11 -050095 recout = []
96 if len(hashfiles) == 0:
97 recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2))
98 elif not hash1 in hashfiles:
99 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash1))
100 elif not hash2 in hashfiles:
101 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash2))
102 else:
103 out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb, color=color)
104 for change in out2:
105 for line in change.splitlines():
106 recout.append(' ' + line)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500108 return recout
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 # Recurse into signature comparison
111 logger.debug("Signature file (previous): %s" % latestfiles[-2])
112 logger.debug("Signature file (latest): %s" % latestfiles[-1])
113 output = bb.siggen.compare_sigfiles(latestfiles[-2], latestfiles[-1], recursecb, color=color)
114 if output:
115 print('\n'.join(output))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 sys.exit(0)
117
118
119
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500120parser = argparse.ArgumentParser(
121 description="Compares siginfo/sigdata files written out by BitBake")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123parser.add_argument('-d', '--debug',
124 help='Enable debug output',
125 action='store_true')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500127parser.add_argument('--color',
128 help='Colorize output (where %(metavar)s is %(choices)s)',
129 choices=['auto', 'always', 'never'], default='auto', metavar='color')
130
131parser.add_argument("-t", "--task",
132 help="find the signature data files for last two runs of the specified task and compare them",
133 action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
134
135parser.add_argument("-s", "--signature",
136 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'))
138
139parser.add_argument("sigdatafile1",
140 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='?')
142
143parser.add_argument("sigdatafile2",
144 help="Second signature file to compare",
145 action="store", nargs='?')
146
147
148options = parser.parse_args()
149
150if options.debug:
151 logger.setLevel(logging.DEBUG)
152
153color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty()))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154
155if options.taskargs:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156 with bb.tinfoil.Tinfoil() as tinfoil:
157 tinfoil.prepare(config_only=True)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 if options.sigargs:
159 find_compare_task(tinfoil, options.taskargs[0], options.taskargs[1], options.sigargs[0], options.sigargs[1], color=color)
160 else:
161 find_compare_task(tinfoil, options.taskargs[0], options.taskargs[1], color=color)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500163 if options.sigargs:
164 logger.error('-s/--signature can only be used together with -t/--task')
165 sys.exit(1)
166 try:
167 if options.sigdatafile1 and options.sigdatafile2:
168 output = bb.siggen.compare_sigfiles(options.sigdatafile1, options.sigdatafile2, color=color)
169 elif options.sigdatafile1:
170 output = bb.siggen.dump_sigfile(options.sigdatafile1)
171 else:
172 logger.error('Must specify signature file(s) or -t/--task')
173 parser.print_help()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174 sys.exit(1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500175 except IOError as e:
176 logger.error(str(e))
177 sys.exit(1)
178 except (pickle.UnpicklingError, EOFError):
179 logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files')
180 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500182 if output:
183 print('\n'.join(output))