🔧 Replaced deprecated FancyURLopener

This commit is contained in:
Edgar 2022-10-24 15:52:56 +02:00
parent b5218d50f6
commit 9d2c6f31db
No known key found for this signature in database
GPG Key ID: 17D930BB616061A5

View File

@ -281,19 +281,21 @@ def fetch_url(src, dst):
Fetch file from URL src and save it to dst.
"""
import urllib.request
class URLopener(urllib.request.FancyURLopener):
def http_error_default(self, url, fp, errcode, errmsg, headers):
sys.stderr.write("ERROR: could not fetch {0}\n".format(url))
sys.exit(-1)
import shutil
dirname = os.path.dirname(dst)
if dirname != '':
if not os.path.isdir(dirname):
os.makedirs(dirname)
opener = URLopener()
opener.retrieve(src, dst)
try:
with urllib.request.urlopen(src) as response, open(dst, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
except urllib.error.HTTPError as e:
sys.stderr.write(f"\n\nERROR: could not fetch {src}, request failed with error {e.code} \n\t {e.reason}\n")
sys.exit(-1)
except urllib.error.URLError as e:
sys.stderr.write(f"\n\nERROR: could not fetch {src}, {e.reason}\n")
sys.exit(-1)
if __name__ == '__main__':
check_for_yaml()