forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import argparse | ||
import logging | ||
|
||
from dvc.cli import completion | ||
from dvc.cli.command import CmdBaseNoRepo | ||
from dvc.cli.utils import DictAction, append_doc_link | ||
from dvc.ui import ui | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CmdDU(CmdBaseNoRepo): | ||
def run(self): | ||
from dvc.repo import Repo | ||
from dvc.utils.humanize import naturalsize | ||
|
||
entries = Repo.du( | ||
self.args.url, | ||
self.args.path, | ||
rev=self.args.rev, | ||
summarize=self.args.summarize, | ||
config=self.args.config, | ||
remote=self.args.remote, | ||
remote_config=self.args.remote_config, | ||
) | ||
ui.table([(naturalsize(size), path) for path, size in entries]) | ||
return 0 | ||
|
||
|
||
def add_parser(subparsers, parent_parser): | ||
DU_HELP = "Show disk usage." | ||
du_parser = subparsers.add_parser( | ||
"du", | ||
parents=[parent_parser], | ||
description=append_doc_link(DU_HELP, "du"), | ||
help=DU_HELP, | ||
formatter_class=argparse.RawTextHelpFormatter, | ||
) | ||
du_parser.add_argument("url", help="Location of DVC repository") | ||
du_parser.add_argument( | ||
"--rev", | ||
nargs="?", | ||
help="Git revision (e.g. SHA, branch, tag)", | ||
metavar="<commit>", | ||
) | ||
du_parser.add_argument( | ||
"-s", | ||
"--summarize", | ||
action="store_true", | ||
help="Show total disk usage.", | ||
) | ||
du_parser.add_argument( | ||
"--config", | ||
type=str, | ||
help=( | ||
"Path to a config file that will be merged with the config " | ||
"in the target repository." | ||
), | ||
) | ||
du_parser.add_argument( | ||
"--remote", | ||
type=str, | ||
help="Remote name to set as a default in the target repository.", | ||
) | ||
du_parser.add_argument( | ||
"--remote-config", | ||
type=str, | ||
nargs="*", | ||
action=DictAction, | ||
help=( | ||
"Remote config options to merge with a remote's config (default or one " | ||
"specified by '--remote') in the target repository." | ||
), | ||
) | ||
du_parser.add_argument( | ||
"path", | ||
nargs="?", | ||
help="Path to directory within the repository", | ||
).complete = completion.DIR | ||
du_parser.set_defaults(func=CmdDU) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Any, Dict, Optional, Union | ||
|
||
|
||
def du( | ||
url: str, | ||
path: Optional[str] = None, | ||
rev: Optional[str] = None, | ||
summarize: bool = False, | ||
config: Union[None, Dict[str, Any], str] = None, | ||
remote: Optional[str] = None, | ||
remote_config: Optional[dict] = None, | ||
): | ||
from dvc.config import Config | ||
|
||
from . import Repo | ||
|
||
if config and not isinstance(config, dict): | ||
config_dict = Config.load_file(config) | ||
else: | ||
config_dict = None | ||
|
||
with Repo.open( | ||
url, | ||
rev=rev, | ||
subrepos=True, | ||
uninitialized=True, | ||
config=config_dict, | ||
remote=remote, | ||
remote_config=remote_config, | ||
) as repo: | ||
path = path or "" | ||
|
||
fs = repo.dvcfs | ||
|
||
if summarize or not fs.isdir(path): | ||
return [(path, fs.du(path, total=True))] | ||
|
||
ret = [ | ||
(entry_path, fs.du(entry_path, total=True)) for entry_path in fs.ls(path) | ||
] | ||
ret.append((path, sum(entry[1] for entry in ret))) | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
|
||
|
||
def test_du(tmp_dir, dvc): | ||
tmp_dir.gen( | ||
{ | ||
"file": b"file", | ||
"dvcfile": b"dvcfile", | ||
"dir": { | ||
"dirfile": b"dirfile", | ||
"subdir": { | ||
"subdirfile": b"subdirfile", | ||
}, | ||
"dvcsubdir": { | ||
"dvcsubdirfile": b"dvcsubdirfile", | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
dvc.add("dvcfile") | ||
dvc.add(os.path.join("dir", "dvcsubdir")) | ||
|
||
assert dvc.du(".", "file") == [("file", 4)] | ||
assert dvc.du(".", "dvcfile") == [("dvcfile", 7)] | ||
assert set(dvc.du(".", "dir/subdir")) == { | ||
("dir/subdir/subdirfile", 10), | ||
("dir/subdir", 10), | ||
} | ||
assert dvc.du(".", "dir/subdir", summarize=True) == [("dir/subdir", 10)] | ||
assert set(dvc.du(".", "dir/dvcsubdir")) == { | ||
("dir/dvcsubdir/dvcsubdirfile", 13), | ||
("dir/dvcsubdir", 13), | ||
} | ||
assert dvc.du(".", "dir/dvcsubdir", summarize=True) == [("dir/dvcsubdir", 13)] | ||
assert set(dvc.du(".", "dir")) == { | ||
("dir/dvcsubdir", 13), | ||
("dir/subdir", 10), | ||
("dir/dirfile", 7), | ||
("dir", 30), | ||
} | ||
assert dvc.du(".", "dir", summarize=True) == [("dir", 30)] | ||
assert set(dvc.du(".", "/")) == { | ||
("/dvcfile", 7), | ||
("/dir", 30), | ||
("/file", 4), | ||
("/", 41), | ||
} | ||
assert dvc.du(".", "/", summarize=True) == [("/", 41)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from dvc.cli import parse_args | ||
from dvc.commands.du import CmdDU | ||
|
||
|
||
def test_du(mocker): | ||
cli_args = parse_args(["du", "myurl", "mypath", "--summarize", "--rev", "myrev"]) | ||
assert cli_args.func == CmdDU | ||
|
||
cmd = cli_args.func(cli_args) | ||
mock_du = mocker.patch("dvc.repo.Repo.du") | ||
|
||
assert cmd.run() == 0 | ||
mock_du.assert_called_once_with( | ||
"myurl", | ||
"mypath", | ||
rev="myrev", | ||
summarize=True, | ||
config=None, | ||
remote=None, | ||
remote_config=None, | ||
) |