-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
repo: add API and CLI for reading artifacts #9770
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8f51810
deps: add GTO as a dependency
pmrowla 4770faa
deps: bump dvc-studio-client to 0.13.0
pmrowla d3fb635
fs: return downloaded count in fs.download()
pmrowla de6b122
utils: add type hints for resolve_output
pmrowla 21f97a8
artifacts: add `dvc artifacts get`
pmrowla 6f4a20a
api: add `api.artifacts_show`
pmrowla 9f332fa
artifacts get: support --remote, --remote-config
pmrowla c6094e0
gto: update to 1.3.0
pmrowla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Any, Dict, Optional | ||
|
||
from dvc.repo import Repo | ||
|
||
|
||
def artifacts_show( | ||
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import argparse | ||
import logging | ||
|
||
from dvc.cli import completion | ||
from dvc.cli.command import CmdBaseNoRepo | ||
from dvc.cli.utils import DictAction, append_doc_link, fix_subparsers | ||
from dvc.exceptions import DvcException | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CmdArtifactsGet(CmdBaseNoRepo): | ||
def run(self): | ||
from dvc.repo.artifacts import Artifacts | ||
from dvc.scm import CloneError | ||
from dvc.ui import ui | ||
|
||
try: | ||
count, out = Artifacts.get( | ||
self.args.url, | ||
name=self.args.name, | ||
version=self.args.rev, | ||
stage=self.args.stage, | ||
force=self.args.force, | ||
config=self.args.config, | ||
remote=self.args.remote, | ||
remote_config=self.args.remote_config, | ||
out=self.args.out, | ||
) | ||
ui.write(f"Downloaded {count} file(s) to '{out}'") | ||
return 0 | ||
except CloneError: | ||
logger.exception("failed to get '%s'", self.args.name) | ||
return 1 | ||
except DvcException: | ||
logger.exception( | ||
"failed to get '%s' from '%s'", self.args.name, self.args.url | ||
) | ||
return 1 | ||
|
||
|
||
def add_parser(subparsers, parent_parser): | ||
ARTIFACTS_HELP = "DVC model registry artifact commands." | ||
|
||
artifacts_parser = subparsers.add_parser( | ||
"artifacts", | ||
parents=[parent_parser], | ||
description=append_doc_link(ARTIFACTS_HELP, "artifacts"), | ||
help=ARTIFACTS_HELP, | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
) | ||
artifacts_subparsers = artifacts_parser.add_subparsers( | ||
dest="cmd", | ||
help="Use `dvc artifacts CMD --help` to display command-specific help.", | ||
) | ||
fix_subparsers(artifacts_subparsers) | ||
|
||
ARTIFACTS_GET_HELP = "Download an artifact from a DVC project." | ||
get_parser = artifacts_subparsers.add_parser( | ||
"get", | ||
parents=[parent_parser], | ||
description=append_doc_link(ARTIFACTS_GET_HELP, "artifacts/get"), | ||
help=ARTIFACTS_HELP, | ||
formatter_class=argparse.RawDescriptionHelpFormatter, | ||
) | ||
get_parser.add_argument("url", help="Location of DVC repository to download from") | ||
dberenbaum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
get_parser.add_argument( | ||
"name", help="Name of artifact in the repository" | ||
).complete = completion.FILE | ||
get_parser.add_argument( | ||
"--rev", | ||
nargs="?", | ||
help="Artifact version", | ||
metavar="<version>", | ||
) | ||
Comment on lines
+70
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't use |
||
get_parser.add_argument( | ||
"--stage", | ||
nargs="?", | ||
help="Artifact stage", | ||
metavar="<stage>", | ||
) | ||
get_parser.add_argument( | ||
"-o", | ||
"--out", | ||
nargs="?", | ||
help="Destination path to download artifact to", | ||
metavar="<path>", | ||
).complete = completion.DIR | ||
get_parser.add_argument( | ||
"-j", | ||
"--jobs", | ||
type=int, | ||
help=( | ||
"Number of jobs to run simultaneously. " | ||
"The default value is 4 * cpu_count(). " | ||
), | ||
metavar="<number>", | ||
) | ||
get_parser.add_argument( | ||
"-f", | ||
"--force", | ||
action="store_true", | ||
default=False, | ||
help="Override local file or folder if exists.", | ||
) | ||
get_parser.add_argument( | ||
"--config", | ||
type=str, | ||
help=( | ||
"Path to a config file that will be merged with the config " | ||
"in the target repository." | ||
), | ||
) | ||
get_parser.add_argument( | ||
"--remote", | ||
type=str, | ||
help=( | ||
"Remote name to set as a default in the target repository " | ||
"(only applicable when downloading from DVC remote)." | ||
), | ||
) | ||
get_parser.add_argument( | ||
"--remote-config", | ||
type=str, | ||
nargs="*", | ||
action=DictAction, | ||
help=( | ||
"Remote config options to merge with a remote's config (default or one " | ||
"specified by '--remote') in the target repository (only applicable " | ||
"when downloading from DVC remote)." | ||
), | ||
) | ||
get_parser.set_defaults(func=CmdArtifactsGet) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think all these options are actually mutually exclusive. Would be good to explicitly handle that early in the code.
I am a little confused about the GTO code as it just silently overrides
tags
in:https://github.com/iterative/gto/blob/c82563d988ea927d9cd0275bb0c2f288dd73e0b6/gto/tag.py#L158C13-L163
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it seems like they are supposed to be mutually exclusive, but I wasn't sure either since GTO doesn't check for it.
CC: @aguschin