Skip to content

Commit 4d36bc4

Browse files
authored
params: introduce --targets (#5122)
* params: add targets option * fixup * remove unnecessary changes * fix isort * params: diff: add test for --targets * tests: roll back metric/plots unification, introduce collect test * metrics/plots: refactoring * missing plots test added
1 parent af26d54 commit 4d36bc4

File tree

21 files changed

+251
-139
lines changed

21 files changed

+251
-139
lines changed

dvc/command/params.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
from collections import OrderedDict
44

5+
from dvc.command import completion
56
from dvc.command.base import CmdBase, append_doc_link, fix_subparsers
67
from dvc.exceptions import DvcException
78

@@ -30,11 +31,14 @@ def _show_diff(diff, markdown=False, no_path=False):
3031

3132

3233
class CmdParamsDiff(CmdBase):
34+
UNINITIALIZED = True
35+
3336
def run(self):
3437
try:
3538
diff = self.repo.params.diff(
3639
a_rev=self.args.a_rev,
3740
b_rev=self.args.b_rev,
41+
targets=self.args.targets,
3842
all=self.args.all,
3943
)
4044

@@ -91,6 +95,12 @@ def add_parser(subparsers, parent_parser):
9195
nargs="?",
9296
help=("New Git commit to compare (defaults to the current workspace)"),
9397
)
98+
params_diff_parser.add_argument(
99+
"--targets",
100+
nargs="*",
101+
help="Limit command scope to these params files.",
102+
metavar="<path>",
103+
).complete = completion.FILE
94104
params_diff_parser.add_argument(
95105
"--all",
96106
action="store_true",

dvc/exceptions.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Exceptions raised by the dvc."""
2+
from typing import List
3+
24
from funcy import first
35

46
from dvc.utils import error_link, format_link, relpath
@@ -170,19 +172,19 @@ def __init__(self, paths):
170172
)
171173

172174

173-
class NoMetricsError(DvcException):
175+
class MetricsError(DvcException):
174176
pass
175177

176178

177-
class NoMetricsParsedError(NoMetricsError):
179+
class NoMetricsParsedError(MetricsError):
178180
def __init__(self, command):
179181
super().__init__(
180182
f"Could not parse {command} files. Use `-v` option to see more "
181183
"details."
182184
)
183185

184186

185-
class NoMetricsFoundError(NoMetricsError):
187+
class NoMetricsFoundError(MetricsError):
186188
def __init__(self, command, run_options):
187189
super().__init__(
188190
f"No {command} files in this repository. "
@@ -191,6 +193,15 @@ def __init__(self, command, run_options):
191193
)
192194

193195

196+
class MetricDoesNotExistError(MetricsError):
197+
def __init__(self, targets: List[str]):
198+
if len(targets) == 1:
199+
msg = "File: '{}' does not exist."
200+
else:
201+
msg = "Files: '{}' do not exist."
202+
super().__init__(msg.format(", ".join(targets)))
203+
204+
194205
class RecursiveAddingWhileUsingFilename(DvcException):
195206
def __init__(self):
196207
super().__init__(

dvc/repo/metrics/diff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from dvc.exceptions import NoMetricsError
1+
from dvc.exceptions import MetricsError
22
from dvc.utils.diff import diff as _diff
33
from dvc.utils.diff import format_dict
44

@@ -7,7 +7,7 @@ def _get_metrics(repo, *args, revs=None, **kwargs):
77
try:
88
metrics = repo.metrics.show(*args, **kwargs, revs=revs)
99
return metrics
10-
except NoMetricsError:
10+
except MetricsError:
1111
return {}
1212

1313

dvc/repo/metrics/show.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import logging
22

3-
from dvc.exceptions import NoMetricsFoundError, NoMetricsParsedError
3+
from dvc.exceptions import (
4+
MetricDoesNotExistError,
5+
NoMetricsFoundError,
6+
NoMetricsParsedError,
7+
)
48
from dvc.repo import locked
59
from dvc.repo.collect import collect
610
from dvc.scm.base import SCMError
@@ -105,6 +109,8 @@ def show(
105109
if not res:
106110
if metrics_found:
107111
raise NoMetricsParsedError("metrics")
112+
elif targets:
113+
raise MetricDoesNotExistError(targets)
108114
else:
109115
raise NoMetricsFoundError("metrics", "-m/-M")
110116

dvc/repo/params/show.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from dvc.path_info import PathInfo
88
from dvc.repo import locked
99
from dvc.repo.collect import collect
10+
from dvc.scm.base import SCMError
1011
from dvc.stage import PipelineStage
1112
from dvc.utils.serialize import LOADERS, ParseError
1213

@@ -25,11 +26,20 @@ def _is_params(dep: "BaseOutput"):
2526
return isinstance(dep, ParamsDependency)
2627

2728

28-
def _collect_configs(repo: "Repo", rev) -> List[PathInfo]:
29-
params, _ = collect(repo, deps=True, output_filter=_is_params, rev=rev)
30-
configs = {p.path_info for p in params}
31-
configs.add(PathInfo(repo.root_dir) / ParamsDependency.DEFAULT_PARAMS_FILE)
32-
return list(configs)
29+
def _collect_configs(repo: "Repo", rev, targets=None) -> List[PathInfo]:
30+
params, path_infos = collect(
31+
repo,
32+
targets=targets or [],
33+
deps=True,
34+
output_filter=_is_params,
35+
rev=rev,
36+
)
37+
path_infos.update({p.path_info for p in params})
38+
if not targets:
39+
path_infos.add(
40+
PathInfo(repo.root_dir) / ParamsDependency.DEFAULT_PARAMS_FILE
41+
)
42+
return list(path_infos)
3343

3444

3545
def _read_params(repo, configs, rev):
@@ -66,11 +76,11 @@ def _collect_vars(repo, params):
6676

6777

6878
@locked
69-
def show(repo, revs=None):
79+
def show(repo, revs=None, targets=None):
7080
res = {}
7181

7282
for branch in repo.brancher(revs=revs):
73-
configs = _collect_configs(repo, branch)
83+
configs = _collect_configs(repo, branch, targets)
7484
params = _read_params(repo, configs, branch)
7585
vars_params = _collect_vars(repo, params)
7686

@@ -87,8 +97,10 @@ def show(repo, revs=None):
8797
# Hide workspace params if they are the same as in the active branch
8898
try:
8999
active_branch = repo.scm.active_branch()
90-
except TypeError:
91-
pass # Detached head
100+
except (TypeError, SCMError):
101+
# TypeError - detached head
102+
# SCMError - no repo case
103+
pass
92104
else:
93105
if res.get("workspace") == res.get(active_branch):
94106
res.pop("workspace", None)

dvc/repo/plots/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from dvc.exceptions import (
66
DvcException,
7+
MetricDoesNotExistError,
78
NoMetricsFoundError,
89
NoMetricsParsedError,
910
)
@@ -96,7 +97,6 @@ def render(data, revs=None, props=None, templates=None):
9697
return result
9798

9899
def show(self, targets=None, revs=None, props=None, templates=None):
99-
from .data import NoMetricInHistoryError
100100

101101
data = self.collect(targets, revs)
102102

@@ -105,7 +105,7 @@ def show(self, targets=None, revs=None, props=None, templates=None):
105105
for target in targets:
106106
rpath = relpath(target, self.repo.root_dir)
107107
if not any("data" in d[rpath] for d in data.values()):
108-
raise NoMetricInHistoryError(target)
108+
raise MetricDoesNotExistError([target])
109109

110110
# No data at all is a special error with a special message
111111
if not data:

dvc/repo/plots/data.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ def __init__(self):
2626
)
2727

2828

29-
class NoMetricInHistoryError(DvcException):
30-
def __init__(self, path):
31-
super().__init__(f"Could not find '{path}'.")
32-
33-
3429
class PlotParsingError(ParseError):
3530
def __init__(self, path, revision):
3631
self.path = path

tests/func/metrics/plots/__init__.py

Whitespace-only changes.

tests/func/metrics/test_common.py

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)