From eb53c030c6cb3eb00705062e51f53471c95021ed Mon Sep 17 00:00:00 2001 From: pawel Date: Mon, 30 Dec 2019 18:18:28 +0100 Subject: [PATCH 1/3] local-related methods should verify that WorkingTree is in use --- dvc/remote/local.py | 4 +++- dvc/utils/fs.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dvc/remote/local.py b/dvc/remote/local.py index 4a6204dcd4..beb5e3a188 100644 --- a/dvc/remote/local.py +++ b/dvc/remote/local.py @@ -429,7 +429,9 @@ def _unprotect_file(path): os.chmod(path, os.stat(path).st_mode | stat.S_IWRITE) def _unprotect_dir(self, path): - assert isinstance(self.repo.tree, CleanTree) + assert isinstance(self.repo.tree, CleanTree) and isinstance( + self.repo.tree.tree, WorkingTree + ) for fname in self.repo.tree.walk_files(path): RemoteLOCAL._unprotect_file(fname) diff --git a/dvc/utils/fs.py b/dvc/utils/fs.py index 929867bb6b..7cea7e314e 100644 --- a/dvc/utils/fs.py +++ b/dvc/utils/fs.py @@ -33,8 +33,9 @@ def get_inode(path): def get_mtime_and_size(path, tree): from dvc.ignore import CleanTree + from dvc.scm.tree import WorkingTree - assert isinstance(tree, CleanTree) + assert isinstance(tree, CleanTree) and isinstance(tree.tree, WorkingTree) if os.path.isdir(fspath_py35(path)): size = 0 From 1e1a9cf774018d68565e6a78717165666b17cd84 Mon Sep 17 00:00:00 2001 From: pawel Date: Fri, 3 Jan 2020 10:03:47 +0100 Subject: [PATCH 2/3] tree: introduce is_working_tree --- dvc/remote/local.py | 11 +++-------- dvc/scm/tree.py | 6 ++++++ dvc/utils/fs.py | 6 ++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/dvc/remote/local.py b/dvc/remote/local.py index beb5e3a188..f7a1ff4c80 100644 --- a/dvc/remote/local.py +++ b/dvc/remote/local.py @@ -11,7 +11,6 @@ from dvc.exceptions import DownloadError from dvc.exceptions import DvcException from dvc.exceptions import UploadError -from dvc.ignore import CleanTree from dvc.path_info import PathInfo from dvc.progress import Tqdm from dvc.remote.base import RemoteBASE @@ -20,7 +19,7 @@ from dvc.remote.base import STATUS_MISSING from dvc.remote.base import STATUS_NEW from dvc.scheme import Schemes -from dvc.scm.tree import WorkingTree +from dvc.scm.tree import is_working_tree from dvc.system import System from dvc.utils import copyfile from dvc.utils import file_md5 @@ -136,9 +135,7 @@ def getsize(path_info): return os.path.getsize(fspath_py35(path_info)) def walk_files(self, path_info): - assert isinstance(self.repo.tree, CleanTree) and isinstance( - self.repo.tree.tree, WorkingTree - ) + assert is_working_tree(self.repo.tree) for fname in self.repo.tree.walk_files(path_info): yield PathInfo(fname) @@ -429,9 +426,7 @@ def _unprotect_file(path): os.chmod(path, os.stat(path).st_mode | stat.S_IWRITE) def _unprotect_dir(self, path): - assert isinstance(self.repo.tree, CleanTree) and isinstance( - self.repo.tree.tree, WorkingTree - ) + assert is_working_tree(self.repo.tree) for fname in self.repo.tree.walk_files(path): RemoteLOCAL._unprotect_file(fname) diff --git a/dvc/scm/tree.py b/dvc/scm/tree.py index 3187e300ab..1409b626ce 100644 --- a/dvc/scm/tree.py +++ b/dvc/scm/tree.py @@ -74,3 +74,9 @@ def onerror(e): top, topdown=topdown, onerror=onerror ): yield os.path.normpath(root), dirs, files + + +def is_working_tree(tree): + return isinstance(tree, WorkingTree) or isinstance( + getattr(tree, "tree", None), WorkingTree + ) diff --git a/dvc/utils/fs.py b/dvc/utils/fs.py index 7cea7e314e..6829c3c787 100644 --- a/dvc/utils/fs.py +++ b/dvc/utils/fs.py @@ -8,6 +8,7 @@ from shortuuid import uuid from dvc.exceptions import DvcException +from dvc.scm.tree import is_working_tree from dvc.system import System from dvc.utils import dict_md5 from dvc.utils import fspath @@ -32,10 +33,7 @@ def get_inode(path): def get_mtime_and_size(path, tree): - from dvc.ignore import CleanTree - from dvc.scm.tree import WorkingTree - - assert isinstance(tree, CleanTree) and isinstance(tree.tree, WorkingTree) + assert is_working_tree(tree) if os.path.isdir(fspath_py35(path)): size = 0 From 36427387c053fa77b8fef1f529235f6f7107f467 Mon Sep 17 00:00:00 2001 From: pawel Date: Fri, 3 Jan 2020 10:32:32 +0100 Subject: [PATCH 3/3] get_mtime_and_size: move working tree assertion after veryfing dir --- dvc/utils/fs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dvc/utils/fs.py b/dvc/utils/fs.py index 6829c3c787..21e0118ef3 100644 --- a/dvc/utils/fs.py +++ b/dvc/utils/fs.py @@ -33,9 +33,10 @@ def get_inode(path): def get_mtime_and_size(path, tree): - assert is_working_tree(tree) if os.path.isdir(fspath_py35(path)): + assert is_working_tree(tree) + size = 0 files_mtimes = {} for file_path in tree.walk_files(path):