def test_walk(tmp_dir, dvc): tmp_dir.gen( { "dir": { "subdir1": {"foo1": "foo1", "bar1": "bar1"}, "subdir2": {"foo2": "foo2"}, "foo": "foo", "bar": "bar", } } ) dvc.add("dir", recursive=True) tree = DvcTree(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"), ] actual = [] for root, dirs, files in tree.walk("dir"): for entry in dirs + files: actual.append(os.path.join(root, entry)) assert set(actual) == set(expected) assert len(actual) == len(expected)
def test_walk_dir(tmp_dir, dvc, fetch, expected): tmp_dir.gen({ "dir": { "subdir1": { "foo1": "foo1", "bar1": "bar1" }, "subdir2": { "foo2": "foo2" }, "foo": "foo", "bar": "bar", } }) dvc.add("dir") tree = DvcTree(dvc, fetch=fetch) expected = [str(tmp_dir / path) for path in expected] with dvc.state: actual = [] for root, dirs, files in tree.walk("dir"): for entry in dirs + files: actual.append(os.path.join(root, entry)) assert set(actual) == set(expected) assert len(actual) == len(expected)
def test_walk_onerror(tmp_dir, dvc): def onerror(exc): raise exc tmp_dir.dvc_gen("foo", "foo") tree = DvcTree(dvc) # path does not exist for _ in tree.walk("dir"): pass with pytest.raises(OSError): for _ in tree.walk("dir", onerror=onerror): pass # path is not a directory for _ in tree.walk("foo"): pass with pytest.raises(OSError): for _ in tree.walk("foo", onerror=onerror): pass
def _ls(repo, path_info, recursive=None, dvc=False): from dvc.ignore import CleanTree from dvc.repo.tree import DvcTree from dvc.scm.tree import WorkingTree if dvc: tree = DvcTree(repo) else: tree = CleanTree(WorkingTree(repo.root_dir)) ret = {} try: for root, dirs, files in tree.walk(path_info.fspath): for fname in files: info = PathInfo(root) / fname path = str(info.relative_to(path_info)) ret[path] = { "isout": dvc, "isdir": False, "isexec": False if dvc else tree.isexec(info.fspath), } if not recursive: for dname in dirs: info = PathInfo(root) / dname path = str(info.relative_to(path_info)) ret[path] = { "isout": tree.isdvc(info.fspath) if dvc else False, "isdir": True, "isexec": False if dvc else tree.isexec(info.fspath), } break except NotADirectoryError: return { path_info.name: { "isout": dvc, "isdir": False, "isexec": False if dvc else tree.isexec(path_info.fspath), } } except FileNotFoundError: return {} return ret