amprolla is an apt repository merger originally intended for use with the Devuan infrastructure. This version is the third iteration of the software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.0 KiB

6 years ago
# See LICENSE file for copyright and license details.
6 years ago
"""
Network functions/helpers
"""
from os import makedirs
from os.path import dirname
import requests
6 years ago
6 years ago
from lib.log import info, warn
6 years ago
def download(uris):
"""
Downloads a file by providing it an url and a write path in a tuple
"""
url = uris[0]
path = uris[1]
info("dl: %s" % url)
try:
6 years ago
rfile = requests.get(url, stream=True, timeout=20)
except (requests.exceptions.ConnectionError,
requests.exceptions.ReadTimeout,
ConnectionResetError) as err:
6 years ago
warn('Caught exception: "%s". Retrying...' % err)
return download(uris)
6 years ago
if rfile.status_code != 200:
warn('%s failed: %d' % (url, rfile.status_code))
6 years ago
return
makedirs(dirname(path), exist_ok=True)
6 years ago
lfile = open(path, 'wb')
# chunk_size {sh,c}ould be more on gbit servers
6 years ago
for chunk in rfile.iter_content(chunk_size=1024):
if chunk:
6 years ago
lfile.write(chunk)
# f.flush()
6 years ago
lfile.close()