Skip to content

Commit

Permalink
parse: parse target correctly for generated stages (iterative#4961)
Browse files Browse the repository at this point in the history
  • Loading branch information
skshetry authored Nov 25, 2020
1 parent c989b28 commit 1294450
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
14 changes: 12 additions & 2 deletions dvc/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,31 @@ def error_link(name):
return format_link(f"https://error.dvc.org/{name}")


def parse_target(target, default=None):
def parse_target(target: str, default: str = None):
from dvc.dvcfile import PIPELINE_FILE, PIPELINE_LOCK, is_valid_filename
from dvc.exceptions import DvcException
from dvc.parsing import JOIN

if not target:
return None, None

match = TARGET_REGEX.match(target)
# look for first "@", so as not to assume too much about stage name
# eg: it might contain ":" in a generated stages from dict which might
# affect further parsings with the regex.
group, _, key = target.partition(JOIN)
match = TARGET_REGEX.match(group)

if not match:
return target, None

path, name = (
match.group("path"),
match.group("name"),
)

if name and key:
name += f"{JOIN}{key}"

if path:
if os.path.basename(path) == PIPELINE_LOCK:
raise DvcException(
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ def test_resolve_output(inp, out, is_dir, expected, mocker):
["something.dvc:name", ("something.dvc", "name"), None],
["../something.dvc:name", ("../something.dvc", "name"), None],
["file", (None, "file"), None],
["build@15", (None, "build@15"), None],
["build@{'level': 35}", (None, "build@{'level': 35}"), None],
[":build@15", ("dvc.yaml", "build@15"), None],
[":build@{'level': 35}", ("dvc.yaml", "build@{'level': 35}"), None],
["dvc.yaml:build@15", ("dvc.yaml", "build@15"), None],
[
"dvc.yaml:build@{'level': 35}",
("dvc.yaml", "build@{'level': 35}"),
None,
],
[
"build2@{'level': [1, 2, 3]}",
(None, "build2@{'level': [1, 2, 3]}"),
None,
],
[
":build2@{'level': [1, 2, 3]}",
("dvc.yaml", "build2@{'level': [1, 2, 3]}"),
None,
],
[
"dvc.yaml:build2@{'level': [1, 2, 3]}",
("dvc.yaml", "build2@{'level': [1, 2, 3]}"),
None,
],
],
)
def test_parse_target(inp, out, default):
Expand Down

0 comments on commit 1294450

Please sign in to comment.