@ -16,7 +16,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import json
import subprocess
import libcalamares
import libcalamares
from time import strftime
from time import strftime
import urllib . request
import urllib . request
@ -83,10 +82,30 @@ 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"""
SUBDOMAINS_BY_COUNTRY_CODE = { " US " : " us. " ,
def getcountry ( ) :
" AU " : " au. " ,
# This URI hardcoded for now, but should eventually be put into the config
" SE " : " no. " ,
geoipurl = " https://ipapi.co/json "
" NO " : " no. " ,
" NZ " : " nz. " ,
" NL " : " nl. " ,
" KR " : " kr. " ,
" DE " : " de. " ,
" GE " : " ge. " ,
" PF " : " pf. " ,
" CZ " : " cz. " ,
" HR " : " hr. " }
def getcountrycode ( ) :
"""
Return the two - letter country code or an empty string .
Tries to determine the country code based on the public IP address ,
if the device is connected to the Internet . Otherwise it returns
an empty string .
"""
if libcalamares . globalstorage . value ( " hasInternet " ) :
geoipurl = libcalamares . job . configuration [ " geoIpUrl " ]
try :
try :
with urllib . request . urlopen ( geoipurl , timeout = 75 ) as url :
with urllib . request . urlopen ( geoipurl , timeout = 75 ) as url :
localedata = json . loads ( url . read ( ) . decode ( ) )
localedata = json . loads ( url . read ( ) . decode ( ) )
@ -99,27 +118,15 @@ def getcountry():
else :
else :
logging . error ( " Non-timeout protocol error. " )
logging . error ( " Non-timeout protocol error. " )
else :
else :
print ( " Country successfully determined. " )
logging . info ( " Country successfully determined. " )
return localedata [ " country " ]
return localedata [ " country " ]
else :
return " "
def getmirror ( country ) :
def get_subdomain_by_country ( countrycode ) :
mirrorlisturl = libcalamares . job . configuration [ " mirrorList " ]
if countrycode in SUBDOMAINS_BY_COUNTRY_CODE . keys ( ) :
try :
return SUBDOMAINS_BY_COUNTRY_CODE [ countrycode ]
with urllib . request . urlopen ( mirrorlisturl , timeout = 75 ) as url :
mirrors = json . loads ( url . read ( ) . decode ( ) )
except HTTPError as error :
logging . error ( " Data of %s not retrieved because %s - URL: %s " ,
name , error , url )
except URLError as error :
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 :
else :
return " "
return " "
@ -128,34 +135,28 @@ def getcodename():
return get_distro_information ( ) [ " CODENAME " ]
return get_distro_information ( ) [ " CODENAME " ]
def changesources ( prefix ) :
def changesources ( subdomain ) :
root = libcalamares . globalstorage . value ( " rootMountPoint " )
url = prefix + libcalamares . job . configuration [ " baseUrl " ]
if not url . startswith ( " http:// " ) and not url . startswith ( " https:// " ) :
url = " http:// " + url
if libcalamares . job . configuration [ " backend " ] == " apt " :
distro = libcalamares . job . configuration [ " distribution " ]
distro = libcalamares . job . configuration [ " distribution " ]
if " ubuntu " in distro . lower ( ) :
url = " http:// {} {} " . format ( subdomain ,
libcalamares . job . configuration [ " baseUrl " ] )
global sources
global sources
sources = sources . replace ( " DISTRIBUTION " , distro )
sources = sources . replace ( " DISTRIBUTION " , distro )
sources = sources . replace ( " CODENAME " , getcodename ( ) )
sources = sources . replace ( " CODENAME " , getcodename ( ) )
sources = sources . replace ( " URL " , url )
sources = sources . replace ( " URL " , url )
sources = sources . replace ( " DATE " , strftime ( " % Y- % m- %d " ) )
sources = sources . replace ( " DATE " , strftime ( " % Y- % m- %d " ) )
with open ( root + " /etc/apt/sources.list " , " r+ " ) as sourcesfile :
filepath = libcalamares . globalstorage . value ( " rootMountPoint " )
filepath + = " /etc/apt/sources.list "
with open ( filepath , " r+ " ) as sourcesfile :
sourcesfile . seek ( 0 )
sourcesfile . seek ( 0 )
sourcesfile . write ( sources )
sourcesfile . write ( sources )
sourcesfile . truncate ( )
sourcesfile . truncate ( )
def run ( ) :
def run ( ) :
""" Autoselect a mirror from a list. """
""" Autoselect a mirror and create the sources.list file. """
if libcalamares . globalstorage . value ( " hasInternet " ) :
countrycode = getcountrycode ( )
country = getcountry ( )
subdomain = get_subdomain_by_country ( countrycode )
prefix = getmirror ( country )
else :
prefix = " "
changesources ( prefix )
changesources ( subdomain )