def download_to(url, download_dir, insecure=False):
name = url.split('/')[-1]
file = os.path.join(download_dir, name)
click.echo("Downloading {0}".format(url))
bar_len = 1000
with click.progressbar(length=bar_len, width=70) as bar:
def hook(count, block_size, total_size):
percent = int(count*block_size*bar_len/total_size)
if percent > 0 and percent < bar_len:
# Hack because we can't set the position
bar.pos = percent
bar.update(0)
context = None
if insecure: context = ssl._create_unverified_context()
CGetURLOpener(context=context).retrieve(url, filename=file, reporthook=hook, data=None)
bar.update(bar_len)
if not os.path.exists(file):
raise BuildError("Download failed for: {0}".format(url))
return file
评论列表
文章目录