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