Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
svartkanin committed Oct 18, 2024
1 parent e51f9a8 commit ca828f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
34 changes: 20 additions & 14 deletions archinstall/lib/mirrors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import json
import pathlib
from dataclasses import dataclass, field
Expand All @@ -6,7 +7,7 @@

from .menu import AbstractSubMenu, Selector, MenuSelectionType, Menu, ListManager, TextInput
from .networking import fetch_data_from_url
from .output import warn, FormattedOutput
from .output import FormattedOutput, debug
from .storage import storage
from .models.mirrors import MirrorStatusListV3, MirrorStatusEntryV3

Expand Down Expand Up @@ -324,17 +325,22 @@ def _parse_mirror_list(mirrorlist: str) -> Dict[str, List[MirrorStatusEntryV3]]:


def list_mirrors() -> Dict[str, List[MirrorStatusEntryV3]]:
regions: Dict[str, List[MirrorStatusEntryV3]] = {}

if storage['arguments']['offline']:
with pathlib.Path('/etc/pacman.d/mirrorlist').open('r') as fp:
mirrorlist = fp.read()
else:
if not storage['arguments']['offline']:
url = "https://archlinux.org/mirrors/status/json/"
try:
mirrorlist = fetch_data_from_url(url)
except ValueError as err:
warn(f'Could not fetch an active mirror-list: {err}')
return regions

return _parse_mirror_list(mirrorlist)
attempts = 3

for attempt_nr in range(attempts):
try:
mirrorlist = fetch_data_from_url(url)
return _parse_mirror_list(mirrorlist)
except Exception as e:
debug(f'Error while fetching mirror list: {e}')
time.sleep(attempt_nr + 1)

debug('Unable to fetch mirror list remotely, falling back to local mirror list')

# we'll use the local mirror list if the offline flag is set
# or if fetching the mirror list remotely failed
with pathlib.Path('/etc/pacman.d/mirrorlist').open('r') as fp:
mirrorlist = fp.read()
return _parse_mirror_list(mirrorlist)
29 changes: 10 additions & 19 deletions archinstall/lib/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from urllib.request import urlopen

from .exceptions import SysCallError, DownloadTimeout
from .output import error, info, debug
from .output import error, info
from .pacman import Pacman


Expand Down Expand Up @@ -118,7 +118,7 @@ def enrich_iface_types(interfaces: Union[dict[str, Any], list[str]]) -> dict[str
return result


def fetch_data_from_url(url: str, params: Optional[dict] = None, retry: int = 3) -> str:
def fetch_data_from_url(url: str, params: Optional[dict] = None) -> str:
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
Expand All @@ -129,23 +129,14 @@ def fetch_data_from_url(url: str, params: Optional[dict] = None, retry: int = 3)
else:
full_url = url

error_msg: str = ''

while retry > 0:
retry -= 1

try:
response = urlopen(full_url, context=ssl_context)
data = response.read().decode('UTF-8')
return data
except URLError as e:
error_msg = f'Unable to fetch data from url: {url}\n{e}'
debug(error_msg)
except Exception as e:
error_msg = f'Unexpected error when parsing response: {e}'
debug(error_msg)

raise ValueError(f'Failed fetching data: {error_msg}')
try:
response = urlopen(full_url, context=ssl_context)
data = response.read().decode('UTF-8')
return data
except URLError as e:
raise ValueError(f'Unable to fetch data from url: {url}\n{e}')
except Exception as e:
raise ValueError(f'Unexpected error when parsing response: {e}')


def calc_checksum(icmp_packet) -> int:
Expand Down

0 comments on commit ca828f8

Please sign in to comment.