stage: remove outs upon import (#1796)

* stage: remove outs upon import

* Update dvc/remote/local.py

Co-Authored-By: pared <[email protected]>
This commit is contained in:
Paweł Redzyński
2019-03-28 16:45:47 +02:00
committed by Ruslan Kuprieiev
parent c48a5e99be
commit 6f9dac280f
4 changed files with 29 additions and 4 deletions
+5 -1
View File
@@ -755,7 +755,11 @@ class RemoteLOCAL(RemoteBase):
# would get only the part of file. So, at first, the file should be
# copied with the temporary name, and then original file should be
# replaced by new.
copyfile(path, tmp)
copyfile(
path,
tmp,
name="Unprotecting '{}'".format(os.path.relpath(path)),
)
remove(path)
os.rename(tmp, path)
+1 -1
View File
@@ -730,7 +730,7 @@ class Stage(object):
raise StageCmdFailedError(self)
def run(self, dry=False, resume=False, no_commit=False, force=False):
if self.cmd and not self.locked and not dry:
if (self.cmd or self.is_import) and not self.locked and not dry:
self.remove_outs(ignore_remove=False, force=False)
if self.locked:
+22 -1
View File
@@ -1,3 +1,5 @@
import dvc
from dvc.utils.compat import str
import os
@@ -9,7 +11,7 @@ from dvc.exceptions import DvcException
from dvc.main import main
from mock import patch, mock_open, call
from tests.basic_env import TestDvc
from tests.utils import reset_logger_error_output
from tests.utils import reset_logger_error_output, spy
from tests.utils.httpd import StaticFileServer
from tests.utils.logger import MockLoggerHandlers, ConsoleFontColorsRemover
@@ -137,3 +139,22 @@ class TestShouldNotResumeDownload(TestInterruptedDownload):
m_handle = m()
expected_calls = [call(b"f"), call(b"o"), call(b"o")]
m_handle.write.assert_has_calls(expected_calls, any_order=False)
class TestShouldRemoveOutsBeforeImport(TestDvc):
def setUp(self):
super(TestShouldRemoveOutsBeforeImport, self).setUp()
tmp_dir = self.mkdtemp()
self.external_source = os.path.join(tmp_dir, "file")
with open(self.external_source, "w") as fobj:
fobj.write("content")
def test(self):
remove_outs_call_counter = spy(dvc.stage.Stage.remove_outs)
with patch.object(
dvc.stage.Stage, "remove_outs", remove_outs_call_counter
):
ret = main(["import", self.external_source])
self.assertEqual(0, ret)
self.assertEqual(1, remove_outs_call_counter.mock.call_count)
+1 -1
View File
@@ -864,7 +864,7 @@ class TestShouldRaiseOnOverlappingOutputPaths(TestDvc):
class TestRerunWithSameOutputs(TestDvc):
def _read_content_only(self, path):
with open(path, "r") as fobj:
return [line.rstrip() for line in fobj.readlines()]
return [line.rstrip() for line in fobj]
@property
def _outs_command(self):