def test_atomic_writer_success(tmp_path): dst = tmp_path / "dst" with atomic_writer(dst) as tmp: tmp.write_text("dst") assert dst.read_text() == "dst" assert not tmp.exists()
def test_atomic_writer_failure(tmp_path): dst = tmp_path / "dst" with pytest.raises(Exception): with atomic_writer(dst) as tmp: tmp.write_text("dst") raise Exception("test") assert not dst.exists() assert not tmp.exists()
def copy_file(source, dest, follow_symlinks=True): """Efficient atomic copy. shutil.copy uses sendfile on linux, so should be fast. """ # ensure path dest = Path(dest) dest.parent.mkdir(parents=True, exist_ok=True) with atomic_writer(dest) as tmp: shutil.copy(source, tmp, follow_symlinks=follow_symlinks)
def test_atomic_writer_overwrite_symlink(tmp_path): target = tmp_path / "target" target.write_text("target") dst = tmp_path / "link" dst.symlink_to(target) with atomic_writer(dst) as tmp: tmp.write_text("dst") assert dst.read_text() == "dst" assert not dst.is_symlink() assert target.read_text() == "target" assert not tmp.exists()
def copy_from_volume(volume_name, source, dest, timeout=None): """ Copy the contents of `source` from the root of the named volume to `dest` on local disk As this command can potentially take a long time with large files it does not, by default, have any timeout. """ with atomic_writer(dest) as tmp: docker( [ "cp", "--follow-link", f"{manager_name(volume_name)}:{VOLUME_MOUNT_POINT}/{source}", tmp, ], check=True, capture_output=True, timeout=timeout, )