Skip to content

Commit

Permalink
Add JSON output support for subcommand reqcheck
Browse files Browse the repository at this point in the history
This will make the post-processing easier.
Additionally, we depreciate the use of '--spec'
in favor of '--output spec'.

Change-Id: Ib8eb4f778208db876cc2d8d97a307664b99164c8
Depends-On: https://softwarefactory-project.io/r/#/c/17281/
  • Loading branch information
jcapiitao committed Mar 16, 2020
1 parent 80248ad commit 7a5dbeb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
33 changes: 23 additions & 10 deletions rdopkg/actionmods/reqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from distroinfo.query import get_distrepos
import re
import pymod2pkg
import json

from rdopkg.actionmods import rdoinfo
from rdopkg.actionmods import query
Expand Down Expand Up @@ -259,28 +260,40 @@ def reqcheck(desired_reqs, reqs):
def print_reqcheck(met, any_version, wrong_version, missing, excess, removed,
format=None):
cats = [
("{t.bold_green}MET{t.normal}:", met),
("{t.bold}VERSION NOT ENFORCED{t.normal}:", any_version),
("{t.bold}ADDITIONAL REQUIRES{t.normal}:", excess),
("{t.bold_yellow}VERSION MISMATCH{t.normal}:", wrong_version),
("{t.bold_red}MISSING{t.normal}:", missing),
("{t.bold_red}REMOVED{t.normal}:", removed),
("{t.bold_green}{category}{t.normal}:", "met", met),
("{t.bold}{category}{t.normal}:", "any", any_version),
("{t.bold}{category}{t.normal}:", "excess", excess),
("{t.bold_yellow}{category}{t.normal}:", "mismatch",
wrong_version),
("{t.bold_red}{category}{t.normal}:", "missing", missing),
("{t.bold_red}{category}{t.normal}:", "removed", removed),
]
if format == 'spec':
if format == 'json':
output = {}
for title, category, reqs in cats:
if category not in output:
output[category] = list()
for req in reqs:
output[category].append({'name': req.name, 'vers': req.vers})
json_output = json.JSONEncoder().encode(output)
print(json_output)
return
elif format == 'spec':
# get alignment from .spec file
spec = specfile.Spec()
pre = 'Requires:' + (spec.get_tag_align_ws('Requires') or ' ')
else:
elif format == 'text':
pre = ' '

first = True
for title, reqs in cats:
for title, category, reqs in cats:
if not reqs:
continue
if first:
first = False
else:
print("")
print(title.format(t=log.term))
print(title.format(t=log.term, category=category.upper()))
reqs = [x.__str__(format=format) for x in reqs]
helpers.print_list(reqs, pre=pre)

Expand Down
6 changes: 5 additions & 1 deletion rdopkg/actions/reqs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
],
optional_args=[
Arg('spec', shortcut='-s', action='store_true',
help="output .spec Requires: for easy pasting"),
help="output .spec Requires: for easy pasting"
"Deprecated: use output instead."),
Arg('strict', shortcut='-S', action='store_true',
help="Fail if not strictly matching"),
Arg('version', shortcut='-R', metavar='VERSION',
help="query requirements.txt from VERSION git ref"),
Arg('output', shortcut='-o', default='text',
help="output format to be used "
"(e.g: json, spec, text [default])"),
Arg('python_version', shortcut='-p',
help="set the python version reference to evaluate if a "
"dependency should be checked or not (default 3.6)"),
Expand Down
10 changes: 6 additions & 4 deletions rdopkg/actions/reqs/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def reqdiff(version_tag_from, version_tag_to):
_reqs.print_reqdiff(*rdiff)


def reqcheck(version, spec=False, strict=False, python_version='3.6'):
def reqcheck(version, spec=False, output=None, strict=False,
python_version='3.6'):
m = re.search(r'^[\d]\.[\d]$', python_version)
if not m:
raise exception.WrongPythonVersion()
Expand All @@ -44,10 +45,11 @@ def reqcheck(version, spec=False, strict=False, python_version='3.6'):
check = _reqs.reqcheck_spec(python_version, reqs_txt=path)
else:
check = _reqs.reqcheck_spec(python_version, ref=version)
format = None
if output not in ['spec', 'json', 'text']:
raise exception.WrongOutputFormat()
if spec:
format = 'spec'
_reqs.print_reqcheck(*check, format=format)
output = 'spec'
_reqs.print_reqcheck(*check, format=output)
if strict:
# missing
if len(check[-1]) > 0:
Expand Down
5 changes: 5 additions & 0 deletions rdopkg/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,8 @@ class LintProblemsFound(RdopkgException):

class WrongPythonVersion(RdopkgException):
msg_fmt = "The python version specified is wrong (set '3.6' for python3.6)"


class WrongOutputFormat(RdopkgException):
msg_fmt = "The output format used is not supported, use rdopkg reqcheck -h"
exit_code = 1
2 changes: 1 addition & 1 deletion tests/test_reqcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_reqcheck_excess(tmpdir, capsys):
cap = capsys.readouterr()
o = cap.out
_assert_sanity_out(o)
assert 'ADDITIONAL REQUIRES:' in o
assert 'EXCESS:' in o


def test_reqcheck_wrong_python_version(tmpdir, capsys):
Expand Down

0 comments on commit 7a5dbeb

Please sign in to comment.