-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add JSON output for updateinfo #2200
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,8 @@ | |
from dnf.i18n import _, exact_width | ||
from dnf.pycomp import unicode | ||
|
||
import sys | ||
import json | ||
|
||
def _maxlen(iterable): | ||
"""Return maximum length of items in a non-empty iterable.""" | ||
|
@@ -102,6 +104,9 @@ def set_argparser(parser): | |
parser.add_argument("--with-bz", dest='with_bz', default=False, | ||
action='store_true', | ||
help=_('show only advisories with bugzilla reference')) | ||
parser.add_argument("--json", dest='json', default=False, | ||
action='store_true', | ||
help=_('Display in JSON format.')) | ||
parser.add_argument('spec', nargs='*', metavar='SPEC', | ||
choices=cmds, default=cmds[0], | ||
action=OptionParser.PkgNarrowCallback, | ||
|
@@ -157,6 +162,10 @@ def configure(self): | |
else: | ||
self.opts.spec.insert(0, spec) | ||
|
||
# Keep quiet when dumping JSON output | ||
if self.opts.json: | ||
self.cli.redirect_logger(stdout=sys.maxsize, stderr=sys.maxsize) | ||
|
||
if self.opts.advisory: | ||
self.opts.spec.extend(self.opts.advisory) | ||
|
||
|
@@ -327,10 +336,10 @@ def type2label(typ, sev): | |
elif ref.type == hawkey.REFERENCE_CVE and not self.opts.with_cve: | ||
continue | ||
nevra_inst_dict.setdefault((nevra, installed, advisory.updated), dict())[ref.id] = ( | ||
advisory.type, advisory.severity) | ||
advisory.type, advisory.severity) | ||
else: | ||
nevra_inst_dict.setdefault((nevra, installed, advisory.updated), dict())[advisory.id] = ( | ||
advisory.type, advisory.severity) | ||
advisory.type, advisory.severity) | ||
|
||
advlist = [] | ||
# convert types to labels, find max len of advisory IDs and types | ||
|
@@ -339,15 +348,84 @@ def type2label(typ, sev): | |
nw = max(nw, len(nevra)) | ||
for aid, atypesev in id2type.items(): | ||
idw = max(idw, len(aid)) | ||
typ, sev = atypesev | ||
label = type2label(*atypesev) | ||
# use dnf5 style for JSON output | ||
atype = self.TYPE2LABEL.get(typ, _('unspecified')) | ||
asev = self.SECURITY2LABEL.get(sev, _('None')) | ||
asev = asev.split("/")[0].strip() | ||
Comment on lines
+355
to
+356
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a simpler solution would be to just do: asev = sev It is also what dnf5 does so the output will be more compatible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ack There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for severity, for
while for dnf5 json output, it's simply
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For dnf4 the list output contains a With dnf5 json both |
||
tlw = max(tlw, len(label)) | ||
advlist.append((inst2mark(inst), aid, label, nevra, aupdated)) | ||
|
||
for (inst, aid, label, nevra, aupdated) in advlist: | ||
if self.base.conf.verbose: | ||
print('%s%-*s %-*s %-*s %s' % (inst, idw, aid, tlw, label, nw, nevra, aupdated)) | ||
else: | ||
print('%s%-*s %-*s %s' % (inst, idw, aid, tlw, label, nevra)) | ||
advlist.append((inst2mark(inst), aid, label, atype, asev, nevra, aupdated)) | ||
if self.opts.json: | ||
dtlst = [] | ||
for (inst, aid, label, atype, asev, nevra, aupdated) in advlist: | ||
dtlst.append( | ||
{ | ||
"name": aid, | ||
"type": atype, | ||
"severity": asev, | ||
"nevra": nevra, | ||
"buildtime": aupdated, | ||
Comment on lines
+362
to
+368
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dnf5 has special logic when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, ack. |
||
} | ||
) | ||
print(json.dumps(dtlst, default=str, indent=2)) | ||
else: | ||
for (inst, aid, label, atype, asev, nevra, aupdated) in advlist: | ||
if self.base.conf.verbose: | ||
print('%s%-*s %-*s %-*s %s' % (inst, idw, aid, tlw, label, nw, nevra, aupdated)) | ||
else: | ||
print('%s%-*s %-*s %s' % (inst, idw, aid, tlw, label, nevra)) | ||
|
||
def _process_advisory(self, advisory): | ||
"""Convert DNF advisory object directly to desired format.""" | ||
advisory_id = getattr(advisory, 'id', None) | ||
|
||
package_list = [] | ||
for pkg in getattr(advisory, 'packages', []): | ||
if not getattr(pkg, 'name', None): | ||
continue | ||
pkg_info = { | ||
'name': getattr(pkg, 'name', None), | ||
'evr': getattr(pkg, 'evr', None), | ||
'arch': getattr(pkg, 'arch', None), | ||
} | ||
pkg_str = f"{pkg_info.get('name')}-{pkg_info.get('evr')}" | ||
if pkg_info.get('arch'): | ||
pkg_str += f".{pkg_info.get('arch')}" | ||
package_list.append(pkg_str) | ||
|
||
REFERENCE_TYPES = {0: 'unknown', 1: 'bugzilla', 2: 'cve', 3: 'vendor', 4: 'security'} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is no reference type Also you should not define the numbers here by hand, it would be better to use the exported hawkey constatns: REFERENCE_TYPES = {hawkey.REFERENCE_UNKNOWN: 'unknown', hawkey.REFERENCE_BUGZILLA: 'bugzilla',
hawkey.REFERENCE_CVE: 'cve', hawkey.REFERENCE_VENDOR: 'vendor'} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ack |
||
references = [] | ||
for ref in getattr(advisory, 'references', []): | ||
ref_dict = { | ||
'Title': getattr(ref, 'title', None), | ||
'Id': getattr(ref, 'id', None), | ||
'Type': REFERENCE_TYPES.get(getattr(ref, 'type', 0), 'unknown'), | ||
'Url': getattr(ref, 'href', None) or getattr(ref, 'url', None) or getattr(ref, 'link', None) | ||
} | ||
ref_dict = {k: v for k, v in ref_dict.items() if v is not None} | ||
references.append(ref_dict) | ||
|
||
result = { | ||
advisory_id: { | ||
'Name': advisory_id, | ||
'Title': getattr(advisory, 'title', None), | ||
'Severity': getattr(advisory, 'severity', 'None'), | ||
'Type': self.TYPE2LABEL.get(getattr(advisory, 'type'), _('unspecified')), | ||
'Issued': getattr(advisory, 'updated', '').strftime("%Y-%m-%d %H:%M:%S") | ||
if getattr(advisory, 'updated', None) | ||
else None, | ||
'Description': getattr(advisory, 'description', None), | ||
'Message': '', | ||
'Rights': getattr(advisory, 'rights', None), | ||
'references': references, | ||
'collections': { | ||
'packages': package_list | ||
} | ||
} | ||
} | ||
|
||
return result | ||
|
||
|
||
def display_info(self, apkg_adv_insts): | ||
|
@@ -398,8 +476,14 @@ def advisory2info(advisory, installed): | |
lines.append('%*s%s: %s' % (key_padding, "", key, line)) | ||
return '\n'.join(lines) | ||
|
||
dt_advisories = {} | ||
advisories = set() | ||
for apkg, advisory, installed in apkg_adv_insts: | ||
advisories.add(advisory2info(advisory, installed)) | ||
dt_advisories.update(self._process_advisory(advisory)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer if we did this only when the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ack. |
||
formatted_attributes = advisory2info(advisory, installed) | ||
advisories.add(formatted_attributes) | ||
|
||
print("\n\n".join(sorted(advisories, key=lambda x: x.lower()))) | ||
if self.opts.json: | ||
print(json.dumps(dt_advisories, default=str, indent=2)) | ||
else: | ||
print("\n\n".join(sorted(advisories, key=lambda x: x.lower()))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good but there still are some redundant messages in the output, namely I think the download progress bars.
We could additionally do something similar to what the
--quiet
option does: https://github.com/rpm-software-management/dnf/blob/master/dnf/cli/cli.py#L834-L836To take effect I believe it would have to be set in
pre_configure
of the command.I would add something like:
Perhaps don't change the errorlevel since those message are more crucial.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack. will test this;)