Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # bitbake-diffsigs |
| 4 | # BitBake task signature data comparison utility |
| 5 | # |
| 6 | # Copyright (C) 2012-2013 Intel Corporation |
| 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 |
| 25 | import optparse |
| 26 | import logging |
| 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 |
| 32 | |
| 33 | def logger_create(name, output=sys.stderr): |
| 34 | logger = logging.getLogger(name) |
| 35 | console = logging.StreamHandler(output) |
| 36 | format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s") |
| 37 | if output.isatty(): |
| 38 | format.enable_color() |
| 39 | console.setFormatter(format) |
| 40 | logger.addHandler(console) |
| 41 | logger.setLevel(logging.INFO) |
| 42 | return logger |
| 43 | |
| 44 | logger = logger_create('bitbake-diffsigs') |
| 45 | |
| 46 | def find_compare_task(bbhandler, pn, taskname): |
| 47 | """ Find the most recent signature files for the specified PN/task and compare them """ |
| 48 | |
| 49 | def get_hashval(siginfo): |
| 50 | if siginfo.endswith('.siginfo'): |
| 51 | return siginfo.rpartition(':')[2].partition('_')[0] |
| 52 | else: |
| 53 | return siginfo.rpartition('.')[2] |
| 54 | |
| 55 | if not hasattr(bb.siggen, 'find_siginfo'): |
| 56 | logger.error('Metadata does not support finding signature data files') |
| 57 | sys.exit(1) |
| 58 | |
| 59 | if not taskname.startswith('do_'): |
| 60 | taskname = 'do_%s' % taskname |
| 61 | |
| 62 | filedates = bb.siggen.find_siginfo(pn, taskname, None, bbhandler.config_data) |
| 63 | latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-3:] |
| 64 | if not latestfiles: |
| 65 | logger.error('No sigdata files found matching %s %s' % (pn, taskname)) |
| 66 | sys.exit(1) |
| 67 | elif len(latestfiles) < 2: |
| 68 | logger.error('Only one matching sigdata file found for the specified task (%s %s)' % (pn, taskname)) |
| 69 | sys.exit(1) |
| 70 | else: |
| 71 | # It's possible that latestfiles contain 3 elements and the first two have the same hash value. |
| 72 | # In this case, we delete the second element. |
| 73 | # The above case is actually the most common one. Because we may have sigdata file and siginfo |
| 74 | # file having the same hash value. Comparing such two files makes no sense. |
| 75 | if len(latestfiles) == 3: |
| 76 | hash0 = get_hashval(latestfiles[0]) |
| 77 | hash1 = get_hashval(latestfiles[1]) |
| 78 | if hash0 == hash1: |
| 79 | latestfiles.pop(1) |
| 80 | |
| 81 | # Define recursion callback |
| 82 | def recursecb(key, hash1, hash2): |
| 83 | hashes = [hash1, hash2] |
| 84 | hashfiles = bb.siggen.find_siginfo(key, None, hashes, bbhandler.config_data) |
| 85 | |
| 86 | recout = [] |
| 87 | if len(hashfiles) == 2: |
| 88 | out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb) |
| 89 | recout.extend(list(' ' + l for l in out2)) |
| 90 | else: |
| 91 | recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2)) |
| 92 | |
| 93 | return recout |
| 94 | |
| 95 | # Recurse into signature comparison |
| 96 | output = bb.siggen.compare_sigfiles(latestfiles[0], latestfiles[1], recursecb) |
| 97 | if output: |
| 98 | print '\n'.join(output) |
| 99 | sys.exit(0) |
| 100 | |
| 101 | |
| 102 | |
| 103 | parser = optparse.OptionParser( |
| 104 | description = "Compares siginfo/sigdata files written out by BitBake", |
| 105 | usage = """ |
| 106 | %prog -t recipename taskname |
| 107 | %prog sigdatafile1 sigdatafile2 |
| 108 | %prog sigdatafile1""") |
| 109 | |
| 110 | parser.add_option("-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 | options, args = parser.parse_args(sys.argv) |
| 115 | |
| 116 | if options.taskargs: |
| 117 | tinfoil = bb.tinfoil.Tinfoil() |
| 118 | tinfoil.prepare(config_only = True) |
| 119 | find_compare_task(tinfoil, options.taskargs[0], options.taskargs[1]) |
| 120 | else: |
| 121 | if len(args) == 1: |
| 122 | parser.print_help() |
| 123 | else: |
| 124 | import cPickle |
| 125 | try: |
| 126 | if len(args) == 2: |
| 127 | output = bb.siggen.dump_sigfile(sys.argv[1]) |
| 128 | else: |
| 129 | output = bb.siggen.compare_sigfiles(sys.argv[1], sys.argv[2]) |
| 130 | except IOError as e: |
| 131 | logger.error(str(e)) |
| 132 | sys.exit(1) |
| 133 | except cPickle.UnpicklingError, EOFError: |
| 134 | logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files') |
| 135 | sys.exit(1) |
| 136 | |
| 137 | if output: |
| 138 | print '\n'.join(output) |