@ -19,7 +19,10 @@ import json
import subprocess
import subprocess
import libcalamares
import libcalamares
from time import strftime
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
from lsb_release import get_distro_information
global sources
global sources
@ -80,23 +83,51 @@ deb URL/ubuntu/ CODENAME-backports main restricted universe multiverse
# deb http://archive.canonical.com/ubuntu CODENAME partner
# deb http://archive.canonical.com/ubuntu CODENAME partner
# deb-src http://archive.canonical.com/ubuntu CODENAME partner"""
# deb-src http://archive.canonical.com/ubuntu CODENAME partner"""
def getcountry ( ) :
def getcountry ( ) :
# This is hardcoded for now, but should eventually be put into the config
# This URI hardcoded for now, but should eventually be put into the config
with request . urlopen ( " https://ipapi.co/json " ) as url :
geoipurl = " https://ipapi.co/json "
try :
with urllib . request . urlopen ( geoipurl , timeout = 75 ) as url :
localedata = json . loads ( url . read ( ) . decode ( ) )
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 " ]
return localedata [ " country " ]
def getmirror ( country ) :
def getmirror ( country ) :
with request . urlopen ( libcalamares . job . configuration [ " mirrorList " ] ) as url :
mirrorlisturl = libcalamares . job . configuration [ " mirrorList " ]
try :
with urllib . request . urlopen ( mirrorlisturl , timeout = 75 ) as url :
mirrors = json . loads ( url . read ( ) . decode ( ) )
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 ( ) :
if country in mirrors . keys ( ) :
return mirrors [ country ] + " . "
return mirrors [ country ] + " . "
else :
else :
return " "
return " "
def getcodename ( ) :
def getcodename ( ) :
return get_distro_information ( ) [ " CODENAME " ]
return get_distro_information ( ) [ " CODENAME " ]
def changesources ( prefix ) :
def changesources ( prefix ) :
root = libcalamares . globalstorage . value ( " rootMountPoint " )
root = libcalamares . globalstorage . value ( " rootMountPoint " )
@ -118,6 +149,7 @@ def changesources(prefix):
sourcesfile . write ( sources )
sourcesfile . write ( sources )
sourcesfile . truncate ( )
sourcesfile . truncate ( )
def run ( ) :
def run ( ) :
""" Autoselect a mirror from a list. """
""" Autoselect a mirror from a list. """
if libcalamares . globalstorage . value ( " hasInternet " ) :
if libcalamares . globalstorage . value ( " hasInternet " ) :