|
5 | 5 | import os |
6 | 6 | import posixpath |
7 | 7 | import threading |
| 8 | +from collections import deque |
8 | 9 | from contextlib import ExitStack, suppress |
9 | 10 | from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple, Type, Union |
10 | 11 |
|
@@ -60,6 +61,7 @@ def _merge_info(repo, key, fs_info, dvc_info): |
60 | 61 | if fs_info: |
61 | 62 | ret["type"] = fs_info["type"] |
62 | 63 | ret["size"] = fs_info["size"] |
| 64 | + ret["fs_info"] = fs_info |
63 | 65 | isexec = False |
64 | 66 | if fs_info["type"] == "file": |
65 | 67 | isexec = utils.is_exec(fs_info["mode"]) |
@@ -427,6 +429,45 @@ def get_file(self, rpath, lpath, **kwargs): # pylint: disable=arguments-differ |
427 | 429 | dvc_path = _get_dvc_path(dvc_fs, subkey) |
428 | 430 | return dvc_fs.get_file(dvc_path, lpath, **kwargs) |
429 | 431 |
|
| 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 | + |
430 | 471 | def close(self): |
431 | 472 | self._repo_stack.close() |
432 | 473 |
|
|
0 commit comments