|
|
|
@ -24,6 +24,7 @@ from urllib.error import URLError
|
|
|
|
|
import socket
|
|
|
|
|
import logging
|
|
|
|
|
import distro
|
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
|
|
global sources
|
|
|
|
|
sources = """# Automatically generated by Calamares on DATE.
|
|
|
|
@ -99,10 +100,20 @@ def getcountrycode():
|
|
|
|
|
an empty string.
|
|
|
|
|
"""
|
|
|
|
|
if libcalamares.globalstorage.value("hasInternet"):
|
|
|
|
|
geoip_style = libcalamares.job.configuration["geoip"]["style"]
|
|
|
|
|
geoipurl = libcalamares.job.configuration["geoip"]["url"]
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with urllib.request.urlopen(geoipurl, timeout=75) as http_response:
|
|
|
|
|
if geoip_style == "json":
|
|
|
|
|
localedata = json.loads(http_response.read().decode())
|
|
|
|
|
return localedata["country_code"]
|
|
|
|
|
elif geoip_style == "xml":
|
|
|
|
|
tree = ET.parse(http_response)
|
|
|
|
|
root = tree.getroot()
|
|
|
|
|
return root.find("CountryCode").text
|
|
|
|
|
else:
|
|
|
|
|
logging.error("Unknown geoip style: %s", geoip_style)
|
|
|
|
|
except HTTPError as http_error:
|
|
|
|
|
logging.error("Data not retrieved because %s - URL: %s",
|
|
|
|
|
http_error, geoipurl)
|
|
|
|
@ -113,8 +124,6 @@ def getcountrycode():
|
|
|
|
|
logging.error("Non-timeout protocol error.")
|
|
|
|
|
else:
|
|
|
|
|
logging.info("Country successfully determined.")
|
|
|
|
|
return localedata["country"]
|
|
|
|
|
else:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|