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