Commit 48eeca98 authored by David Diederich's avatar David Diederich
Browse files

Created FossaNoticDiff and FossaSectionDiff objects to allow programmatic analysis of the diffs

Each of them has a __str__ that returns the old behavior (stringify diff, with colors)
parent fc6a02e7
Pipeline #87037 failed with stages
in 21 minutes and 28 seconds
......@@ -50,7 +50,7 @@ def checkOutput(context):
@then('the difference is')
def checkDiff(context):
diff = context.notice.compare(context.basis)
diff = str(context.notice.compare(context.basis))
expected = context.text + '\n'
colorRegex = re.compile('\\{(green|red):(.*?)\\}')
......
......@@ -4,6 +4,20 @@ import json
from colorama import Fore, Style
def bannerLines(title):
if title is not None:
return ['=' * 72, title, '=' * 72]
return []
def attrStr(name, urls):
attr = name
if len(urls) > 0:
attr += ' (from ' + ', '.join(sorted(urls)) + ')'
return attr
class FossaConfig:
def __init__(self, configData = {}):
self.data = {}
......@@ -28,6 +42,31 @@ class FossaConfig:
return set()
class FossaSectionDiff:
def __init__(self, title, missing, extra, changed):
self.title = title
self.missing = missing
self.extra = extra
self.changed = changed
def __str__(self):
red = lambda x: Fore.RED + Style.BRIGHT + x + Style.RESET_ALL
green = lambda x: Fore.GREEN + Style.BRIGHT + x + Style.RESET_ALL
def urlString(name):
missing = ['-%s' % red(url) for url in self.changed[name]['missing']]
extra = ['+%s' % green(url) for url in self.changed[name]['extra']]
return ', '.join(missing + extra)
lines = bannerLines(self.title)
lines.extend(['- %s' % red(attrStr(name, self.missing[name])) for name in sorted(self.missing.keys())])
lines.extend(['+ %s' % green(attrStr(name, self.extra[name])) for name in sorted(self.extra.keys())])
lines.extend([
'~ ' + name + ' (from ' + urlString(name) + ')'
for name in sorted(self.changed.keys())
])
return '\n'.join(lines) + '\n'
class FossaSection:
def __init__(self, title, lines, config):
self.title = title
......@@ -68,12 +107,9 @@ class FossaSection:
basisNames = set(basis.attributions.keys())
selfNames = set(self.attributions.keys())
red = lambda x: Fore.RED + Style.BRIGHT + x + Style.RESET_ALL
green = lambda x: Fore.GREEN + Style.BRIGHT + x + Style.RESET_ALL
missing = ['- %s' % red(basis._attrStr(name)) for name in sorted(basisNames.difference(selfNames))]
extra = ['+ %s' % green(self._attrStr(name)) for name in sorted(selfNames.difference(basisNames))]
changed = []
missing = { name: basis.attributions[name] for name in basisNames.difference(selfNames) }
extra = { name: self.attributions[name] for name in selfNames.difference(basisNames) }
changed = {}
for name in sorted(basisNames.intersection(selfNames)):
basisUrls = basis.attributions[name]
......@@ -86,28 +122,27 @@ class FossaSection:
for url in sorted(allUrls):
if url in basisUrls and url not in selfUrls:
urlMissing.append('-' + red(url))
urlMissing.append(url)
elif url in selfUrls and url not in basisUrls:
urlExtra.append('+' + green(url))
urlExtra.append(url)
changed.append('~ ' + name + ' (from ' + ', '.join(urlMissing + urlExtra) + ')')
changed[name] = {
'missing': urlMissing,
'extra': urlExtra
}
if len(missing) + len(extra) + len(changed) > 0:
lines = self._bannerLines()
lines.extend(missing)
lines.extend(extra)
lines.extend(changed)
return '\n'.join(lines) + '\n'
return FossaSectionDiff(self.title, missing, extra, changed)
return None
def __str__(self):
lines = self._bannerLines()
lines = bannerLines(self.title)
lines.extend(self.header)
for name in sorted(self.attributions):
lines.append('- ' + self._attrStr(name))
lines.append('- ' + attrStr(name, self.attributions[name]))
lines.extend(self.footer)
......@@ -116,19 +151,12 @@ class FossaSection:
return '\n'.join(lines) + '\n'
def _bannerLines(self):
if self.title is not None:
return ['=' * 72, self.title, '=' * 72]
return []
def _attrStr(self, name):
attr = name
urls = self.attributions[name]
class FossaNoticeDiff:
def __init__(self, diffs):
self.diffs = diffs
if len(urls) > 0:
attr += ' (from ' + ', '.join(sorted(urls)) + ')'
return attr
def __str__(self):
return '\n'.join([str(diff) for diff in self.diffs])
class FossaNotice:
def __init__(self, noticeFileContents, config = None):
......@@ -162,7 +190,7 @@ class FossaNotice:
diffs.append(diff)
if len(diffs) > 0:
return '\n'.join(diffs)
return FossaNoticeDiff(diffs)
return None
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment