blob: ad7fceb8bbccf6799018d4eee35c1e8332c5385f [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#
6# Note: requires GitPython 0.3.1+
7#
8# You can use this from the command line by running scripts/buildhistory-diff
9#
10
11import sys
12import os.path
13import difflib
14import git
15import re
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080016import shlex
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017import hashlib
18import collections
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019import bb.utils
Brad Bishop6e60e8b2018-02-01 10:27:11 -050020import bb.tinfoil
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021
22
23# How to display fields
24list_fields = ['DEPENDS', 'RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RSUGGESTS', 'RREPLACES', 'RCONFLICTS', 'FILES', 'FILELIST', 'USER_CLASSES', 'IMAGE_CLASSES', 'IMAGE_FEATURES', 'IMAGE_LINGUAS', 'IMAGE_INSTALL', 'BAD_RECOMMENDATIONS', 'PACKAGE_EXCLUDE']
25list_order_fields = ['PACKAGES']
26defaultval_map = {'PKG': 'PKG', 'PKGE': 'PE', 'PKGV': 'PV', 'PKGR': 'PR'}
27numeric_fields = ['PKGSIZE', 'IMAGESIZE']
28# Fields to monitor
29monitor_fields = ['RPROVIDES', 'RDEPENDS', 'RRECOMMENDS', 'RREPLACES', 'RCONFLICTS', 'PACKAGES', 'FILELIST', 'PKGSIZE', 'IMAGESIZE', 'PKG']
30ver_monitor_fields = ['PKGE', 'PKGV', 'PKGR']
31# Percentage change to alert for numeric fields
32monitor_numeric_threshold = 10
33# Image files to monitor (note that image-info.txt is handled separately)
34img_monitor_files = ['installed-package-names.txt', 'files-in-image.txt']
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035
Brad Bishop316dfdd2018-06-25 12:45:53 -040036colours = {
37 'colour_default': '',
38 'colour_add': '',
39 'colour_remove': '',
40}
41
42def 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 Williamsc124f4f2015-09-15 14:41:29 -050056
57class 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 Williamsc124f4f2015-09-15 14:41:29 -050064 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 Williamsc0f7c042017-02-23 20:41:17 -060080 for k,v in depver.items():
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 if v:
82 pkglist.append("%s (%s)" % (k,v))
83 else:
84 pkglist.append(k)
85 return pkglist
86
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 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 Bishop316dfdd2018-06-25 12:45:53 -040094 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 Bishop6e60e8b2018-02-01 10:27:11 -0500105 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500111 if self.fieldname in list_fields or self.fieldname in list_order_fields:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 renamed_dirs = []
Brad Bishop316dfdd2018-06-25 12:45:53 -0400113 changed_order = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114 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 Bishop6e60e8b2018-02-01 10:27:11 -0500119 if self.fieldname == 'FILELIST':
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800120 aitems = shlex.split(self.oldvalue)
121 bitems = shlex.split(self.newvalue)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 renamed_dirs, aitems, bitems = detect_renamed_dirs(aitems, bitems)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800123 else:
124 aitems = self.oldvalue.split()
125 bitems = self.newvalue.split()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500126
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127 removed = list(set(aitems) - set(bitems))
128 added = list(set(bitems) - set(aitems))
129
Brad Bishop316dfdd2018-06-25 12:45:53 -0400130 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 Bishop6e60e8b2018-02-01 10:27:11 -0500138 lines = []
139 if renamed_dirs:
140 for dfrom, dto in renamed_dirs:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400141 lines.append('directory renamed {colour_remove}{}{colour_default} -> {colour_add}{}{colour_default}'.format(dfrom, dto, **colours))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142 if removed or added:
143 if removed and not bitems:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400144 lines.append('removed all items "{colour_remove}{}{colour_default}"'.format(' '.join(removed), **colours))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500146 if removed:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400147 lines.append('removed "{colour_remove}{value}{colour_default}"'.format(value=' '.join(removed), **colours))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500148 if added:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400149 lines.append('added "{colour_add}{value}{colour_default}"'.format(value=' '.join(added), **colours))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500151 lines.append('changed order')
152
Brad Bishop316dfdd2018-06-25 12:45:53 -0400153 if not (removed or added or changed_order):
154 out = ''
155 else:
156 out = '%s: %s' % (self.fieldname, ', '.join(lines))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500157
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158 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 Bishop316dfdd2018-06-25 12:45:53 -0400165 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 -0500166 elif self.fieldname in defaultval_map:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400167 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 -0500168 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 Bishopd7bf8c12018-02-25 22:55:05 -0500183 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 Williamsc124f4f2015-09-15 14:41:29 -0500200 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500201 out = ''
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202 else:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400203 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 -0500204
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500205 return '%s%s' % (prefix, out) if out else ''
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206
207class 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
256def blob_to_dict(blob):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600257 alines = [line for line in blob.data_stream.read().decode('utf-8').splitlines()]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258 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
266def 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
283def compare_file_lists(alines, blines):
284 adict = file_list_to_dict(alines)
285 bdict = file_list_to_dict(blines)
286 filechanges = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600287 for path, splitv in adict.items():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288 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
324def 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
337def 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
373def 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 Bishop1a4b7ee2018-12-16 17:11:34 -0800416 if key == 'FILELIST':
417 alist = shlex.split(astr)
418 blist = shlex.split(bstr)
419 else:
420 alist = astr.split()
421 blist = bstr.split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500422 alist.sort()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500423 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 Williamsc0f7c042017-02-23 20:41:17 -0600430 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 Williamsc124f4f2015-09-15 14:41:29 -0500448 chg = ChangeRecord(path, key, astr, bstr, monitored)
449 changes.append(chg)
450 return changes
451
452
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500453def 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 Bishopd7bf8c12018-02-25 22:55:05 -0500548def process_changes(repopath, revision1, revision2='HEAD', report_all=False, report_ver=False,
549 sigs=False, sigsdiff=False, exclude_path=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500550 repo = git.Repo(repopath)
551 assert repo.bare == False
552 commit = repo.commit(revision1)
553 diff = commit.diff(revision2)
554
555 changes = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500556
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 Williamsc124f4f2015-09-15 14:41:29 -0500563 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 Williamsc0f7c042017-02-23 20:41:17 -0600570 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 -0500571 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 Williamsc0f7c042017-02-23 20:41:17 -0600576 alines = d.a_blob.data_stream.read().decode('utf-8').splitlines()
577 blines = d.b_blob.data_stream.read().decode('utf-8').splitlines()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500578 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 Williamsc0f7c042017-02-23 20:41:17 -0600584 alines = d.a_blob.data_stream.read().decode('utf-8').splitlines()
585 blines = d.b_blob.data_stream.read().decode('utf-8').splitlines()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500586 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 Williamsc0f7c042017-02-23 20:41:17 -0600592 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 -0500593 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 Williamsc0f7c042017-02-23 20:41:17 -0600597 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 -0500598 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 Williamsc0f7c042017-02-23 20:41:17 -0600611 chg = ChangeRecord(path, filename[7:], '', d.b_blob.data_stream.read().decode('utf-8'), True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500612 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 Williamsc0f7c042017-02-23 20:41:17 -0600628 chg = ChangeRecord(path, filename[7:], d.a_blob.data_stream.read().decode('utf-8'), '', True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500629 changes.append(chg)
630
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500631 # 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 Williamsc124f4f2015-09-15 14:41:29 -0500644 if report_all:
645 return changes
646 else:
647 return [chg for chg in changes if chg.monitored]