Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | import hashlib |
| 2 | import logging |
| 3 | import os |
| 4 | import re |
| 5 | import tempfile |
| 6 | import bb.data |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 7 | from bb.checksum import FileChecksumCache |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | logger = logging.getLogger('BitBake.SigGen') |
| 10 | |
| 11 | try: |
| 12 | import cPickle as pickle |
| 13 | except ImportError: |
| 14 | import pickle |
| 15 | logger.info('Importing cPickle failed. Falling back to a very slow implementation.') |
| 16 | |
| 17 | def init(d): |
| 18 | siggens = [obj for obj in globals().itervalues() |
| 19 | if type(obj) is type and issubclass(obj, SignatureGenerator)] |
| 20 | |
| 21 | desired = d.getVar("BB_SIGNATURE_HANDLER", True) or "noop" |
| 22 | for sg in siggens: |
| 23 | if desired == sg.name: |
| 24 | return sg(d) |
| 25 | break |
| 26 | else: |
| 27 | logger.error("Invalid signature generator '%s', using default 'noop'\n" |
| 28 | "Available generators: %s", desired, |
| 29 | ', '.join(obj.name for obj in siggens)) |
| 30 | return SignatureGenerator(d) |
| 31 | |
| 32 | class SignatureGenerator(object): |
| 33 | """ |
| 34 | """ |
| 35 | name = "noop" |
| 36 | |
| 37 | def __init__(self, data): |
| 38 | self.taskhash = {} |
| 39 | self.runtaskdeps = {} |
| 40 | self.file_checksum_values = {} |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 41 | self.taints = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | |
| 43 | def finalise(self, fn, d, varient): |
| 44 | return |
| 45 | |
| 46 | def get_taskhash(self, fn, task, deps, dataCache): |
| 47 | return "0" |
| 48 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 49 | def writeout_file_checksum_cache(self): |
| 50 | """Write/update the file checksum cache onto disk""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 51 | return |
| 52 | |
| 53 | def stampfile(self, stampbase, file_name, taskname, extrainfo): |
| 54 | return ("%s.%s.%s" % (stampbase, taskname, extrainfo)).rstrip('.') |
| 55 | |
| 56 | def stampcleanmask(self, stampbase, file_name, taskname, extrainfo): |
| 57 | return ("%s.%s.%s" % (stampbase, taskname, extrainfo)).rstrip('.') |
| 58 | |
| 59 | def dump_sigtask(self, fn, task, stampbase, runtime): |
| 60 | return |
| 61 | |
| 62 | def invalidate_task(self, task, d, fn): |
| 63 | bb.build.del_stamp(task, d, fn) |
| 64 | |
| 65 | def dump_sigs(self, dataCache, options): |
| 66 | return |
| 67 | |
| 68 | def get_taskdata(self): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 69 | return (self.runtaskdeps, self.taskhash, self.file_checksum_values, self.taints) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 70 | |
| 71 | def set_taskdata(self, data): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 72 | self.runtaskdeps, self.taskhash, self.file_checksum_values, self.taints = data |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | |
| 74 | |
| 75 | class SignatureGeneratorBasic(SignatureGenerator): |
| 76 | """ |
| 77 | """ |
| 78 | name = "basic" |
| 79 | |
| 80 | def __init__(self, data): |
| 81 | self.basehash = {} |
| 82 | self.taskhash = {} |
| 83 | self.taskdeps = {} |
| 84 | self.runtaskdeps = {} |
| 85 | self.file_checksum_values = {} |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 86 | self.taints = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | self.gendeps = {} |
| 88 | self.lookupcache = {} |
| 89 | self.pkgnameextract = re.compile("(?P<fn>.*)\..*") |
| 90 | self.basewhitelist = set((data.getVar("BB_HASHBASE_WHITELIST", True) or "").split()) |
| 91 | self.taskwhitelist = None |
| 92 | self.init_rundepcheck(data) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 93 | checksum_cache_file = data.getVar("BB_HASH_CHECKSUM_CACHE_FILE", True) |
| 94 | if checksum_cache_file: |
| 95 | self.checksum_cache = FileChecksumCache() |
| 96 | self.checksum_cache.init_cache(data, checksum_cache_file) |
| 97 | else: |
| 98 | self.checksum_cache = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 99 | |
| 100 | def init_rundepcheck(self, data): |
| 101 | self.taskwhitelist = data.getVar("BB_HASHTASK_WHITELIST", True) or None |
| 102 | if self.taskwhitelist: |
| 103 | self.twl = re.compile(self.taskwhitelist) |
| 104 | else: |
| 105 | self.twl = None |
| 106 | |
| 107 | def _build_data(self, fn, d): |
| 108 | |
| 109 | tasklist, gendeps, lookupcache = bb.data.generate_dependencies(d) |
| 110 | |
| 111 | taskdeps = {} |
| 112 | basehash = {} |
| 113 | |
| 114 | for task in tasklist: |
| 115 | data = lookupcache[task] |
| 116 | |
| 117 | if data is None: |
| 118 | bb.error("Task %s from %s seems to be empty?!" % (task, fn)) |
| 119 | data = '' |
| 120 | |
| 121 | gendeps[task] -= self.basewhitelist |
| 122 | newdeps = gendeps[task] |
| 123 | seen = set() |
| 124 | while newdeps: |
| 125 | nextdeps = newdeps |
| 126 | seen |= nextdeps |
| 127 | newdeps = set() |
| 128 | for dep in nextdeps: |
| 129 | if dep in self.basewhitelist: |
| 130 | continue |
| 131 | gendeps[dep] -= self.basewhitelist |
| 132 | newdeps |= gendeps[dep] |
| 133 | newdeps -= seen |
| 134 | |
| 135 | alldeps = sorted(seen) |
| 136 | for dep in alldeps: |
| 137 | data = data + dep |
| 138 | var = lookupcache[dep] |
| 139 | if var is not None: |
| 140 | data = data + str(var) |
| 141 | self.basehash[fn + "." + task] = hashlib.md5(data).hexdigest() |
| 142 | taskdeps[task] = alldeps |
| 143 | |
| 144 | self.taskdeps[fn] = taskdeps |
| 145 | self.gendeps[fn] = gendeps |
| 146 | self.lookupcache[fn] = lookupcache |
| 147 | |
| 148 | return taskdeps |
| 149 | |
| 150 | def finalise(self, fn, d, variant): |
| 151 | |
| 152 | if variant: |
| 153 | fn = "virtual:" + variant + ":" + fn |
| 154 | |
| 155 | try: |
| 156 | taskdeps = self._build_data(fn, d) |
| 157 | except: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 158 | bb.warn("Error during finalise of %s" % fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 159 | raise |
| 160 | |
| 161 | #Slow but can be useful for debugging mismatched basehashes |
| 162 | #for task in self.taskdeps[fn]: |
| 163 | # self.dump_sigtask(fn, task, d.getVar("STAMP", True), False) |
| 164 | |
| 165 | for task in taskdeps: |
| 166 | d.setVar("BB_BASEHASH_task-%s" % task, self.basehash[fn + "." + task]) |
| 167 | |
| 168 | def rundep_check(self, fn, recipename, task, dep, depname, dataCache): |
| 169 | # Return True if we should keep the dependency, False to drop it |
| 170 | # We only manipulate the dependencies for packages not in the whitelist |
| 171 | if self.twl and not self.twl.search(recipename): |
| 172 | # then process the actual dependencies |
| 173 | if self.twl.search(depname): |
| 174 | return False |
| 175 | return True |
| 176 | |
| 177 | def read_taint(self, fn, task, stampbase): |
| 178 | taint = None |
| 179 | try: |
| 180 | with open(stampbase + '.' + task + '.taint', 'r') as taintf: |
| 181 | taint = taintf.read() |
| 182 | except IOError: |
| 183 | pass |
| 184 | return taint |
| 185 | |
| 186 | def get_taskhash(self, fn, task, deps, dataCache): |
| 187 | k = fn + "." + task |
| 188 | data = dataCache.basetaskhash[k] |
| 189 | self.runtaskdeps[k] = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 190 | self.file_checksum_values[k] = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 191 | recipename = dataCache.pkg_fn[fn] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 192 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 193 | for dep in sorted(deps, key=clean_basepath): |
| 194 | depname = dataCache.pkg_fn[self.pkgnameextract.search(dep).group('fn')] |
| 195 | if not self.rundep_check(fn, recipename, task, dep, depname, dataCache): |
| 196 | continue |
| 197 | if dep not in self.taskhash: |
| 198 | bb.fatal("%s is not in taskhash, caller isn't calling in dependency order?", dep) |
| 199 | data = data + self.taskhash[dep] |
| 200 | self.runtaskdeps[k].append(dep) |
| 201 | |
| 202 | if task in dataCache.file_checksums[fn]: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 203 | if self.checksum_cache: |
| 204 | checksums = self.checksum_cache.get_checksums(dataCache.file_checksums[fn][task], recipename) |
| 205 | else: |
| 206 | checksums = bb.fetch2.get_file_checksums(dataCache.file_checksums[fn][task], recipename) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 207 | for (f,cs) in checksums: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 208 | self.file_checksum_values[k].append((f,cs)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 209 | if cs: |
| 210 | data = data + cs |
| 211 | |
| 212 | taskdep = dataCache.task_deps[fn] |
| 213 | if 'nostamp' in taskdep and task in taskdep['nostamp']: |
| 214 | # Nostamp tasks need an implicit taint so that they force any dependent tasks to run |
| 215 | import uuid |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 216 | taint = str(uuid.uuid4()) |
| 217 | data = data + taint |
| 218 | self.taints[k] = "nostamp:" + taint |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 219 | |
| 220 | taint = self.read_taint(fn, task, dataCache.stamp[fn]) |
| 221 | if taint: |
| 222 | data = data + taint |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 223 | self.taints[k] = taint |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 224 | logger.warn("%s is tainted from a forced run" % k) |
| 225 | |
| 226 | h = hashlib.md5(data).hexdigest() |
| 227 | self.taskhash[k] = h |
| 228 | #d.setVar("BB_TASKHASH_task-%s" % task, taskhash[task]) |
| 229 | return h |
| 230 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 231 | def writeout_file_checksum_cache(self): |
| 232 | """Write/update the file checksum cache onto disk""" |
| 233 | if self.checksum_cache: |
| 234 | self.checksum_cache.save_extras() |
| 235 | self.checksum_cache.save_merge() |
| 236 | else: |
| 237 | bb.fetch2.fetcher_parse_save() |
| 238 | bb.fetch2.fetcher_parse_done() |
| 239 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 240 | def dump_sigtask(self, fn, task, stampbase, runtime): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 241 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 242 | k = fn + "." + task |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 243 | referencestamp = stampbase |
| 244 | if isinstance(runtime, str) and runtime.startswith("customfile"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 245 | sigfile = stampbase |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 246 | referencestamp = runtime[11:] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 247 | elif runtime and k in self.taskhash: |
| 248 | sigfile = stampbase + "." + task + ".sigdata" + "." + self.taskhash[k] |
| 249 | else: |
| 250 | sigfile = stampbase + "." + task + ".sigbasedata" + "." + self.basehash[k] |
| 251 | |
| 252 | bb.utils.mkdirhier(os.path.dirname(sigfile)) |
| 253 | |
| 254 | data = {} |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 255 | data['task'] = task |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 256 | data['basewhitelist'] = self.basewhitelist |
| 257 | data['taskwhitelist'] = self.taskwhitelist |
| 258 | data['taskdeps'] = self.taskdeps[fn][task] |
| 259 | data['basehash'] = self.basehash[k] |
| 260 | data['gendeps'] = {} |
| 261 | data['varvals'] = {} |
| 262 | data['varvals'][task] = self.lookupcache[fn][task] |
| 263 | for dep in self.taskdeps[fn][task]: |
| 264 | if dep in self.basewhitelist: |
| 265 | continue |
| 266 | data['gendeps'][dep] = self.gendeps[fn][dep] |
| 267 | data['varvals'][dep] = self.lookupcache[fn][dep] |
| 268 | |
| 269 | if runtime and k in self.taskhash: |
| 270 | data['runtaskdeps'] = self.runtaskdeps[k] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 271 | data['file_checksum_values'] = [(os.path.basename(f), cs) for f,cs in self.file_checksum_values[k]] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 272 | data['runtaskhashes'] = {} |
| 273 | for dep in data['runtaskdeps']: |
| 274 | data['runtaskhashes'][dep] = self.taskhash[dep] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 275 | data['taskhash'] = self.taskhash[k] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 276 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 277 | taint = self.read_taint(fn, task, referencestamp) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 278 | if taint: |
| 279 | data['taint'] = taint |
| 280 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 281 | if runtime and k in self.taints: |
| 282 | if 'nostamp:' in self.taints[k]: |
| 283 | data['taint'] = self.taints[k] |
| 284 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 285 | fd, tmpfile = tempfile.mkstemp(dir=os.path.dirname(sigfile), prefix="sigtask.") |
| 286 | try: |
| 287 | with os.fdopen(fd, "wb") as stream: |
| 288 | p = pickle.dump(data, stream, -1) |
| 289 | stream.flush() |
| 290 | os.chmod(tmpfile, 0664) |
| 291 | os.rename(tmpfile, sigfile) |
| 292 | except (OSError, IOError) as err: |
| 293 | try: |
| 294 | os.unlink(tmpfile) |
| 295 | except OSError: |
| 296 | pass |
| 297 | raise err |
| 298 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 299 | computed_basehash = calc_basehash(data) |
| 300 | if computed_basehash != self.basehash[k]: |
| 301 | bb.error("Basehash mismatch %s verses %s for %s" % (computed_basehash, self.basehash[k], k)) |
| 302 | if k in self.taskhash: |
| 303 | computed_taskhash = calc_taskhash(data) |
| 304 | if computed_taskhash != self.taskhash[k]: |
| 305 | bb.error("Taskhash mismatch %s verses %s for %s" % (computed_taskhash, self.taskhash[k], k)) |
| 306 | |
| 307 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 308 | def dump_sigs(self, dataCache, options): |
| 309 | for fn in self.taskdeps: |
| 310 | for task in self.taskdeps[fn]: |
| 311 | k = fn + "." + task |
| 312 | if k not in self.taskhash: |
| 313 | continue |
| 314 | if dataCache.basetaskhash[k] != self.basehash[k]: |
| 315 | bb.error("Bitbake's cached basehash does not match the one we just generated (%s)!" % k) |
| 316 | bb.error("The mismatched hashes were %s and %s" % (dataCache.basetaskhash[k], self.basehash[k])) |
| 317 | self.dump_sigtask(fn, task, dataCache.stamp[fn], True) |
| 318 | |
| 319 | class SignatureGeneratorBasicHash(SignatureGeneratorBasic): |
| 320 | name = "basichash" |
| 321 | |
| 322 | def stampfile(self, stampbase, fn, taskname, extrainfo, clean=False): |
| 323 | if taskname != "do_setscene" and taskname.endswith("_setscene"): |
| 324 | k = fn + "." + taskname[:-9] |
| 325 | else: |
| 326 | k = fn + "." + taskname |
| 327 | if clean: |
| 328 | h = "*" |
| 329 | elif k in self.taskhash: |
| 330 | h = self.taskhash[k] |
| 331 | else: |
| 332 | # If k is not in basehash, then error |
| 333 | h = self.basehash[k] |
| 334 | return ("%s.%s.%s.%s" % (stampbase, taskname, h, extrainfo)).rstrip('.') |
| 335 | |
| 336 | def stampcleanmask(self, stampbase, fn, taskname, extrainfo): |
| 337 | return self.stampfile(stampbase, fn, taskname, extrainfo, clean=True) |
| 338 | |
| 339 | def invalidate_task(self, task, d, fn): |
| 340 | bb.note("Tainting hash to force rebuild of task %s, %s" % (fn, task)) |
| 341 | bb.build.write_taint(task, d, fn) |
| 342 | |
| 343 | def dump_this_task(outfile, d): |
| 344 | import bb.parse |
| 345 | fn = d.getVar("BB_FILENAME", True) |
| 346 | task = "do_" + d.getVar("BB_CURRENTTASK", True) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 347 | referencestamp = bb.build.stamp_internal(task, d, None, True) |
| 348 | bb.parse.siggen.dump_sigtask(fn, task, outfile, "customfile:" + referencestamp) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 349 | |
| 350 | def clean_basepath(a): |
| 351 | b = a.rsplit("/", 2)[1] + a.rsplit("/", 2)[2] |
| 352 | if a.startswith("virtual:"): |
| 353 | b = b + ":" + a.rsplit(":", 1)[0] |
| 354 | return b |
| 355 | |
| 356 | def clean_basepaths(a): |
| 357 | b = {} |
| 358 | for x in a: |
| 359 | b[clean_basepath(x)] = a[x] |
| 360 | return b |
| 361 | |
| 362 | def clean_basepaths_list(a): |
| 363 | b = [] |
| 364 | for x in a: |
| 365 | b.append(clean_basepath(x)) |
| 366 | return b |
| 367 | |
| 368 | def compare_sigfiles(a, b, recursecb = None): |
| 369 | output = [] |
| 370 | |
| 371 | p1 = pickle.Unpickler(open(a, "rb")) |
| 372 | a_data = p1.load() |
| 373 | p2 = pickle.Unpickler(open(b, "rb")) |
| 374 | b_data = p2.load() |
| 375 | |
| 376 | def dict_diff(a, b, whitelist=set()): |
| 377 | sa = set(a.keys()) |
| 378 | sb = set(b.keys()) |
| 379 | common = sa & sb |
| 380 | changed = set() |
| 381 | for i in common: |
| 382 | if a[i] != b[i] and i not in whitelist: |
| 383 | changed.add(i) |
| 384 | added = sb - sa |
| 385 | removed = sa - sb |
| 386 | return changed, added, removed |
| 387 | |
| 388 | def file_checksums_diff(a, b): |
| 389 | from collections import Counter |
| 390 | # Handle old siginfo format |
| 391 | if isinstance(a, dict): |
| 392 | a = [(os.path.basename(f), cs) for f, cs in a.items()] |
| 393 | if isinstance(b, dict): |
| 394 | b = [(os.path.basename(f), cs) for f, cs in b.items()] |
| 395 | # Compare lists, ensuring we can handle duplicate filenames if they exist |
| 396 | removedcount = Counter(a) |
| 397 | removedcount.subtract(b) |
| 398 | addedcount = Counter(b) |
| 399 | addedcount.subtract(a) |
| 400 | added = [] |
| 401 | for x in b: |
| 402 | if addedcount[x] > 0: |
| 403 | addedcount[x] -= 1 |
| 404 | added.append(x) |
| 405 | removed = [] |
| 406 | changed = [] |
| 407 | for x in a: |
| 408 | if removedcount[x] > 0: |
| 409 | removedcount[x] -= 1 |
| 410 | for y in added: |
| 411 | if y[0] == x[0]: |
| 412 | changed.append((x[0], x[1], y[1])) |
| 413 | added.remove(y) |
| 414 | break |
| 415 | else: |
| 416 | removed.append(x) |
| 417 | added = [x[0] for x in added] |
| 418 | removed = [x[0] for x in removed] |
| 419 | return changed, added, removed |
| 420 | |
| 421 | if 'basewhitelist' in a_data and a_data['basewhitelist'] != b_data['basewhitelist']: |
| 422 | output.append("basewhitelist changed from '%s' to '%s'" % (a_data['basewhitelist'], b_data['basewhitelist'])) |
| 423 | if a_data['basewhitelist'] and b_data['basewhitelist']: |
| 424 | output.append("changed items: %s" % a_data['basewhitelist'].symmetric_difference(b_data['basewhitelist'])) |
| 425 | |
| 426 | if 'taskwhitelist' in a_data and a_data['taskwhitelist'] != b_data['taskwhitelist']: |
| 427 | output.append("taskwhitelist changed from '%s' to '%s'" % (a_data['taskwhitelist'], b_data['taskwhitelist'])) |
| 428 | if a_data['taskwhitelist'] and b_data['taskwhitelist']: |
| 429 | output.append("changed items: %s" % a_data['taskwhitelist'].symmetric_difference(b_data['taskwhitelist'])) |
| 430 | |
| 431 | if a_data['taskdeps'] != b_data['taskdeps']: |
| 432 | output.append("Task dependencies changed from:\n%s\nto:\n%s" % (sorted(a_data['taskdeps']), sorted(b_data['taskdeps']))) |
| 433 | |
| 434 | if a_data['basehash'] != b_data['basehash']: |
| 435 | output.append("basehash changed from %s to %s" % (a_data['basehash'], b_data['basehash'])) |
| 436 | |
| 437 | changed, added, removed = dict_diff(a_data['gendeps'], b_data['gendeps'], a_data['basewhitelist'] & b_data['basewhitelist']) |
| 438 | if changed: |
| 439 | for dep in changed: |
| 440 | output.append("List of dependencies for variable %s changed from '%s' to '%s'" % (dep, a_data['gendeps'][dep], b_data['gendeps'][dep])) |
| 441 | if a_data['gendeps'][dep] and b_data['gendeps'][dep]: |
| 442 | output.append("changed items: %s" % a_data['gendeps'][dep].symmetric_difference(b_data['gendeps'][dep])) |
| 443 | if added: |
| 444 | for dep in added: |
| 445 | output.append("Dependency on variable %s was added" % (dep)) |
| 446 | if removed: |
| 447 | for dep in removed: |
| 448 | output.append("Dependency on Variable %s was removed" % (dep)) |
| 449 | |
| 450 | |
| 451 | changed, added, removed = dict_diff(a_data['varvals'], b_data['varvals']) |
| 452 | if changed: |
| 453 | for dep in changed: |
| 454 | output.append("Variable %s value changed from '%s' to '%s'" % (dep, a_data['varvals'][dep], b_data['varvals'][dep])) |
| 455 | |
| 456 | changed, added, removed = file_checksums_diff(a_data['file_checksum_values'], b_data['file_checksum_values']) |
| 457 | if changed: |
| 458 | for f, old, new in changed: |
| 459 | output.append("Checksum for file %s changed from %s to %s" % (f, old, new)) |
| 460 | if added: |
| 461 | for f in added: |
| 462 | output.append("Dependency on checksum of file %s was added" % (f)) |
| 463 | if removed: |
| 464 | for f in removed: |
| 465 | output.append("Dependency on checksum of file %s was removed" % (f)) |
| 466 | |
| 467 | |
| 468 | if len(a_data['runtaskdeps']) != len(b_data['runtaskdeps']): |
| 469 | changed = ["Number of task dependencies changed"] |
| 470 | else: |
| 471 | changed = [] |
| 472 | for idx, task in enumerate(a_data['runtaskdeps']): |
| 473 | a = a_data['runtaskdeps'][idx] |
| 474 | b = b_data['runtaskdeps'][idx] |
| 475 | if a_data['runtaskhashes'][a] != b_data['runtaskhashes'][b]: |
| 476 | changed.append("%s with hash %s\n changed to\n%s with hash %s" % (a, a_data['runtaskhashes'][a], b, b_data['runtaskhashes'][b])) |
| 477 | |
| 478 | if changed: |
| 479 | output.append("runtaskdeps changed from %s to %s" % (clean_basepaths_list(a_data['runtaskdeps']), clean_basepaths_list(b_data['runtaskdeps']))) |
| 480 | output.append("\n".join(changed)) |
| 481 | |
| 482 | |
| 483 | if 'runtaskhashes' in a_data and 'runtaskhashes' in b_data: |
| 484 | a = a_data['runtaskhashes'] |
| 485 | b = b_data['runtaskhashes'] |
| 486 | changed, added, removed = dict_diff(a, b) |
| 487 | if added: |
| 488 | for dep in added: |
| 489 | bdep_found = False |
| 490 | if removed: |
| 491 | for bdep in removed: |
| 492 | if b[dep] == a[bdep]: |
| 493 | #output.append("Dependency on task %s was replaced by %s with same hash" % (dep, bdep)) |
| 494 | bdep_found = True |
| 495 | if not bdep_found: |
| 496 | output.append("Dependency on task %s was added with hash %s" % (clean_basepath(dep), b[dep])) |
| 497 | if removed: |
| 498 | for dep in removed: |
| 499 | adep_found = False |
| 500 | if added: |
| 501 | for adep in added: |
| 502 | if b[adep] == a[dep]: |
| 503 | #output.append("Dependency on task %s was replaced by %s with same hash" % (adep, dep)) |
| 504 | adep_found = True |
| 505 | if not adep_found: |
| 506 | output.append("Dependency on task %s was removed with hash %s" % (clean_basepath(dep), a[dep])) |
| 507 | if changed: |
| 508 | for dep in changed: |
| 509 | output.append("Hash for dependent task %s changed from %s to %s" % (clean_basepath(dep), a[dep], b[dep])) |
| 510 | if callable(recursecb): |
| 511 | # If a dependent hash changed, might as well print the line above and then defer to the changes in |
| 512 | # that hash since in all likelyhood, they're the same changes this task also saw. |
| 513 | recout = recursecb(dep, a[dep], b[dep]) |
| 514 | if recout: |
| 515 | output = [output[-1]] + recout |
| 516 | |
| 517 | a_taint = a_data.get('taint', None) |
| 518 | b_taint = b_data.get('taint', None) |
| 519 | if a_taint != b_taint: |
| 520 | output.append("Taint (by forced/invalidated task) changed from %s to %s" % (a_taint, b_taint)) |
| 521 | |
| 522 | return output |
| 523 | |
| 524 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 525 | def calc_basehash(sigdata): |
| 526 | task = sigdata['task'] |
| 527 | basedata = sigdata['varvals'][task] |
| 528 | |
| 529 | if basedata is None: |
| 530 | basedata = '' |
| 531 | |
| 532 | alldeps = sigdata['taskdeps'] |
| 533 | for dep in alldeps: |
| 534 | basedata = basedata + dep |
| 535 | val = sigdata['varvals'][dep] |
| 536 | if val is not None: |
| 537 | basedata = basedata + str(val) |
| 538 | |
| 539 | return hashlib.md5(basedata).hexdigest() |
| 540 | |
| 541 | def calc_taskhash(sigdata): |
| 542 | data = sigdata['basehash'] |
| 543 | |
| 544 | for dep in sigdata['runtaskdeps']: |
| 545 | data = data + sigdata['runtaskhashes'][dep] |
| 546 | |
| 547 | for c in sigdata['file_checksum_values']: |
| 548 | data = data + c[1] |
| 549 | |
| 550 | if 'taint' in sigdata: |
| 551 | if 'nostamp:' in sigdata['taint']: |
| 552 | data = data + sigdata['taint'][8:] |
| 553 | else: |
| 554 | data = data + sigdata['taint'] |
| 555 | |
| 556 | return hashlib.md5(data).hexdigest() |
| 557 | |
| 558 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 559 | def dump_sigfile(a): |
| 560 | output = [] |
| 561 | |
| 562 | p1 = pickle.Unpickler(open(a, "rb")) |
| 563 | a_data = p1.load() |
| 564 | |
| 565 | output.append("basewhitelist: %s" % (a_data['basewhitelist'])) |
| 566 | |
| 567 | output.append("taskwhitelist: %s" % (a_data['taskwhitelist'])) |
| 568 | |
| 569 | output.append("Task dependencies: %s" % (sorted(a_data['taskdeps']))) |
| 570 | |
| 571 | output.append("basehash: %s" % (a_data['basehash'])) |
| 572 | |
| 573 | for dep in a_data['gendeps']: |
| 574 | output.append("List of dependencies for variable %s is %s" % (dep, a_data['gendeps'][dep])) |
| 575 | |
| 576 | for dep in a_data['varvals']: |
| 577 | output.append("Variable %s value is %s" % (dep, a_data['varvals'][dep])) |
| 578 | |
| 579 | if 'runtaskdeps' in a_data: |
| 580 | output.append("Tasks this task depends on: %s" % (a_data['runtaskdeps'])) |
| 581 | |
| 582 | if 'file_checksum_values' in a_data: |
| 583 | output.append("This task depends on the checksums of files: %s" % (a_data['file_checksum_values'])) |
| 584 | |
| 585 | if 'runtaskhashes' in a_data: |
| 586 | for dep in a_data['runtaskhashes']: |
| 587 | output.append("Hash for dependent task %s is %s" % (dep, a_data['runtaskhashes'][dep])) |
| 588 | |
| 589 | if 'taint' in a_data: |
| 590 | output.append("Tainted (by forced/invalidated task): %s" % a_data['taint']) |
| 591 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 592 | if 'task' in a_data: |
| 593 | computed_basehash = calc_basehash(a_data) |
| 594 | output.append("Computed base hash is %s and from file %s" % (computed_basehash, a_data['basehash'])) |
| 595 | else: |
| 596 | output.append("Unable to compute base hash") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 597 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 598 | computed_taskhash = calc_taskhash(a_data) |
| 599 | output.append("Computed task hash is %s" % computed_taskhash) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 600 | |
| 601 | return output |