Skip to content

Commit b9092cf

Browse files
committed
dvcfs: implement basic du
1 parent 36fa116 commit b9092cf

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

dvc/fs/dvc.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import posixpath
77
import threading
8+
from collections import deque
89
from contextlib import ExitStack, suppress
910
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple, Type, Union
1011

@@ -60,6 +61,7 @@ def _merge_info(repo, key, fs_info, dvc_info):
6061
if fs_info:
6162
ret["type"] = fs_info["type"]
6263
ret["size"] = fs_info["size"]
64+
ret["fs_info"] = fs_info
6365
isexec = False
6466
if fs_info["type"] == "file":
6567
isexec = utils.is_exec(fs_info["mode"])
@@ -427,6 +429,45 @@ def get_file(self, rpath, lpath, **kwargs): # pylint: disable=arguments-differ
427429
dvc_path = _get_dvc_path(dvc_fs, subkey)
428430
return dvc_fs.get_file(dvc_path, lpath, **kwargs)
429431

432+
def du(self, path, total=True, maxdepth=None, withdirs=False, **kwargs):
433+
if maxdepth is not None:
434+
raise NotImplementedError
435+
436+
sizes = {}
437+
dus = {}
438+
todo = deque([self.info(path)])
439+
while todo:
440+
info = todo.popleft()
441+
isdir = info["type"] == "directory"
442+
size = info["size"] or 0
443+
name = info["name"]
444+
445+
if not isdir:
446+
sizes[name] = size
447+
continue
448+
449+
dvc_info = info.get("dvc_info") or {}
450+
fs_info = info.get("fs_info")
451+
entry = dvc_info.get("entry")
452+
if (
453+
dvc_info
454+
and not fs_info
455+
and entry is not None
456+
and entry.size is not None
457+
):
458+
dus[name] = entry.size
459+
continue
460+
461+
if withdirs:
462+
sizes[name] = size
463+
464+
todo.extend(self.ls(info["name"], detail=True))
465+
466+
if total:
467+
return sum(sizes.values()) + sum(dus.values())
468+
469+
return sizes
470+
430471
def close(self):
431472
self._repo_stack.close()
432473

0 commit comments

Comments
 (0)