def test_path_object_and_str_are_valid_types_get_mtime_and_size( path, repo_dir): tree = CleanTree(WorkingTree(repo_dir.root_dir)) time, size = get_mtime_and_size(path, tree) object_time, object_size = get_mtime_and_size(PathInfo(path), tree) assert time == object_time assert size == object_size
def tree(self, tree): if is_working_tree(tree) or tree.tree_root == self.root_dir: root = None else: root = self.root_dir self._tree = (tree if isinstance(tree, CleanTree) else CleanTree( tree, root)) # Our graph cache is no longer valid, as it was based on the previous # tree. self._reset()
def __init__(self, root_dir=None): from dvc.state import State from dvc.lock import make_lock from dvc.scm import SCM from dvc.cache import Cache from dvc.data_cloud import DataCloud from dvc.repo.metrics import Metrics from dvc.scm.tree import WorkingTree from dvc.repo.tag import Tag from dvc.utils import makedirs root_dir = self.find_root(root_dir) self.root_dir = os.path.abspath(os.path.realpath(root_dir)) self.dvc_dir = os.path.join(self.root_dir, self.DVC_DIR) self.config = Config(self.dvc_dir) self.scm = SCM(self.root_dir) self.tree = CleanTree(WorkingTree(self.root_dir)) self.tmp_dir = os.path.join(self.dvc_dir, "tmp") makedirs(self.tmp_dir, exist_ok=True) hardlink_lock = self.config.config["core"].get("hardlink_lock", False) self.lock = make_lock( os.path.join(self.dvc_dir, "lock"), tmp_dir=os.path.join(self.dvc_dir, "tmp"), hardlink_lock=hardlink_lock, friendly=True, ) # NOTE: storing state and link_state in the repository itself to avoid # any possible state corruption in 'shared cache dir' scenario. self.state = State(self, self.config.config) core = self.config.config[Config.SECTION_CORE] level = core.get(Config.SECTION_CORE_LOGLEVEL) if level: logger.setLevel(level.upper()) self.cache = Cache(self) self.cloud = DataCloud(self) self.metrics = Metrics(self) self.tag = Tag(self) self._ignore()
def test_nobranch(self): tree = CleanTree(WorkingTree(self._root_dir)) self.assertWalkEqual( tree.walk("."), [ (".", ["data_dir"], ["bar", "тест", "code.py", "foo"]), (join("data_dir"), ["data_sub_dir"], ["data"]), (join("data_dir", "data_sub_dir"), [], ["data_sub"]), ], ) self.assertWalkEqual( tree.walk(join("data_dir", "data_sub_dir")), [(join("data_dir", "data_sub_dir"), [], ["data_sub"])], )
def test_nobranch(self): tree = CleanTree(LocalRemoteTree(None, {"url": self._root_dir})) self.assertWalkEqual( tree.walk("."), [ (".", ["data_dir"], ["bar", "тест", "code.py", "foo"]), (join("data_dir"), ["data_sub_dir"], ["data"]), (join("data_dir", "data_sub_dir"), [], ["data_sub"]), ], ) self.assertWalkEqual( tree.walk(join("data_dir", "data_sub_dir")), [(join("data_dir", "data_sub_dir"), [], ["data_sub"])], )
def test_path_object_and_str_are_valid_types_get_mtime_and_size(tmp_dir): tmp_dir.gen( {"dir": {"dir_file": "dir file content"}, "file": "file_content"} ) tree = CleanTree(WorkingTree(tmp_dir)) time, size = get_mtime_and_size("dir", tree) object_time, object_size = get_mtime_and_size(PathInfo("dir"), tree) assert time == object_time assert size == object_size time, size = get_mtime_and_size("file", tree) object_time, object_size = get_mtime_and_size(PathInfo("file"), tree) assert time == object_time assert size == object_size
def test_ignore_on_branch(tmp_dir, scm, dvc): tmp_dir.scm_gen({"foo": "foo", "bar": "bar"}, commit="add files") scm.checkout("branch", create_new=True) tmp_dir.scm_gen(DvcIgnore.DVCIGNORE_FILE, "foo", commit="add ignore") scm.checkout("master") assert _files_set(".", dvc.tree) == {"./foo", "./bar"} tree = CleanTree(scm.get_tree("branch")) assert _files_set(".", tree) == { to_posixpath(os.path.join(tree.tree_root, DvcIgnore.DVCIGNORE_FILE)), to_posixpath(os.path.join(tree.tree_root, "bar")), }
def test(self): tree = CleanTree(LocalRemoteTree(None, {"url": self.root_dir})) file_time, file_size = get_mtime_and_size(self.DATA, tree) dir_time, dir_size = get_mtime_and_size(self.DATA_DIR, tree) actual_file_size = os.path.getsize(self.DATA) actual_dir_size = os.path.getsize(self.DATA) + os.path.getsize( self.DATA_SUB) self.assertIs(type(file_time), str) self.assertIs(type(file_size), str) self.assertEqual(file_size, str(actual_file_size)) self.assertIs(type(dir_time), str) self.assertIs(type(dir_size), str) self.assertEqual(dir_size, str(actual_dir_size))
def test(self): tree = CleanTree(WorkingTree(self.root_dir)) file_time, file_size = get_mtime_and_size(self.DATA, tree) dir_time, dir_size = get_mtime_and_size(self.DATA_DIR, tree) actual_file_size = os.path.getsize(self.DATA) actual_dir_size = os.path.getsize(self.DATA) + os.path.getsize( self.DATA_SUB) self.assertIs(type(file_time), str) self.assertIs(type(file_size), str) self.assertEqual(file_size, str(actual_file_size)) self.assertIs(type(dir_time), str) self.assertIs(type(dir_size), str) self.assertEqual(dir_size, str(actual_dir_size))
def test_path_object_and_str_are_valid_types_get_mtime_and_size(tmp_dir): tmp_dir.gen({ "dir": { "dir_file": "dir file content" }, "file": "file_content" }) tree = CleanTree(LocalRemoteTree(None, {"url": os.fspath(tmp_dir)})) time, size = get_mtime_and_size("dir", tree) object_time, object_size = get_mtime_and_size(PathInfo("dir"), tree) assert time == object_time assert size == object_size time, size = get_mtime_and_size("file", tree) object_time, object_size = get_mtime_and_size(PathInfo("file"), tree) assert time == object_time assert size == object_size
def _ls(repo, path_info, recursive=None, dvc=False): from dvc.ignore import CleanTree from dvc.repo.tree import DvcTree from dvc.scm.tree import WorkingTree if dvc: tree = DvcTree(repo) else: tree = CleanTree(WorkingTree(repo.root_dir)) ret = {} try: for root, dirs, files in tree.walk(path_info.fspath): for fname in files: info = PathInfo(root) / fname path = str(info.relative_to(path_info)) ret[path] = { "isout": dvc, "isdir": False, "isexec": False if dvc else tree.isexec(info.fspath), } if not recursive: for dname in dirs: info = PathInfo(root) / dname path = str(info.relative_to(path_info)) ret[path] = { "isout": tree.isdvc(info.fspath) if dvc else False, "isdir": True, "isexec": False if dvc else tree.isexec(info.fspath), } break except NotADirectoryError: return { path_info.name: { "isout": dvc, "isdir": False, "isexec": False if dvc else tree.isexec(path_info.fspath), } } except FileNotFoundError: return {} return ret
def _ls_files_repo(path_info, recursive=None): from dvc.compat import fspath from dvc.ignore import CleanTree from dvc.path_info import PathInfo from dvc.scm.tree import WorkingTree if not os.path.exists(fspath(path_info)): return [] files = [] tree = CleanTree(WorkingTree(path_info)) try: for dirpath, dirnames, filenames in tree.walk(path_info): files.extend(PathInfo(dirpath, f) for f in filenames) if not recursive: files.extend(PathInfo(dirpath, d) for d in dirnames) break except NotADirectoryError: if os.path.isfile(fspath(path_info)): files = [path_info] return [_get_fs_node(f) for f in files]
def _ls_files_repo(target_path_info, recursive=None): from dvc.compat import fspath from dvc.ignore import CleanTree from dvc.path_info import PathInfo from dvc.scm.tree import WorkingTree if not os.path.exists(fspath(target_path_info)): return [] files = [] tree = CleanTree(WorkingTree(target_path_info)) try: for dirpath, dirnames, filenames in tree.walk(target_path_info): files.extend(map(lambda f: PathInfo(dirpath, f), filenames)) if not recursive: files.extend(map(lambda d: PathInfo(dirpath, d), dirnames)) break except NotADirectoryError: if os.path.isfile(fspath(target_path_info)): return [target_path_info] return files
def tree(self, tree): self._tree = tree if isinstance(tree, CleanTree) else CleanTree(tree) # Our graph cache is no longer valid, as it was based on the previous # tree. self._reset()