Refactoring: make methods smaller

This commit is contained in:
Dmitry
2017-09-03 19:01:05 -07:00
parent a1a68c00c9
commit b3b4daf7fa
2 changed files with 35 additions and 23 deletions
+28 -19
View File
@@ -260,29 +260,38 @@ class GitWrapper(GitWrapperI):
files = set(Executor.exec_cmd_only_success(git_cmd).split('\n'))
if target in files:
try:
settings.path_factory.data_item(target)
except DataItemError as ex:
Logger.warn('Target file {} is not data item: {}'.format(target, ex))
return True, None
symlink_content = self._get_symlink_content(hash, target, settings)
if symlink_content is not None:
metric = self.target_metric_from_git_history(hash, symlink_content, target, settings)
else:
metric = None
try:
cmd_symlink_data = ['git', 'show', '{}:{}'.format(hash, target)]
symlink_content = Executor.exec_cmd_only_success(cmd_symlink_data).split('\n')
except ExecutorError as ex:
msg = '[dvc-git] Cannot obtain content of target symbolic file {} with hash {}: {}'
Logger.warn(msg.format(target, hash, ex))
return True, None
if not symlink_content or len(symlink_content) != 1:
msg = '[dvc-git] Target symbolic file {} with hash {} has wrong format'
Logger.warn(msg.format(target, hash))
return True, None
return True, self.target_metric_from_git_history(hash, symlink_content[0], target, settings)
return True, metric
return False, None
def _get_symlink_content(self, hash, target, settings):
try:
settings.path_factory.data_item(target)
except DataItemError as ex:
Logger.warn('Target file {} is not data item: {}'.format(target, ex))
return None
try:
cmd_symlink_data = ['git', 'show', '{}:{}'.format(hash, target)]
symlink_content = Executor.exec_cmd_only_success(cmd_symlink_data).split('\n')
except ExecutorError as ex:
msg = '[dvc-git] Cannot obtain content of target symbolic file {} with hash {}: {}'
Logger.warn(msg.format(target, hash, ex))
return None
if not symlink_content or len(symlink_content) != 1:
msg = '[dvc-git] Target symbolic file {} with hash {} has wrong format'
Logger.warn(msg.format(target, hash))
return None
return symlink_content[0]
def target_metric_from_git_history(self, hash, symlink_content, target, settings):
cache_rel_to_data = os.path.relpath(settings.config.cache_dir, settings.config.data_dir)
common_prefix = os.path.commonprefix([symlink_content, cache_rel_to_data])
+7 -4
View File
@@ -152,10 +152,7 @@ class Workflow(object):
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):
if commit.has_target_metric:
for hash in child_commit_hashes:
if not self._commits[hash].has_target_metric:
self._commits[hash].set_target_metric(commit.target_metric)
self._upstream_target_metric(child_commit_hashes, commit)
for hash in child_commit_hashes:
self._commits[hash].add_parents(commit.parent_hashes)
@@ -170,6 +167,12 @@ class Workflow(object):
return False
pass
def _upstream_target_metric(self, child_commit_hashes, commit):
if commit.has_target_metric:
for hash in child_commit_hashes:
if not self._commits[hash].has_target_metric:
self._commits[hash].set_target_metric(commit.target_metric)
@staticmethod
def _redirect_edges(edges, child_commit_hashes, hash, commit_hashes):
for h in commit_hashes: