api: use Repo.open (#5401)

This commit is contained in:
Ruslan Kuprieiev
2021-02-03 18:39:40 +02:00
committed by GitHub
parent adedd19c52
commit 2c81bf1748
2 changed files with 11 additions and 24 deletions
+3 -22
View File
@@ -1,15 +1,9 @@
import os
from contextlib import _GeneratorContextManager as GCM
from contextlib import contextmanager
from funcy import reraise
from dvc.exceptions import (
NotDvcRepoError,
OutputNotFoundError,
PathMissingError,
)
from dvc.external_repo import external_repo
from dvc.exceptions import OutputNotFoundError, PathMissingError
from dvc.path_info import PathInfo
from dvc.repo import Repo
@@ -25,7 +19,7 @@ def get_url(path, repo=None, rev=None, remote=None):
NOTE: This function does not check for the actual existence of the file or
directory in the remote storage.
"""
with _make_repo(repo, rev=rev) as _repo:
with Repo.open(repo, rev=rev, subrepos=True, uninitialized=True) as _repo:
path_info = PathInfo(_repo.root_dir) / path
with reraise(FileNotFoundError, PathMissingError(path, repo)):
metadata = _repo.repo_tree.metadata(path_info)
@@ -78,7 +72,7 @@ class _OpenContextManager(GCM):
def _open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
with _make_repo(repo, rev=rev) as _repo:
with Repo.open(repo, rev=rev, subrepos=True, uninitialized=True) as _repo:
with _repo.open_by_relpath(
path, remote=remote, mode=mode, encoding=encoding
) as fd:
@@ -97,19 +91,6 @@ def read(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
return fd.read()
@contextmanager
def _make_repo(repo_url=None, rev=None):
repo_url = repo_url or os.getcwd()
if rev is None and os.path.exists(repo_url):
try:
yield Repo(repo_url, subrepos=True)
return
except NotDvcRepoError:
pass # fallthrough to external_repo
with external_repo(url=repo_url, rev=rev) as repo:
yield repo
def make_checkpoint():
"""
Signal DVC to create a checkpoint experiment.
+8 -2
View File
@@ -205,9 +205,15 @@ class Repo:
@staticmethod
@contextmanager
def open(url, *args, **kwargs):
if url is None:
url = os.getcwd()
if os.path.exists(url):
yield Repo(url, *args, **kwargs)
return
try:
yield Repo(url, *args, **kwargs)
return
except NotDvcRepoError:
pass # fallthrough to external_repo
from dvc.external_repo import external_repo