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