From b07c386d0202d28ebe84d83c372f30534885393f Mon Sep 17 00:00:00 2001 From: vyloy Date: Mon, 15 Apr 2019 14:38:49 +0800 Subject: [PATCH 1/3] import: support the '--file' option --- dvc/command/imp.py | 14 +++++++++++++- dvc/repo/imp.py | 6 ++++-- tests/func/test_import.py | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/dvc/command/imp.py b/dvc/command/imp.py index 726bb193c2..3cd992bac8 100644 --- a/dvc/command/imp.py +++ b/dvc/command/imp.py @@ -19,7 +19,9 @@ def run(self): out = self.args.out or default_out - self.repo.imp(self.args.url, out, self.args.resume) + self.repo.imp( + self.args.url, out, self.args.resume, fname=self.args.file + ) except DvcException: logger.exception( "failed to import {}. You could also try downloading " @@ -62,4 +64,14 @@ def add_parser(subparsers, parent_parser): import_parser.add_argument( "out", nargs="?", help="Destination path to put files to." ) + import_parser.add_argument( + "-f", + "--file", + help="Specify name of the stage file. It should be " + "either 'Dvcfile' or have a '.dvc' suffix (e.g. " + "'prepare.dvc', 'clean.dvc', etc) in order for " + "dvc to be able to find it later. By default " + "the first output basename + .dvc is used as " + "a stage filename.", + ) import_parser.set_defaults(func=CmdImport) diff --git a/dvc/repo/imp.py b/dvc/repo/imp.py index e317eb008f..acbbc4afd6 100644 --- a/dvc/repo/imp.py +++ b/dvc/repo/imp.py @@ -2,11 +2,13 @@ @scm_context -def imp(self, url, out, resume=False): +def imp(self, url, out, resume=False, fname=None): from dvc.stage import Stage with self.state: - stage = Stage.create(repo=self, cmd=None, deps=[url], outs=[out]) + stage = Stage.create( + repo=self, cmd=None, deps=[url], outs=[out], fname=fname + ) if stage is None: return None diff --git a/tests/func/test_import.py b/tests/func/test_import.py index 8d770cacd5..5d47bcdc2c 100644 --- a/tests/func/test_import.py +++ b/tests/func/test_import.py @@ -156,3 +156,23 @@ def test(self): self.assertEqual(0, ret) self.assertEqual(1, remove_outs_call_counter.mock.call_count) + + +class TestImportFilename(TestDvc): + def setUp(self): + super(TestImportFilename, self).setUp() + tmp_dir = self.mkdtemp() + self.external_source = os.path.join(tmp_dir, "file") + with open(self.external_source, "w") as fobj: + fobj.write("content") + + def test(self): + ret = main(["import", "-f", "bar.dvc", self.external_source]) + self.assertEqual(0, ret) + self.assertTrue(os.path.exists("bar.dvc")) + + os.remove("bar.dvc") + + ret = main(["import", "--file", "bar.dvc", self.external_source]) + self.assertEqual(0, ret) + self.assertTrue(os.path.exists("bar.dvc")) From 01a28f2de9cebebb840209d001699cb3012226f1 Mon Sep 17 00:00:00 2001 From: vyloy Date: Tue, 16 Apr 2019 10:35:59 +0800 Subject: [PATCH 2/3] import: improve some details about the '--file' option. --- dvc/command/add.py | 9 +-------- dvc/command/imp.py | 9 +-------- dvc/command/run.py | 9 +-------- tests/func/test_import.py | 7 +++++++ 4 files changed, 10 insertions(+), 24 deletions(-) diff --git a/dvc/command/add.py b/dvc/command/add.py index f6f9ab2799..43a1d2b516 100644 --- a/dvc/command/add.py +++ b/dvc/command/add.py @@ -54,14 +54,7 @@ def add_parser(subparsers, parent_parser): help="Don't put files/directories into cache.", ) add_parser.add_argument( - "-f", - "--file", - help="Specify name of the stage file. It should be " - "either 'Dvcfile' or have a '.dvc' suffix (e.g. " - "'prepare.dvc', 'clean.dvc', etc) in order for " - "dvc to be able to find it later. By default " - "the output basename + .dvc is used as a stage filename. " - "(NOTE: It can't be used when specifying multiple targets)", + "-f", "--file", help="Specify name of the DVC file it generates." ) add_parser.add_argument( "targets", nargs="+", help="Input files/directories." diff --git a/dvc/command/imp.py b/dvc/command/imp.py index 3cd992bac8..b549bac4e7 100644 --- a/dvc/command/imp.py +++ b/dvc/command/imp.py @@ -65,13 +65,6 @@ def add_parser(subparsers, parent_parser): "out", nargs="?", help="Destination path to put files to." ) import_parser.add_argument( - "-f", - "--file", - help="Specify name of the stage file. It should be " - "either 'Dvcfile' or have a '.dvc' suffix (e.g. " - "'prepare.dvc', 'clean.dvc', etc) in order for " - "dvc to be able to find it later. By default " - "the first output basename + .dvc is used as " - "a stage filename.", + "-f", "--file", help="Specify name of the DVC file it generates." ) import_parser.set_defaults(func=CmdImport) diff --git a/dvc/command/run.py b/dvc/command/run.py index 3a69a88527..e711744eec 100644 --- a/dvc/command/run.py +++ b/dvc/command/run.py @@ -127,14 +127,7 @@ def add_parser(subparsers, parent_parser): "(do not put into DVC cache).", ) run_parser.add_argument( - "-f", - "--file", - help="Specify name of the stage file. It should be " - "either 'Dvcfile' or have a '.dvc' suffix (e.g. " - "'prepare.dvc', 'clean.dvc', etc) in order for " - "dvc to be able to find it later. By default " - "the first output basename + .dvc is used as " - "a stage filename.", + "-f", "--file", help="Specify name of the DVC file it generates." ) run_parser.add_argument( "-c", "--cwd", default=None, help="Deprecated, use -w and -f instead." diff --git a/tests/func/test_import.py b/tests/func/test_import.py index 5d47bcdc2c..4a7fd5dd09 100644 --- a/tests/func/test_import.py +++ b/tests/func/test_import.py @@ -176,3 +176,10 @@ def test(self): ret = main(["import", "--file", "bar.dvc", self.external_source]) self.assertEqual(0, ret) self.assertTrue(os.path.exists("bar.dvc")) + + os.remove("bar.dvc") + os.mkdir("sub") + + ret = main(["import", "--file", "sub/bar.dvc", self.external_source]) + self.assertEqual(0, ret) + self.assertTrue(os.path.exists("sub/bar.dvc")) From 60c89adeb5dcc293d8661d6aabeb1da6d05466f5 Mon Sep 17 00:00:00 2001 From: vyloy Date: Thu, 18 Apr 2019 09:25:38 +0800 Subject: [PATCH 3/3] import: use os.path.join instead of hardcode path in the test --- tests/func/test_import.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/func/test_import.py b/tests/func/test_import.py index 4a7fd5dd09..db908cf5ab 100644 --- a/tests/func/test_import.py +++ b/tests/func/test_import.py @@ -180,6 +180,7 @@ def test(self): os.remove("bar.dvc") os.mkdir("sub") - ret = main(["import", "--file", "sub/bar.dvc", self.external_source]) + path = os.path.join("sub", "bar.dvc") + ret = main(["import", "--file", path, self.external_source]) self.assertEqual(0, ret) - self.assertTrue(os.path.exists("sub/bar.dvc")) + self.assertTrue(os.path.exists(path))