From 556143fd7e55bb66fd5e8477217ad218058e2333 Mon Sep 17 00:00:00 2001 From: Ioana Grigoropol <14314993+ju0gri@users.noreply.github.com> Date: Wed, 11 Nov 2020 18:38:01 +0000 Subject: [PATCH] api: add support for simple wildcards (#4864) * api: add support for simple wildcards Related to #4816. Signed-off-by: Ioana Grigoropol * api: make wildcard interpretation optional Adds a new argument for the add command `glob` that is disabled by default and when enabled it passes the input targets through glob filtering. Related: #4816 Signed-off-by: Ioana Grigoropol * Update dvc/repo/add.py * Update dvc/repo/add.py * Update dvc/repo/add.py * Update dvc/repo/add.py * Update dvc/repo/add.py Co-authored-by: Ruslan Kuprieiev --- dvc/command/add.py | 7 +++++ dvc/repo/add.py | 35 +++++++++++++++++---- tests/func/test_add.py | 71 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 6 deletions(-) diff --git a/dvc/command/add.py b/dvc/command/add.py index e81631d52..fb70ef279 100644 --- a/dvc/command/add.py +++ b/dvc/command/add.py @@ -20,6 +20,7 @@ class CmdAdd(CmdBase): no_commit=self.args.no_commit, fname=self.args.file, external=self.args.external, + glob=self.args.glob, ) except DvcException: @@ -57,6 +58,12 @@ def add_parser(subparsers, parent_parser): default=False, help="Allow targets that are outside of the DVC repository.", ) + parser.add_argument( + "--glob", + action="store_true", + default=False, + help="Allows targets containing shell-style wildcards.", + ) parser.add_argument( "--file", help="Specify name of the DVC-file this command will generate.", diff --git a/dvc/repo/add.py b/dvc/repo/add.py index 187de1894..b78a81a7a 100644 --- a/dvc/repo/add.py +++ b/dvc/repo/add.py @@ -23,7 +23,13 @@ logger = logging.getLogger(__name__) @locked @scm_context def add( - repo, targets, recursive=False, no_commit=False, fname=None, external=False + repo, + targets, + recursive=False, + no_commit=False, + fname=None, + external=False, + glob=False, ): if recursive and fname: raise RecursiveAddingWhileUsingFilename() @@ -57,7 +63,12 @@ def add( ) stages = _create_stages( - repo, sub_targets, fname, pbar=pbar, external=external + repo, + sub_targets, + fname, + pbar=pbar, + external=external, + glob=glob, ) try: @@ -149,15 +160,27 @@ def _find_all_targets(repo, target, recursive): return [target] -def _create_stages(repo, targets, fname, pbar=None, external=False): +def _create_stages( + repo, targets, fname, pbar=None, external=False, glob=False +): + from glob import iglob + from dvc.stage import Stage, create_stage - stages = [] + if glob: + expanded_targets = [ + exp_target + for target in targets + for exp_target in iglob(target, recursive=True) + ] + else: + expanded_targets = targets + stages = [] for out in Tqdm( - targets, + expanded_targets, desc="Creating DVC-files", - disable=len(targets) < LARGE_DIR_SIZE, + disable=len(expanded_targets) < LARGE_DIR_SIZE, unit="file", ): path, wdir, out = resolve_paths(repo, out) diff --git a/tests/func/test_add.py b/tests/func/test_add.py index 017f02d0b..483cdb857 100644 --- a/tests/func/test_add.py +++ b/tests/func/test_add.py @@ -190,6 +190,77 @@ def test_add_file_in_dir(tmp_dir, dvc): assert stage.outs[0].def_path == "subdata" +@pytest.mark.parametrize( + "target, expected_def_paths, expected_rel_paths", + [ + ( + os.path.join("dir", "subdir", "subdata*"), + ["subdata", "subdata123"], + [ + os.path.join("dir", "subdir", "subdata") + ".dvc", + os.path.join("dir", "subdir", "subdata123") + ".dvc", + ], + ), + ( + os.path.join("dir", "subdir", "?subdata"), + ["esubdata", "isubdata"], + [ + os.path.join("dir", "subdir", "esubdata") + ".dvc", + os.path.join("dir", "subdir", "isubdata") + ".dvc", + ], + ), + ( + os.path.join("dir", "subdir", "[aiou]subdata"), + ["isubdata"], + [os.path.join("dir", "subdir", "isubdata") + ".dvc"], + ), + ( + os.path.join("dir", "**", "subdata*"), + ["subdata", "subdata123", "subdata4", "subdata5"], + [ + os.path.join("dir", "subdir", "subdata") + ".dvc", + os.path.join("dir", "subdir", "subdata123") + ".dvc", + os.path.join("dir", "anotherdir", "subdata4") + ".dvc", + os.path.join("dir", "subdata5") + ".dvc", + ], + ), + ], +) +def test_add_filtered_files_in_dir( + tmp_dir, dvc, target, expected_def_paths, expected_rel_paths +): + tmp_dir.gen( + { + "dir": { + "subdir": { + "subdata": "subdata content", + "esubdata": "extra subdata content", + "isubdata": "i subdata content", + "subdata123": "subdata content 123", + }, + "anotherdir": { + "subdata4": "subdata 4 content", + "esubdata": "extra 2 subdata content", + }, + "subdata5": "subdata 5 content", + } + } + ) + + stages = dvc.add(target, glob=True) + + assert len(stages) == len(expected_def_paths) + for stage in stages: + assert stage is not None + assert len(stage.deps) == 0 + assert len(stage.outs) == 1 + assert stage.relpath in expected_rel_paths + + # Current dir should not be taken into account + assert stage.wdir == os.path.dirname(stage.path) + assert stage.outs[0].def_path in expected_def_paths + + @pytest.mark.parametrize( "workspace, hash_name, hash_value", [