def test_open(tmp_dir, dvc): tmp_dir.gen("foo", "foo") dvc.add("foo") (tmp_dir / "foo").unlink() fs = DataFileSystem(repo=dvc) with fs.open("foo", "r") as fobj: assert fobj.read() == "foo"
def test_open_dirty_hash(tmp_dir, dvc): tmp_dir.dvc_gen("file", "file") (tmp_dir / "file").write_text("something") fs = DataFileSystem(repo=dvc) with fs.open("file", "r") as fobj: # NOTE: Unlike DvcFileSystem, DataFileSystem should not # be affected by a dirty workspace. assert fobj.read() == "file"
def test_open_dirty_no_hash(tmp_dir, dvc): tmp_dir.gen("file", "file") (tmp_dir / "file.dvc").write_text("outs:\n- path: file\n") fs = DataFileSystem(repo=dvc) # NOTE: Unlike DvcFileSystem, DataFileSystem should not # be affected by a dirty workspace. with pytest.raises(FileNotFoundError): with fs.open("file", "r"): pass
def test_open_no_remote(tmp_dir, dvc): tmp_dir.dvc_gen("file", "file") (tmp_dir / "file").unlink() remove(dvc.odb.local.cache_dir) fs = DataFileSystem(repo=dvc) with pytest.raises(FileNotFoundError) as exc_info: with fs.open("file", "r"): pass assert isinstance(exc_info.value.__cause__, NoRemoteError)
def test_open_in_history(tmp_dir, scm, dvc): tmp_dir.gen("foo", "foo") dvc.add("foo") dvc.scm.add(["foo.dvc", ".gitignore"]) dvc.scm.commit("foo") tmp_dir.gen("foo", "foofoo") dvc.add("foo") dvc.scm.add(["foo.dvc", ".gitignore"]) dvc.scm.commit("foofoo") for rev in dvc.brancher(revs=["HEAD~1"]): if rev == "workspace": continue fs = DataFileSystem(repo=dvc) with fs.open("foo", "r") as fobj: assert fobj.read() == "foo"
def open_by_relpath(self, path, remote=None, mode="r", encoding=None): """Opens a specified resource as a file descriptor""" from dvc.fs.data import DataFileSystem from dvc.fs.dvc import DvcFileSystem if os.path.isabs(path): fs = DataFileSystem(repo=self, workspace="local") fs_path = path else: fs = DvcFileSystem(repo=self, subrepos=True) fs_path = fs.from_os_path(path) try: with fs.open( fs_path, mode=mode, encoding=encoding, remote=remote, ) as fobj: yield fobj except FileNotFoundError as exc: raise FileMissingError(path) from exc except IsADirectoryError as exc: raise DvcIsADirectoryError(f"'{path}' is a directory") from exc