Skip to content

Commit

Permalink
check disk space before downloading (#261)
Browse files Browse the repository at this point in the history
add decorator check_disk_space
  • Loading branch information
CunliangGeng authored Jul 6, 2024
1 parent fbca57b commit 0ad8b21
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/nplinker/utils.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
# Copyright 2021 The NPLinker Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations
import bz2
import csv
import functools
import gzip
import hashlib
import logging
import lzma
import os
import os.path
import shutil
import sys
import tarfile
import warnings
import zipfile
from collections.abc import Callable
from collections.abc import Sequence
Expand All @@ -41,6 +30,34 @@

logger = logging.getLogger(__name__)

#
# Decorators
#


def check_disk_space(func):
"""A decorator to check available disk space.
If the available disk space is less than 500GB, a warning is logged and a warning is raised.
"""

@functools.wraps(func)
def wrapper_check_disk_space(*args, **kwargs):
_, _, free = shutil.disk_usage("/")
free_gb = free // (2**30)
if free_gb < 50:
warning_message = f"Available disk space is {free_gb}GB. Is it enough for your project?"
logger.warning(warning_message)
warnings.warn(warning_message, UserWarning)
return func(*args, **kwargs)

return wrapper_check_disk_space


#
# Other utility functions
#


def find_delimiter(file: str | PathLike) -> str:
"""Detect the delimiter for the given tabular file.
Expand Down Expand Up @@ -142,6 +159,7 @@ def check_md5(fpath: str | PathLike, md5: str) -> bool:
return md5 == calculate_md5(fpath)


@check_disk_space
def download_url(
url: str,
root: str | PathLike,
Expand Down

0 comments on commit 0ad8b21

Please sign in to comment.