From 4c9cfff13433897f157c865e8b870bec06dd8b21 Mon Sep 17 00:00:00 2001 From: Peter Rowlands Date: Mon, 7 Aug 2023 18:31:48 +0900 Subject: [PATCH] api: add api.artifacts_show --- dvc/api/__init__.py | 4 ++-- dvc/api/artifacts.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 dvc/api/artifacts.py diff --git a/dvc/api/__init__.py b/dvc/api/__init__.py index 64959a0254..c82de3b24f 100644 --- a/dvc/api/__init__.py +++ b/dvc/api/__init__.py @@ -1,6 +1,6 @@ from dvc.fs.dvc import _DVCFileSystem as DVCFileSystem -from . import artifacts +from .artifacts import artifacts_show from .data import open # pylint: disable=redefined-builtin from .data import get_url, read from .experiments import exp_save, exp_show @@ -11,7 +11,7 @@ "all_branches", "all_commits", "all_tags", - "artifacts", + "artifacts_show", "exp_save", "exp_show", "get_url", diff --git a/dvc/api/artifacts.py b/dvc/api/artifacts.py new file mode 100644 index 0000000000..943c1d6ce1 --- /dev/null +++ b/dvc/api/artifacts.py @@ -0,0 +1,47 @@ +from typing import Any, Dict, Optional + +from dvc.repo import Repo + + +def artifacts_show( # noqa, pylint: disable=redefined-builtin + name: str, + version: Optional[str] = None, + stage: Optional[str] = None, + repo: Optional[str] = None, +) -> Dict[str, str]: + """ + Return path and Git revision for an artifact in a DVC project. + + The resulting path and revision can be used in conjunction with other dvc.api + calls to open and read the artifact. + + Args: + name (str): name of the artifact to open. + version (str, optional): version of the artifact to open. Defaults to + the latest version. + stage (str, optional): name of the model registry stage. + repo: (str, optional): path or URL for the DVC repo. + + Returns: + Dictionary of the form: + { + "rev": ..., + "path": ..., + } + + Raises: + dvc.exceptions.ArtifactNotFoundError: The specified artifact was not found in + the repo. + """ + if version and stage: + raise ValueError("Artifact version and stage are mutually exclusive.") + + repo_kwargs: Dict[str, Any] = { + "subrepos": True, + "uninitialized": True, + } + with Repo.open(repo, **repo_kwargs) as _repo: + rev = _repo.artifacts.get_rev(name, version=version, stage=stage) + with _repo.switch(rev): + path = _repo.artifacts.get_path(name) + return {"rev": rev, "path": path}