From 73339568d014c1595d7859e65a265c3c465c7fa8 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 3 Sep 2017 16:19:29 -0700 Subject: [PATCH] REfactoring: extract _remove_or_collapse() --- dvc/command/show.py | 1 - dvc/workflow.py | 38 +++++++++++++++++++++----------------- tests/test_workflow.py | 4 ++-- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/dvc/command/show.py b/dvc/command/show.py index da2583085..1ec8842db 100644 --- a/dvc/command/show.py +++ b/dvc/command/show.py @@ -47,7 +47,6 @@ class CmdShow(Traverse): return 0 def run(self): - if self.parsed_args.workflow: # Workflow wf = self.git.get_all_commits('data/eval_auc.txt') diff --git a/dvc/workflow.py b/dvc/workflow.py index 93098d06d..fc0edbd51 100644 --- a/dvc/workflow.py +++ b/dvc/workflow.py @@ -82,7 +82,7 @@ class Workflow(object): g = nx.DiGraph(name='DVC Workflow', directed=False) if self._no_repro_commits: - self.collaps_repro_commits() + self.collapse_repro_commits() for hash in set(self._edges.keys() + self._back_edges.keys()): commit = self._commits[hash] @@ -115,26 +115,12 @@ class Workflow(object): self._build_graph(next, g) - def collaps_repro_commits(self): + def collapse_repro_commits(self): hashes_to_remove = [] for commit in self._commits.values(): if commit.is_repro: - parent_commit_hashes = self._back_edges[commit.hash] - child_commit_hashes = self._edges.get(commit.hash) - - # Remove if next commit is dvc-repro as well - if child_commit_hashes and all(self._commits[hash].is_repro for hash in child_commit_hashes): - for hash in child_commit_hashes: - self._commits[hash].add_parents(commit.parent_hashes) - self._commits[hash].remove_parent(commit.hash) - pass - - self._redirect_edges(self._edges, child_commit_hashes, commit.hash, parent_commit_hashes) - self._redirect_edges(self._back_edges, parent_commit_hashes, commit.hash, child_commit_hashes) - + if self._remove_or_collapse(commit): hashes_to_remove.append(commit.hash) - else: - commit.make_colapsed() for hash in hashes_to_remove: del self._commits[hash] @@ -142,6 +128,24 @@ class Workflow(object): del self._back_edges[hash] pass + def _remove_or_collapse(self, commit): + parent_commit_hashes = self._back_edges[commit.hash] + child_commit_hashes = self._edges.get(commit.hash) + + if child_commit_hashes and all(self._commits[hash].is_repro for hash in child_commit_hashes): + for hash in child_commit_hashes: + self._commits[hash].add_parents(commit.parent_hashes) + self._commits[hash].remove_parent(commit.hash) + + self._redirect_edges(self._edges, child_commit_hashes, commit.hash, parent_commit_hashes) + self._redirect_edges(self._back_edges, parent_commit_hashes, commit.hash, child_commit_hashes) + + return True + else: + commit.make_colapsed() + return False + pass + @staticmethod def _redirect_edges(edges, child_commit_hashes, hash, commit_hashes): for h in commit_hashes: diff --git a/tests/test_workflow.py b/tests/test_workflow.py index db9211f36..35402bda9 100644 --- a/tests/test_workflow.py +++ b/tests/test_workflow.py @@ -39,7 +39,7 @@ class TestWorkflow(TestCase): wf.add_commit(self._commit3) wf.add_commit(self._commit4) - wf.collaps_repro_commits() + wf.collapse_repro_commits() self.assertEqual(len(wf._commits), 3) self.assertEqual(wf._commits[self._commit1.hash].text, self._commit1._comment + '\n' + self._commit1.hash) @@ -57,7 +57,7 @@ class TestWorkflow(TestCase): wf.add_commit(self._commit3) # Dead end which cannot be collapsed self.assertEqual(len(wf._commits), 3) - wf.collaps_repro_commits() + wf.collapse_repro_commits() self.assertEqual(len(wf._commits), 2) self.assertEqual(wf._commits[self._commit1.hash].text, self._commit1._comment + '\n' + self._commit1.hash)