Skip to content

Commit

Permalink
non-strict digraph, but remove exact duplicates from edge list
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Jan 21, 2023
1 parent 295124d commit a21e4e7
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion johnnydep/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Display dependency tree of Python distribution"""

__version__ = "1.17.3"
__version__ = "1.17.4"

from johnnydep.lib import *
9 changes: 7 additions & 2 deletions johnnydep/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def main(argv=None, stdout=None):
)
parser.add_argument(
"--no-deps",
help="Don't recurse the dependency tree. Has no effect for output format 'human'",
help="Don't recurse the dependency tree",
dest="recurse",
action="store_false",
)
Expand Down Expand Up @@ -87,6 +87,11 @@ def main(argv=None, stdout=None):
extra_index_url=args.extra_index_url,
ignore_errors=args.ignore_errors,
)
print(dist.serialise(fields=args.fields, format=args.output_format, recurse=args.recurse), file=stdout)
rendered = dist.serialise(
fields=args.fields,
format=args.output_format,
recurse=args.recurse,
)
print(rendered, file=stdout)
if (args.recurse and has_error(dist)) or (not args.recurse and dist.error is not None):
sys.exit(1)
4 changes: 3 additions & 1 deletion johnnydep/dot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from anytree import LevelOrderIter

import johnnydep
from johnnydep.compat import dict
from johnnydep.util import CircularMarker


template = """\
%(comment)s
strict digraph %(title)s {
digraph %(title)s {
%(edges)s
}
"""
Expand Down Expand Up @@ -36,6 +37,7 @@ def jd2dot(dist, comment=None):
label = ' [label="{}"]'.format(spec) if spec else ""
edge = '"{}" -> "{}"'.format(parent_node_name, node_name)
edges.append(edge + label + ";")
edges = list(dict.fromkeys(edges)) # order-preserving de-dupe
edges = "\n ".join(edges)
if not edges:
# project with no dependencies - it's just a single node
Expand Down
4 changes: 2 additions & 2 deletions johnnydep/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ def serialise(self, fields=("name", "summary"), recurse=True, format=None):
if format == "pinned":
# user-specified fields are ignored/invalid in this case
fields = ("pinned",)
if format == "dot":
return jd2dot(self)
data = [dict([(f, getattr(self, f, None)) for f in fields])]
if format == "human":
table = gen_table(self, extra_cols=fields)
Expand All @@ -302,8 +304,6 @@ def serialise(self, fields=("name", "summary"), recurse=True, format=None):
result = "\n".join([toml.dumps(d) for d in data])
elif format == "pinned":
result = "\n".join([d["pinned"] for d in data])
elif format == "dot":
result = jd2dot(self)
else:
raise JohnnyError("Unsupported format")
return result
Expand Down
2 changes: 2 additions & 0 deletions johnnydep/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import unicode_literals

import json
import structlog
from argparse import ArgumentTypeError
from subprocess import check_output
from subprocess import CalledProcessError
Expand Down Expand Up @@ -45,6 +46,7 @@ def __init__(self, summary, parent):
self.name = CircularMarker.glyph
self.summary = summary
self.parent = parent
self.log = structlog.get_logger()

def __getattr__(self, name):
if name.startswith("_"):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_dot_export(make_dist):
expected = dedent(
"""
# generated by https://github.com/wimglenn/johnnydep v1.0
strict digraph parent {
digraph parent {
"parent[x]" -> "child" [label=">0.1"];
"parent[x]" -> "Extra";
}
Expand All @@ -34,7 +34,7 @@ def test_nodupes(make_dist):
expected = dedent(
"""
// diamond
strict digraph root {
digraph root {
"root" -> "dep1";
"root" -> "dep2";
"dep1" -> "leaf";
Expand All @@ -54,7 +54,7 @@ def test_circular_graph(make_dist):
expected = dedent(
"""
# cyclic, oh no!
strict digraph dist1 {
digraph dist1 {
"dist1" -> "dist2";
"dist2" -> "dist3";
"dist3" -> "dist1";
Expand All @@ -70,7 +70,7 @@ def test_graph_with_no_edges(make_dist):
actual = jd2dot(dist, comment="")
expected = dedent(
"""
strict digraph lonely {
digraph lonely {
"lonely";
}
"""
Expand Down
9 changes: 9 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import pytest

from johnnydep import JohnnyDist
from johnnydep.cli import FIELDS
from johnnydep.compat import text_type
from johnnydep.util import CircularMarker
from johnnydep.util import python_interpreter


Expand Down Expand Up @@ -46,3 +49,9 @@ def test_good_python_env():
"sys_platform",
"wheel_version",
]


def test_placeholder_serializes():
# this just checks that the placeholder can render to text without issue
dist = CircularMarker(summary=".", parent=None)
JohnnyDist.serialise(dist, fields=FIELDS, format="human")

0 comments on commit a21e4e7

Please sign in to comment.