def download_package_from_url(url, dest):
logging.info('Attempting to download missing package from: ' + url)
tmp_path = internal_path("work/dl_package.zip")
try:
req = requests.get(url, stream=True, timeout=10)
if req.status_code == 200:
with open(tmp_path, 'wb') as tmp_file:
req.raw.decode_content = True
shutil.copyfileobj(req.raw, tmp_file)
else:
raise PackageLoadError('Package download failed, server said: {} {}'.format(req.status_code, req.reason))
except (RuntimeError, IOError) as e:
raise PackageLoadError('Package download failed due to an error') from e
logging.info('Extracting package...')
try:
with ZipFile(tmp_path, "r") as z:
z.extractall(dest)
except BadZipfile as e:
raise PackageLoadError('Malformed package zip file') from e
os.remove(tmp_path)
评论列表
文章目录