mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-01 23:08:43 +01:00
metrics: accept any viable target in DvcRepo (#4590)
This commit is contained in:
+19
-16
@@ -11,28 +11,31 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _collect_metrics(repo, targets, recursive):
|
||||
metrics = set()
|
||||
|
||||
if targets:
|
||||
target_infos = [
|
||||
PathInfo(os.path.abspath(target)) for target in targets
|
||||
]
|
||||
tree = RepoTree(repo)
|
||||
|
||||
rec_files = []
|
||||
if recursive:
|
||||
for target_info in target_infos:
|
||||
if tree.isdir(target_info):
|
||||
rec_files.extend(list(tree.walk_files(target_info)))
|
||||
|
||||
result = [t for t in target_infos if tree.isfile(t)]
|
||||
result.extend(rec_files)
|
||||
|
||||
return result
|
||||
|
||||
metrics = set()
|
||||
for stage in repo.stages:
|
||||
for out in stage.outs:
|
||||
if not out.metric:
|
||||
continue
|
||||
|
||||
metrics.add(out.path_info)
|
||||
|
||||
if not targets:
|
||||
return list(metrics)
|
||||
|
||||
target_infos = [PathInfo(os.path.abspath(target)) for target in targets]
|
||||
|
||||
def _filter(path_info):
|
||||
for info in target_infos:
|
||||
func = path_info.isin_or_eq if recursive else path_info.__eq__
|
||||
if func(info):
|
||||
return True
|
||||
return False
|
||||
|
||||
return list(filter(_filter, metrics))
|
||||
return list(metrics)
|
||||
|
||||
|
||||
def _extract_metrics(metrics, path, rev):
|
||||
|
||||
@@ -195,3 +195,17 @@ def test_metrics_diff_cli(tmp_dir, scm, dvc, run_copy_metrics, caplog, capsys):
|
||||
"Path Metric Old New Change\n"
|
||||
"m.yaml foo 1.23457 3.45679 2.22222"
|
||||
)
|
||||
|
||||
|
||||
def test_metrics_diff_non_metrics(tmp_dir, scm, dvc):
|
||||
def _gen(val):
|
||||
tmp_dir.scm_gen({"some_file.yaml": f"foo: {val}"}, commit=str(val))
|
||||
|
||||
_gen(1)
|
||||
_gen(2)
|
||||
_gen(3)
|
||||
|
||||
result = dvc.metrics.diff(targets=["some_file.yaml"], a_rev="HEAD~2")
|
||||
assert result == {
|
||||
"some_file.yaml": {"foo": {"old": 1, "new": 3, "diff": 2}}
|
||||
}
|
||||
|
||||
@@ -110,3 +110,41 @@ def test_missing_cache(tmp_dir, dvc, run_copy_metrics):
|
||||
remove(stage.outs[0].cache_path)
|
||||
|
||||
assert dvc.metrics.show() == {"": {"metrics.yaml": 1.1}}
|
||||
|
||||
|
||||
def test_show_non_metric(tmp_dir, dvc):
|
||||
tmp_dir.gen("metrics.yaml", "foo: 1.1")
|
||||
|
||||
assert dvc.metrics.show(targets=["metrics.yaml"]) == {
|
||||
"": {"metrics.yaml": {"foo": 1.1}}
|
||||
}
|
||||
|
||||
|
||||
def test_show_non_metric_branch(tmp_dir, scm, dvc):
|
||||
tmp_dir.scm_gen("metrics.yaml", "foo: 1.1", commit="init")
|
||||
with tmp_dir.branch("branch", new=True):
|
||||
tmp_dir.scm_gen("metrics.yaml", "foo: 2.2", commit="other")
|
||||
|
||||
assert dvc.metrics.show(targets=["metrics.yaml"], revs=["branch"]) == {
|
||||
"workspace": {"metrics.yaml": {"foo": 1.1}},
|
||||
"branch": {"metrics.yaml": {"foo": 2.2}},
|
||||
}
|
||||
|
||||
|
||||
def test_non_metric_and_recurisve_show(tmp_dir, dvc, run_copy_metrics):
|
||||
tmp_dir.gen(
|
||||
{"metrics_t.yaml": "foo: 1.1", "metrics": {"metric1.yaml": "bar: 1.2"}}
|
||||
)
|
||||
|
||||
metric2 = os.fspath(tmp_dir / "metrics" / "metric2.yaml")
|
||||
run_copy_metrics("metrics_t.yaml", metric2, metrics=[metric2])
|
||||
|
||||
assert dvc.metrics.show(
|
||||
targets=["metrics_t.yaml", "metrics"], recursive=True
|
||||
) == {
|
||||
"": {
|
||||
os.path.join("metrics", "metric1.yaml"): {"bar": 1.2},
|
||||
os.path.join("metrics", "metric2.yaml"): {"foo": 1.1},
|
||||
"metrics_t.yaml": {"foo": 1.1},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user