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