From 5bba102c45a9709891c5ad7ed20a42b1f036d9a2 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Tue, 1 Sep 2020 12:22:44 +0200 Subject: Added list_mirrors() and prepared for selecting mirrors in guided.py --- archinstall/lib/mirrors.py | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'archinstall/lib') diff --git a/archinstall/lib/mirrors.py b/archinstall/lib/mirrors.py index 6cfccf19..53849a2a 100644 --- a/archinstall/lib/mirrors.py +++ b/archinstall/lib/mirrors.py @@ -1,7 +1,9 @@ +import urllib.request + from .exceptions import * from .general import * -def filter_mirrors_by_region(regions, *args, **kwargs): +def filter_mirrors_by_region(regions, destination='/etc/pacman.d/mirrorlist', tmp_dir='/root', *args, **kwargs): """ This function will change the active mirrors on the live medium by filtering which regions are active based on `regions`. @@ -12,9 +14,9 @@ def filter_mirrors_by_region(regions, *args, **kwargs): region_list = [] for region in regions.split(','): region_list.append(f'country={region}') - o = b''.join(sys_command((f"/usr/bin/wget 'https://www.archlinux.org/mirrorlist/?{'&'.join(region_list)}&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' -O /root/mirrorlist"))) - o = b''.join(sys_command(("/usr/bin/sed -i 's/#Server/Server/' /root/mirrorlist"))) - o = b''.join(sys_command(("/usr/bin/mv /root/mirrorlist /etc/pacman.d/"))) + o = b''.join(sys_command((f"/usr/bin/wget 'https://www.archlinux.org/mirrorlist/?{'&'.join(region_list)}&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' -O {tmp_dir}/mirrorlist"))) + o = b''.join(sys_command((f"/usr/bin/sed -i 's/#Server/Server/' {tmp_dir}/mirrorlist"))) + o = b''.join(sys_command((f"/usr/bin/mv {tmp_dir}/mirrorlist {destination}"))) return True @@ -54,7 +56,37 @@ def insert_mirrors(mirrors, *args, **kwargs): return True +def use_mirrors(regions :dict, destination='/etc/pacman.d/mirrorlist'): + for region, mirrors in regions.items(): + with open(destination, 'w') as mirrorlist: + for mirror in mirrors: + mirrorlist.write(f'## {region}\n') + mirrorlist.write(f'Server = {mirror}\n') + return True + def re_rank_mirrors(top=10, *positionals, **kwargs): if sys_command((f'/usr/bin/rankmirrors -n {top} /etc/pacman.d/mirrorlist > /etc/pacman.d/mirrorlist')).exit_code == 0: return True - return False \ No newline at end of file + return False + +def list_mirrors(): + url = f"https://www.archlinux.org/mirrorlist/?protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" + + response = urllib.request.urlopen(url) + regions = {} + + region = 'Unknown region' + for line in response.readlines(): + if len(line.strip()) == 0: continue + + line = line.decode('UTF-8').strip('\n').strip('\r') + if line[:3] == '## ': + region = line[3:] + elif line[:10] == '#Server = ': + if not region in regions: + regions[region] = {} + + url = line[1:].lstrip('Server = ') + regions[region][url] = True + + return regions \ No newline at end of file -- cgit v1.2.3-70-g09d2