mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
remove: use stage files as targets and only remove outputs
Fixes #516 Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
+2
-2
@@ -194,10 +194,10 @@ def parse_args(argv=None):
|
||||
remove_parser = subparsers.add_parser(
|
||||
'remove',
|
||||
parents=[parent_parser],
|
||||
help='Remove data file or data directory')
|
||||
help='Remove outputs of DVC file.')
|
||||
remove_parser.add_argument('targets',
|
||||
nargs='+',
|
||||
help='Target to remove - file or directory.')
|
||||
help='DVC files.')
|
||||
remove_parser.set_defaults(func=CmdRemove)
|
||||
|
||||
# Garbage collector
|
||||
|
||||
+7
-13
@@ -91,21 +91,15 @@ class Project(object):
|
||||
stage.dump()
|
||||
return stage
|
||||
|
||||
def remove(self, fname):
|
||||
stages = []
|
||||
output = Output.loads(self, fname)
|
||||
for out in self.outs():
|
||||
if out.path == output.path:
|
||||
stage = out.stage()
|
||||
stages.append(stage)
|
||||
def remove(self, target):
|
||||
if not Stage.is_stage_file(target):
|
||||
raise StageNotFoundError(target)
|
||||
|
||||
if len(stages) == 0:
|
||||
raise StageNotFoundError(fname)
|
||||
stage = Stage.load(self, target)
|
||||
for out in stage.outs:
|
||||
out.remove()
|
||||
|
||||
for stage in stages:
|
||||
stage.remove()
|
||||
|
||||
return stages
|
||||
return stage
|
||||
|
||||
def run(self,
|
||||
cmd=None,
|
||||
|
||||
+10
-18
@@ -12,43 +12,35 @@ from tests.basic_env import TestDvc
|
||||
class TestRemove(TestDvc):
|
||||
def test(self):
|
||||
stage = self.dvc.add(self.FOO)
|
||||
stages_removed = self.dvc.remove(self.FOO)
|
||||
stage_removed = self.dvc.remove(stage.path)
|
||||
|
||||
self.assertEqual(len(stages_removed), 1)
|
||||
self.assertIsInstance(stages_removed[0], Stage)
|
||||
self.assertEqual(stage.path, stages_removed[0].path)
|
||||
self.assertIsInstance(stage_removed, Stage)
|
||||
self.assertEqual(stage.path, stage_removed.path)
|
||||
self.assertFalse(os.path.isfile(self.FOO))
|
||||
|
||||
|
||||
class TestRemoveNonExistentFile(TestDvc):
|
||||
def test(self):
|
||||
with self.assertRaises(StageNotFoundError) as cx:
|
||||
self.dvc.remove('non_existent_file')
|
||||
|
||||
|
||||
class TestRemoveFileOutsideOfRepo(TestDvc):
|
||||
def test(self):
|
||||
with self.assertRaises(CmdOutputOutsideOfRepoError) as cx:
|
||||
self.dvc.remove(os.path.join(os.path.dirname(self.dvc.root_dir), self.FOO))
|
||||
self.dvc.remove('non_existent_dvc_file')
|
||||
|
||||
|
||||
class TestRemoveDirectory(TestDvc):
|
||||
def test(self):
|
||||
stage_add = self.dvc.add(self.DATA_DIR)
|
||||
stages_removed = self.dvc.remove(self.DATA_DIR)
|
||||
self.assertEqual(len(stages_removed), 1)
|
||||
stage_removed = stages_removed[0]
|
||||
stage_removed = self.dvc.remove(stage_add.path)
|
||||
self.assertEqual(stage_add.path, stage_removed.path)
|
||||
self.assertFalse(os.path.exists(self.DATA_DIR))
|
||||
self.assertFalse(os.path.exists(stage_removed.path))
|
||||
self.assertTrue(os.path.exists(stage_removed.path))
|
||||
|
||||
|
||||
class TestCmdRemove(TestDvc):
|
||||
def test(self):
|
||||
self.dvc.add(self.FOO)
|
||||
stage = self.dvc.add(self.FOO)
|
||||
ret = main(['remove',
|
||||
self.FOO])
|
||||
stage.path])
|
||||
self.assertEqual(ret, 0)
|
||||
|
||||
ret = main(['remove',
|
||||
'non-existing-file'])
|
||||
'non-existing-dvc-file'])
|
||||
self.assertNotEqual(ret, 0)
|
||||
|
||||
Reference in New Issue
Block a user