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