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 | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 37 | 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] | 38 | """ Find the most recent signature files for the specified PN/task and compare them """ |
| 39 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | if not hasattr(bb.siggen, 'find_siginfo'): |
| 41 | logger.error('Metadata does not support finding signature data files') |
| 42 | sys.exit(1) |
| 43 | |
| 44 | if not taskname.startswith('do_'): |
| 45 | taskname = 'do_%s' % taskname |
| 46 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 47 | if sig1 and sig2: |
| 48 | sigfiles = bb.siggen.find_siginfo(pn, taskname, [sig1, sig2], bbhandler.config_data) |
| 49 | if len(sigfiles) == 0: |
| 50 | logger.error('No sigdata files found matching %s %s matching either %s or %s' % (pn, taskname, sig1, sig2)) |
| 51 | sys.exit(1) |
| 52 | elif not sig1 in sigfiles: |
| 53 | logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig1)) |
| 54 | sys.exit(1) |
| 55 | elif not sig2 in sigfiles: |
| 56 | logger.error('No sigdata files found matching %s %s with signature %s' % (pn, taskname, sig2)) |
| 57 | sys.exit(1) |
| 58 | latestfiles = [sigfiles[sig1], sigfiles[sig2]] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 60 | filedates = bb.siggen.find_siginfo(pn, taskname, None, bbhandler.config_data) |
| 61 | latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-3:] |
| 62 | if not latestfiles: |
| 63 | logger.error('No sigdata files found matching %s %s' % (pn, taskname)) |
| 64 | sys.exit(1) |
| 65 | elif len(latestfiles) < 2: |
| 66 | logger.error('Only one matching sigdata file found for the specified task (%s %s)' % (pn, taskname)) |
| 67 | sys.exit(1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 69 | # Define recursion callback |
| 70 | def recursecb(key, hash1, hash2): |
| 71 | hashes = [hash1, hash2] |
| 72 | hashfiles = bb.siggen.find_siginfo(key, None, hashes, bbhandler.config_data) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 74 | recout = [] |
| 75 | if len(hashfiles) == 0: |
| 76 | recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2)) |
| 77 | elif not hash1 in hashfiles: |
| 78 | recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash1)) |
| 79 | elif not hash2 in hashfiles: |
| 80 | recout.append("Unable to find matching sigdata for %s with hash %s" % (key, hash2)) |
| 81 | else: |
| 82 | out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb, color=color) |
| 83 | for change in out2: |
| 84 | for line in change.splitlines(): |
| 85 | recout.append(' ' + line) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 87 | return recout |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 89 | # Recurse into signature comparison |
| 90 | logger.debug("Signature file (previous): %s" % latestfiles[-2]) |
| 91 | logger.debug("Signature file (latest): %s" % latestfiles[-1]) |
| 92 | output = bb.siggen.compare_sigfiles(latestfiles[-2], latestfiles[-1], recursecb, color=color) |
| 93 | if output: |
| 94 | print('\n'.join(output)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | sys.exit(0) |
| 96 | |
| 97 | |
| 98 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 99 | parser = argparse.ArgumentParser( |
| 100 | description="Compares siginfo/sigdata files written out by BitBake") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 102 | parser.add_argument('-d', '--debug', |
| 103 | help='Enable debug output', |
| 104 | action='store_true') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 106 | parser.add_argument('--color', |
| 107 | help='Colorize output (where %(metavar)s is %(choices)s)', |
| 108 | choices=['auto', 'always', 'never'], default='auto', metavar='color') |
| 109 | |
| 110 | parser.add_argument("-t", "--task", |
| 111 | help="find the signature data files for last two runs of the specified task and compare them", |
| 112 | action="store", dest="taskargs", nargs=2, metavar=('recipename', 'taskname')) |
| 113 | |
| 114 | parser.add_argument("-s", "--signature", |
| 115 | help="With -t/--task, specify the signatures to look for instead of taking the last two", |
| 116 | action="store", dest="sigargs", nargs=2, metavar=('fromsig', 'tosig')) |
| 117 | |
| 118 | parser.add_argument("sigdatafile1", |
| 119 | help="First signature file to compare (or signature file to dump, if second not specified). Not used when using -t/--task.", |
| 120 | action="store", nargs='?') |
| 121 | |
| 122 | parser.add_argument("sigdatafile2", |
| 123 | help="Second signature file to compare", |
| 124 | action="store", nargs='?') |
| 125 | |
| 126 | |
| 127 | options = parser.parse_args() |
| 128 | |
| 129 | if options.debug: |
| 130 | logger.setLevel(logging.DEBUG) |
| 131 | |
| 132 | color = (options.color == 'always' or (options.color == 'auto' and sys.stdout.isatty())) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 133 | |
| 134 | if options.taskargs: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 135 | with bb.tinfoil.Tinfoil() as tinfoil: |
| 136 | tinfoil.prepare(config_only=True) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 137 | if options.sigargs: |
| 138 | find_compare_task(tinfoil, options.taskargs[0], options.taskargs[1], options.sigargs[0], options.sigargs[1], color=color) |
| 139 | else: |
| 140 | find_compare_task(tinfoil, options.taskargs[0], options.taskargs[1], color=color) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 142 | if options.sigargs: |
| 143 | logger.error('-s/--signature can only be used together with -t/--task') |
| 144 | sys.exit(1) |
| 145 | try: |
| 146 | if options.sigdatafile1 and options.sigdatafile2: |
| 147 | output = bb.siggen.compare_sigfiles(options.sigdatafile1, options.sigdatafile2, color=color) |
| 148 | elif options.sigdatafile1: |
| 149 | output = bb.siggen.dump_sigfile(options.sigdatafile1) |
| 150 | else: |
| 151 | logger.error('Must specify signature file(s) or -t/--task') |
| 152 | parser.print_help() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 153 | sys.exit(1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 154 | except IOError as e: |
| 155 | logger.error(str(e)) |
| 156 | sys.exit(1) |
| 157 | except (pickle.UnpicklingError, EOFError): |
| 158 | logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files') |
| 159 | sys.exit(1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 160 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 161 | if output: |
| 162 | print('\n'.join(output)) |