Skip to content

Commit

Permalink
dvc: raise when Repo.open() fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Suor committed Jul 1, 2019
1 parent 01e2fe3 commit 3187040
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions dvc/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,10 @@ def __init__(self, etag, cached_etag):
"ETag mismatch detected when copying file to cache! "
"(expected: '{}', actual: '{}')".format(etag, cached_etag)
)


class OutputFileMissingError(DvcException):
def __init__(self, path):
super(OutputFileMissingError, self).__init__(
"Can't find {} neither locally nor on remote".format(path)
)
13 changes: 10 additions & 3 deletions dvc/repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
NotDvcRepoError,
OutputNotFoundError,
TargetNotDirectoryError,
OutputFileMissingError,
)
from dvc.ignore import DvcIgnoreFileHandler
from dvc.path_info import PathInfo
from dvc.utils.compat import open as _open
from dvc.utils.compat import open as _open, fspath_py35
from dvc.utils import relpath

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -455,9 +456,15 @@ def open(self, path, remote=None, mode="r", encoding=None):
if out.isdir():
raise ValueError("Can't open a dir")

cache_file = self.cache.local.checksum_to_path_info(out.checksum)
cache_file = fspath_py35(cache_file)

with self.state:
cache_info = out.get_used_cache(remote=remote)
self.cloud.pull(cache_info, remote=remote)

cache_file = self.cache.local.checksum_to_path_info(out.checksum)
return _open(cache_file.fspath, mode=mode, encoding=encoding)
# Since pull may just skip with a warning, we need to check it here
if not os.path.exists(cache_file):
raise OutputFileMissingError(relpath(path, self.root_dir))

return _open(cache_file, mode=mode, encoding=encoding)
10 changes: 10 additions & 0 deletions tests/func/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil

from dvc import api
from dvc.exceptions import OutputFileMissingError
from dvc.main import main
from dvc.path_info import URLInfo
from dvc.remote.config import RemoteConfig
Expand Down Expand Up @@ -113,6 +114,15 @@ def test_open_external(repo_dir, dvc_repo, erepo, remote_url):
assert fd.read() == repo_dir.FOO_CONTENTS


def test_open_missing(erepo):
# Remove cache to make foo missing
shutil.rmtree(erepo.dvc.cache.local.cache_dir)

repo_url = "file://" + erepo.dvc.root_dir
with pytest.raises(OutputFileMissingError):
api.read(erepo.FOO, repo=repo_url)


def _set_remote_url_and_commit(repo, remote_url):
rconfig = RemoteConfig(repo.config)
rconfig.modify("upstream", "url", remote_url)
Expand Down

0 comments on commit 3187040

Please sign in to comment.