Source code for jittor_geometric.data.download
from __future__ import print_function
import ssl
import os
import os.path as osp
import urllib.request as ur
import zipfile
from six.moves import urllib
import gzip
from .makedirs import makedirs
import tarfile
GBFACTOR = float(1 << 30)
[docs]
def download_url(url, folder, log=True):
r"""Downloads the content of an URL to a specific folder.
Args:
url (string): The url.
folder (string): The folder.
log (bool, optional): If :obj:`False`, will not print anything to the
console. (default: :obj:`True`)
"""
filename = url.rpartition('/')[2].split('?')[0]
path = osp.join(folder, filename)
if osp.exists(path): # pragma: no cover
if log:
print('Using exist file', filename)
return path
if log:
print('Downloading', url)
makedirs(folder)
context = ssl._create_unverified_context()
data = urllib.request.urlopen(url, context=context)
with open(path, 'wb') as f:
f.write(data.read())
return path
[docs]
def decide_download(url):
d = ur.urlopen(url)
size = int(d.info()["Content-Length"])/GBFACTOR
### confirm if larger than 1GB
if size > 1:
return input("This will download %.2fGB. Will you proceed? (y/N)\n" % (size)).lower() == "y"
else:
return True
def maybe_log(path, log=True):
if log:
print('Extracting', path)
[docs]
def extract_zip(path, folder, log=True):
r"""Extracts a zip archive to a specific folder.
Args:
path (string): The path to the tar archive.
folder (string): The folder.
log (bool, optional): If :obj:`False`, will not print anything to the
console. (default: :obj:`True`)
"""
maybe_log(path, log)
with zipfile.ZipFile(path, 'r') as f:
f.extractall(folder)
[docs]
def extract_gz(path: str, folder: str, log: bool = True) -> None:
r"""Extracts a gz archive to a specific folder.
Args:
path (str): The path to the tar archive.
folder (str): The folder.
log (bool, optional): If :obj:`False`, will not print anything to the
console. (default: :obj:`True`)
"""
maybe_log(path, log)
path = osp.abspath(path)
with gzip.open(path, 'r') as r:
with open(osp.join(folder, '.'.join(path.split('.')[:-1])), 'wb') as w:
w.write(r.read())
[docs]
def extract_tar(
path: str,
folder: str,
mode: str = 'r:gz',
log: bool = True,
) -> None:
r"""Extracts a tar archive to a specific folder.
Args:
path (str): The path to the tar archive.
folder (str): The folder.
mode (str, optional): The compression mode. (default: :obj:`"r:gz"`)
log (bool, optional): If :obj:`False`, will not print anything to the
console. (default: :obj:`True`)
"""
maybe_log(path, log)
with tarfile.open(path, mode) as f:
f.extractall(folder, filter='data')