From e430693fc81b0ba27131756039e3191d0b93d83d Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Mon, 7 Feb 2022 19:12:10 +0200 Subject: [PATCH] dvcfs: use paths relative to the repo root Currently we use both absolute and relative (depend on cwd) paths, which is terrible for an api, as their interpretation depends on external factors. This PR makes dvcfs accept only paths relative to the repo root for regular outputs and absolute paths for external outputs. Pre-requisite for making repofs use same relative paths. --- dvc/api.py | 3 +- dvc/fs/dvc.py | 15 ++++---- dvc/fs/repo.py | 67 +++++++++++++++++++-------------- tests/func/test_fs.py | 4 +- tests/unit/fs/test_dvc.py | 76 ++++++++++++++++---------------------- tests/unit/fs/test_repo.py | 2 +- 6 files changed, 83 insertions(+), 84 deletions(-) diff --git a/dvc/api.py b/dvc/api.py index f63e7b20b..b1ac0a0f2 100644 --- a/dvc/api.py +++ b/dvc/api.py @@ -27,7 +27,8 @@ def get_url(path, repo=None, rev=None, remote=None): raise OutputNotFoundError(path, repo) cloud = info["repo"].cloud - md5 = info["repo"].dvcfs.info(fs_path)["md5"] + dvc_path = _repo.fs.path.relpath(fs_path, info["repo"].root_dir) + md5 = info["repo"].dvcfs.info(dvc_path)["md5"] return cloud.get_url_for(remote, checksum=md5) diff --git a/dvc/fs/dvc.py b/dvc/fs/dvc.py index f0eacf5ba..1e85314c4 100644 --- a/dvc/fs/dvc.py +++ b/dvc/fs/dvc.py @@ -32,16 +32,18 @@ class DvcFileSystem(FileSystem): # pylint:disable=abstract-method raise NotImplementedError def _get_key(self, path): + from dvc.fs.local import LocalFileSystem + from . import get_cloud_fs cls, kwargs, fs_path = get_cloud_fs(None, url=path) - if not path.startswith(self.repo.root_dir): + if cls != LocalFileSystem or os.path.isabs(path): fs = cls(**kwargs) return (cls.scheme, *fs.path.parts(fs_path)) fs_key = "repo" - key = self.path.relparts(path, self.repo.root_dir) + key = self.path.parts(path) if key == (".",): key = () @@ -132,9 +134,8 @@ class DvcFileSystem(FileSystem): # pylint:disable=abstract-method def walk(self, top, topdown=True, onerror=None, **kwargs): assert topdown - root = os.path.abspath(top) try: - info = self.info(root) + info = self.info(top) except FileNotFoundError: if onerror is not None: onerror(FileNotFoundError(top)) @@ -145,7 +146,7 @@ class DvcFileSystem(FileSystem): # pylint:disable=abstract-method onerror(NotADirectoryError(top)) return - yield from self._walk(root, topdown=topdown, **kwargs) + yield from self._walk(top, topdown=topdown, **kwargs) def find(self, path, prefix=None): for root, _, files in self.walk(path): @@ -165,9 +166,7 @@ class DvcFileSystem(FileSystem): # pylint:disable=abstract-method def info(self, path): from dvc.data.meta import Meta - abspath = os.path.abspath(path) - - key = self._get_key(abspath) + key = self._get_key(path) try: outs = list(self.repo.index.tree.iteritems(key)) diff --git a/dvc/fs/repo.py b/dvc/fs/repo.py index 3941d324e..28d43adc6 100644 --- a/dvc/fs/repo.py +++ b/dvc/fs/repo.py @@ -18,6 +18,11 @@ logger = logging.getLogger(__name__) RepoFactory = Union[Callable[[str], "Repo"], Type["Repo"]] +def _wrap_walk(dvc_fs, *args, **kwargs): + for root, dnames, fnames in dvc_fs.walk(*args, **kwargs): + yield dvc_fs.path.join(dvc_fs.repo.root_dir, root), dnames, fnames + + class RepoFileSystem(FileSystem): # pylint:disable=abstract-method """DVC + git-tracked files fs. @@ -191,7 +196,9 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method repo_path = os.path.join(dir_path, Repo.DVC_DIR) return self._main_repo.fs.isdir(repo_path) - def _get_fs_pair(self, path) -> Tuple[FileSystem, Optional[DvcFileSystem]]: + def _get_fs_pair( + self, path + ) -> Tuple[FileSystem, Optional[DvcFileSystem], str]: """ Returns a pair of fss based on repo the path falls in, using prefix. """ @@ -202,7 +209,13 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method repo = self._get_repo(path) or self._main_repo dvc_fs = self._dvcfss.get(repo.root_dir) - return repo.fs, dvc_fs + + if path.startswith(repo.root_dir): + dvc_path = path[len(repo.root_dir) + 1 :] + else: + dvc_path = path + + return repo.fs, dvc_fs, dvc_path def open( self, path, mode="r", encoding="utf-8", **kwargs @@ -210,19 +223,19 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method if "b" in mode: encoding = None - fs, dvc_fs = self._get_fs_pair(path) + fs, dvc_fs, dvc_path = self._get_fs_pair(path) try: return fs.open(path, mode=mode, encoding=encoding) except FileNotFoundError: if not dvc_fs: raise - return dvc_fs.open(path, mode=mode, encoding=encoding, **kwargs) + return dvc_fs.open(dvc_path, mode=mode, encoding=encoding, **kwargs) def exists(self, path) -> bool: path = os.path.abspath(path) - fs, dvc_fs = self._get_fs_pair(path) + fs, dvc_fs, dvc_path = self._get_fs_pair(path) if not dvc_fs: return fs.exists(path) @@ -233,7 +246,7 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method if fs.exists(path): return True - if not dvc_fs.exists(path): + if not dvc_fs.exists(dvc_path): return False for p in self.path.parents(path): @@ -248,7 +261,7 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method def isdir(self, path): # pylint: disable=arguments-renamed path = os.path.abspath(path) - fs, dvc_fs = self._get_fs_pair(path) + fs, dvc_fs, dvc_path = self._get_fs_pair(path) if dvc_fs and dvc_fs.repo.dvcignore.is_ignored_dir(path): return False @@ -264,7 +277,7 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method return False try: - info = dvc_fs.info(path) + info = dvc_fs.info(dvc_path) except FileNotFoundError: return False @@ -278,13 +291,13 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method return info["type"] == "directory" def isdvc(self, path, **kwargs): - _, dvc_fs = self._get_fs_pair(path) - return dvc_fs is not None and dvc_fs.isdvc(path, **kwargs) + _, dvc_fs, dvc_path = self._get_fs_pair(path) + return dvc_fs is not None and dvc_fs.isdvc(dvc_path, **kwargs) def isfile(self, path): # pylint: disable=arguments-renamed path = os.path.abspath(path) - fs, dvc_fs = self._get_fs_pair(path) + fs, dvc_fs, dvc_path = self._get_fs_pair(path) if dvc_fs and dvc_fs.repo.dvcignore.is_ignored_file(path): return False @@ -300,7 +313,7 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method return False try: - info = dvc_fs.info(path) + info = dvc_fs.info(dvc_path) except FileNotFoundError: return False @@ -328,10 +341,10 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method NOTE: subrepo will only be discovered when walking if ignore_subrepos is set to False. """ - fs, dvc_fs = self._get_fs_pair(dir_path) + fs, dvc_fs, dvc_path = self._get_fs_pair(dir_path) fs_walk = fs.walk(dir_path, topdown=True) if dvc_fs: - dvc_walk = dvc_fs.walk(dir_path, topdown=True, **kwargs) + dvc_walk = _wrap_walk(dvc_fs, dvc_path, topdown=True, **kwargs) else: dvc_walk = None yield from self._walk(fs_walk, dvc_walk, **kwargs) @@ -420,26 +433,26 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method repo = self._get_repo(os.path.abspath(top)) dvcfiles = kwargs.pop("dvcfiles", False) - fs, dvc_fs = self._get_fs_pair(top) + fs, dvc_fs, dvc_path = self._get_fs_pair(top) repo_exists = fs.exists(top) repo_walk = repo.dvcignore.walk( fs, top, topdown=topdown, onerror=onerror, **kwargs ) - if not dvc_fs or (repo_exists and dvc_fs.isdvc(top)): + if not dvc_fs or (repo_exists and dvc_fs.isdvc(dvc_path)): yield from self._walk(repo_walk, None, dvcfiles=dvcfiles) return if not repo_exists: - yield from dvc_fs.walk( - top, topdown=topdown, onerror=onerror, **kwargs + yield from _wrap_walk( + dvc_fs, dvc_path, topdown=topdown, onerror=onerror, **kwargs ) dvc_walk = None - if dvc_fs.exists(top): - dvc_walk = dvc_fs.walk( - top, topdown=topdown, onerror=onerror, **kwargs + if dvc_fs.exists(dvc_path): + dvc_walk = _wrap_walk( + dvc_fs, dvc_path, topdown=topdown, onerror=onerror, **kwargs ) yield from self._walk(repo_walk, dvc_walk, dvcfiles=dvcfiles) @@ -452,7 +465,7 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method def get_file( self, from_info, to_file, callback=DEFAULT_CALLBACK, **kwargs ): - fs, dvc_fs = self._get_fs_pair(from_info) + fs, dvc_fs, dvc_path = self._get_fs_pair(from_info) try: fs.get_file( # pylint: disable=protected-access from_info, to_file, callback=callback, **kwargs @@ -463,14 +476,14 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method raise dvc_fs.get_file( # pylint: disable=protected-access - from_info, to_file, callback=callback, **kwargs + dvc_path, to_file, callback=callback, **kwargs ) def info(self, path): - fs, dvc_fs = self._get_fs_pair(path) + fs, dvc_fs, dvc_path = self._get_fs_pair(path) try: - dvc_info = dvc_fs.info(path) + dvc_info = dvc_fs.info(dvc_path) except FileNotFoundError: dvc_info = None @@ -503,9 +516,9 @@ class RepoFileSystem(FileSystem): # pylint:disable=abstract-method return dvc_info def checksum(self, path): - fs, dvc_fs = self._get_fs_pair(path) + fs, dvc_fs, dvc_path = self._get_fs_pair(path) try: return fs.checksum(path) except FileNotFoundError: - return dvc_fs.checksum(path) + return dvc_fs.checksum(dvc_path) diff --git a/tests/func/test_fs.py b/tests/func/test_fs.py index b81156b07..85d439d2b 100644 --- a/tests/func/test_fs.py +++ b/tests/func/test_fs.py @@ -305,7 +305,7 @@ def test_download_callbacks_on_dvc_git_fs(tmp_dir, dvc, scm, fs_type): callback = fsspec.Callback() fs.download_file( - (tmp_dir / "file").fs_path, + "file", (tmp_dir / "file2").fs_path, callback=callback, ) @@ -317,7 +317,7 @@ def test_download_callbacks_on_dvc_git_fs(tmp_dir, dvc, scm, fs_type): callback = fsspec.Callback() fs.download( - (tmp_dir / "dir").fs_path, + "dir", (tmp_dir / "dir2").fs_path, callback=callback, ) diff --git a/tests/unit/fs/test_dvc.py b/tests/unit/fs/test_dvc.py index bb8dd9cfd..a90791e8c 100644 --- a/tests/unit/fs/test_dvc.py +++ b/tests/unit/fs/test_dvc.py @@ -25,7 +25,7 @@ def test_open(tmp_dir, dvc): (tmp_dir / "foo").unlink() fs = DvcFileSystem(repo=dvc) - with fs.open((tmp_dir / "foo").fs_path, "r") as fobj: + with fs.open("foo", "r") as fobj: assert fobj.read() == "foo" @@ -34,7 +34,7 @@ def test_open_dirty_hash(tmp_dir, dvc): (tmp_dir / "file").write_text("something") fs = DvcFileSystem(repo=dvc) - with fs.open((tmp_dir / "file").fs_path, "r") as fobj: + with fs.open("file", "r") as fobj: # NOTE: Unlike RepoFileSystem, DvcFileSystem should not # be affected by a dirty workspace. assert fobj.read() == "file" @@ -47,7 +47,7 @@ def test_open_no_remote(tmp_dir, dvc): fs = DvcFileSystem(repo=dvc) with pytest.raises(FileNotFoundError) as exc_info: - with fs.open((tmp_dir / "file").fs_path, "r"): + with fs.open("file", "r"): pass assert isinstance(exc_info.value.__cause__, NoRemoteError) @@ -60,7 +60,7 @@ def test_open_dirty_no_hash(tmp_dir, dvc): # NOTE: Unlike RepoFileSystem, DvcFileSystem should not # be affected by a dirty workspace. with pytest.raises(FileNotFoundError): - with fs.open((tmp_dir / "file").fs_path, "r"): + with fs.open("file", "r"): pass @@ -80,7 +80,7 @@ def test_open_in_history(tmp_dir, scm, dvc): continue fs = DvcFileSystem(repo=dvc) - with fs.open((tmp_dir / "foo").fs_path, "r") as fobj: + with fs.open("foo", "r") as fobj: assert fobj.read() == "foo" @@ -129,13 +129,13 @@ def test_walk(tmp_dir, dvc): fs = DvcFileSystem(repo=dvc) expected = [ - str(tmp_dir / "dir" / "subdir1"), - str(tmp_dir / "dir" / "subdir2"), - str(tmp_dir / "dir" / "subdir1" / "foo1"), - str(tmp_dir / "dir" / "subdir1" / "bar1"), - str(tmp_dir / "dir" / "subdir2" / "foo2"), - str(tmp_dir / "dir" / "foo"), - str(tmp_dir / "dir" / "bar"), + os.path.join("dir", "subdir1"), + os.path.join("dir", "subdir2"), + os.path.join("dir", "subdir1", "foo1"), + os.path.join("dir", "subdir1", "bar1"), + os.path.join("dir", "subdir2", "foo2"), + os.path.join("dir", "foo"), + os.path.join("dir", "bar"), ] actual = [] @@ -163,13 +163,13 @@ def test_walk_dir(tmp_dir, dvc): fs = DvcFileSystem(repo=dvc) expected = [ - str(tmp_dir / "dir" / "subdir1"), - str(tmp_dir / "dir" / "subdir2"), - str(tmp_dir / "dir" / "subdir1" / "foo1"), - str(tmp_dir / "dir" / "subdir1" / "bar1"), - str(tmp_dir / "dir" / "subdir2" / "foo2"), - str(tmp_dir / "dir" / "foo"), - str(tmp_dir / "dir" / "bar"), + os.path.join("dir", "subdir1"), + os.path.join("dir", "subdir2"), + os.path.join("dir", "subdir1", "foo1"), + os.path.join("dir", "subdir1", "bar1"), + os.path.join("dir", "subdir2", "foo2"), + os.path.join("dir", "foo"), + os.path.join("dir", "bar"), ] actual = [] @@ -214,10 +214,7 @@ def test_isdvc(tmp_dir, dvc): def test_get_hash_file(tmp_dir, dvc): tmp_dir.dvc_gen({"foo": "foo"}) fs = DvcFileSystem(repo=dvc) - assert ( - fs.info((tmp_dir / "foo").fs_path)["md5"] - == "acbd18db4cc2f85cedef654fccc4a4d8" - ) + assert fs.info("foo")["md5"] == "acbd18db4cc2f85cedef654fccc4a4d8" def test_get_hash_dir(tmp_dir, dvc, mocker): @@ -228,10 +225,7 @@ def test_get_hash_dir(tmp_dir, dvc, mocker): ) fs = DvcFileSystem(repo=dvc) get_file_hash_spy = mocker.spy(dvc_module.data.stage, "get_file_hash") - assert ( - fs.info((tmp_dir / "dir").fs_path)["md5"] - == "8761c4e9acad696bee718615e23e22db.dir" - ) + assert fs.info("dir")["md5"] == "8761c4e9acad696bee718615e23e22db.dir" assert not get_file_hash_spy.called @@ -240,19 +234,15 @@ def test_get_hash_granular(tmp_dir, dvc): {"dir": {"foo": "foo", "bar": "bar", "subdir": {"data": "data"}}} ) fs = DvcFileSystem(repo=dvc) - subdir = tmp_dir / "dir" / "subdir" - assert fs.info(subdir.fs_path).get("md5") is None - _, _, obj = stage(dvc.odb.local, subdir.fs_path, fs, "md5", dry_run=True) + subdir = os.path.join("dir", "subdir") + assert fs.info(subdir).get("md5") is None + _, _, obj = stage(dvc.odb.local, subdir, fs, "md5", dry_run=True) assert obj.hash_info == HashInfo( "md5", "af314506f1622d107e0ed3f14ec1a3b5.dir" ) - assert ( - fs.info((subdir / "data").fs_path)["md5"] - == "8d777f385d3dfec8815d20f7496026dc" - ) - _, _, obj = stage( - dvc.odb.local, (subdir / "data").fs_path, fs, "md5", dry_run=True - ) + data = os.path.join(subdir, "data") + assert fs.info(data)["md5"] == "8d777f385d3dfec8815d20f7496026dc" + _, _, obj = stage(dvc.odb.local, data, fs, "md5", dry_run=True) assert obj.hash_info == HashInfo("md5", "8d777f385d3dfec8815d20f7496026dc") @@ -262,10 +252,8 @@ def test_get_hash_dirty_file(tmp_dir, dvc): fs = DvcFileSystem(repo=dvc) expected = "8c7dd922ad47494fc02c388e12c00eac" - assert fs.info((tmp_dir / "file").fs_path).get("md5") == expected - _, _, obj = stage( - dvc.odb.local, (tmp_dir / "file").fs_path, fs, "md5", dry_run=True - ) + assert fs.info("file").get("md5") == expected + _, _, obj = stage(dvc.odb.local, "file", fs, "md5", dry_run=True) assert obj.hash_info == HashInfo("md5", expected) @@ -275,8 +263,6 @@ def test_get_hash_dirty_dir(tmp_dir, dvc): fs = DvcFileSystem(repo=dvc) expected = "5ea40360f5b4ec688df672a4db9c17d1.dir" - assert fs.info((tmp_dir / "dir").fs_path).get("md5") == expected - _, _, obj = stage( - dvc.odb.local, (tmp_dir / "dir").fs_path, fs, "md5", dry_run=True - ) + assert fs.info("dir").get("md5") == expected + _, _, obj = stage(dvc.odb.local, "dir", fs, "md5", dry_run=True) assert obj.hash_info == HashInfo("md5", expected) diff --git a/tests/unit/fs/test_repo.py b/tests/unit/fs/test_repo.py index 94e324bf4..c7dbb1149 100644 --- a/tests/unit/fs/test_repo.py +++ b/tests/unit/fs/test_repo.py @@ -491,7 +491,7 @@ def test_repo_fs_no_subrepos(tmp_dir, dvc, scm): ] actual = [] - for root, dirs, files in fs.walk(tmp_dir, dvcfiles=True): + for root, dirs, files in fs.walk(tmp_dir.fs_path, dvcfiles=True): for entry in dirs + files: actual.append(os.path.normpath(os.path.join(root, entry)))