Skip to content

dag: add --outs option #4739

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

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions dvc/command/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _show_dot(G):
return dot_file.getvalue()


def _build(G, target=None, full=False):
def _build(G, target=None, full=False, outs=False):
import networkx as nx

from dvc.repo.graph import get_pipeline, get_pipelines
Expand All @@ -44,8 +44,25 @@ def _build(G, target=None, full=False):
else:
H = G

def _relabel(stage):
return stage.addressing
if outs:
G = nx.DiGraph()
for stage in H.nodes:
G.add_nodes_from(stage.outs)

for from_stage, to_stage in nx.edge_dfs(H):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for edge_dfs

G.add_edges_from(
[
(from_out, to_out)
for from_out in from_stage.outs
for to_out in to_stage.outs
]
)
H = G

def _relabel(node):
from dvc.stage import Stage

return node.addressing if isinstance(node, Stage) else str(node)

return nx.relabel_nodes(H, _relabel, copy=False)

Expand All @@ -64,7 +81,12 @@ def run(self):
return 1
target = stages[0]

G = _build(self.repo.graph, target=target, full=self.args.full,)
G = _build(
self.repo.graph,
target=target,
full=self.args.full,
outs=self.args.outs,
)

if self.args.dot:
logger.info(_show_dot(G))
Expand Down Expand Up @@ -108,6 +130,13 @@ def add_parser(subparsers, parent_parser):
"showing DAG consisting only of ancestors."
),
)
dag_parser.add_argument(
"-o",
"--outs",
action="store_true",
default=False,
help="Print output files instead of stages.",
)
dag_parser.add_argument(
"target",
nargs="?",
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/command/test_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ def test_build_target(graph):
assert set(G.edges()) == {("3", "a.dvc"), ("3", "b.dvc")}


def test_build_target_with_outs(graph):
(stage,) = filter(
lambda s: hasattr(s, "name") and s.name == "3", graph.nodes()
)
G = _build(graph, target=stage, outs=True)
assert set(G.nodes()) == {"a", "b", "h", "i"}
assert set(G.edges()) == {
("h", "a"),
("h", "b"),
("i", "a"),
("i", "b"),
}


def test_build_full(graph):
(stage,) = filter(
lambda s: hasattr(s, "name") and s.name == "3", graph.nodes()
Expand Down