Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 3 | # bitbake-diffsigs / bitbake-dumpsig |
| 4 | # BitBake task signature data dump and comparison utility |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 5 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 6 | # Copyright (C) 2012-2013, 2017 Intel Corporation |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 8 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | |
| 11 | import os |
| 12 | import sys |
| 13 | import warnings |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 14 | warnings.simplefilter("default") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 15 | import argparse |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | import logging |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 17 | import pickle |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | |
| 19 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib')) |
| 20 | |
| 21 | import bb.tinfoil |
| 22 | import bb.siggen |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 23 | import bb.msg |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 25 | myname = os.path.basename(sys.argv[0]) |
| 26 | logger = bb.msg.logger_create(myname) |
| 27 | |
| 28 | is_dump = myname == 'bitbake-dumpsig' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 30 | def 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 Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 55 | def find_siginfo_task(bbhandler, pn, taskname, sig1=None, sig2=None): |
| 56 | """ Find the most recent signature files for the specified PN/task """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | if not taskname.startswith('do_'): |
| 59 | taskname = 'do_%s' % taskname |
| 60 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | if sig1 and sig2: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 62 | sigfiles = find_siginfo(bbhandler, pn, taskname, [sig1, sig2]) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 63 | if not sigfiles: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 74 | filedates = find_siginfo(bbhandler, pn, taskname) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 75 | latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-2:] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 76 | if not latestfiles: |
| 77 | logger.error('No sigdata files found matching %s %s' % (pn, taskname)) |
| 78 | sys.exit(1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 80 | return latestfiles |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 83 | # Define recursion callback |
| 84 | def recursecb(key, hash1, hash2): |
| 85 | hashes = [hash1, hash2] |
| 86 | hashfiles = find_siginfo(tinfoil, key, None, hashes) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 88 | recout = [] |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 89 | if not hashfiles: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 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 Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 99 | recout.append(' ' + line) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 101 | return recout |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | |
| 103 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 104 | parser = argparse.ArgumentParser( |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 105 | description=("Dumps" if is_dump else "Compares") + " siginfo/sigdata files written out by BitBake") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 107 | parser.add_argument('-D', '--debug', |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 108 | help='Enable debug output', |
| 109 | action='store_true') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 111 | if 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 115 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 116 | parser.add_argument("sigdatafile1", |
| 117 | help="Signature file to dump. Not used when using -t/--task.", |
| 118 | action="store", nargs='?', metavar="sigdatafile") |
| 119 | else: |
| 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 123 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 124 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 127 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 128 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 131 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 132 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 135 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 136 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 143 | |
| 144 | options = parser.parse_args() |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 145 | if is_dump: |
| 146 | options.color = 'never' |
| 147 | options.dump = True |
| 148 | options.sigdatafile2 = None |
| 149 | options.sigargs = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 150 | |
| 151 | if options.debug: |
| 152 | logger.setLevel(logging.DEBUG) |
| 153 | |
| 154 | color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty())) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | |
| 156 | if options.taskargs: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 157 | with bb.tinfoil.Tinfoil() as tinfoil: |
| 158 | tinfoil.prepare(config_only=True) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 159 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 161 | else: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 162 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 176 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 177 | if options.sigargs: |
| 178 | logger.error('-s/--signature can only be used together with -t/--task') |
| 179 | sys.exit(1) |
| 180 | try: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 181 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 185 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 190 | sys.exit(1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 191 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 197 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 198 | if output: |
| 199 | print('\n'.join(output)) |