From ff577d7a8751a91d2a0fee06f6976ff781d871b7 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Fri, 30 Nov 2018 10:01:46 +0200 Subject: [PATCH] run: use quotes to wrap arguments with spaces in them Fixes #1372 Signed-off-by: Ruslan Kuprieiev --- dvc/command/run.py | 18 +++++++++++++++++- tests/test_run.py | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/dvc/command/run.py b/dvc/command/run.py index 7167c2489..0fa2c2036 100644 --- a/dvc/command/run.py +++ b/dvc/command/run.py @@ -3,12 +3,28 @@ from dvc.exceptions import DvcException class CmdRun(CmdBase): + def _joined_cmd(self): + if len(self.args.command) == 0: + return '' + + if len(self.args.command) == 1: + return self.args.command[0] + + cmd = '' + for chunk in self.args.command: + if len(chunk.split()) != 1: + fmt = ' "{}"' + else: + fmt = ' {}' + cmd += fmt.format(chunk) + return cmd + def run(self): try: if self.args.yes: self.project.prompt.default = True - self.project.run(cmd=' '.join(self.args.command), + self.project.run(cmd=self._joined_cmd(), outs=self.args.outs, outs_no_cache=self.args.outs_no_cache, metrics_no_cache=self.args.metrics_no_cache, diff --git a/tests/test_run.py b/tests/test_run.py index 32ac4c33d..82c9da066 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -156,3 +156,20 @@ class TestCmdRun(TestDvc): ret = main(['run', 'non-existing-command']) self.assertNotEqual(ret, 0) + + def test_run_args_with_spaces(self): + with open(self.CODE, 'w') as fobj: + fobj.write("import sys\nopen(sys.argv[1], 'w+').write(sys.argv[2])") + + arg = 'arg1 arg2' + log = 'log' + ret = main(['run', + 'python', + self.CODE, + log, + 'arg1 arg2']) + + self.assertEqual(ret, 0) + + with open(log, 'r') as fobj: + self.assertEqual(fobj.read(), arg)