Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free disk size (todo) #172

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions tensorflow_datasets/core/dataset_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,22 @@ def _log_download_bytes(self):
# Print is intentional: we want this to always go to stdout so user has
# information needed to cancel download/preparation if needed.
# This comes right before the progress bar.

dataset_size = self.info.size_in_bytes
def _check_disk_size():
if dataset_size > utils.free_disk_size(self._data_dir_root):
raise IOError("Not enough disk space!!\nDataset size : {dataset_size} \nFree size : {free_disk_size} \nYou need to extra {needed_disk_size} to download."
.format(dataset_size=units.size_str(dataset_size),
free_disk_size=units.size_str(utils.free_disk_size(self._data_dir_root)),
needed_disk_size=units.size_str(dataset_size - utils.free_disk_size(self._data_dir_root))))

_check_disk_size()

size_text = units.size_str(self.info.size_in_bytes)
termcolor.cprint(
"Downloading / extracting dataset %s (%s) to %s..." %
(self.name, size_text, self._data_dir),
attrs=["bold"])
# TODO(tfds): Should try to estimate the available free disk space (if
# possible) and raise an error if not.

@abc.abstractmethod
def _info(self):
Expand Down
15 changes: 15 additions & 0 deletions tensorflow_datasets/core/utils/py_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import os
import sys
import uuid
import ctypes
import platform

import six
import tensorflow as tf
Expand Down Expand Up @@ -275,3 +277,16 @@ def rgetattr(obj, attr, *args):
def _getattr(obj, attr):
return getattr(obj, attr, *args)
return functools.reduce(_getattr, [obj] + attr.split("."))


def free_disk_size(dirname):
try:
if platform.system() == 'Windows':
free_bytes = ctypes.c_ulonglong(0)
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dirname), None, None, ctypes.pointer(free_bytes))
return free_bytes.value
else:
stat = os.statvfs(dirname)
return stat.f_bavail * stat.f_frsize
except:
return float("inf")