blob: 527d2c7a9c9db220efb3c78144e4f337abb381d7 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
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
21import os
22import sys
23import warnings
24import fnmatch
25import optparse
26import logging
Patrick Williamsc0f7c042017-02-23 20:41:17 -060027import pickle
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028
29sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(sys.argv[0])), 'lib'))
30
31import bb.tinfoil
32import bb.siggen
33
34def logger_create(name, output=sys.stderr):
35 logger = logging.getLogger(name)
36 console = logging.StreamHandler(output)
37 format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
38 if output.isatty():
39 format.enable_color()
40 console.setFormatter(format)
41 logger.addHandler(console)
42 logger.setLevel(logging.INFO)
43 return logger
44
45logger = logger_create('bitbake-diffsigs')
46
47def find_compare_task(bbhandler, pn, taskname):
48 """ Find the most recent signature files for the specified PN/task and compare them """
49
50 def get_hashval(siginfo):
51 if siginfo.endswith('.siginfo'):
52 return siginfo.rpartition(':')[2].partition('_')[0]
53 else:
54 return siginfo.rpartition('.')[2]
55
56 if not hasattr(bb.siggen, 'find_siginfo'):
57 logger.error('Metadata does not support finding signature data files')
58 sys.exit(1)
59
60 if not taskname.startswith('do_'):
61 taskname = 'do_%s' % taskname
62
63 filedates = bb.siggen.find_siginfo(pn, taskname, None, bbhandler.config_data)
64 latestfiles = sorted(filedates.keys(), key=lambda f: filedates[f])[-3:]
65 if not latestfiles:
66 logger.error('No sigdata files found matching %s %s' % (pn, taskname))
67 sys.exit(1)
68 elif len(latestfiles) < 2:
69 logger.error('Only one matching sigdata file found for the specified task (%s %s)' % (pn, taskname))
70 sys.exit(1)
71 else:
72 # It's possible that latestfiles contain 3 elements and the first two have the same hash value.
73 # In this case, we delete the second element.
74 # The above case is actually the most common one. Because we may have sigdata file and siginfo
75 # file having the same hash value. Comparing such two files makes no sense.
76 if len(latestfiles) == 3:
77 hash0 = get_hashval(latestfiles[0])
78 hash1 = get_hashval(latestfiles[1])
79 if hash0 == hash1:
80 latestfiles.pop(1)
81
82 # Define recursion callback
83 def recursecb(key, hash1, hash2):
84 hashes = [hash1, hash2]
85 hashfiles = bb.siggen.find_siginfo(key, None, hashes, bbhandler.config_data)
86
87 recout = []
88 if len(hashfiles) == 2:
89 out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb)
90 recout.extend(list(' ' + l for l in out2))
91 else:
92 recout.append("Unable to find matching sigdata for %s with hashes %s or %s" % (key, hash1, hash2))
93
94 return recout
95
96 # Recurse into signature comparison
97 output = bb.siggen.compare_sigfiles(latestfiles[0], latestfiles[1], recursecb)
98 if output:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060099 print('\n'.join(output))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 sys.exit(0)
101
102
103
104parser = optparse.OptionParser(
105 description = "Compares siginfo/sigdata files written out by BitBake",
106 usage = """
107 %prog -t recipename taskname
108 %prog sigdatafile1 sigdatafile2
109 %prog sigdatafile1""")
110
111parser.add_option("-t", "--task",
112 help = "find the signature data files for last two runs of the specified task and compare them",
113 action="store", dest="taskargs", nargs=2, metavar='recipename taskname')
114
115options, args = parser.parse_args(sys.argv)
116
117if options.taskargs:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600118 with bb.tinfoil.Tinfoil() as tinfoil:
119 tinfoil.prepare(config_only=True)
120 find_compare_task(tinfoil, options.taskargs[0], options.taskargs[1])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121else:
122 if len(args) == 1:
123 parser.print_help()
124 else:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 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)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600133 except (pickle.UnpicklingError, EOFError):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500134 logger.error('Invalid signature data - ensure you are specifying sigdata/siginfo files')
135 sys.exit(1)
136
137 if output:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600138 print('\n'.join(output))