From 4bf7f727865cd2da11c965de89bf47ad54fcc89d Mon Sep 17 00:00:00 2001 From: Walter Lapchynski Date: Wed, 10 Oct 2018 20:55:38 -0500 Subject: [PATCH] Updated automirror for urllib.request Summary: Updated automirror for urllib.request Test Plan: Verify basic function in US, but then also have non-US users test for functionality, especially paying attention to time. More specifically, this patch should be able to be applied directly to /usr/lib/(i138-linux-gnu|x86_64-linux-gnu)/calamares/modules/automirror/main.py in a live system, then run the installer and see what happens. Reviewers: tsimonq2, wxl Reviewed By: tsimonq2 Differential Revision: https://phab.lubuntu.me/D23 --- common/modules/automirror/main.py | 44 ++++++++++++++++++++++++++----- debian/changelog | 3 ++- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/common/modules/automirror/main.py b/common/modules/automirror/main.py index ff28e59..927a2b9 100644 --- a/common/modules/automirror/main.py +++ b/common/modules/automirror/main.py @@ -19,7 +19,10 @@ import json import subprocess import libcalamares from time import strftime -from urllib import request +import urllib.request +from urllib.error import HTTPError +from urllib.error import URLError +from socket import timeout from lsb_release import get_distro_information global sources @@ -80,23 +83,51 @@ deb URL/ubuntu/ CODENAME-backports main restricted universe multiverse # deb http://archive.canonical.com/ubuntu CODENAME partner # deb-src http://archive.canonical.com/ubuntu CODENAME partner""" + def getcountry(): - # This is hardcoded for now, but should eventually be put into the config - with request.urlopen("https://ipapi.co/json") as url: - localedata = json.loads(url.read().decode()) + # This URI hardcoded for now, but should eventually be put into the config + geoipurl = "https://ipapi.co/json" + try: + with urllib.request.urlopen(geoipurl, timeout=75) as url: + localedata = json.loads(url.read().decode()) + except HTTPError: + logging.error("Data of %s not retrieved because %s - URL: %s", + name, error, url) + except URLError: + if isinstance(error.reason, socket.timeout): + logging.error("Socket timed out - URL %s", url) + else: + logging.error("Non-timeout protocol error.") + else: + print("Country successfully determined.") return localedata["country"] + def getmirror(country): - with request.urlopen(libcalamares.job.configuration["mirrorList"]) as url: - mirrors = json.loads(url.read().decode()) + mirrorlisturl = libcalamares.job.configuration["mirrorList"] + try: + with urllib.request.urlopen(mirrorlisturl, timeout=75) as url: + mirrors = json.loads(url.read().decode()) + except HTTPError: + logging.error("Data of %s not retrieved because %s - URL: %s", + name, error, url) + except URLError: + if isinstance(error.reason, socket.timeout): + logging.error("Socket timed out - URL %s", url) + else: + logging.error("Non-timeout protocol error.") + else: + print("Mirror successfully determined.") if country in mirrors.keys(): return mirrors[country] + "." else: return "" + def getcodename(): return get_distro_information()["CODENAME"] + def changesources(prefix): root = libcalamares.globalstorage.value("rootMountPoint") @@ -118,6 +149,7 @@ def changesources(prefix): sourcesfile.write(sources) sourcesfile.truncate() + def run(): """Autoselect a mirror from a list.""" if libcalamares.globalstorage.value("hasInternet"): diff --git a/debian/changelog b/debian/changelog index 0859d1a..7b88521 100644 --- a/debian/changelog +++ b/debian/changelog @@ -8,8 +8,9 @@ calamares-settings-ubuntu (24) cosmic; urgency=medium [ Walter Lapchynski ] * Generalize installation of GRUB for EFI. + * Add urlopen() error handling to automirror, thanks to Samuel Banya. - -- Walter Lapchynski Tue, 09 Oct 2018 21:51:55 -0700 + -- Walter Lapchynski Wed, 10 Oct 2018 08:36:52 -0700 calamares-settings-ubuntu (23) cosmic; urgency=medium