Skip to content

Commit

Permalink
dvc: add rev param to api.open()/read()/get_url()
Browse files Browse the repository at this point in the history
  • Loading branch information
Suor committed Jul 1, 2019
1 parent 3187040 commit 9ba54c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
23 changes: 14 additions & 9 deletions dvc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
from dvc.external_repo import ExternalRepo


def get_url(path, repo=None, remote=None):
def get_url(path, repo=None, rev=None, remote=None):
"""Returns an url of a resource specified by path in repo"""
with _make_repo(repo) as _repo:
with _make_repo(repo, rev=rev) as _repo:
abspath = os.path.join(_repo.root_dir, path)
out, = _repo.find_outs_by_path(abspath)
remote_obj = _repo.cloud.get_remote(remote)
return str(remote_obj.checksum_to_path_info(out.checksum))


def open(path, repo=None, remote=None, mode="r", encoding=None):
def open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
"""Opens a specified resource as a file descriptor"""
args = (path,)
kwargs = {
"repo": repo,
"remote": remote,
"rev": rev,
"mode": mode,
"encoding": encoding,
}
Expand All @@ -45,30 +46,34 @@ def __getattr__(self, name):
)


def _open(path, repo=None, remote=None, mode="r", encoding=None):
with _make_repo(repo) as _repo:
def _open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
with _make_repo(repo, rev=rev) as _repo:
abspath = os.path.join(_repo.root_dir, path)
with _repo.open(
abspath, remote=remote, mode=mode, encoding=encoding
) as fd:
yield fd


def read(path, repo=None, remote=None, mode="r", encoding=None):
def read(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
"""Read a specified resource into string"""
with open(path, repo, remote=remote, mode=mode, encoding=encoding) as fd:
with open(
path, repo=repo, rev=rev, remote=remote, mode=mode, encoding=encoding
) as fd:
return fd.read()


@contextmanager
def _make_repo(repo_url):
def _make_repo(repo_url, rev=None):
if not repo_url or urlparse(repo_url).scheme == "":
assert rev is None, "Custom revision is not supported for local repo"
yield Repo(repo_url)
else:
tmp_dir = tempfile.mkdtemp("dvc-repo")
try:
ext_repo = ExternalRepo(tmp_dir, url=repo_url)
ext_repo = ExternalRepo(tmp_dir, url=repo_url, rev=rev)
ext_repo.install()
yield ext_repo.repo
finally:
ext_repo.repo.scm.git.close()
remove(tmp_dir)
12 changes: 9 additions & 3 deletions tests/func/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,22 @@ def test_open(repo_dir, dvc_repo, remote_url):


def test_open_external(repo_dir, dvc_repo, erepo, remote_url):
erepo.dvc.scm.checkout("branch")
_set_remote_url_and_commit(erepo.dvc, remote_url)
erepo.dvc.push()
erepo.dvc.scm.checkout("master")
_set_remote_url_and_commit(erepo.dvc, remote_url)

erepo.dvc.push(all_branches=True)

# Remove cache to force download
shutil.rmtree(erepo.dvc.cache.local.cache_dir)

# Using file url to force clone to tmp repo
repo_url = "file://" + erepo.dvc.root_dir
with api.open(repo_dir.FOO, repo=repo_url) as fd:
assert fd.read() == repo_dir.FOO_CONTENTS
with api.open("version", repo=repo_url) as fd:
assert fd.read() == "master"

assert api.read("version", repo=repo_url, rev="branch") == "branch"


def test_open_missing(erepo):
Expand Down

0 comments on commit 9ba54c1

Please sign in to comment.