Skip to content

Commit

Permalink
Merge pull request #2199 from PieterjanMontens/feature/graph-nested-json
Browse files Browse the repository at this point in the history
Added nested json output to pipenv graph command
  • Loading branch information
uranusjr authored May 17, 2018
2 parents c10b275 + 70a7b2b commit 85c641d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pipenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,13 +841,14 @@ def update(
)
@option('--bare', is_flag=True, default=False, help="Minimal output.")
@option('--json', is_flag=True, default=False, help="Output JSON.")
@option('--json-tree', is_flag=True, default=False, help="Output JSON in nested tree.")
@option(
'--reverse', is_flag=True, default=False, help="Reversed dependency graph."
)
def graph(bare=False, json=False, reverse=False):
def graph(bare=False, json=False, json_tree=False, reverse=False):
from .core import do_graph

do_graph(bare=bare, json=json, reverse=reverse)
do_graph(bare=bare, json=json, json_tree=json_tree, reverse=reverse)


@command(short_help="View a given module in your editor.", name="open")
Expand Down
34 changes: 33 additions & 1 deletion pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2409,7 +2409,7 @@ def do_check(three=None, python=False, system=False, unused=False, args=None):
sys.exit(1)


def do_graph(bare=False, json=False, reverse=False):
def do_graph(bare=False, json=False, json_tree=False, reverse=False):
import pipdeptree
try:
python_path = which('python')
Expand All @@ -2433,9 +2433,31 @@ def do_graph(bare=False, json=False, reverse=False):
err=True,
)
sys.exit(1)
if reverse and json_tree:
click.echo(
u'{0}: {1}'.format(
crayons.red('Warning', bold=True),
u'Using both --reverse and --json-tree together is not supported. '
u'Please select one of the two options.',
),
err=True,
)
sys.exit(1)
if json and json_tree:
click.echo(
u'{0}: {1}'.format(
crayons.red('Warning', bold=True),
u'Using both --json and --json-tree together is not supported. '
u'Please select one of the two options.',
),
err=True,
)
sys.exit(1)
flag = ''
if json:
flag = '--json'
if json_tree:
flag = '--json-tree'
if reverse:
flag = '--reverse'
if not project.virtualenv_exists:
Expand Down Expand Up @@ -2465,6 +2487,16 @@ def do_graph(bare=False, json=False, reverse=False):
data.append(d)
click.echo(simplejson.dumps(data, indent=4))
sys.exit(0)
elif json_tree:
def traverse(obj):
if isinstance(obj, list):
return [traverse(package) for package in obj if package['key'] not in BAD_PACKAGES]
else:
obj['dependencies'] = traverse(obj['dependencies'])
return obj
data = traverse(simplejson.loads(c.out))
click.echo(simplejson.dumps(data, indent=4))
sys.exit(0)
else:
for line in c.out.split('\n'):
# Ignore bad packages as top level.
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_pipenv_graph(PipenvInstance, pypi):
p.pipenv('install requests')
assert 'requests' in p.pipenv('graph').out
assert 'requests' in p.pipenv('graph --json').out
assert 'requests' in p.pipenv('graph --json-tree').out


@pytest.mark.cli
Expand Down

0 comments on commit 85c641d

Please sign in to comment.