mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-19 15:28:28 +01:00
@@ -192,6 +192,13 @@ def parse_args(argv=None):
|
||||
default=False,
|
||||
help='Checkout all dependencies of the '
|
||||
'specified target.')
|
||||
checkout_parser.add_argument(
|
||||
'-f',
|
||||
'--force',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Do not prompt when removing '
|
||||
'working directory files.')
|
||||
checkout_parser.add_argument(
|
||||
'targets',
|
||||
nargs='*',
|
||||
|
||||
@@ -4,9 +4,10 @@ from dvc.command.base import CmdBase
|
||||
class CmdCheckout(CmdBase):
|
||||
def run(self):
|
||||
if not self.args.targets:
|
||||
self.project.checkout()
|
||||
self.project.checkout(force=self.args.force)
|
||||
else:
|
||||
for target in self.args.targets:
|
||||
self.project.checkout(target=target,
|
||||
with_deps=self.args.with_deps)
|
||||
with_deps=self.args.with_deps,
|
||||
force=self.args.force)
|
||||
return 0
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ class OutputGS(DependencyGS):
|
||||
|
||||
return False
|
||||
|
||||
def checkout(self):
|
||||
def checkout(self, force=False):
|
||||
if not self.use_cache:
|
||||
return
|
||||
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ class OutputHDFS(DependencyHDFS):
|
||||
|
||||
return False
|
||||
|
||||
def checkout(self):
|
||||
def checkout(self, force=False):
|
||||
if not self.use_cache:
|
||||
return
|
||||
|
||||
|
||||
+4
-2
@@ -60,11 +60,13 @@ class OutputLOCAL(DependencyLOCAL):
|
||||
|
||||
return self.project.cache.local.changed(self.path_info, self.info)
|
||||
|
||||
def checkout(self):
|
||||
def checkout(self, force=False):
|
||||
if not self.use_cache:
|
||||
return
|
||||
|
||||
self.project.cache.local.checkout(self.path_info, self.info)
|
||||
self.project.cache.local.checkout(self.path_info,
|
||||
self.info,
|
||||
force=force)
|
||||
|
||||
def _verify_metric(self):
|
||||
if not self.metric:
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ class OutputS3(DependencyS3):
|
||||
|
||||
return False
|
||||
|
||||
def checkout(self):
|
||||
def checkout(self, force=False):
|
||||
if not self.use_cache:
|
||||
return
|
||||
|
||||
|
||||
+1
-1
@@ -35,7 +35,7 @@ class OutputSSH(DependencySSH):
|
||||
|
||||
return False
|
||||
|
||||
def checkout(self):
|
||||
def checkout(self, force=False):
|
||||
if not self.use_cache:
|
||||
return
|
||||
|
||||
|
||||
+2
-2
@@ -466,7 +466,7 @@ class Project(object):
|
||||
used.append(out.path)
|
||||
self.state.remove_unused_links(used)
|
||||
|
||||
def checkout(self, target=None, with_deps=False):
|
||||
def checkout(self, target=None, with_deps=False, force=False):
|
||||
all_stages = self.active_stages()
|
||||
stages = all_stages
|
||||
|
||||
@@ -482,7 +482,7 @@ class Project(object):
|
||||
'not going to be checked out.'
|
||||
self.logger.warn(msg.format(stage.relpath))
|
||||
|
||||
stage.checkout()
|
||||
stage.checkout(force=force)
|
||||
|
||||
def _get_pipeline(self, node):
|
||||
pipelines = list(filter(lambda g: node in g.nodes(),
|
||||
|
||||
+1
-1
@@ -92,7 +92,7 @@ class RemoteBase(object):
|
||||
def save(self, path_info):
|
||||
raise NotImplementedError
|
||||
|
||||
def checkout(self, path_info, checksum_info):
|
||||
def checkout(self, path_info, checksum_info, force=False):
|
||||
raise NotImplementedError
|
||||
|
||||
def download(self, from_infos, to_infos, no_progress_bar=False, name=None):
|
||||
|
||||
+16
-3
@@ -268,7 +268,7 @@ class RemoteLOCAL(RemoteBase):
|
||||
def is_dir_cache(cls, cache):
|
||||
return cache.endswith(cls.MD5_DIR_SUFFIX)
|
||||
|
||||
def checkout(self, path_info, checksum_info):
|
||||
def checkout(self, path_info, checksum_info, force=False):
|
||||
path = path_info['path']
|
||||
md5 = checksum_info.get(self.PARAM_MD5)
|
||||
cache = self.get(md5)
|
||||
@@ -337,14 +337,14 @@ class RemoteLOCAL(RemoteBase):
|
||||
if bar:
|
||||
progress.update_target(dir_relpath, processed, dir_size)
|
||||
|
||||
self._discard_working_directory_changes(path, dir_info)
|
||||
self._discard_working_directory_changes(path, dir_info, force=force)
|
||||
|
||||
self.state.update_link(path)
|
||||
|
||||
if bar:
|
||||
progress.finish_target(dir_relpath)
|
||||
|
||||
def _discard_working_directory_changes(self, path, dir_info):
|
||||
def _discard_working_directory_changes(self, path, dir_info, force=False):
|
||||
working_dir_files = set(
|
||||
os.path.join(root, file)
|
||||
for root, _, files in os.walk(path)
|
||||
@@ -359,6 +359,19 @@ class RemoteLOCAL(RemoteBase):
|
||||
delta = working_dir_files - cached_files
|
||||
|
||||
for file in delta:
|
||||
msg = (
|
||||
'File "{}" is going to be removed. '
|
||||
'Are you sure you want to proceed?'
|
||||
.format(file)
|
||||
)
|
||||
|
||||
confirmed = force or self.project.prompt.prompt(msg, False)
|
||||
|
||||
if not confirmed:
|
||||
raise DvcException('Unable to remove {} without a confirmation'
|
||||
" from the user. Use '-f' to force."
|
||||
.format(file))
|
||||
|
||||
remove(file)
|
||||
|
||||
def _move(self, inp, outp):
|
||||
|
||||
+2
-2
@@ -478,9 +478,9 @@ class Stage(object):
|
||||
if paths:
|
||||
raise MissingDataSource(paths)
|
||||
|
||||
def checkout(self):
|
||||
def checkout(self, force=False):
|
||||
for out in self.outs:
|
||||
out.checkout()
|
||||
out.checkout(force=force)
|
||||
|
||||
def _status(self, entries, name):
|
||||
ret = {}
|
||||
|
||||
+27
-8
@@ -13,7 +13,9 @@ from tests.basic_env import TestDvc
|
||||
from tests.test_repro import TestRepro
|
||||
from dvc.stage import Stage
|
||||
from dvc.remote.local import RemoteLOCAL
|
||||
from dvc.exceptions import DvcException
|
||||
|
||||
from mock import patch
|
||||
|
||||
class TestCheckout(TestRepro):
|
||||
def setUp(self):
|
||||
@@ -162,22 +164,39 @@ class TestRemoveFilesWhenCheckout(CheckoutBase):
|
||||
class TestCheckoutCleanWorkingDir(CheckoutBase):
|
||||
def test(self):
|
||||
from tests.test_data_cloud import sleep
|
||||
|
||||
|
||||
stages = self.dvc.add(self.DATA_DIR)
|
||||
stage = stages[0]
|
||||
|
||||
sleep()
|
||||
|
||||
|
||||
working_dir_change = os.path.join(self.DATA_DIR, 'not_cached.txt')
|
||||
with open(working_dir_change, 'w') as f:
|
||||
f.write('not_cached')
|
||||
|
||||
sleep()
|
||||
|
||||
ret = main(['checkout', stage.relpath])
|
||||
|
||||
ret = main(['checkout', '--force', stage.relpath])
|
||||
self.assertEqual(ret, 0)
|
||||
self.assertFalse(os.path.exists(working_dir_change))
|
||||
|
||||
@patch('dvc.prompt.Prompt.prompt')
|
||||
def test_force(self, mock_prompt):
|
||||
mock_prompt.return_value = False
|
||||
|
||||
stages = self.dvc.add(self.DATA_DIR)
|
||||
stage = stages[0]
|
||||
|
||||
working_dir_change = os.path.join(self.DATA_DIR, 'not_cached.txt')
|
||||
with open(working_dir_change, 'w') as f:
|
||||
f.write('not_cached')
|
||||
|
||||
ret = main(['checkout', stage.relpath])
|
||||
|
||||
mock_prompt.assert_called()
|
||||
self.assertNotEqual(ret, 0)
|
||||
self.assertRaises(DvcException)
|
||||
|
||||
|
||||
class TestCheckoutSelectiveRemove(CheckoutBase):
|
||||
def test(self):
|
||||
@@ -193,16 +212,16 @@ class TestCheckoutSelectiveRemove(CheckoutBase):
|
||||
staged_files = self.outs_info(stage)
|
||||
|
||||
sleep()
|
||||
|
||||
|
||||
os.remove(staged_files[0].path)
|
||||
|
||||
|
||||
sleep()
|
||||
|
||||
|
||||
ret = main(['checkout', stage.relpath])
|
||||
self.assertEqual(ret, 0)
|
||||
|
||||
sleep()
|
||||
|
||||
|
||||
checkedout_files = self.outs_info(stage)
|
||||
|
||||
self.assertEqual(len(staged_files), len(checkedout_files))
|
||||
|
||||
Reference in New Issue
Block a user