Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # Report significant differences in the buildhistory repository since a specific revision |
| 2 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 3 | # Copyright (C) 2012-2013, 2016-2017 Intel Corporation |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 4 | # Author: Paul Eggleton <paul.eggleton@linux.intel.com> |
| 5 | # |
| 6 | # Note: requires GitPython 0.3.1+ |
| 7 | # |
| 8 | # You can use this from the command line by running scripts/buildhistory-diff |
| 9 | # |
| 10 | |
| 11 | import sys |
| 12 | import os.path |
| 13 | import difflib |
| 14 | import git |
| 15 | import re |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 16 | import shlex |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 17 | import hashlib |
| 18 | import collections |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | import bb.utils |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 20 | import bb.tinfoil |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | |
| 22 | |
| 23 | # How to display fields |
| 24 | list_fields = ['DEPENDS', 'RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RREPLACES', 'RCONFLICTS', 'FILES', 'FILELIST', 'USER_CLASSES', 'IMAGE_CLASSES', 'IMAGE_FEATURES', 'IMAGE_LINGUAS', 'IMAGE_INSTALL', 'BAD_RECOMMENDATIONS', 'PACKAGE_EXCLUDE'] |
| 25 | list_order_fields = ['PACKAGES'] |
| 26 | defaultval_map = {'PKG': 'PKG', 'PKGE': 'PE', 'PKGV': 'PV', 'PKGR': 'PR'} |
| 27 | numeric_fields = ['PKGSIZE', 'IMAGESIZE'] |
| 28 | # Fields to monitor |
| 29 | monitor_fields = ['RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RREPLACES', 'RCONFLICTS', 'PACKAGES', 'FILELIST', 'PKGSIZE', 'IMAGESIZE', 'PKG'] |
| 30 | ver_monitor_fields = ['PKGE', 'PKGV', 'PKGR'] |
| 31 | # Percentage change to alert for numeric fields |
| 32 | monitor_numeric_threshold = 10 |
| 33 | # Image files to monitor (note that image-info.txt is handled separately) |
| 34 | img_monitor_files = ['installed-package-names.txt', 'files-in-image.txt'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 36 | colours = { |
| 37 | 'colour_default': '', |
| 38 | 'colour_add': '', |
| 39 | 'colour_remove': '', |
| 40 | } |
| 41 | |
| 42 | def init_colours(use_colours): |
| 43 | global colours |
| 44 | if use_colours: |
| 45 | colours = { |
| 46 | 'colour_default': '\033[0m', |
| 47 | 'colour_add': '\033[1;32m', |
| 48 | 'colour_remove': '\033[1;31m', |
| 49 | } |
| 50 | else: |
| 51 | colours = { |
| 52 | 'colour_default': '', |
| 53 | 'colour_add': '', |
| 54 | 'colour_remove': '', |
| 55 | } |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | |
| 57 | class ChangeRecord: |
| 58 | def __init__(self, path, fieldname, oldvalue, newvalue, monitored): |
| 59 | self.path = path |
| 60 | self.fieldname = fieldname |
| 61 | self.oldvalue = oldvalue |
| 62 | self.newvalue = newvalue |
| 63 | self.monitored = monitored |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | self.filechanges = None |
| 65 | |
| 66 | def __str__(self): |
| 67 | return self._str_internal(True) |
| 68 | |
| 69 | def _str_internal(self, outer): |
| 70 | if outer: |
| 71 | if '/image-files/' in self.path: |
| 72 | prefix = '%s: ' % self.path.split('/image-files/')[0] |
| 73 | else: |
| 74 | prefix = '%s: ' % self.path |
| 75 | else: |
| 76 | prefix = '' |
| 77 | |
| 78 | def pkglist_combine(depver): |
| 79 | pkglist = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 80 | for k,v in depver.items(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | if v: |
| 82 | pkglist.append("%s (%s)" % (k,v)) |
| 83 | else: |
| 84 | pkglist.append(k) |
| 85 | return pkglist |
| 86 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 87 | def detect_renamed_dirs(aitems, bitems): |
| 88 | adirs = set(map(os.path.dirname, aitems)) |
| 89 | bdirs = set(map(os.path.dirname, bitems)) |
| 90 | files_ab = [(name, sorted(os.path.basename(item) for item in aitems if os.path.dirname(item) == name)) \ |
| 91 | for name in adirs - bdirs] |
| 92 | files_ba = [(name, sorted(os.path.basename(item) for item in bitems if os.path.dirname(item) == name)) \ |
| 93 | for name in bdirs - adirs] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 94 | renamed_dirs = [] |
| 95 | for dir1, files1 in files_ab: |
| 96 | rename = False |
| 97 | for dir2, files2 in files_ba: |
| 98 | if files1 == files2 and not rename: |
| 99 | renamed_dirs.append((dir1,dir2)) |
| 100 | # Make sure that we don't use this (dir, files) pair again. |
| 101 | files_ba.remove((dir2,files2)) |
| 102 | # If a dir has already been found to have a rename, stop and go no further. |
| 103 | rename = True |
| 104 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 105 | # remove files that belong to renamed dirs from aitems and bitems |
| 106 | for dir1, dir2 in renamed_dirs: |
| 107 | aitems = [item for item in aitems if os.path.dirname(item) not in (dir1, dir2)] |
| 108 | bitems = [item for item in bitems if os.path.dirname(item) not in (dir1, dir2)] |
| 109 | return renamed_dirs, aitems, bitems |
| 110 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | if self.fieldname in list_fields or self.fieldname in list_order_fields: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | renamed_dirs = [] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 113 | changed_order = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 | if self.fieldname in ['RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RREPLACES', 'RCONFLICTS']: |
| 115 | (depvera, depverb) = compare_pkg_lists(self.oldvalue, self.newvalue) |
| 116 | aitems = pkglist_combine(depvera) |
| 117 | bitems = pkglist_combine(depverb) |
| 118 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 119 | if self.fieldname == 'FILELIST': |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 120 | aitems = shlex.split(self.oldvalue) |
| 121 | bitems = shlex.split(self.newvalue) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 122 | renamed_dirs, aitems, bitems = detect_renamed_dirs(aitems, bitems) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 123 | else: |
| 124 | aitems = self.oldvalue.split() |
| 125 | bitems = self.newvalue.split() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 126 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 127 | removed = list(set(aitems) - set(bitems)) |
| 128 | added = list(set(bitems) - set(aitems)) |
| 129 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 130 | if not removed and not added: |
| 131 | depvera = bb.utils.explode_dep_versions2(self.oldvalue, sort=False) |
| 132 | depverb = bb.utils.explode_dep_versions2(self.newvalue, sort=False) |
| 133 | for i, j in zip(depvera.items(), depverb.items()): |
| 134 | if i[0] != j[0]: |
| 135 | changed_order = True |
| 136 | break |
| 137 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 138 | lines = [] |
| 139 | if renamed_dirs: |
| 140 | for dfrom, dto in renamed_dirs: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 141 | lines.append('directory renamed {colour_remove}{}{colour_default} -> {colour_add}{}{colour_default}'.format(dfrom, dto, **colours)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 142 | if removed or added: |
| 143 | if removed and not bitems: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 144 | lines.append('removed all items "{colour_remove}{}{colour_default}"'.format(' '.join(removed), **colours)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 145 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 146 | if removed: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 147 | lines.append('removed "{colour_remove}{value}{colour_default}"'.format(value=' '.join(removed), **colours)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 148 | if added: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 149 | lines.append('added "{colour_add}{value}{colour_default}"'.format(value=' '.join(added), **colours)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 150 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 151 | lines.append('changed order') |
| 152 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 153 | if not (removed or added or changed_order): |
| 154 | out = '' |
| 155 | else: |
| 156 | out = '%s: %s' % (self.fieldname, ', '.join(lines)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 157 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | elif self.fieldname in numeric_fields: |
| 159 | aval = int(self.oldvalue or 0) |
| 160 | bval = int(self.newvalue or 0) |
| 161 | if aval != 0: |
| 162 | percentchg = ((bval - aval) / float(aval)) * 100 |
| 163 | else: |
| 164 | percentchg = 100 |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 165 | out = '{} changed from {colour_remove}{}{colour_default} to {colour_add}{}{colour_default} ({}{:.0f}%)'.format(self.fieldname, self.oldvalue or "''", self.newvalue or "''", '+' if percentchg > 0 else '', percentchg, **colours) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | elif self.fieldname in defaultval_map: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 167 | out = '{} changed from {colour_remove}{}{colour_default} to {colour_add}{}{colour_default}'.format(self.fieldname, self.oldvalue, self.newvalue, **colours) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | if self.fieldname == 'PKG' and '[default]' in self.newvalue: |
| 169 | out += ' - may indicate debian renaming failure' |
| 170 | elif self.fieldname in ['pkg_preinst', 'pkg_postinst', 'pkg_prerm', 'pkg_postrm']: |
| 171 | if self.oldvalue and self.newvalue: |
| 172 | out = '%s changed:\n ' % self.fieldname |
| 173 | elif self.newvalue: |
| 174 | out = '%s added:\n ' % self.fieldname |
| 175 | elif self.oldvalue: |
| 176 | out = '%s cleared:\n ' % self.fieldname |
| 177 | alines = self.oldvalue.splitlines() |
| 178 | blines = self.newvalue.splitlines() |
| 179 | diff = difflib.unified_diff(alines, blines, self.fieldname, self.fieldname, lineterm='') |
| 180 | out += '\n '.join(list(diff)[2:]) |
| 181 | out += '\n --' |
| 182 | elif self.fieldname in img_monitor_files or '/image-files/' in self.path: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 183 | if self.filechanges or (self.oldvalue and self.newvalue): |
| 184 | fieldname = self.fieldname |
| 185 | if '/image-files/' in self.path: |
| 186 | fieldname = os.path.join('/' + self.path.split('/image-files/')[1], self.fieldname) |
| 187 | out = 'Changes to %s:\n ' % fieldname |
| 188 | else: |
| 189 | if outer: |
| 190 | prefix = 'Changes to %s ' % self.path |
| 191 | out = '(%s):\n ' % self.fieldname |
| 192 | if self.filechanges: |
| 193 | out += '\n '.join(['%s' % i for i in self.filechanges]) |
| 194 | else: |
| 195 | alines = self.oldvalue.splitlines() |
| 196 | blines = self.newvalue.splitlines() |
| 197 | diff = difflib.unified_diff(alines, blines, fieldname, fieldname, lineterm='') |
| 198 | out += '\n '.join(list(diff)) |
| 199 | out += '\n --' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 200 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 201 | out = '' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 202 | else: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 203 | out = '{} changed from "{colour_remove}{}{colour_default}" to "{colour_add}{}{colour_default}"'.format(self.fieldname, self.oldvalue, self.newvalue, **colours) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 204 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 205 | return '%s%s' % (prefix, out) if out else '' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 206 | |
| 207 | class FileChange: |
| 208 | changetype_add = 'A' |
| 209 | changetype_remove = 'R' |
| 210 | changetype_type = 'T' |
| 211 | changetype_perms = 'P' |
| 212 | changetype_ownergroup = 'O' |
| 213 | changetype_link = 'L' |
| 214 | |
| 215 | def __init__(self, path, changetype, oldvalue = None, newvalue = None): |
| 216 | self.path = path |
| 217 | self.changetype = changetype |
| 218 | self.oldvalue = oldvalue |
| 219 | self.newvalue = newvalue |
| 220 | |
| 221 | def _ftype_str(self, ftype): |
| 222 | if ftype == '-': |
| 223 | return 'file' |
| 224 | elif ftype == 'd': |
| 225 | return 'directory' |
| 226 | elif ftype == 'l': |
| 227 | return 'symlink' |
| 228 | elif ftype == 'c': |
| 229 | return 'char device' |
| 230 | elif ftype == 'b': |
| 231 | return 'block device' |
| 232 | elif ftype == 'p': |
| 233 | return 'fifo' |
| 234 | elif ftype == 's': |
| 235 | return 'socket' |
| 236 | else: |
| 237 | return 'unknown (%s)' % ftype |
| 238 | |
| 239 | def __str__(self): |
| 240 | if self.changetype == self.changetype_add: |
| 241 | return '%s was added' % self.path |
| 242 | elif self.changetype == self.changetype_remove: |
| 243 | return '%s was removed' % self.path |
| 244 | elif self.changetype == self.changetype_type: |
| 245 | return '%s changed type from %s to %s' % (self.path, self._ftype_str(self.oldvalue), self._ftype_str(self.newvalue)) |
| 246 | elif self.changetype == self.changetype_perms: |
| 247 | return '%s changed permissions from %s to %s' % (self.path, self.oldvalue, self.newvalue) |
| 248 | elif self.changetype == self.changetype_ownergroup: |
| 249 | return '%s changed owner/group from %s to %s' % (self.path, self.oldvalue, self.newvalue) |
| 250 | elif self.changetype == self.changetype_link: |
| 251 | return '%s changed symlink target from %s to %s' % (self.path, self.oldvalue, self.newvalue) |
| 252 | else: |
| 253 | return '%s changed (unknown)' % self.path |
| 254 | |
| 255 | |
| 256 | def blob_to_dict(blob): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 257 | alines = [line for line in blob.data_stream.read().decode('utf-8').splitlines()] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 258 | adict = {} |
| 259 | for line in alines: |
| 260 | splitv = [i.strip() for i in line.split('=',1)] |
| 261 | if len(splitv) > 1: |
| 262 | adict[splitv[0]] = splitv[1] |
| 263 | return adict |
| 264 | |
| 265 | |
| 266 | def file_list_to_dict(lines): |
| 267 | adict = {} |
| 268 | for line in lines: |
| 269 | # Leave the last few fields intact so we handle file names containing spaces |
| 270 | splitv = line.split(None,4) |
| 271 | # Grab the path and remove the leading . |
| 272 | path = splitv[4][1:].strip() |
| 273 | # Handle symlinks |
| 274 | if(' -> ' in path): |
| 275 | target = path.split(' -> ')[1] |
| 276 | path = path.split(' -> ')[0] |
| 277 | adict[path] = splitv[0:3] + [target] |
| 278 | else: |
| 279 | adict[path] = splitv[0:3] |
| 280 | return adict |
| 281 | |
| 282 | |
| 283 | def compare_file_lists(alines, blines): |
| 284 | adict = file_list_to_dict(alines) |
| 285 | bdict = file_list_to_dict(blines) |
| 286 | filechanges = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 287 | for path, splitv in adict.items(): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 288 | newsplitv = bdict.pop(path, None) |
| 289 | if newsplitv: |
| 290 | # Check type |
| 291 | oldvalue = splitv[0][0] |
| 292 | newvalue = newsplitv[0][0] |
| 293 | if oldvalue != newvalue: |
| 294 | filechanges.append(FileChange(path, FileChange.changetype_type, oldvalue, newvalue)) |
| 295 | # Check permissions |
| 296 | oldvalue = splitv[0][1:] |
| 297 | newvalue = newsplitv[0][1:] |
| 298 | if oldvalue != newvalue: |
| 299 | filechanges.append(FileChange(path, FileChange.changetype_perms, oldvalue, newvalue)) |
| 300 | # Check owner/group |
| 301 | oldvalue = '%s/%s' % (splitv[1], splitv[2]) |
| 302 | newvalue = '%s/%s' % (newsplitv[1], newsplitv[2]) |
| 303 | if oldvalue != newvalue: |
| 304 | filechanges.append(FileChange(path, FileChange.changetype_ownergroup, oldvalue, newvalue)) |
| 305 | # Check symlink target |
| 306 | if newsplitv[0][0] == 'l': |
| 307 | if len(splitv) > 3: |
| 308 | oldvalue = splitv[3] |
| 309 | else: |
| 310 | oldvalue = None |
| 311 | newvalue = newsplitv[3] |
| 312 | if oldvalue != newvalue: |
| 313 | filechanges.append(FileChange(path, FileChange.changetype_link, oldvalue, newvalue)) |
| 314 | else: |
| 315 | filechanges.append(FileChange(path, FileChange.changetype_remove)) |
| 316 | |
| 317 | # Whatever is left over has been added |
| 318 | for path in bdict: |
| 319 | filechanges.append(FileChange(path, FileChange.changetype_add)) |
| 320 | |
| 321 | return filechanges |
| 322 | |
| 323 | |
| 324 | def compare_lists(alines, blines): |
| 325 | removed = list(set(alines) - set(blines)) |
| 326 | added = list(set(blines) - set(alines)) |
| 327 | |
| 328 | filechanges = [] |
| 329 | for pkg in removed: |
| 330 | filechanges.append(FileChange(pkg, FileChange.changetype_remove)) |
| 331 | for pkg in added: |
| 332 | filechanges.append(FileChange(pkg, FileChange.changetype_add)) |
| 333 | |
| 334 | return filechanges |
| 335 | |
| 336 | |
| 337 | def compare_pkg_lists(astr, bstr): |
| 338 | depvera = bb.utils.explode_dep_versions2(astr) |
| 339 | depverb = bb.utils.explode_dep_versions2(bstr) |
| 340 | |
| 341 | # Strip out changes where the version has increased |
| 342 | remove = [] |
| 343 | for k in depvera: |
| 344 | if k in depverb: |
| 345 | dva = depvera[k] |
| 346 | dvb = depverb[k] |
| 347 | if dva and dvb and len(dva) == len(dvb): |
| 348 | # Since length is the same, sort so that prefixes (e.g. >=) will line up |
| 349 | dva.sort() |
| 350 | dvb.sort() |
| 351 | removeit = True |
| 352 | for dvai, dvbi in zip(dva, dvb): |
| 353 | if dvai != dvbi: |
| 354 | aiprefix = dvai.split(' ')[0] |
| 355 | biprefix = dvbi.split(' ')[0] |
| 356 | if aiprefix == biprefix and aiprefix in ['>=', '=']: |
| 357 | if bb.utils.vercmp(bb.utils.split_version(dvai), bb.utils.split_version(dvbi)) > 0: |
| 358 | removeit = False |
| 359 | break |
| 360 | else: |
| 361 | removeit = False |
| 362 | break |
| 363 | if removeit: |
| 364 | remove.append(k) |
| 365 | |
| 366 | for k in remove: |
| 367 | depvera.pop(k) |
| 368 | depverb.pop(k) |
| 369 | |
| 370 | return (depvera, depverb) |
| 371 | |
| 372 | |
| 373 | def compare_dict_blobs(path, ablob, bblob, report_all, report_ver): |
| 374 | adict = blob_to_dict(ablob) |
| 375 | bdict = blob_to_dict(bblob) |
| 376 | |
| 377 | pkgname = os.path.basename(path) |
| 378 | |
| 379 | defaultvals = {} |
| 380 | defaultvals['PKG'] = pkgname |
| 381 | defaultvals['PKGE'] = '0' |
| 382 | |
| 383 | changes = [] |
| 384 | keys = list(set(adict.keys()) | set(bdict.keys()) | set(defaultval_map.keys())) |
| 385 | for key in keys: |
| 386 | astr = adict.get(key, '') |
| 387 | bstr = bdict.get(key, '') |
| 388 | if key in ver_monitor_fields: |
| 389 | monitored = report_ver or astr or bstr |
| 390 | else: |
| 391 | monitored = key in monitor_fields |
| 392 | mapped_key = defaultval_map.get(key, '') |
| 393 | if mapped_key: |
| 394 | if not astr: |
| 395 | astr = '%s [default]' % adict.get(mapped_key, defaultvals.get(key, '')) |
| 396 | if not bstr: |
| 397 | bstr = '%s [default]' % bdict.get(mapped_key, defaultvals.get(key, '')) |
| 398 | |
| 399 | if astr != bstr: |
| 400 | if (not report_all) and key in numeric_fields: |
| 401 | aval = int(astr or 0) |
| 402 | bval = int(bstr or 0) |
| 403 | if aval != 0: |
| 404 | percentchg = ((bval - aval) / float(aval)) * 100 |
| 405 | else: |
| 406 | percentchg = 100 |
| 407 | if abs(percentchg) < monitor_numeric_threshold: |
| 408 | continue |
| 409 | elif (not report_all) and key in list_fields: |
| 410 | if key == "FILELIST" and path.endswith("-dbg") and bstr.strip() != '': |
| 411 | continue |
| 412 | if key in ['RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RREPLACES', 'RCONFLICTS']: |
| 413 | (depvera, depverb) = compare_pkg_lists(astr, bstr) |
| 414 | if depvera == depverb: |
| 415 | continue |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 416 | if key == 'FILELIST': |
| 417 | alist = shlex.split(astr) |
| 418 | blist = shlex.split(bstr) |
| 419 | else: |
| 420 | alist = astr.split() |
| 421 | blist = bstr.split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 422 | alist.sort() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 423 | blist.sort() |
| 424 | # We don't care about the removal of self-dependencies |
| 425 | if pkgname in alist and not pkgname in blist: |
| 426 | alist.remove(pkgname) |
| 427 | if ' '.join(alist) == ' '.join(blist): |
| 428 | continue |
| 429 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 430 | if key == 'PKGR' and not report_all: |
| 431 | vers = [] |
| 432 | # strip leading 'r' and dots |
| 433 | for ver in (astr.split()[0], bstr.split()[0]): |
| 434 | if ver.startswith('r'): |
| 435 | ver = ver[1:] |
| 436 | vers.append(ver.replace('.', '')) |
| 437 | maxlen = max(len(vers[0]), len(vers[1])) |
| 438 | try: |
| 439 | # pad with '0' and convert to int |
| 440 | vers = [int(ver.ljust(maxlen, '0')) for ver in vers] |
| 441 | except ValueError: |
| 442 | pass |
| 443 | else: |
| 444 | # skip decrements and increments |
| 445 | if abs(vers[0] - vers[1]) == 1: |
| 446 | continue |
| 447 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 448 | chg = ChangeRecord(path, key, astr, bstr, monitored) |
| 449 | changes.append(chg) |
| 450 | return changes |
| 451 | |
| 452 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 453 | def compare_siglists(a_blob, b_blob, taskdiff=False): |
| 454 | # FIXME collapse down a recipe's tasks? |
| 455 | alines = a_blob.data_stream.read().decode('utf-8').splitlines() |
| 456 | blines = b_blob.data_stream.read().decode('utf-8').splitlines() |
| 457 | keys = [] |
| 458 | pnmap = {} |
| 459 | def readsigs(lines): |
| 460 | sigs = {} |
| 461 | for line in lines: |
| 462 | linesplit = line.split() |
| 463 | if len(linesplit) > 2: |
| 464 | sigs[linesplit[0]] = linesplit[2] |
| 465 | if not linesplit[0] in keys: |
| 466 | keys.append(linesplit[0]) |
| 467 | pnmap[linesplit[1]] = linesplit[0].rsplit('.', 1)[0] |
| 468 | return sigs |
| 469 | adict = readsigs(alines) |
| 470 | bdict = readsigs(blines) |
| 471 | out = [] |
| 472 | |
| 473 | changecount = 0 |
| 474 | addcount = 0 |
| 475 | removecount = 0 |
| 476 | if taskdiff: |
| 477 | with bb.tinfoil.Tinfoil() as tinfoil: |
| 478 | tinfoil.prepare(config_only=True) |
| 479 | |
| 480 | changes = collections.OrderedDict() |
| 481 | |
| 482 | def compare_hashfiles(pn, taskname, hash1, hash2): |
| 483 | hashes = [hash1, hash2] |
| 484 | hashfiles = bb.siggen.find_siginfo(pn, taskname, hashes, tinfoil.config_data) |
| 485 | |
| 486 | if not taskname: |
| 487 | (pn, taskname) = pn.rsplit('.', 1) |
| 488 | pn = pnmap.get(pn, pn) |
| 489 | desc = '%s.%s' % (pn, taskname) |
| 490 | |
| 491 | if len(hashfiles) == 0: |
| 492 | out.append("Unable to find matching sigdata for %s with hashes %s or %s" % (desc, hash1, hash2)) |
| 493 | elif not hash1 in hashfiles: |
| 494 | out.append("Unable to find matching sigdata for %s with hash %s" % (desc, hash1)) |
| 495 | elif not hash2 in hashfiles: |
| 496 | out.append("Unable to find matching sigdata for %s with hash %s" % (desc, hash2)) |
| 497 | else: |
| 498 | out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb, collapsed=True) |
| 499 | for line in out2: |
| 500 | m = hashlib.sha256() |
| 501 | m.update(line.encode('utf-8')) |
| 502 | entry = changes.get(m.hexdigest(), (line, [])) |
| 503 | if desc not in entry[1]: |
| 504 | changes[m.hexdigest()] = (line, entry[1] + [desc]) |
| 505 | |
| 506 | # Define recursion callback |
| 507 | def recursecb(key, hash1, hash2): |
| 508 | compare_hashfiles(key, None, hash1, hash2) |
| 509 | return [] |
| 510 | |
| 511 | for key in keys: |
| 512 | siga = adict.get(key, None) |
| 513 | sigb = bdict.get(key, None) |
| 514 | if siga is not None and sigb is not None and siga != sigb: |
| 515 | changecount += 1 |
| 516 | (pn, taskname) = key.rsplit('.', 1) |
| 517 | compare_hashfiles(pn, taskname, siga, sigb) |
| 518 | elif siga is None: |
| 519 | addcount += 1 |
| 520 | elif sigb is None: |
| 521 | removecount += 1 |
| 522 | for key, item in changes.items(): |
| 523 | line, tasks = item |
| 524 | if len(tasks) == 1: |
| 525 | desc = tasks[0] |
| 526 | elif len(tasks) == 2: |
| 527 | desc = '%s and %s' % (tasks[0], tasks[1]) |
| 528 | else: |
| 529 | desc = '%s and %d others' % (tasks[-1], len(tasks)-1) |
| 530 | out.append('%s: %s' % (desc, line)) |
| 531 | else: |
| 532 | for key in keys: |
| 533 | siga = adict.get(key, None) |
| 534 | sigb = bdict.get(key, None) |
| 535 | if siga is not None and sigb is not None and siga != sigb: |
| 536 | out.append('%s changed from %s to %s' % (key, siga, sigb)) |
| 537 | changecount += 1 |
| 538 | elif siga is None: |
| 539 | out.append('%s was added' % key) |
| 540 | addcount += 1 |
| 541 | elif sigb is None: |
| 542 | out.append('%s was removed' % key) |
| 543 | removecount += 1 |
| 544 | out.append('Summary: %d tasks added, %d tasks removed, %d tasks modified (%.1f%%)' % (addcount, removecount, changecount, (changecount / float(len(bdict)) * 100))) |
| 545 | return '\n'.join(out) |
| 546 | |
| 547 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 548 | def process_changes(repopath, revision1, revision2='HEAD', report_all=False, report_ver=False, |
| 549 | sigs=False, sigsdiff=False, exclude_path=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 550 | repo = git.Repo(repopath) |
| 551 | assert repo.bare == False |
| 552 | commit = repo.commit(revision1) |
| 553 | diff = commit.diff(revision2) |
| 554 | |
| 555 | changes = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 556 | |
| 557 | if sigs or sigsdiff: |
| 558 | for d in diff.iter_change_type('M'): |
| 559 | if d.a_blob.path == 'siglist.txt': |
| 560 | changes.append(compare_siglists(d.a_blob, d.b_blob, taskdiff=sigsdiff)) |
| 561 | return changes |
| 562 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 563 | for d in diff.iter_change_type('M'): |
| 564 | path = os.path.dirname(d.a_blob.path) |
| 565 | if path.startswith('packages/'): |
| 566 | filename = os.path.basename(d.a_blob.path) |
| 567 | if filename == 'latest': |
| 568 | changes.extend(compare_dict_blobs(path, d.a_blob, d.b_blob, report_all, report_ver)) |
| 569 | elif filename.startswith('latest.'): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 570 | chg = ChangeRecord(path, filename, d.a_blob.data_stream.read().decode('utf-8'), d.b_blob.data_stream.read().decode('utf-8'), True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 571 | changes.append(chg) |
| 572 | elif path.startswith('images/'): |
| 573 | filename = os.path.basename(d.a_blob.path) |
| 574 | if filename in img_monitor_files: |
| 575 | if filename == 'files-in-image.txt': |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 576 | alines = d.a_blob.data_stream.read().decode('utf-8').splitlines() |
| 577 | blines = d.b_blob.data_stream.read().decode('utf-8').splitlines() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 578 | filechanges = compare_file_lists(alines,blines) |
| 579 | if filechanges: |
| 580 | chg = ChangeRecord(path, filename, None, None, True) |
| 581 | chg.filechanges = filechanges |
| 582 | changes.append(chg) |
| 583 | elif filename == 'installed-package-names.txt': |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 584 | alines = d.a_blob.data_stream.read().decode('utf-8').splitlines() |
| 585 | blines = d.b_blob.data_stream.read().decode('utf-8').splitlines() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 586 | filechanges = compare_lists(alines,blines) |
| 587 | if filechanges: |
| 588 | chg = ChangeRecord(path, filename, None, None, True) |
| 589 | chg.filechanges = filechanges |
| 590 | changes.append(chg) |
| 591 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 592 | chg = ChangeRecord(path, filename, d.a_blob.data_stream.read().decode('utf-8'), d.b_blob.data_stream.read().decode('utf-8'), True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 593 | changes.append(chg) |
| 594 | elif filename == 'image-info.txt': |
| 595 | changes.extend(compare_dict_blobs(path, d.a_blob, d.b_blob, report_all, report_ver)) |
| 596 | elif '/image-files/' in path: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 597 | chg = ChangeRecord(path, filename, d.a_blob.data_stream.read().decode('utf-8'), d.b_blob.data_stream.read().decode('utf-8'), True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 598 | changes.append(chg) |
| 599 | |
| 600 | # Look for added preinst/postinst/prerm/postrm |
| 601 | # (without reporting newly added recipes) |
| 602 | addedpkgs = [] |
| 603 | addedchanges = [] |
| 604 | for d in diff.iter_change_type('A'): |
| 605 | path = os.path.dirname(d.b_blob.path) |
| 606 | if path.startswith('packages/'): |
| 607 | filename = os.path.basename(d.b_blob.path) |
| 608 | if filename == 'latest': |
| 609 | addedpkgs.append(path) |
| 610 | elif filename.startswith('latest.'): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 611 | chg = ChangeRecord(path, filename[7:], '', d.b_blob.data_stream.read().decode('utf-8'), True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 612 | addedchanges.append(chg) |
| 613 | for chg in addedchanges: |
| 614 | found = False |
| 615 | for pkg in addedpkgs: |
| 616 | if chg.path.startswith(pkg): |
| 617 | found = True |
| 618 | break |
| 619 | if not found: |
| 620 | changes.append(chg) |
| 621 | |
| 622 | # Look for cleared preinst/postinst/prerm/postrm |
| 623 | for d in diff.iter_change_type('D'): |
| 624 | path = os.path.dirname(d.a_blob.path) |
| 625 | if path.startswith('packages/'): |
| 626 | filename = os.path.basename(d.a_blob.path) |
| 627 | if filename != 'latest' and filename.startswith('latest.'): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 628 | chg = ChangeRecord(path, filename[7:], d.a_blob.data_stream.read().decode('utf-8'), '', True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 629 | changes.append(chg) |
| 630 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 631 | # filter out unwanted paths |
| 632 | if exclude_path: |
| 633 | for chg in changes: |
| 634 | if chg.filechanges: |
| 635 | fchgs = [] |
| 636 | for fchg in chg.filechanges: |
| 637 | for epath in exclude_path: |
| 638 | if fchg.path.startswith(epath): |
| 639 | break |
| 640 | else: |
| 641 | fchgs.append(fchg) |
| 642 | chg.filechanges = fchgs |
| 643 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 644 | if report_all: |
| 645 | return changes |
| 646 | else: |
| 647 | return [chg for chg in changes if chg.monitored] |