From 0919dec56a114d995e3d7e8c24c0dd4ef46c5a5a Mon Sep 17 00:00:00 2001 From: Matthew Gidden Date: Tue, 2 May 2017 06:47:17 -0500 Subject: [PATCH] dl tutorial files to tmp directory, then move them once successful closes #1392 --- xarray/tutorial.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/xarray/tutorial.py b/xarray/tutorial.py index 858f08d77e9..b4e606c0ee3 100644 --- a/xarray/tutorial.py +++ b/xarray/tutorial.py @@ -10,6 +10,7 @@ from __future__ import print_function import os as _os +import shutil as _shutil from .backends.api import open_dataset as _open_dataset from .core.pycompat import urlretrieve as _urlretrieve @@ -42,8 +43,10 @@ def load_dataset(name, cache=True, cache_dir=_default_cache_dir, """ longdir = _os.path.expanduser(cache_dir) + tmpdir = _os.sep.join((longdir, '.tmp')) fullname = name + '.nc' localfile = _os.sep.join((longdir, fullname)) + tmpfile = _os.sep.join((longdir, fullname)) if not _os.path.exists(localfile): @@ -51,9 +54,16 @@ def load_dataset(name, cache=True, cache_dir=_default_cache_dir, # May want to add an option to remove it. if not _os.path.isdir(longdir): _os.mkdir(longdir) + if not _os.path.isdir(tmpdir): + _os.mkdir(tmpdir) url = '/'.join((github_url, 'raw', 'master', fullname)) - _urlretrieve(url, localfile) + _urlretrieve(url, tmpfile) + + if not _os.path.exists(tmpfile): + raise ValueError('File could not be downloaded, please try again') + + _shutil.move(tmpfile, localfile) ds = _open_dataset(localfile, **kws).load()