Skip to content

Commit

Permalink
Added disk free space verification before download. (#66)
Browse files Browse the repository at this point in the history
* Added disk free space verification before download.

* Removed unused import.

* - made disk full message more readable.
- added handler to directory not found exception.

* removed relative import from __init__.py

* changed from relative import to absolute import at test_disk_usage test..

* Fixed a couple of flake8 warnings.

* Refactored unnecessary overengineering.

* Fixed linting at _verify_disk_space.

* Fixed pre-commit errors.
  • Loading branch information
gbmap authored Jul 7, 2022
1 parent 26bb67e commit 1ba3b1b
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions blender_downloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,12 @@ def download_release(download_url, output_directory, quiet=False):
chunksize = 8192
downloaded_size = chunksize
res = urlopen(Request(download_url))
total_size_bits = int(res.info()["Content-Length"])
total_size_bytes = int(res.info()["Content-Length"])

_verify_disk_space(output_directory, total_size_bytes)

progress_bar_kwargs = dict(
total=total_size_bits,
total=total_size_bytes,
unit="B",
desc=f"Downloading '{output_filename}'",
unit_scale=True,
Expand All @@ -768,7 +770,7 @@ def download_release(download_url, output_directory, quiet=False):
f.write(data)
progress_bar.update(chunksize)
downloaded_size += chunksize
if downloaded_size >= total_size_bits:
if downloaded_size >= total_size_bytes:
break
except KeyboardInterrupt:
sys.stderr.write("Download interrupted\n")
Expand All @@ -786,6 +788,17 @@ def download_release(download_url, output_directory, quiet=False):
return output_filepath


def _verify_disk_space(output_dir, total_size):
free_space = shutil.disk_usage(output_dir).free
if free_space < total_size:
sys.stderr.write(
f"Not enough free space at {output_dir}."
f" Free space: {free_space} bytes."
f" Needed: {total_size} bytes."
)
sys.exit(1)


def extract_release(zipped_filepath, quiet=False):
"""Extracts, if needed, a Blender release file depending on their file type.
Expand Down

0 comments on commit 1ba3b1b

Please # to comment.