From 5bfaf1feb47a911855e5930c428b9cb91afb15f2 Mon Sep 17 00:00:00 2001 From: Konrad Weihmann Date: Wed, 17 May 2023 07:43:56 +0000 Subject: [PATCH 1/3] Add random flag to randomly select a mirror from the determined list. This should help working around the fact, that even though a mirror is ranked highest it could be still having temporary difficulties. This can be used in CI systems where interactive --choose flag is not an option Signed-off-by: Konrad Weihmann --- README.rst | 2 ++ apt_select/__main__.py | 3 +++ apt_select/arguments.py | 11 +++++++++++ 3 files changed, 16 insertions(+) diff --git a/README.rst b/README.rst index 7dc716e..0a1f6a8 100644 --- a/README.rst +++ b/README.rst @@ -71,6 +71,8 @@ Invocation requires -t/--top-num NUMBER where NUMBER > 1 -l, --list print list of mirrors only, don't generate file cannot be used with -c/--choose + -r, --random pick a random mirror from top list + cannot be used with -c/--choose The exit code is 0 on success, 1 on error, and 4 if sources.list already has the chosen mirror and a new one was not generated. diff --git a/apt_select/__main__.py b/apt_select/__main__.py index 200a579..7cfc43c 100644 --- a/apt_select/__main__.py +++ b/apt_select/__main__.py @@ -1,6 +1,7 @@ #!/usr/bin/env python """Main apt-select script""" +import random import requests import re @@ -200,6 +201,8 @@ def set_hostname_len(url, i): key = 0 if args.choose: key = get_selected_mirror(len(archives.top_list)) - 1 + elif args.random: + key = random.randint(0, len(archives.top_list) - 1) if args.list_only: exit() diff --git a/apt_select/arguments.py b/apt_select/arguments.py index d0fe763..6fa22c3 100644 --- a/apt_select/arguments.py +++ b/apt_select/arguments.py @@ -112,6 +112,17 @@ def get_args(): ), default=False ) + output_group.add_argument( + '-r', + '--random', + dest='random', + action='store_true', + help=( + "pick a random mirror from top list\n" + "cannot be used with -c/--choose\n" + ), + default=False + ) return parser From 8ae377362d8daea62c02cef58474fc88313aa988 Mon Sep 17 00:00:00 2001 From: Konrad Weihmann Date: Mon, 21 Aug 2023 15:35:38 +0000 Subject: [PATCH 2/3] apt: hardcode main ubuntu archive as fallback latest github runner images only come with deb mirror+file scheme style information, thus the parse here fails to extract the main URL. Instead of raising an exception, let's just hardcode the http://archive.ubuntu.com/ubuntu Signed-off-by: Konrad Weihmann --- apt_select/apt.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apt_select/apt.py b/apt_select/apt.py index 3b4fa28..5b48dbf 100644 --- a/apt_select/apt.py +++ b/apt_select/apt.py @@ -157,6 +157,9 @@ def set_current_archives(self): raise SourcesFileError(err) urls = self.__get_current_archives() + if 'current' not in urls: + # hardcode a fallback to main ubuntu archive + urls['current'] = 'http://archive.ubuntu.com/ubuntu' if not urls: raise SourcesFileError(( "Error finding current %s URI in %s\n%s\n" % From 9a408a732eb4eb10b764b0e0f4c9f9a3656a9cf9 Mon Sep 17 00:00:00 2001 From: Konrad Weihmann Date: Mon, 21 Aug 2023 16:14:05 +0000 Subject: [PATCH 3/3] apt: override _CONFIG_PATH if environment var APT_CONFIGPATH is set, so we can point to a not hardcoded path instead Signed-off-by: Konrad Weihmann --- apt_select/apt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apt_select/apt.py b/apt_select/apt.py index 5b48dbf..c7c1454 100644 --- a/apt_select/apt.py +++ b/apt_select/apt.py @@ -1,7 +1,7 @@ #!/usr/bin/env python from subprocess import check_output -from os import path +from os import path, environ from apt_select.utils import utf8_decode SUPPORTED_KERNEL = 'Linux' @@ -97,7 +97,7 @@ class Sources(object): DIRECTORY = '/etc/apt/' LIST_FILE = 'sources.list' - _CONFIG_PATH = DIRECTORY + LIST_FILE + _CONFIG_PATH = environ.get('APT_CONFIGPATH', DIRECTORY + LIST_FILE) def __init__(self, codename): self._codename = codename.lower()