blob: eb2f859793d31d1db3ee13ec779e92b929f05250 [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 Bishop6e60e8b2018-02-01 10:27:11 -050037def find_compare_task(bbhandler, pn, taskname, sig1=None, sig2=None, color=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 """ Find the most recent signature files for the specified PN/task and compare them """
39
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040 if not hasattr(bb.siggen, 'find_siginfo'):
41 logger.error('Metadata does not support finding signature data files')
42 sys.exit(1)
43
44 if not taskname.startswith('do_'):
45 taskname = 'do_%s' % taskname
46
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 if sig1 and sig2:
48 sigfiles = bb.siggen.find_siginfo(pn, taskname, [sig1, sig2], bbhandler.config_data)
49 if len(sigfiles) == 0:
50 logger.error('No sigdata files found matching %s %s matching either %s or %s' % (pn, taskname, sig1, sig2))
51 sys.exit(1)
52 elif not sig1 in sigfiles:
53 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig1))
54 sys.exit(1)
55 elif not sig2 in sigfiles:
56 logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig2))
57 sys.exit(1)
58 latestfiles = [sigfiles[sig1], sigfiles[sig2]]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 filedates = bb.siggen.find_siginfo(pn, taskname, None, bbhandler.config_data)
61 latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-3:]
62 if not latestfiles:
63 logger.error('No sigdata files found matching %s %s' % (pn, taskname))
64 sys.exit(1)
65 elif len(latestfiles) < 2:
66 logger.error('Only one matching sigdata file found for the specified task (%s %s)' % (pn, taskname))
67 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068
Brad Bishop6e60e8b2018-02-01 10:27:11 -050069 # Define recursion callback
70 def recursecb(key, hash1, hash2):
71 hashes = [hash1, hash2]
72 hashfiles = bb.siggen.find_siginfo(key, None, hashes, bbhandler.config_data)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073
Brad Bishop6e60e8b2018-02-01 10:27:11 -050074 recout = []
75 if len(hashfiles) == 0:
76 recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2))
77 elif not hash1 in hashfiles:
78 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash1))
79 elif not hash2 in hashfiles:
80 recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash2))
81 else:
82 out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb, color=color)
83 for change in out2:
84 for line in change.splitlines():
85 recout.append(' ' + line)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 return recout
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 # Recurse into signature comparison
90 logger.debug("Signature file (previous): %s" % latestfiles[-2])
91 logger.debug("Signature file (latest): %s" % latestfiles[-1])
92 output = bb.siggen.compare_sigfiles(latestfiles[-2], latestfiles[-1], recursecb, color=color)
93 if output:
94 print('\n'.join(output))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095 sys.exit(0)
96
97
98
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099parser = argparse.ArgumentParser(
100 description="Compares siginfo/sigdata files written out by BitBake")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102parser.add_argument('-d', '--debug',
103 help='Enable debug output',
104 action='store_true')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106parser.add_argument('--color',
107 help='Colorize output (where %(metavar)s is %(choices)s)',
108 choices=['auto', 'always', 'never'], default='auto', metavar='color')
109
110parser.add_argument("-t", "--task",
111 help="find the signature data files for last two runs of the specified task and compare them",
112 action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname'))
113
114parser.add_argument("-s", "--signature",
115 help="With -t/--task, specify the signatures to look for instead of taking the last two",
116 action="store", dest="sigargs", nargs=2, metavar=('fromsig', 'tosig'))
117
118parser.add_argument("sigdatafile1",
119 help="First signature file to compare (or signature file to dump, if second not specified). Not used when using -t/--task.",
120 action="store", nargs='?')
121
122parser.add_argument("sigdatafile2",
123 help="Second signature file to compare",
124 action="store", nargs='?')
125
126
127options = parser.parse_args()
128
129if options.debug:
130 logger.setLevel(logging.DEBUG)
131
132color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty()))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133
134if options.taskargs:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600135 with bb.tinfoil.Tinfoil() as tinfoil:
136 tinfoil.prepare(config_only=True)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 if options.sigargs:
138 find_compare_task(tinfoil, options.taskargs[0], options.taskargs[1], options.sigargs[0], options.sigargs[1], color=color)
139 else:
140 find_compare_task(tinfoil, options.taskargs[0], options.taskargs[1], color=color)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500142 if options.sigargs:
143 logger.error('-s/--signature can only be used together with -t/--task')
144 sys.exit(1)
145 try:
146 if options.sigdatafile1 and options.sigdatafile2:
147 output = bb.siggen.compare_sigfiles(options.sigdatafile1, options.sigdatafile2, color=color)
148 elif options.sigdatafile1:
149 output = bb.siggen.dump_sigfile(options.sigdatafile1)
150 else:
151 logger.error('Must specify signature file(s) or -t/--task')
152 parser.print_help()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153 sys.exit(1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154 except IOError as e:
155 logger.error(str(e))
156 sys.exit(1)
157 except (pickle.UnpicklingError, EOFError):
158 logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files')
159 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 if output:
162 print('\n'.join(output))