exceptions: move AuthError and InvalidRemote errors

This commit is contained in:
Saugat Pachhai (सौगात)
2021-11-23 19:21:51 +02:00
committed by Ruslan Kuprieiev
parent c19a11152a
commit 8ec1ff44de
7 changed files with 80 additions and 26 deletions
+9 -2
View File
@@ -183,9 +183,11 @@ class BaseExecutor(ABC):
on_diverged: Callback in the form on_diverged(ref, is_checkpoint)
to be called when an experiment ref has diverged.
"""
from ..utils import iter_remote_refs
refs = []
has_checkpoint = False
for ref in dest_scm.iter_remote_refs(url, base=EXPS_NAMESPACE):
for ref in iter_remote_refs(dest_scm, url, base=EXPS_NAMESPACE):
if ref == EXEC_CHECKPOINT:
has_checkpoint = True
elif not ref.startswith(EXEC_NAMESPACE) and ref != EXPS_STASH:
@@ -222,6 +224,8 @@ class BaseExecutor(ABC):
@classmethod
def _validate_remotes(cls, dvc: "Repo", git_remote: Optional[str]):
from dvc.scm.base import InvalidRemoteSCMRepo
from dvc.scm.exceptions import InvalidRemote
if git_remote == dvc.root_dir:
logger.warning(
@@ -230,7 +234,10 @@ class BaseExecutor(ABC):
"will automatically be pushed to the default DVC remote "
"(if any) on each experiment commit."
)
dvc.scm.validate_git_remote(git_remote)
try:
dvc.scm.validate_git_remote(git_remote)
except InvalidRemote as exc:
raise InvalidRemoteSCMRepo(str(exc))
dvc.cloud.get_remote_odb()
@classmethod
+8 -3
View File
@@ -4,7 +4,7 @@ from dvc.exceptions import DvcException, InvalidArgumentError
from dvc.repo import locked
from dvc.repo.scm_context import scm_context
from .utils import exp_commits, resolve_exp_ref
from .utils import exp_commits, push_refspec, resolve_exp_ref
logger = logging.getLogger(__name__)
@@ -37,8 +37,13 @@ def push(
refname = str(exp_ref)
logger.debug("git push experiment '%s' -> '%s'", exp_ref, git_remote)
repo.scm.push_refspec(
git_remote, refname, refname, force=force, on_diverged=on_diverged
push_refspec(
repo.scm,
git_remote,
refname,
refname,
force=force,
on_diverged=on_diverged,
)
if push_cache:
+2 -2
View File
@@ -6,7 +6,7 @@ from dvc.repo import locked
from dvc.repo.scm_context import scm_context
from dvc.scm.base import RevError
from .utils import exp_refs, remove_exp_refs, resolve_exp_ref
from .utils import exp_refs, push_refspec, remove_exp_refs, resolve_exp_ref
logger = logging.getLogger(__name__)
@@ -78,7 +78,7 @@ def _remove_commited_exps(
remove_exp_refs(repo.scm, remove_list)
else:
for ref_info in remove_list:
repo.scm.push_refspec(remote, None, str(ref_info))
push_refspec(repo.scm, remote, None, str(ref_info))
return remain_list
+37 -3
View File
@@ -1,4 +1,4 @@
from typing import Generator, Iterable, Optional, Set
from typing import Callable, Generator, Iterable, Optional, Set
from dvc.exceptions import InvalidArgumentError
from dvc.scm.git import Git
@@ -49,11 +49,45 @@ def exp_refs_by_baseline(
yield ExpRefInfo.from_ref(ref)
def iter_remote_refs(
scm: "Git", url: str, base: Optional[str] = None, **kwargs
):
from dvc.scm.base import GitAuthError, InvalidRemoteSCMRepo
from dvc.scm.exceptions import AuthError, InvalidRemote
try:
yield from scm.iter_remote_refs(url, base=base, **kwargs)
except InvalidRemote as exc:
raise InvalidRemoteSCMRepo(str(exc))
except AuthError as exc:
raise GitAuthError(str(exc))
def push_refspec(
scm: "Git",
url: str,
src: Optional[str],
dest: str,
force: bool = False,
on_diverged: Optional[Callable[[str, str], bool]] = None,
**kwargs,
):
from dvc.scm.base import GitAuthError
from dvc.scm.exceptions import AuthError
try:
return scm.push_refspec(
url, src, dest, force=force, on_diverged=on_diverged, **kwargs
)
except AuthError as exc:
raise GitAuthError(str(exc))
def remote_exp_refs(
scm: "Git", url: str
) -> Generator["ExpRefInfo", None, None]:
"""Iterate over all remote experiment refs."""
for ref in scm.iter_remote_refs(url, base=EXPS_NAMESPACE):
for ref in iter_remote_refs(scm, url, base=EXPS_NAMESPACE):
if ref.startswith(EXEC_NAMESPACE) or ref == EXPS_STASH:
continue
yield ExpRefInfo.from_ref(ref)
@@ -73,7 +107,7 @@ def remote_exp_refs_by_baseline(
) -> Generator["ExpRefInfo", None, None]:
"""Iterate over all remote experiment refs with the specified baseline."""
ref_info = ExpRefInfo(baseline_sha=rev)
for ref in scm.iter_remote_refs(url, base=str(ref_info)):
for ref in iter_remote_refs(scm, url, base=str(ref_info)):
if ref.startswith(EXEC_NAMESPACE) or ref == EXPS_STASH:
continue
yield ExpRefInfo.from_ref(ref)
+4 -9
View File
@@ -29,18 +29,13 @@ class NoSCMError(SCMError):
class InvalidRemoteSCMRepo(SCMError):
def __init__(self, url: str):
msg = f"'{url}' is not a valid Git remote or URL"
super().__init__(msg)
pass
class GitAuthError(SCMError):
def __init__(self, url: str):
super().__init__(
f"HTTP Git authentication is not supported: '{url}'"
"\nSee https://dvc.org/doc//user-guide/"
"troubleshooting#git-auth"
)
def __init__(self, reason: str) -> None:
doc = "See https://dvc.org/doc//user-guide/troubleshooting#git-auth"
super().__init__(f"{reason}\n{doc}")
class Base:
+12
View File
@@ -16,3 +16,15 @@ class FileNotInRepoError(SCMError):
class MergeConflictError(SCMError):
pass
class InvalidRemote(SCMError):
def __init__(self, url: str) -> None:
self.url = url
super().__init__(f"'{url}' is not a valid Git remote or URL")
class AuthError(SCMError):
def __init__(self, url: str) -> None:
self.url = url
super().__init__(f"HTTP Git authentication is not supported: '{url}'")
+8 -7
View File
@@ -19,7 +19,8 @@ from typing import (
from funcy import cached_property
from dvc.progress import Tqdm
from dvc.scm.base import GitAuthError, InvalidRemoteSCMRepo, SCMError
from dvc.scm.base import SCMError
from dvc.scm.exceptions import AuthError, InvalidRemote
from dvc.utils import relpath
from ...objects import GitObject
@@ -370,7 +371,7 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
_remote, location = get_remote_repo(self.repo, url)
client, path = get_transport_and_path(location, **kwargs)
except Exception as exc:
raise InvalidRemoteSCMRepo(url) from exc
raise InvalidRemote(url) from exc
try:
if base:
@@ -382,9 +383,9 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
else:
yield from (os.fsdecode(ref) for ref in client.get_refs(path))
except NotGitRepository as exc:
raise InvalidRemoteSCMRepo(url) from exc
raise InvalidRemote(url) from exc
except HTTPUnauthorized:
raise GitAuthError(url)
raise AuthError(url)
def get_refs_containing(self, rev: str, pattern: Optional[str] = None):
raise NotImplementedError
@@ -458,7 +459,7 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
except (NotGitRepository, SendPackError) as exc:
raise SCMError("Git failed to push '{src}' to '{url}'") from exc
except HTTPUnauthorized:
raise GitAuthError(url)
raise AuthError(url)
def _push_dest_refs(
self, src: Optional[str], dest: str
@@ -676,11 +677,11 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
_, location = get_remote_repo(self.repo, url)
client, path = get_transport_and_path(location, **kwargs)
except Exception as exc:
raise InvalidRemoteSCMRepo(url) from exc
raise InvalidRemote(url) from exc
if isinstance(client, LocalGitClient) and not os.path.exists(
os.path.join("", path)
):
raise InvalidRemoteSCMRepo(url)
raise InvalidRemote(url)
def check_ref_format(self, refname: str):
from dulwich.refs import check_ref_format