From 11354392d3751b346330be5a967273eb3f5a10cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Redzy=C5=84ski?= Date: Wed, 12 Feb 2020 12:07:02 +0100 Subject: [PATCH] imp_url: make default stage fname accompany import target --- dvc/repo/add.py | 4 +++- dvc/repo/imp_url.py | 8 +++++++- dvc/stage.py | 7 +++---- tests/func/test_import_url.py | 12 ++++++++++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/dvc/repo/add.py b/dvc/repo/add.py index e469119b93..7cab157d61 100644 --- a/dvc/repo/add.py +++ b/dvc/repo/add.py @@ -105,7 +105,9 @@ def _create_stages(repo, targets, fname, pbar=None): disable=True if len(targets) < LARGE_DIR_SIZE else None, unit="file", ): - stage = Stage.create(repo, outs=[out], add=True, fname=fname) + stage = Stage.create( + repo, outs=[out], accompany_outs=True, fname=fname + ) if not stage: if pbar is not None: diff --git a/dvc/repo/imp_url.py b/dvc/repo/imp_url.py index d59659354b..d62f867788 100644 --- a/dvc/repo/imp_url.py +++ b/dvc/repo/imp_url.py @@ -11,7 +11,13 @@ def imp_url(self, url, out=None, fname=None, erepo=None, locked=True): out = resolve_output(url, out) stage = Stage.create( - self, cmd=None, deps=[url], outs=[out], fname=fname, erepo=erepo + self, + cmd=None, + deps=[url], + outs=[out], + fname=fname, + erepo=erepo, + accompany_outs=True, ) if stage is None: diff --git a/dvc/stage.py b/dvc/stage.py index 4efdb7b12d..954be11ba1 100644 --- a/dvc/stage.py +++ b/dvc/stage.py @@ -502,12 +502,11 @@ def is_cached(self): return True @staticmethod - def create(repo, **kwargs): + def create(repo, accompany_outs=False, **kwargs): wdir = kwargs.get("wdir", None) cwd = kwargs.get("cwd", None) fname = kwargs.get("fname", None) - add = kwargs.get("add", False) # Backward compatibility for `cwd` option if wdir is None and cwd is not None: @@ -539,12 +538,12 @@ def create(repo, **kwargs): stage._check_duplicated_arguments() if not fname: - fname = Stage._stage_fname(stage.outs, add) + fname = Stage._stage_fname(stage.outs, accompany_outs) stage._check_dvc_filename(fname) # Autodetecting wdir for add, we need to create outs first to do that, # so we start with wdir = . and remap out paths later. - if add and kwargs.get("wdir") is None and cwd is None: + if accompany_outs and kwargs.get("wdir") is None and cwd is None: wdir = os.path.dirname(fname) for out in chain(stage.outs, stage.deps): diff --git a/tests/func/test_import_url.py b/tests/func/test_import_url.py index 4eea903179..d49099c833 100644 --- a/tests/func/test_import_url.py +++ b/tests/func/test_import_url.py @@ -92,3 +92,15 @@ def test_import_url_to_dir(dname, tmp_dir, dvc): assert stage.outs[0].path_info == dst assert os.path.isdir(dname) assert dst.read_text() == "file content" + + +def test_import_stage_accompanies_target(tmp_dir, dvc, erepo_dir): + with erepo_dir.chdir(): + erepo_dir.dvc_gen("file1", "file1 content", commit="commit file") + + tmp_dir.gen({"dir": {}}) + erepo = {"url": fspath(erepo_dir)} + dvc.imp_url("file1", out=os.path.join("dir", "imported_file"), erepo=erepo) + + assert (tmp_dir / "dir" / "imported_file").exists() + assert (tmp_dir / "dir" / "imported_file.dvc").exists()