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
ubuntu/focal
Walter Lapchynski 6 years ago committed by Simon Quigley
parent 9c6c1de9fe
commit 4bf7f72786

@ -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"):

3
debian/changelog vendored

@ -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 <wxl@ubuntu.com> Tue, 09 Oct 2018 21:51:55 -0700
-- Walter Lapchynski <wxl@ubuntu.com> Wed, 10 Oct 2018 08:36:52 -0700
calamares-settings-ubuntu (23) cosmic; urgency=medium

Loading…
Cancel
Save