From 003b5c392a368371ac3f227a9ed8da2bf4a406ec Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Thu, 23 Jan 2020 11:21:22 +0545 Subject: [PATCH 1/2] api: fallback to external_repo on error --- dvc/api.py | 13 ++++++++----- dvc/external_repo.py | 4 ++-- tests/func/test_api.py | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/dvc/api.py b/dvc/api.py index 2ae39bae7c..0025f64663 100644 --- a/dvc/api.py +++ b/dvc/api.py @@ -10,7 +10,7 @@ from voluptuous import Schema, Required, Invalid from dvc.repo import Repo -from dvc.exceptions import DvcException +from dvc.exceptions import DvcException, NotDvcRepoError from dvc.external_repo import external_repo @@ -110,10 +110,13 @@ def read(path, repo=None, rev=None, remote=None, mode="r", encoding=None): @contextmanager def _make_repo(repo_url, rev=None): if rev is None and (not repo_url or os.path.exists(repo_url)): - yield Repo(repo_url) - else: - with external_repo(url=repo_url, rev=rev) as repo: - yield repo + try: + yield Repo(repo_url) + return + except NotDvcRepoError: + pass # fallthrough to external_repo + with external_repo(url=repo_url, rev=rev) as repo: + yield repo def summon(name, repo=None, rev=None, summon_file="dvcsummon.yaml", args=None): diff --git a/dvc/external_repo.py b/dvc/external_repo.py index 470646c6e5..306e7d7c63 100644 --- a/dvc/external_repo.py +++ b/dvc/external_repo.py @@ -128,10 +128,10 @@ def pull_to(self, path, to_info): raise PathMissingError(path, self.url) @contextmanager - def open_by_relpath(self, path, mode="r", encoding=None): + def open_by_relpath(self, path, mode="r", encoding=None, **kwargs): try: abs_path = os.path.join(self.root_dir, path) - with open(abs_path, mode, encoding) as fd: + with open(abs_path, mode, encoding=encoding) as fd: yield fd except FileNotFoundError: raise PathMissingError(path, self.url) diff --git a/tests/func/test_api.py b/tests/func/test_api.py index 687476d619..04f7078268 100644 --- a/tests/func/test_api.py +++ b/tests/func/test_api.py @@ -8,7 +8,7 @@ from dvc import api from dvc.api import SummonError, UrlNotDvcRepoError from dvc.compat import fspath -from dvc.exceptions import FileMissingError, NotDvcRepoError +from dvc.exceptions import FileMissingError from dvc.main import main from dvc.path_info import URLInfo from dvc.remote.config import RemoteConfig @@ -54,7 +54,7 @@ def test_get_url_external(erepo_dir, remote_url): def test_get_url_requires_dvc(tmp_dir, scm): tmp_dir.scm_gen({"foo": "foo"}, commit="initial") - with pytest.raises(NotDvcRepoError, match="not inside of a DVC repo"): + with pytest.raises(UrlNotDvcRepoError, match="not a DVC repository"): api.get_url("foo", repo=fspath(tmp_dir)) with pytest.raises(UrlNotDvcRepoError): From 2e0b143856d617be3475eac54488250ff7148527 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Thu, 23 Jan 2020 15:17:39 +0545 Subject: [PATCH 2/2] api: use current directory for repo if not specified --- dvc/api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dvc/api.py b/dvc/api.py index 0025f64663..7efaf524a6 100644 --- a/dvc/api.py +++ b/dvc/api.py @@ -108,8 +108,9 @@ def read(path, repo=None, rev=None, remote=None, mode="r", encoding=None): @contextmanager -def _make_repo(repo_url, rev=None): - if rev is None and (not repo_url or os.path.exists(repo_url)): +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) return