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 |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 14 | |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 15 | warnings.simplefilter("default") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 16 | import argparse |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | import logging |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 18 | import pickle |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | |
| 20 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib')) |
| 21 | |
| 22 | import bb.tinfoil |
| 23 | import bb.siggen |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 24 | import bb.msg |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 26 | myname = os.path.basename(sys.argv[0]) |
| 27 | logger = bb.msg.logger_create(myname) |
| 28 | |
| 29 | is_dump = myname == 'bitbake-dumpsig' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 31 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 32 | def find_siginfo(tinfoil, pn, taskname, sigs=None): |
| 33 | result = None |
| 34 | tinfoil.set_event_mask(['bb.event.FindSigInfoResult', |
| 35 | 'logging.LogRecord', |
| 36 | 'bb.command.CommandCompleted', |
| 37 | 'bb.command.CommandFailed']) |
| 38 | ret = tinfoil.run_command('findSigInfo', pn, taskname, sigs) |
| 39 | if ret: |
| 40 | while True: |
| 41 | event = tinfoil.wait_event(1) |
| 42 | if event: |
| 43 | if isinstance(event, bb.command.CommandCompleted): |
| 44 | break |
| 45 | elif isinstance(event, bb.command.CommandFailed): |
| 46 | logger.error(str(event)) |
| 47 | sys.exit(2) |
| 48 | elif isinstance(event, bb.event.FindSigInfoResult): |
| 49 | result = event.result |
| 50 | elif isinstance(event, logging.LogRecord): |
| 51 | logger.handle(event) |
| 52 | else: |
| 53 | logger.error('No result returned from findSigInfo command') |
| 54 | sys.exit(2) |
| 55 | return result |
| 56 | |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 57 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 58 | def find_siginfo_task(bbhandler, pn, taskname, sig1=None, sig2=None): |
| 59 | """ Find the most recent signature files for the specified PN/task """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | if not taskname.startswith('do_'): |
| 62 | taskname = 'do_%s' % taskname |
| 63 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 64 | if sig1 and sig2: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 65 | sigfiles = find_siginfo(bbhandler, pn, taskname, [sig1, sig2]) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 66 | if not sigfiles: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 67 | logger.error('No sigdata files found matching %s %s matching either %s or %s' % (pn, taskname, sig1, sig2)) |
| 68 | sys.exit(1) |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 69 | elif sig1 not in sigfiles: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 70 | logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig1)) |
| 71 | sys.exit(1) |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 72 | elif sig2 not in sigfiles: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 73 | logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig2)) |
| 74 | sys.exit(1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | else: |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame] | 76 | sigfiles = find_siginfo(bbhandler, pn, taskname) |
| 77 | latestsigs = sorted(sigfiles.keys(), key=lambda h: sigfiles[h]['time'])[-2:] |
| 78 | if not latestsigs: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 79 | logger.error('No sigdata files found matching %s %s' % (pn, taskname)) |
| 80 | sys.exit(1) |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame] | 81 | sig1 = latestsigs[0] |
| 82 | sig2 = latestsigs[1] |
| 83 | |
| 84 | latestfiles = [sigfiles[sig1]['path'], sigfiles[sig2]['path']] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 86 | return latestfiles |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 89 | # Define recursion callback |
| 90 | def recursecb(key, hash1, hash2): |
| 91 | hashes = [hash1, hash2] |
| 92 | hashfiles = find_siginfo(tinfoil, key, None, hashes) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 94 | recout = [] |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 95 | if not hashfiles: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 96 | recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2)) |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 97 | elif hash1 not in hashfiles: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 98 | recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash1)) |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 99 | elif hash2 not in hashfiles: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 100 | recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash2)) |
| 101 | else: |
| 102 | out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb, color=color) |
| 103 | for change in out2: |
| 104 | for line in change.splitlines(): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 105 | recout.append(' ' + line) |
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 | return recout |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 | |
| 109 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | parser = argparse.ArgumentParser( |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 111 | 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] | 112 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 113 | parser.add_argument('-D', '--debug', |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 114 | help='Enable debug output', |
| 115 | action='store_true') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 117 | if is_dump: |
| 118 | parser.add_argument("-t", "--task", |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 119 | help="find the signature data file for the last run of the specified task", |
| 120 | action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname')) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 121 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 122 | parser.add_argument("sigdatafile1", |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 123 | help="Signature file to dump. Not used when using -t/--task.", |
| 124 | action="store", nargs='?', metavar="sigdatafile") |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 125 | else: |
| 126 | parser.add_argument('-c', '--color', |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 127 | help='Colorize the output (where %(metavar)s is %(choices)s)', |
| 128 | choices=['auto', 'always', 'never'], default='auto', metavar='color') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 129 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 130 | parser.add_argument('-d', '--dump', |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 131 | help='Dump the last signature data instead of comparing (equivalent to using bitbake-dumpsig)', |
| 132 | action='store_true') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 133 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 134 | parser.add_argument("-t", "--task", |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 135 | help="find the signature data files for the last two runs of the specified task and compare them", |
| 136 | action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname')) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 137 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 138 | parser.add_argument("-s", "--signature", |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 139 | help="With -t/--task, specify the signatures to look for instead of taking the last two", |
| 140 | action="store", dest="sigargs", nargs=2, metavar=('fromsig', 'tosig')) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 141 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 142 | parser.add_argument("sigdatafile1", |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 143 | help="First signature file to compare (or signature file to dump, if second not specified). Not used when using -t/--task.", |
| 144 | action="store", nargs='?') |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 145 | |
| 146 | parser.add_argument("sigdatafile2", |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 147 | help="Second signature file to compare", |
| 148 | action="store", nargs='?') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 149 | |
| 150 | options = parser.parse_args() |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 151 | if is_dump: |
| 152 | options.color = 'never' |
| 153 | options.dump = True |
| 154 | options.sigdatafile2 = None |
| 155 | options.sigargs = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 156 | |
| 157 | if options.debug: |
| 158 | logger.setLevel(logging.DEBUG) |
| 159 | |
| 160 | color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty())) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 161 | |
| 162 | if options.taskargs: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 163 | with bb.tinfoil.Tinfoil() as tinfoil: |
| 164 | tinfoil.prepare(config_only=True) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 165 | if not options.dump and options.sigargs: |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 166 | files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1], options.sigargs[0], |
| 167 | options.sigargs[1]) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 168 | else: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 169 | files = find_siginfo_task(tinfoil, options.taskargs[0], options.taskargs[1]) |
| 170 | |
| 171 | if options.dump: |
| 172 | logger.debug("Signature file: %s" % files[-1]) |
| 173 | output = bb.siggen.dump_sigfile(files[-1]) |
| 174 | else: |
| 175 | if len(files) < 2: |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 176 | logger.error('Only one matching sigdata file found for the specified task (%s %s)' % ( |
| 177 | options.taskargs[0], options.taskargs[1])) |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 178 | sys.exit(1) |
| 179 | |
| 180 | # Recurse into signature comparison |
| 181 | logger.debug("Signature file (previous): %s" % files[-2]) |
| 182 | logger.debug("Signature file (latest): %s" % files[-1]) |
| 183 | output = bb.siggen.compare_sigfiles(files[-2], files[-1], recursecb, color=color) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 184 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 185 | if options.sigargs: |
| 186 | logger.error('-s/--signature can only be used together with -t/--task') |
| 187 | sys.exit(1) |
| 188 | try: |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 189 | if not options.dump and options.sigdatafile1 and options.sigdatafile2: |
| 190 | with bb.tinfoil.Tinfoil() as tinfoil: |
| 191 | tinfoil.prepare(config_only=True) |
| 192 | output = bb.siggen.compare_sigfiles(options.sigdatafile1, options.sigdatafile2, recursecb, color=color) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 193 | elif options.sigdatafile1: |
| 194 | output = bb.siggen.dump_sigfile(options.sigdatafile1) |
| 195 | else: |
| 196 | logger.error('Must specify signature file(s) or -t/--task') |
| 197 | parser.print_help() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 198 | sys.exit(1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 199 | except IOError as e: |
| 200 | logger.error(str(e)) |
| 201 | sys.exit(1) |
| 202 | except (pickle.UnpicklingError, EOFError): |
| 203 | logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files') |
| 204 | sys.exit(1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 205 | |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 206 | if output: |
| 207 | print('\n'.join(output)) |