blob: 62c7a2e58eafe8fd89230cc83b0e0db619e032ae [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# Report significant differences in the buildhistory repository since a specific revision
2#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05003# Copyright (C) 2012-2013, 2016-2017 Intel Corporation
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004# Author: Paul Eggleton <paul.eggleton@linux.intel.com>
5#
Brad Bishopc342db32019-05-15 21:57:59 -04006# SPDX-License-Identifier: GPL-2.0-only
7#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008# Note: requires GitPython 0.3.1+
9#
10# You can use this from the command line by running scripts/buildhistory-diff
11#
12
13import sys
14import os.path
15import difflib
16import git
17import re
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080018import shlex
Brad Bishop6e60e8b2018-02-01 10:27:11 -050019import hashlib
20import collections
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021import bb.utils
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022import bb.tinfoil
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023
24
25# How to display fields
26list_fields = ['DEPENDS', 'RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RREPLACES', 'RCONFLICTS', 'FILES', 'FILELIST', 'USER_CLASSES', 'IMAGE_CLASSES', 'IMAGE_FEATURES', 'IMAGE_LINGUAS', 'IMAGE_INSTALL', 'BAD_RECOMMENDATIONS', 'PACKAGE_EXCLUDE']
27list_order_fields = ['PACKAGES']
28defaultval_map = {'PKG': 'PKG', 'PKGE': 'PE', 'PKGV': 'PV', 'PKGR': 'PR'}
29numeric_fields = ['PKGSIZE', 'IMAGESIZE']
30# Fields to monitor
31monitor_fields = ['RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RREPLACES', 'RCONFLICTS', 'PACKAGES', 'FILELIST', 'PKGSIZE', 'IMAGESIZE', 'PKG']
32ver_monitor_fields = ['PKGE', 'PKGV', 'PKGR']
33# Percentage change to alert for numeric fields
34monitor_numeric_threshold = 10
35# Image files to monitor (note that image-info.txt is handled separately)
36img_monitor_files = ['installed-package-names.txt', 'files-in-image.txt']
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
Brad Bishop316dfdd2018-06-25 12:45:53 -040038colours = {
39 'colour_default': '',
40 'colour_add': '',
41 'colour_remove': '',
42}
43
44def 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 Williamsc124f4f2015-09-15 14:41:29 -050058
59class 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 Williamsc124f4f2015-09-15 14:41:29 -050066 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 Williamsc0f7c042017-02-23 20:41:17 -060082 for k,v in depver.items():
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 if v:
84 pkglist.append("%s (%s)" % (k,v))
85 else:
86 pkglist.append(k)
87 return pkglist
88
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 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 Bishop316dfdd2018-06-25 12:45:53 -040096 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 Bishop6e60e8b2018-02-01 10:27:11 -0500107 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500113 if self.fieldname in list_fields or self.fieldname in list_order_fields:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500114 renamed_dirs = []
Brad Bishop316dfdd2018-06-25 12:45:53 -0400115 changed_order = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 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 Bishop6e60e8b2018-02-01 10:27:11 -0500121 if self.fieldname == 'FILELIST':
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800122 aitems = shlex.split(self.oldvalue)
123 bitems = shlex.split(self.newvalue)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 renamed_dirs, aitems, bitems = detect_renamed_dirs(aitems, bitems)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800125 else:
126 aitems = self.oldvalue.split()
127 bitems = self.newvalue.split()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500128
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129 removed = list(set(aitems) - set(bitems))
130 added = list(set(bitems) - set(aitems))
131
Brad Bishopc342db32019-05-15 21:57:59 -0400132 if not removed and not added and self.fieldname in ['RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RREPLACES', 'RCONFLICTS']:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400133 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 Bishop6e60e8b2018-02-01 10:27:11 -0500140 lines = []
141 if renamed_dirs:
142 for dfrom, dto in renamed_dirs:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400143 lines.append('directory renamed {colour_remove}{}{colour_default} -> {colour_add}{}{colour_default}'.format(dfrom, dto, **colours))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 if removed or added:
145 if removed and not bitems:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400146 lines.append('removed all items "{colour_remove}{}{colour_default}"'.format(' '.join(removed), **colours))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 if removed:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400149 lines.append('removed "{colour_remove}{value}{colour_default}"'.format(value=' '.join(removed), **colours))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150 if added:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400151 lines.append('added "{colour_add}{value}{colour_default}"'.format(value=' '.join(added), **colours))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153 lines.append('changed order')
154
Brad Bishop316dfdd2018-06-25 12:45:53 -0400155 if not (removed or added or changed_order):
156 out = ''
157 else:
158 out = '%s: %s' % (self.fieldname, ', '.join(lines))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500159
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 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 Bishop316dfdd2018-06-25 12:45:53 -0400167 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 Williamsc124f4f2015-09-15 14:41:29 -0500168 elif self.fieldname in defaultval_map:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400169 out = '{} changed from {colour_remove}{}{colour_default} to {colour_add}{}{colour_default}'.format(self.fieldname, self.oldvalue, self.newvalue, **colours)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170 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 --'
184 elif self.fieldname in img_monitor_files or '/image-files/' in self.path:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500185 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 Williamsc124f4f2015-09-15 14:41:29 -0500202 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500203 out = ''
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204 else:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400205 out = '{} changed from "{colour_remove}{}{colour_default}" to "{colour_add}{}{colour_default}"'.format(self.fieldname, self.oldvalue, self.newvalue, **colours)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500207 return '%s%s' % (prefix, out) if out else ''
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208
209class 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'
216
217 def __init__(self, path, changetype, oldvalue = None, newvalue = None):
218 self.path = path
219 self.changetype = changetype
220 self.oldvalue = oldvalue
221 self.newvalue = newvalue
222
223 def _ftype_str(self, ftype):
224 if ftype == '-':
225 return 'file'
226 elif ftype == 'd':
227 return 'directory'
228 elif ftype == 'l':
229 return 'symlink'
230 elif ftype == 'c':
231 return 'char device'
232 elif ftype == 'b':
233 return 'block device'
234 elif ftype == 'p':
235 return 'fifo'
236 elif ftype == 's':
237 return 'socket'
238 else:
239 return 'unknown (%s)' % ftype
240
241 def __str__(self):
242 if self.changetype == self.changetype_add:
243 return '%s was added' % self.path
244 elif self.changetype == self.changetype_remove:
245 return '%s was removed' % self.path
246 elif self.changetype == self.changetype_type:
247 return '%s changed type from %s to %s' % (self.path, self._ftype_str(self.oldvalue), self._ftype_str(self.newvalue))
248 elif self.changetype == self.changetype_perms:
249 return '%s changed permissions from %s to %s' % (self.path, self.oldvalue, self.newvalue)
250 elif self.changetype == self.changetype_ownergroup:
251 return '%s changed owner/group from %s to %s' % (self.path, self.oldvalue, self.newvalue)
252 elif self.changetype == self.changetype_link:
253 return '%s changed symlink target from %s to %s' % (self.path, self.oldvalue, self.newvalue)
254 else:
255 return '%s changed (unknown)' % self.path
256
257
258def blob_to_dict(blob):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600259 alines = [line for line in blob.data_stream.read().decode('utf-8').splitlines()]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260 adict = {}
261 for line in alines:
262 splitv = [i.strip() for i in line.split('=',1)]
263 if len(splitv) > 1:
264 adict[splitv[0]] = splitv[1]
265 return adict
266
267
268def file_list_to_dict(lines):
269 adict = {}
270 for line in lines:
271 # Leave the last few fields intact so we handle file names containing spaces
272 splitv = line.split(None,4)
273 # Grab the path and remove the leading .
274 path = splitv[4][1:].strip()
275 # Handle symlinks
276 if(' -> ' in path):
277 target = path.split(' -> ')[1]
278 path = path.split(' -> ')[0]
279 adict[path] = splitv[0:3] + [target]
280 else:
281 adict[path] = splitv[0:3]
282 return adict
283
284
285def compare_file_lists(alines, blines):
286 adict = file_list_to_dict(alines)
287 bdict = file_list_to_dict(blines)
288 filechanges = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600289 for path, splitv in adict.items():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500290 newsplitv = bdict.pop(path, None)
291 if newsplitv:
292 # Check type
293 oldvalue = splitv[0][0]
294 newvalue = newsplitv[0][0]
295 if oldvalue != newvalue:
296 filechanges.append(FileChange(path, FileChange.changetype_type, oldvalue, newvalue))
297 # Check permissions
298 oldvalue = splitv[0][1:]
299 newvalue = newsplitv[0][1:]
300 if oldvalue != newvalue:
301 filechanges.append(FileChange(path, FileChange.changetype_perms, oldvalue, newvalue))
302 # Check owner/group
303 oldvalue = '%s/%s' % (splitv[1], splitv[2])
304 newvalue = '%s/%s' % (newsplitv[1], newsplitv[2])
305 if oldvalue != newvalue:
306 filechanges.append(FileChange(path, FileChange.changetype_ownergroup, oldvalue, newvalue))
307 # Check symlink target
308 if newsplitv[0][0] == 'l':
309 if len(splitv) > 3:
310 oldvalue = splitv[3]
311 else:
312 oldvalue = None
313 newvalue = newsplitv[3]
314 if oldvalue != newvalue:
315 filechanges.append(FileChange(path, FileChange.changetype_link, oldvalue, newvalue))
316 else:
317 filechanges.append(FileChange(path, FileChange.changetype_remove))
318
319 # Whatever is left over has been added
320 for path in bdict:
321 filechanges.append(FileChange(path, FileChange.changetype_add))
322
323 return filechanges
324
325
326def compare_lists(alines, blines):
327 removed = list(set(alines) - set(blines))
328 added = list(set(blines) - set(alines))
329
330 filechanges = []
331 for pkg in removed:
332 filechanges.append(FileChange(pkg, FileChange.changetype_remove))
333 for pkg in added:
334 filechanges.append(FileChange(pkg, FileChange.changetype_add))
335
336 return filechanges
337
338
339def compare_pkg_lists(astr, bstr):
340 depvera = bb.utils.explode_dep_versions2(astr)
341 depverb = bb.utils.explode_dep_versions2(bstr)
342
343 # Strip out changes where the version has increased
344 remove = []
345 for k in depvera:
346 if k in depverb:
347 dva = depvera[k]
348 dvb = depverb[k]
349 if dva and dvb and len(dva) == len(dvb):
350 # Since length is the same, sort so that prefixes (e.g. >=) will line up
351 dva.sort()
352 dvb.sort()
353 removeit = True
354 for dvai, dvbi in zip(dva, dvb):
355 if dvai != dvbi:
356 aiprefix = dvai.split(' ')[0]
357 biprefix = dvbi.split(' ')[0]
358 if aiprefix == biprefix and aiprefix in ['>=', '=']:
359 if bb.utils.vercmp(bb.utils.split_version(dvai), bb.utils.split_version(dvbi)) > 0:
360 removeit = False
361 break
362 else:
363 removeit = False
364 break
365 if removeit:
366 remove.append(k)
367
368 for k in remove:
369 depvera.pop(k)
370 depverb.pop(k)
371
372 return (depvera, depverb)
373
374
375def compare_dict_blobs(path, ablob, bblob, report_all, report_ver):
376 adict = blob_to_dict(ablob)
377 bdict = blob_to_dict(bblob)
378
379 pkgname = os.path.basename(path)
380
381 defaultvals = {}
382 defaultvals['PKG'] = pkgname
383 defaultvals['PKGE'] = '0'
384
385 changes = []
386 keys = list(set(adict.keys()) | set(bdict.keys()) | set(defaultval_map.keys()))
387 for key in keys:
388 astr = adict.get(key, '')
389 bstr = bdict.get(key, '')
390 if key in ver_monitor_fields:
391 monitored = report_ver or astr or bstr
392 else:
393 monitored = key in monitor_fields
394 mapped_key = defaultval_map.get(key, '')
395 if mapped_key:
396 if not astr:
397 astr = '%s [default]' % adict.get(mapped_key, defaultvals.get(key, ''))
398 if not bstr:
399 bstr = '%s [default]' % bdict.get(mapped_key, defaultvals.get(key, ''))
400
401 if astr != bstr:
402 if (not report_all) and key in numeric_fields:
403 aval = int(astr or 0)
404 bval = int(bstr or 0)
405 if aval != 0:
406 percentchg = ((bval - aval) / float(aval)) * 100
407 else:
408 percentchg = 100
409 if abs(percentchg) < monitor_numeric_threshold:
410 continue
411 elif (not report_all) and key in list_fields:
412 if key == "FILELIST" and path.endswith("-dbg") and bstr.strip() != '':
413 continue
414 if key in ['RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RREPLACES', 'RCONFLICTS']:
415 (depvera, depverb) = compare_pkg_lists(astr, bstr)
416 if depvera == depverb:
417 continue
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800418 if key == 'FILELIST':
419 alist = shlex.split(astr)
420 blist = shlex.split(bstr)
421 else:
422 alist = astr.split()
423 blist = bstr.split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500424 alist.sort()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500425 blist.sort()
426 # We don't care about the removal of self-dependencies
427 if pkgname in alist and not pkgname in blist:
428 alist.remove(pkgname)
429 if ' '.join(alist) == ' '.join(blist):
430 continue
431
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600432 if key == 'PKGR' and not report_all:
433 vers = []
434 # strip leading 'r' and dots
435 for ver in (astr.split()[0], bstr.split()[0]):
436 if ver.startswith('r'):
437 ver = ver[1:]
438 vers.append(ver.replace('.', ''))
439 maxlen = max(len(vers[0]), len(vers[1]))
440 try:
441 # pad with '0' and convert to int
442 vers = [int(ver.ljust(maxlen, '0')) for ver in vers]
443 except ValueError:
444 pass
445 else:
446 # skip decrements and increments
447 if abs(vers[0] - vers[1]) == 1:
448 continue
449
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500450 chg = ChangeRecord(path, key, astr, bstr, monitored)
451 changes.append(chg)
452 return changes
453
454
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500455def compare_siglists(a_blob, b_blob, taskdiff=False):
456 # FIXME collapse down a recipe's tasks?
457 alines = a_blob.data_stream.read().decode('utf-8').splitlines()
458 blines = b_blob.data_stream.read().decode('utf-8').splitlines()
459 keys = []
460 pnmap = {}
461 def readsigs(lines):
462 sigs = {}
463 for line in lines:
464 linesplit = line.split()
465 if len(linesplit) > 2:
466 sigs[linesplit[0]] = linesplit[2]
467 if not linesplit[0] in keys:
468 keys.append(linesplit[0])
469 pnmap[linesplit[1]] = linesplit[0].rsplit('.', 1)[0]
470 return sigs
471 adict = readsigs(alines)
472 bdict = readsigs(blines)
473 out = []
474
475 changecount = 0
476 addcount = 0
477 removecount = 0
478 if taskdiff:
479 with bb.tinfoil.Tinfoil() as tinfoil:
480 tinfoil.prepare(config_only=True)
481
482 changes = collections.OrderedDict()
483
484 def compare_hashfiles(pn, taskname, hash1, hash2):
485 hashes = [hash1, hash2]
486 hashfiles = bb.siggen.find_siginfo(pn, taskname, hashes, tinfoil.config_data)
487
488 if not taskname:
489 (pn, taskname) = pn.rsplit('.', 1)
490 pn = pnmap.get(pn, pn)
491 desc = '%s.%s' % (pn, taskname)
492
493 if len(hashfiles) == 0:
494 out.append("Unable to find matching sigdata for %s with hashes %s or %s" % (desc, hash1, hash2))
495 elif not hash1 in hashfiles:
496 out.append("Unable to find matching sigdata for %s with hash %s" % (desc, hash1))
497 elif not hash2 in hashfiles:
498 out.append("Unable to find matching sigdata for %s with hash %s" % (desc, hash2))
499 else:
500 out2 = bb.siggen.compare_sigfiles(hashfiles[hash1], hashfiles[hash2], recursecb, collapsed=True)
501 for line in out2:
502 m = hashlib.sha256()
503 m.update(line.encode('utf-8'))
504 entry = changes.get(m.hexdigest(), (line, []))
505 if desc not in entry[1]:
506 changes[m.hexdigest()] = (line, entry[1] + [desc])
507
508 # Define recursion callback
509 def recursecb(key, hash1, hash2):
510 compare_hashfiles(key, None, hash1, hash2)
511 return []
512
513 for key in keys:
514 siga = adict.get(key, None)
515 sigb = bdict.get(key, None)
516 if siga is not None and sigb is not None and siga != sigb:
517 changecount += 1
518 (pn, taskname) = key.rsplit('.', 1)
519 compare_hashfiles(pn, taskname, siga, sigb)
520 elif siga is None:
521 addcount += 1
522 elif sigb is None:
523 removecount += 1
524 for key, item in changes.items():
525 line, tasks = item
526 if len(tasks) == 1:
527 desc = tasks[0]
528 elif len(tasks) == 2:
529 desc = '%s and %s' % (tasks[0], tasks[1])
530 else:
531 desc = '%s and %d others' % (tasks[-1], len(tasks)-1)
532 out.append('%s: %s' % (desc, line))
533 else:
534 for key in keys:
535 siga = adict.get(key, None)
536 sigb = bdict.get(key, None)
537 if siga is not None and sigb is not None and siga != sigb:
538 out.append('%s changed from %s to %s' % (key, siga, sigb))
539 changecount += 1
540 elif siga is None:
541 out.append('%s was added' % key)
542 addcount += 1
543 elif sigb is None:
544 out.append('%s was removed' % key)
545 removecount += 1
546 out.append('Summary: %d tasks added, %d tasks removed, %d tasks modified (%.1f%%)' % (addcount, removecount, changecount, (changecount / float(len(bdict)) * 100)))
547 return '\n'.join(out)
548
549
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500550def process_changes(repopath, revision1, revision2='HEAD', report_all=False, report_ver=False,
551 sigs=False, sigsdiff=False, exclude_path=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500552 repo = git.Repo(repopath)
553 assert repo.bare == False
554 commit = repo.commit(revision1)
555 diff = commit.diff(revision2)
556
557 changes = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500558
559 if sigs or sigsdiff:
560 for d in diff.iter_change_type('M'):
561 if d.a_blob.path == 'siglist.txt':
562 changes.append(compare_siglists(d.a_blob, d.b_blob, taskdiff=sigsdiff))
563 return changes
564
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500565 for d in diff.iter_change_type('M'):
566 path = os.path.dirname(d.a_blob.path)
567 if path.startswith('packages/'):
568 filename = os.path.basename(d.a_blob.path)
569 if filename == 'latest':
570 changes.extend(compare_dict_blobs(path, d.a_blob, d.b_blob, report_all, report_ver))
571 elif filename.startswith('latest.'):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600572 chg = ChangeRecord(path, filename, d.a_blob.data_stream.read().decode('utf-8'), d.b_blob.data_stream.read().decode('utf-8'), True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500573 changes.append(chg)
574 elif path.startswith('images/'):
575 filename = os.path.basename(d.a_blob.path)
576 if filename in img_monitor_files:
577 if filename == 'files-in-image.txt':
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600578 alines = d.a_blob.data_stream.read().decode('utf-8').splitlines()
579 blines = d.b_blob.data_stream.read().decode('utf-8').splitlines()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500580 filechanges = compare_file_lists(alines,blines)
581 if filechanges:
582 chg = ChangeRecord(path, filename, None, None, True)
583 chg.filechanges = filechanges
584 changes.append(chg)
585 elif filename == 'installed-package-names.txt':
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600586 alines = d.a_blob.data_stream.read().decode('utf-8').splitlines()
587 blines = d.b_blob.data_stream.read().decode('utf-8').splitlines()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500588 filechanges = compare_lists(alines,blines)
589 if filechanges:
590 chg = ChangeRecord(path, filename, None, None, True)
591 chg.filechanges = filechanges
592 changes.append(chg)
593 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600594 chg = ChangeRecord(path, filename, d.a_blob.data_stream.read().decode('utf-8'), d.b_blob.data_stream.read().decode('utf-8'), True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500595 changes.append(chg)
596 elif filename == 'image-info.txt':
597 changes.extend(compare_dict_blobs(path, d.a_blob, d.b_blob, report_all, report_ver))
598 elif '/image-files/' in path:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600599 chg = ChangeRecord(path, filename, d.a_blob.data_stream.read().decode('utf-8'), d.b_blob.data_stream.read().decode('utf-8'), True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500600 changes.append(chg)
601
602 # Look for added preinst/postinst/prerm/postrm
603 # (without reporting newly added recipes)
604 addedpkgs = []
605 addedchanges = []
606 for d in diff.iter_change_type('A'):
607 path = os.path.dirname(d.b_blob.path)
608 if path.startswith('packages/'):
609 filename = os.path.basename(d.b_blob.path)
610 if filename == 'latest':
611 addedpkgs.append(path)
612 elif filename.startswith('latest.'):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600613 chg = ChangeRecord(path, filename[7:], '', d.b_blob.data_stream.read().decode('utf-8'), True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500614 addedchanges.append(chg)
615 for chg in addedchanges:
616 found = False
617 for pkg in addedpkgs:
618 if chg.path.startswith(pkg):
619 found = True
620 break
621 if not found:
622 changes.append(chg)
623
624 # Look for cleared preinst/postinst/prerm/postrm
625 for d in diff.iter_change_type('D'):
626 path = os.path.dirname(d.a_blob.path)
627 if path.startswith('packages/'):
628 filename = os.path.basename(d.a_blob.path)
629 if filename != 'latest' and filename.startswith('latest.'):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600630 chg = ChangeRecord(path, filename[7:], d.a_blob.data_stream.read().decode('utf-8'), '', True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500631 changes.append(chg)
632
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500633 # filter out unwanted paths
634 if exclude_path:
635 for chg in changes:
636 if chg.filechanges:
637 fchgs = []
638 for fchg in chg.filechanges:
639 for epath in exclude_path:
640 if fchg.path.startswith(epath):
641 break
642 else:
643 fchgs.append(fchg)
644 chg.filechanges = fchgs
645
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500646 if report_all:
647 return changes
648 else:
649 return [chg for chg in changes if chg.monitored]