import tarfile
from pathlib import Path
from labml import monit, logger
from labml.logger import Text
_TAR_BUFFER_SIZE = 100_000
[docs]
def download_file(url: str, path: Path):
"""
Download a file from ``url``.
Arguments:
url (str): URL to download the file from
path (Path): The location to save the downloaded file
"""
if path.exists():
return
import urllib.request
if not path.parent.exists():
path.parent.mkdir(parents=True)
with monit.section("Download"):
def reporthook(count, block_size, total_size):
monit.progress(count * block_size / total_size)
urllib.request.urlretrieve(url, path, reporthook=reporthook)
def _extract_tar_file(tar: tarfile.TarFile, f: tarfile.TarInfo, path: Path):
if not path.parent.exists():
path.parent.mkdir(parents=True)
with tar.extractfile(f) as ef:
with open(str(path), 'wb') as o:
while True:
r = ef.read(_TAR_BUFFER_SIZE)
if r:
o.write(r)
else:
break