From 83ee6e46e037d7c471c8da70d3af689203e2a266 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Tue, 4 Dec 2018 07:49:21 +0200 Subject: [PATCH] run: introduce --remove-outs option Signed-off-by: Ruslan Kuprieiev --- dvc/cli.py | 5 +++++ dvc/command/run.py | 3 ++- dvc/project.py | 6 ++++-- dvc/stage.py | 7 ++++++- tests/test_run.py | 17 +++++++++++++++++ 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/dvc/cli.py b/dvc/cli.py index 15a92bc55..7522557f0 100644 --- a/dvc/cli.py +++ b/dvc/cli.py @@ -285,6 +285,11 @@ def parse_args(argv=None): help="Run this stage even if it has been already " "ran with the same command/dependencies/outputs/etc " "before.") + run_parser.add_argument( + '--remove-outs', + action='store_true', + default=False, + help="Remove outputs before running the command.") run_parser.add_argument( 'command', nargs=argparse.REMAINDER, diff --git a/dvc/command/run.py b/dvc/command/run.py index cba595a8e..79857919d 100644 --- a/dvc/command/run.py +++ b/dvc/command/run.py @@ -31,7 +31,8 @@ class CmdRun(CmdBase): cwd=self.args.cwd, no_exec=self.args.no_exec, overwrite=overwrite, - ignore_build_cache=self.args.ignore_build_cache) + ignore_build_cache=self.args.ignore_build_cache, + remove_outs=self.args.remove_outs) except DvcException as ex: self.project.logger.error('Failed to run command', ex) return 1 diff --git a/dvc/project.py b/dvc/project.py index a56c50cfa..84d7ddf58 100644 --- a/dvc/project.py +++ b/dvc/project.py @@ -364,7 +364,8 @@ class Project(object): cwd=os.curdir, no_exec=False, overwrite=False, - ignore_build_cache=False): + ignore_build_cache=False, + remove_outs=False): from dvc.stage import Stage with self.state: @@ -377,7 +378,8 @@ class Project(object): metrics_no_cache=metrics_no_cache, deps=deps, overwrite=overwrite, - ignore_build_cache=ignore_build_cache) + ignore_build_cache=ignore_build_cache, + remove_outs=remove_outs) if stage is None: return None diff --git a/dvc/stage.py b/dvc/stage.py index 58b36c348..9c1b754e0 100644 --- a/dvc/stage.py +++ b/dvc/stage.py @@ -340,7 +340,8 @@ class Stage(object): locked=False, add=False, overwrite=True, - ignore_build_cache=False): + ignore_build_cache=False, + remove_outs=False): stage = Stage(project=project, cwd=cwd, @@ -368,6 +369,10 @@ class Stage(object): stage.cwd = cwd stage.path = path + # NOTE: remove outs before we check build cache + if remove_outs: + stage.remove_outs(ignore_remove=False) + if os.path.exists(path): if not ignore_build_cache and stage.is_cached(): Logger.info('Stage is cached, skipping.') diff --git a/tests/test_run.py b/tests/test_run.py index 8770620bd..158929583 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -185,6 +185,23 @@ class TestCmdRun(TestDvc): self.assertEqual(ret, 252) +class TestRunRemoveOuts(TestDvc): + def test(self): + with open(self.CODE, 'w+') as fobj: + fobj.write("import sys\n") + fobj.write("import os\n") + fobj.write("if os.path.exists(sys.argv[1]):\n") + fobj.write(" sys.exit(1)\n") + fobj.write("open(sys.argv[1], 'w+').close()\n") + + ret = main(['run', + '--remove-outs', + '-d', self.CODE, + '-o', self.FOO, + 'python', self.CODE, self.FOO]) + self.assertEqual(ret, 0) + + class TestCmdRunOverwrite(TestDvc): def test(self): ret = main(['run',