def test_copy_fobj_to_file(tmp_dir): tmp_dir.gen({"foo": "foo content"}) src = tmp_dir / "foo" dest = "path" with open(src, "rb") as fobj: copy_fobj_to_file(fobj, dest) assert filecmp.cmp(src, dest)
def _upload_fobj(self, fobj, to_info): self.makedirs(to_info.parent) tmp_info = to_info.parent / tmp_fname("") try: copy_fobj_to_file(fobj, tmp_info) os.rename(tmp_info, to_info) except Exception: self.remove(tmp_info) raise
def upload_fobj(self, fobj, to_info, **kwargs): self.makedirs(self.path.parent(to_info)) tmp_info = self.path.join(self.path.parent(to_info), tmp_fname("")) try: copy_fobj_to_file(fobj, tmp_info) os.rename(tmp_info, to_info) except Exception: self.remove(tmp_info) raise
def copy_fobj(self, fobj, to_info, chunk_size=None): self.makedirs(to_info.parent) tmp_info = to_info.parent / tmp_fname("") try: copy_fobj_to_file(fobj, tmp_info, chunk_size=chunk_size) os.rename(tmp_info, to_info) except Exception: self.remove(tmp_info) raise
def copy_fobj(self, fobj, to_info): self.makedirs(to_info.parent) tmp_info = to_info.parent / tmp_fname(to_info.name) try: copy_fobj_to_file(fobj, tmp_info) os.chmod(tmp_info, self.file_mode) os.rename(tmp_info, to_info) except Exception: self.remove(tmp_info) raise
def __init__(self, src_tree: BaseTree, baseline_rev: str, **kwargs): dvc_dir = kwargs.pop("dvc_dir") cache_dir = kwargs.pop("cache_dir") super().__init__(src_tree, baseline_rev, **kwargs) self.tmp_dir = TemporaryDirectory() logger.debug("Init local executor in dir '%s'.", self.tmp_dir) self.dvc_dir = os.path.join(self.tmp_dir.name, dvc_dir) try: for fname in src_tree.walk_files(src_tree.tree_root): dest = self.path_info / relpath(fname, src_tree.tree_root) if not os.path.exists(dest.parent): makedirs(dest.parent) with src_tree.open(fname, "rb") as fobj: copy_fobj_to_file(fobj, dest) except Exception: self.tmp_dir.cleanup() raise self._config(cache_dir)
def copytree(self, top, dest): top = PathInfo(top) dest = PathInfo(dest) if not self.exists(top): raise FileNotFoundError if self.isfile(top): makedirs(dest.parent, exist_ok=True) with self.open(top, mode="rb") as fobj: copy_fobj_to_file(fobj, dest) return for root, _, files in self.walk(top): root = PathInfo(root) makedirs(dest, exist_ok=True) for fname in files: src = root / fname with self.open(src, mode="rb") as fobj: copy_fobj_to_file(fobj, dest / fname)
def copytree(self, top, dest): from dvc.utils.fs import copy_fobj_to_file, makedirs top = PathInfo(top) dest = PathInfo(dest) if not self.exists(top): raise FileNotFoundError if self.isfile(top): makedirs(dest.parent, exist_ok=True) with self.open(top, mode="rb") as fobj: copy_fobj_to_file(fobj, dest) return for root, _, files in self.walk(top): root = PathInfo(root) dest_dir = dest / root.relative_to(top) makedirs(dest_dir, exist_ok=True) for fname in files: src = root / fname with self.open(src, mode="rb") as fobj: copy_fobj_to_file(fobj, dest_dir / fname)