blob: 5ef82d7be0e222a90b2297cb414ebc2ffd126103 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001import hashlib
2import logging
3import os
4import re
5import tempfile
Patrick Williamsc0f7c042017-02-23 20:41:17 -06006import pickle
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007import bb.data
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008import difflib
9import simplediff
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050010from bb.checksum import FileChecksumCache
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
12logger = logging.getLogger('BitBake.SigGen')
13
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014def init(d):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060015 siggens = [obj for obj in globals().values()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016 if type(obj) is type and issubclass(obj, SignatureGenerator)]
17
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018 desired = d.getVar("BB_SIGNATURE_HANDLER") or "noop"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019 for sg in siggens:
20 if desired == sg.name:
21 return sg(d)
22 break
23 else:
24 logger.error("Invalid signature generator '%s', using default 'noop'\n"
25 "Available generators: %s", desired,
26 ', '.join(obj.name for obj in siggens))
27 return SignatureGenerator(d)
28
29class SignatureGenerator(object):
30 """
31 """
32 name = "noop"
33
34 def __init__(self, data):
Brad Bishop37a0e4d2017-12-04 01:01:44 -050035 self.basehash = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036 self.taskhash = {}
37 self.runtaskdeps = {}
38 self.file_checksum_values = {}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050039 self.taints = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040
41 def finalise(self, fn, d, varient):
42 return
43
44 def get_taskhash(self, fn, task, deps, dataCache):
45 return "0"
46
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047 def writeout_file_checksum_cache(self):
48 """Write/update the file checksum cache onto disk"""
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 return
50
51 def stampfile(self, stampbase, file_name, taskname, extrainfo):
52 return ("%s.%s.%s" % (stampbase, taskname, extrainfo)).rstrip('.')
53
54 def stampcleanmask(self, stampbase, file_name, taskname, extrainfo):
55 return ("%s.%s.%s" % (stampbase, taskname, extrainfo)).rstrip('.')
56
57 def dump_sigtask(self, fn, task, stampbase, runtime):
58 return
59
60 def invalidate_task(self, task, d, fn):
61 bb.build.del_stamp(task, d, fn)
62
63 def dump_sigs(self, dataCache, options):
64 return
65
66 def get_taskdata(self):
Brad Bishop37a0e4d2017-12-04 01:01:44 -050067 return (self.runtaskdeps, self.taskhash, self.file_checksum_values, self.taints, self.basehash)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068
69 def set_taskdata(self, data):
Brad Bishop37a0e4d2017-12-04 01:01:44 -050070 self.runtaskdeps, self.taskhash, self.file_checksum_values, self.taints, self.basehash = data
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072 def reset(self, data):
73 self.__init__(data)
74
75
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076class SignatureGeneratorBasic(SignatureGenerator):
77 """
78 """
79 name = "basic"
80
81 def __init__(self, data):
82 self.basehash = {}
83 self.taskhash = {}
84 self.taskdeps = {}
85 self.runtaskdeps = {}
86 self.file_checksum_values = {}
Patrick Williamsf1e5d692016-03-30 15:21:19 -050087 self.taints = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088 self.gendeps = {}
89 self.lookupcache = {}
90 self.pkgnameextract = re.compile("(?P<fn>.*)\..*")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 self.basewhitelist = set((data.getVar("BB_HASHBASE_WHITELIST") or "").split())
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 self.taskwhitelist = None
93 self.init_rundepcheck(data)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050094 checksum_cache_file = data.getVar("BB_HASH_CHECKSUM_CACHE_FILE")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050095 if checksum_cache_file:
96 self.checksum_cache = FileChecksumCache()
97 self.checksum_cache.init_cache(data, checksum_cache_file)
98 else:
99 self.checksum_cache = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100
101 def init_rundepcheck(self, data):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102 self.taskwhitelist = data.getVar("BB_HASHTASK_WHITELIST") or None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 if self.taskwhitelist:
104 self.twl = re.compile(self.taskwhitelist)
105 else:
106 self.twl = None
107
108 def _build_data(self, fn, d):
109
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 ignore_mismatch = ((d.getVar("BB_HASH_IGNORE_MISMATCH") or '') == '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 tasklist, gendeps, lookupcache = bb.data.generate_dependencies(d)
112
113 taskdeps = {}
114 basehash = {}
115
116 for task in tasklist:
117 data = lookupcache[task]
118
119 if data is None:
120 bb.error("Task %s from %s seems to be empty?!" % (task, fn))
121 data = ''
122
123 gendeps[task] -= self.basewhitelist
124 newdeps = gendeps[task]
125 seen = set()
126 while newdeps:
127 nextdeps = newdeps
128 seen |= nextdeps
129 newdeps = set()
130 for dep in nextdeps:
131 if dep in self.basewhitelist:
132 continue
133 gendeps[dep] -= self.basewhitelist
134 newdeps |= gendeps[dep]
135 newdeps -= seen
136
137 alldeps = sorted(seen)
138 for dep in alldeps:
139 data = data + dep
140 var = lookupcache[dep]
141 if var is not None:
142 data = data + str(var)
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500143 datahash = hashlib.md5(data.encode("utf-8")).hexdigest()
144 k = fn + "." + task
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145 if not ignore_mismatch and k in self.basehash and self.basehash[k] != datahash:
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500146 bb.error("When reparsing %s, the basehash value changed from %s to %s. The metadata is not deterministic and this needs to be fixed." % (k, self.basehash[k], datahash))
147 self.basehash[k] = datahash
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148 taskdeps[task] = alldeps
149
150 self.taskdeps[fn] = taskdeps
151 self.gendeps[fn] = gendeps
152 self.lookupcache[fn] = lookupcache
153
154 return taskdeps
155
156 def finalise(self, fn, d, variant):
157
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600158 mc = d.getVar("__BBMULTICONFIG", False) or ""
159 if variant or mc:
160 fn = bb.cache.realfn2virtual(fn, variant, mc)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161
162 try:
163 taskdeps = self._build_data(fn, d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 except bb.parse.SkipRecipe:
165 raise
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 except:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167 bb.warn("Error during finalise of %s" % fn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 raise
169
170 #Slow but can be useful for debugging mismatched basehashes
171 #for task in self.taskdeps[fn]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500172 # self.dump_sigtask(fn, task, d.getVar("STAMP"), False)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173
174 for task in taskdeps:
175 d.setVar("BB_BASEHASH_task-%s" % task, self.basehash[fn + "." + task])
176
177 def rundep_check(self, fn, recipename, task, dep, depname, dataCache):
178 # Return True if we should keep the dependency, False to drop it
179 # We only manipulate the dependencies for packages not in the whitelist
180 if self.twl and not self.twl.search(recipename):
181 # then process the actual dependencies
182 if self.twl.search(depname):
183 return False
184 return True
185
186 def read_taint(self, fn, task, stampbase):
187 taint = None
188 try:
189 with open(stampbase + '.' + task + '.taint', 'r') as taintf:
190 taint = taintf.read()
191 except IOError:
192 pass
193 return taint
194
195 def get_taskhash(self, fn, task, deps, dataCache):
196 k = fn + "." + task
197 data = dataCache.basetaskhash[k]
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500198 self.basehash[k] = data
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 self.runtaskdeps[k] = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500200 self.file_checksum_values[k] = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201 recipename = dataCache.pkg_fn[fn]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500202
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 for dep in sorted(deps, key=clean_basepath):
204 depname = dataCache.pkg_fn[self.pkgnameextract.search(dep).group('fn')]
205 if not self.rundep_check(fn, recipename, task, dep, depname, dataCache):
206 continue
207 if dep not in self.taskhash:
208 bb.fatal("%s is not in taskhash, caller isn't calling in dependency order?", dep)
209 data = data + self.taskhash[dep]
210 self.runtaskdeps[k].append(dep)
211
212 if task in dataCache.file_checksums[fn]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500213 if self.checksum_cache:
214 checksums = self.checksum_cache.get_checksums(dataCache.file_checksums[fn][task], recipename)
215 else:
216 checksums = bb.fetch2.get_file_checksums(dataCache.file_checksums[fn][task], recipename)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217 for (f,cs) in checksums:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500218 self.file_checksum_values[k].append((f,cs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500219 if cs:
220 data = data + cs
221
222 taskdep = dataCache.task_deps[fn]
223 if 'nostamp' in taskdep and task in taskdep['nostamp']:
224 # Nostamp tasks need an implicit taint so that they force any dependent tasks to run
225 import uuid
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500226 taint = str(uuid.uuid4())
227 data = data + taint
228 self.taints[k] = "nostamp:" + taint
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229
230 taint = self.read_taint(fn, task, dataCache.stamp[fn])
231 if taint:
232 data = data + taint
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500233 self.taints[k] = taint
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600234 logger.warning("%s is tainted from a forced run" % k)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500235
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600236 h = hashlib.md5(data.encode("utf-8")).hexdigest()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237 self.taskhash[k] = h
238 #d.setVar("BB_TASKHASH_task-%s" % task, taskhash[task])
239 return h
240
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500241 def writeout_file_checksum_cache(self):
242 """Write/update the file checksum cache onto disk"""
243 if self.checksum_cache:
244 self.checksum_cache.save_extras()
245 self.checksum_cache.save_merge()
246 else:
247 bb.fetch2.fetcher_parse_save()
248 bb.fetch2.fetcher_parse_done()
249
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250 def dump_sigtask(self, fn, task, stampbase, runtime):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500251
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252 k = fn + "." + task
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500253 referencestamp = stampbase
254 if isinstance(runtime, str) and runtime.startswith("customfile"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255 sigfile = stampbase
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500256 referencestamp = runtime[11:]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257 elif runtime and k in self.taskhash:
258 sigfile = stampbase + "." + task + ".sigdata" + "." + self.taskhash[k]
259 else:
260 sigfile = stampbase + "." + task + ".sigbasedata" + "." + self.basehash[k]
261
262 bb.utils.mkdirhier(os.path.dirname(sigfile))
263
264 data = {}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500265 data['task'] = task
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500266 data['basewhitelist'] = self.basewhitelist
267 data['taskwhitelist'] = self.taskwhitelist
268 data['taskdeps'] = self.taskdeps[fn][task]
269 data['basehash'] = self.basehash[k]
270 data['gendeps'] = {}
271 data['varvals'] = {}
272 data['varvals'][task] = self.lookupcache[fn][task]
273 for dep in self.taskdeps[fn][task]:
274 if dep in self.basewhitelist:
275 continue
276 data['gendeps'][dep] = self.gendeps[fn][dep]
277 data['varvals'][dep] = self.lookupcache[fn][dep]
278
279 if runtime and k in self.taskhash:
280 data['runtaskdeps'] = self.runtaskdeps[k]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500281 data['file_checksum_values'] = [(os.path.basename(f), cs) for f,cs in self.file_checksum_values[k]]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500282 data['runtaskhashes'] = {}
283 for dep in data['runtaskdeps']:
284 data['runtaskhashes'][dep] = self.taskhash[dep]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500285 data['taskhash'] = self.taskhash[k]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500286
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500287 taint = self.read_taint(fn, task, referencestamp)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288 if taint:
289 data['taint'] = taint
290
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500291 if runtime and k in self.taints:
292 if 'nostamp:' in self.taints[k]:
293 data['taint'] = self.taints[k]
294
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500295 computed_basehash = calc_basehash(data)
296 if computed_basehash != self.basehash[k]:
297 bb.error("Basehash mismatch %s versus %s for %s" % (computed_basehash, self.basehash[k], k))
298 if runtime and k in self.taskhash:
299 computed_taskhash = calc_taskhash(data)
300 if computed_taskhash != self.taskhash[k]:
301 bb.error("Taskhash mismatch %s versus %s for %s" % (computed_taskhash, self.taskhash[k], k))
302 sigfile = sigfile.replace(self.taskhash[k], computed_taskhash)
303
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500304 fd, tmpfile = tempfile.mkstemp(dir=os.path.dirname(sigfile), prefix="sigtask.")
305 try:
306 with os.fdopen(fd, "wb") as stream:
307 p = pickle.dump(data, stream, -1)
308 stream.flush()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600309 os.chmod(tmpfile, 0o664)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500310 os.rename(tmpfile, sigfile)
311 except (OSError, IOError) as err:
312 try:
313 os.unlink(tmpfile)
314 except OSError:
315 pass
316 raise err
317
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500318 def dump_sigfn(self, fn, dataCaches, options):
319 if fn in self.taskdeps:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500320 for task in self.taskdeps[fn]:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600321 tid = fn + ":" + task
322 (mc, _, _) = bb.runqueue.split_tid(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500323 k = fn + "." + task
324 if k not in self.taskhash:
325 continue
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600326 if dataCaches[mc].basetaskhash[k] != self.basehash[k]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500327 bb.error("Bitbake's cached basehash does not match the one we just generated (%s)!" % k)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600328 bb.error("The mismatched hashes were %s and %s" % (dataCaches[mc].basetaskhash[k], self.basehash[k]))
329 self.dump_sigtask(fn, task, dataCaches[mc].stamp[fn], True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500330
331class SignatureGeneratorBasicHash(SignatureGeneratorBasic):
332 name = "basichash"
333
334 def stampfile(self, stampbase, fn, taskname, extrainfo, clean=False):
335 if taskname != "do_setscene" and taskname.endswith("_setscene"):
336 k = fn + "." + taskname[:-9]
337 else:
338 k = fn + "." + taskname
339 if clean:
340 h = "*"
341 elif k in self.taskhash:
342 h = self.taskhash[k]
343 else:
344 # If k is not in basehash, then error
345 h = self.basehash[k]
346 return ("%s.%s.%s.%s" % (stampbase, taskname, h, extrainfo)).rstrip('.')
347
348 def stampcleanmask(self, stampbase, fn, taskname, extrainfo):
349 return self.stampfile(stampbase, fn, taskname, extrainfo, clean=True)
350
351 def invalidate_task(self, task, d, fn):
352 bb.note("Tainting hash to force rebuild of task %s, %s" % (fn, task))
353 bb.build.write_taint(task, d, fn)
354
355def dump_this_task(outfile, d):
356 import bb.parse
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500357 fn = d.getVar("BB_FILENAME")
358 task = "do_" + d.getVar("BB_CURRENTTASK")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500359 referencestamp = bb.build.stamp_internal(task, d, None, True)
360 bb.parse.siggen.dump_sigtask(fn, task, outfile, "customfile:" + referencestamp)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500361
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500362def init_colors(enable_color):
363 """Initialise colour dict for passing to compare_sigfiles()"""
364 # First set up the colours
365 colors = {'color_title': '\033[1;37;40m',
366 'color_default': '\033[0;37;40m',
367 'color_add': '\033[1;32;40m',
368 'color_remove': '\033[1;31;40m',
369 }
370 # Leave all keys present but clear the values
371 if not enable_color:
372 for k in colors.keys():
373 colors[k] = ''
374 return colors
375
376def worddiff_str(oldstr, newstr, colors=None):
377 if not colors:
378 colors = init_colors(False)
379 diff = simplediff.diff(oldstr.split(' '), newstr.split(' '))
380 ret = []
381 for change, value in diff:
382 value = ' '.join(value)
383 if change == '=':
384 ret.append(value)
385 elif change == '+':
386 item = '{color_add}{{+{value}+}}{color_default}'.format(value=value, **colors)
387 ret.append(item)
388 elif change == '-':
389 item = '{color_remove}[-{value}-]{color_default}'.format(value=value, **colors)
390 ret.append(item)
391 whitespace_note = ''
392 if oldstr != newstr and ' '.join(oldstr.split()) == ' '.join(newstr.split()):
393 whitespace_note = ' (whitespace changed)'
394 return '"%s"%s' % (' '.join(ret), whitespace_note)
395
396def list_inline_diff(oldlist, newlist, colors=None):
397 if not colors:
398 colors = init_colors(False)
399 diff = simplediff.diff(oldlist, newlist)
400 ret = []
401 for change, value in diff:
402 value = ' '.join(value)
403 if change == '=':
404 ret.append("'%s'" % value)
405 elif change == '+':
406 item = '{color_add}+{value}{color_default}'.format(value=value, **colors)
407 ret.append(item)
408 elif change == '-':
409 item = '{color_remove}-{value}{color_default}'.format(value=value, **colors)
410 ret.append(item)
411 return '[%s]' % (', '.join(ret))
412
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500413def clean_basepath(a):
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500414 mc = None
415 if a.startswith("multiconfig:"):
416 _, mc, a = a.split(":", 2)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500417 b = a.rsplit("/", 2)[1] + '/' + a.rsplit("/", 2)[2]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500418 if a.startswith("virtual:"):
419 b = b + ":" + a.rsplit(":", 1)[0]
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500420 if mc:
421 b = b + ":multiconfig:" + mc
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500422 return b
423
424def clean_basepaths(a):
425 b = {}
426 for x in a:
427 b[clean_basepath(x)] = a[x]
428 return b
429
430def clean_basepaths_list(a):
431 b = []
432 for x in a:
433 b.append(clean_basepath(x))
434 return b
435
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500436def compare_sigfiles(a, b, recursecb=None, color=False, collapsed=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500437 output = []
438
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500439 colors = init_colors(color)
440 def color_format(formatstr, **values):
441 """
442 Return colour formatted string.
443 NOTE: call with the format string, not an already formatted string
444 containing values (otherwise you could have trouble with { and }
445 characters)
446 """
447 if not formatstr.endswith('{color_default}'):
448 formatstr += '{color_default}'
449 # In newer python 3 versions you can pass both of these directly,
450 # but we only require 3.4 at the moment
451 formatparams = {}
452 formatparams.update(colors)
453 formatparams.update(values)
454 return formatstr.format(**formatparams)
455
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600456 with open(a, 'rb') as f:
457 p1 = pickle.Unpickler(f)
458 a_data = p1.load()
459 with open(b, 'rb') as f:
460 p2 = pickle.Unpickler(f)
461 b_data = p2.load()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500462
463 def dict_diff(a, b, whitelist=set()):
464 sa = set(a.keys())
465 sb = set(b.keys())
466 common = sa & sb
467 changed = set()
468 for i in common:
469 if a[i] != b[i] and i not in whitelist:
470 changed.add(i)
471 added = sb - sa
472 removed = sa - sb
473 return changed, added, removed
474
475 def file_checksums_diff(a, b):
476 from collections import Counter
477 # Handle old siginfo format
478 if isinstance(a, dict):
479 a = [(os.path.basename(f), cs) for f, cs in a.items()]
480 if isinstance(b, dict):
481 b = [(os.path.basename(f), cs) for f, cs in b.items()]
482 # Compare lists, ensuring we can handle duplicate filenames if they exist
483 removedcount = Counter(a)
484 removedcount.subtract(b)
485 addedcount = Counter(b)
486 addedcount.subtract(a)
487 added = []
488 for x in b:
489 if addedcount[x] > 0:
490 addedcount[x] -= 1
491 added.append(x)
492 removed = []
493 changed = []
494 for x in a:
495 if removedcount[x] > 0:
496 removedcount[x] -= 1
497 for y in added:
498 if y[0] == x[0]:
499 changed.append((x[0], x[1], y[1]))
500 added.remove(y)
501 break
502 else:
503 removed.append(x)
504 added = [x[0] for x in added]
505 removed = [x[0] for x in removed]
506 return changed, added, removed
507
508 if 'basewhitelist' in a_data and a_data['basewhitelist'] != b_data['basewhitelist']:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500509 output.append(color_format("{color_title}basewhitelist changed{color_default} from '%s' to '%s'") % (a_data['basewhitelist'], b_data['basewhitelist']))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500510 if a_data['basewhitelist'] and b_data['basewhitelist']:
511 output.append("changed items: %s" % a_data['basewhitelist'].symmetric_difference(b_data['basewhitelist']))
512
513 if 'taskwhitelist' in a_data and a_data['taskwhitelist'] != b_data['taskwhitelist']:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500514 output.append(color_format("{color_title}taskwhitelist changed{color_default} from '%s' to '%s'") % (a_data['taskwhitelist'], b_data['taskwhitelist']))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500515 if a_data['taskwhitelist'] and b_data['taskwhitelist']:
516 output.append("changed items: %s" % a_data['taskwhitelist'].symmetric_difference(b_data['taskwhitelist']))
517
518 if a_data['taskdeps'] != b_data['taskdeps']:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500519 output.append(color_format("{color_title}Task dependencies changed{color_default} from:\n%s\nto:\n%s") % (sorted(a_data['taskdeps']), sorted(b_data['taskdeps'])))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500520
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500521 if a_data['basehash'] != b_data['basehash'] and not collapsed:
522 output.append(color_format("{color_title}basehash changed{color_default} from %s to %s") % (a_data['basehash'], b_data['basehash']))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500523
524 changed, added, removed = dict_diff(a_data['gendeps'], b_data['gendeps'], a_data['basewhitelist'] & b_data['basewhitelist'])
525 if changed:
526 for dep in changed:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500527 output.append(color_format("{color_title}List of dependencies for variable %s changed from '{color_default}%s{color_title}' to '{color_default}%s{color_title}'") % (dep, a_data['gendeps'][dep], b_data['gendeps'][dep]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500528 if a_data['gendeps'][dep] and b_data['gendeps'][dep]:
529 output.append("changed items: %s" % a_data['gendeps'][dep].symmetric_difference(b_data['gendeps'][dep]))
530 if added:
531 for dep in added:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500532 output.append(color_format("{color_title}Dependency on variable %s was added") % (dep))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500533 if removed:
534 for dep in removed:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500535 output.append(color_format("{color_title}Dependency on Variable %s was removed") % (dep))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500536
537
538 changed, added, removed = dict_diff(a_data['varvals'], b_data['varvals'])
539 if changed:
540 for dep in changed:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500541 oldval = a_data['varvals'][dep]
542 newval = b_data['varvals'][dep]
543 if newval and oldval and ('\n' in oldval or '\n' in newval):
544 diff = difflib.unified_diff(oldval.splitlines(), newval.splitlines(), lineterm='')
545 # Cut off the first two lines, since we aren't interested in
546 # the old/new filename (they are blank anyway in this case)
547 difflines = list(diff)[2:]
548 if color:
549 # Add colour to diff output
550 for i, line in enumerate(difflines):
551 if line.startswith('+'):
552 line = color_format('{color_add}{line}', line=line)
553 difflines[i] = line
554 elif line.startswith('-'):
555 line = color_format('{color_remove}{line}', line=line)
556 difflines[i] = line
557 output.append(color_format("{color_title}Variable {var} value changed:{color_default}\n{diff}", var=dep, diff='\n'.join(difflines)))
558 elif newval and oldval and (' ' in oldval or ' ' in newval):
559 output.append(color_format("{color_title}Variable {var} value changed:{color_default}\n{diff}", var=dep, diff=worddiff_str(oldval, newval, colors)))
560 else:
561 output.append(color_format("{color_title}Variable {var} value changed from '{color_default}{oldval}{color_title}' to '{color_default}{newval}{color_title}'{color_default}", var=dep, oldval=oldval, newval=newval))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500562
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600563 if not 'file_checksum_values' in a_data:
564 a_data['file_checksum_values'] = {}
565 if not 'file_checksum_values' in b_data:
566 b_data['file_checksum_values'] = {}
567
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500568 changed, added, removed = file_checksums_diff(a_data['file_checksum_values'], b_data['file_checksum_values'])
569 if changed:
570 for f, old, new in changed:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500571 output.append(color_format("{color_title}Checksum for file %s changed{color_default} from %s to %s") % (f, old, new))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500572 if added:
573 for f in added:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500574 output.append(color_format("{color_title}Dependency on checksum of file %s was added") % (f))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500575 if removed:
576 for f in removed:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500577 output.append(color_format("{color_title}Dependency on checksum of file %s was removed") % (f))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500578
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600579 if not 'runtaskdeps' in a_data:
580 a_data['runtaskdeps'] = {}
581 if not 'runtaskdeps' in b_data:
582 b_data['runtaskdeps'] = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500583
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500584 if not collapsed:
585 if len(a_data['runtaskdeps']) != len(b_data['runtaskdeps']):
586 changed = ["Number of task dependencies changed"]
587 else:
588 changed = []
589 for idx, task in enumerate(a_data['runtaskdeps']):
590 a = a_data['runtaskdeps'][idx]
591 b = b_data['runtaskdeps'][idx]
592 if a_data['runtaskhashes'][a] != b_data['runtaskhashes'][b] and not collapsed:
593 changed.append("%s with hash %s\n changed to\n%s with hash %s" % (clean_basepath(a), a_data['runtaskhashes'][a], clean_basepath(b), b_data['runtaskhashes'][b]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500594
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500595 if changed:
596 clean_a = clean_basepaths_list(a_data['runtaskdeps'])
597 clean_b = clean_basepaths_list(b_data['runtaskdeps'])
598 if clean_a != clean_b:
599 output.append(color_format("{color_title}runtaskdeps changed:{color_default}\n%s") % list_inline_diff(clean_a, clean_b, colors))
600 else:
601 output.append(color_format("{color_title}runtaskdeps changed:"))
602 output.append("\n".join(changed))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500603
604
605 if 'runtaskhashes' in a_data and 'runtaskhashes' in b_data:
606 a = a_data['runtaskhashes']
607 b = b_data['runtaskhashes']
608 changed, added, removed = dict_diff(a, b)
609 if added:
610 for dep in added:
611 bdep_found = False
612 if removed:
613 for bdep in removed:
614 if b[dep] == a[bdep]:
615 #output.append("Dependency on task %s was replaced by %s with same hash" % (dep, bdep))
616 bdep_found = True
617 if not bdep_found:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500618 output.append(color_format("{color_title}Dependency on task %s was added{color_default} with hash %s") % (clean_basepath(dep), b[dep]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500619 if removed:
620 for dep in removed:
621 adep_found = False
622 if added:
623 for adep in added:
624 if b[adep] == a[dep]:
625 #output.append("Dependency on task %s was replaced by %s with same hash" % (adep, dep))
626 adep_found = True
627 if not adep_found:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500628 output.append(color_format("{color_title}Dependency on task %s was removed{color_default} with hash %s") % (clean_basepath(dep), a[dep]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500629 if changed:
630 for dep in changed:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500631 if not collapsed:
632 output.append(color_format("{color_title}Hash for dependent task %s changed{color_default} from %s to %s") % (clean_basepath(dep), a[dep], b[dep]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500633 if callable(recursecb):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500634 recout = recursecb(dep, a[dep], b[dep])
635 if recout:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500636 if collapsed:
637 output.extend(recout)
638 else:
639 # If a dependent hash changed, might as well print the line above and then defer to the changes in
640 # that hash since in all likelyhood, they're the same changes this task also saw.
641 output = [output[-1]] + recout
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500642
643 a_taint = a_data.get('taint', None)
644 b_taint = b_data.get('taint', None)
645 if a_taint != b_taint:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500646 output.append(color_format("{color_title}Taint (by forced/invalidated task) changed{color_default} from %s to %s") % (a_taint, b_taint))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500647
648 return output
649
650
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500651def calc_basehash(sigdata):
652 task = sigdata['task']
653 basedata = sigdata['varvals'][task]
654
655 if basedata is None:
656 basedata = ''
657
658 alldeps = sigdata['taskdeps']
659 for dep in alldeps:
660 basedata = basedata + dep
661 val = sigdata['varvals'][dep]
662 if val is not None:
663 basedata = basedata + str(val)
664
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600665 return hashlib.md5(basedata.encode("utf-8")).hexdigest()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500666
667def calc_taskhash(sigdata):
668 data = sigdata['basehash']
669
670 for dep in sigdata['runtaskdeps']:
671 data = data + sigdata['runtaskhashes'][dep]
672
673 for c in sigdata['file_checksum_values']:
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500674 if c[1]:
675 data = data + c[1]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500676
677 if 'taint' in sigdata:
678 if 'nostamp:' in sigdata['taint']:
679 data = data + sigdata['taint'][8:]
680 else:
681 data = data + sigdata['taint']
682
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600683 return hashlib.md5(data.encode("utf-8")).hexdigest()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500684
685
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500686def dump_sigfile(a):
687 output = []
688
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600689 with open(a, 'rb') as f:
690 p1 = pickle.Unpickler(f)
691 a_data = p1.load()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500692
693 output.append("basewhitelist: %s" % (a_data['basewhitelist']))
694
695 output.append("taskwhitelist: %s" % (a_data['taskwhitelist']))
696
697 output.append("Task dependencies: %s" % (sorted(a_data['taskdeps'])))
698
699 output.append("basehash: %s" % (a_data['basehash']))
700
701 for dep in a_data['gendeps']:
702 output.append("List of dependencies for variable %s is %s" % (dep, a_data['gendeps'][dep]))
703
704 for dep in a_data['varvals']:
705 output.append("Variable %s value is %s" % (dep, a_data['varvals'][dep]))
706
707 if 'runtaskdeps' in a_data:
708 output.append("Tasks this task depends on: %s" % (a_data['runtaskdeps']))
709
710 if 'file_checksum_values' in a_data:
711 output.append("This task depends on the checksums of files: %s" % (a_data['file_checksum_values']))
712
713 if 'runtaskhashes' in a_data:
714 for dep in a_data['runtaskhashes']:
715 output.append("Hash for dependent task %s is %s" % (dep, a_data['runtaskhashes'][dep]))
716
717 if 'taint' in a_data:
718 output.append("Tainted (by forced/invalidated task): %s" % a_data['taint'])
719
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500720 if 'task' in a_data:
721 computed_basehash = calc_basehash(a_data)
722 output.append("Computed base hash is %s and from file %s" % (computed_basehash, a_data['basehash']))
723 else:
724 output.append("Unable to compute base hash")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500725
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500726 computed_taskhash = calc_taskhash(a_data)
727 output.append("Computed task hash is %s" % computed_taskhash)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500728
729 return output