Skip to content

Commit

Permalink
dvc: add --cwd for dvc repro
Browse files Browse the repository at this point in the history
Fixes iterative#401

Signed-off-by: Ruslan Kuprieiev <[email protected]>
  • Loading branch information
efiop committed Jun 5, 2018
1 parent 5d16991 commit 9389422
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ innosetup/config.ini
.coverage

dvc/version.py

*.swp
4 changes: 4 additions & 0 deletions dvc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ def parse_args(argv=None):
action='store_true',
default=False,
help='Reproduce only single data item without recursive dependencies check.')
repro_parser.add_argument('-c',
'--cwd',
default=os.path.curdir,
help='Directory to reproduce from.')
repro_parser.set_defaults(func=CmdRepro)

# Remove
Expand Down
18 changes: 14 additions & 4 deletions dvc/command/repro.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
class CmdRepro(CmdBase):
def run(self):
recursive = not self.args.single_item
saved_dir = os.path.realpath(os.curdir)
if self.args.cwd:
os.chdir(self.args.cwd)

ret = 0
for target in self.args.targets:
try:
self.project.reproduce(target,
Expand All @@ -16,7 +21,8 @@ def run(self):
except ReproductionError as ex:
msg = 'Failed to reproduce \'{}\''.format(target)
self.project.logger.error(msg, ex)
return 1
ret = 1
break
except StageNotFoundError as ex:
if os.path.exists(target):
msg = '\'{}\' is not a dvc file.'.format(target)
Expand All @@ -26,9 +32,13 @@ def run(self):
else:
msg = '\'{}\' does not exist.'.format(target)
self.project.logger.error(msg)
return 1
ret = 1
break
except Exception as ex:
msg = 'Failed to reproduce \'{}\' - unexpected error'.format(target)
self.project.logger.error(msg, ex)
return 1
return 0
ret = 1
break

os.chdir(saved_dir)
return ret
31 changes: 31 additions & 0 deletions tests/test_repro.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,37 @@ def test(self):
self.assertNotEqual(ret, 0)


class TestCmdReproChdir(TestDvc):
def test(self):
dname = 'dir'
os.mkdir(dname)
foo = os.path.join(dname, self.FOO)
bar = os.path.join(dname, self.BAR)
code = os.path.join(dname, self.CODE)
shutil.copyfile(self.FOO, foo)
shutil.copyfile(self.CODE, code)

ret = main(['run',
'-f', 'Dvcfile',
'-c', dname,
'-d', self.FOO,
'-o', self.BAR,
'python {} {} {}'.format(self.CODE, self.FOO, self.BAR)])
self.assertEqual(ret, 0)
self.assertTrue(os.path.isfile(foo))
self.assertTrue(os.path.isfile(bar))
self.assertTrue(filecmp.cmp(foo, bar))

os.unlink(bar)

ret = main(['repro',
'-c', dname])
self.assertEqual(ret, 0)
self.assertTrue(os.path.isfile(foo))
self.assertTrue(os.path.isfile(bar))
self.assertTrue(filecmp.cmp(foo, bar))


class TestReproExternalBase(TestDvc):
def should_test(self):
return False
Expand Down

0 comments on commit 9389422

Please sign in to comment.