From 15aae28e54c3fdfe2eb4fd4563cf095147e16fa2 Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Sat, 5 Sep 2020 20:42:56 +0200 Subject: reflector-2020.8.tar.xz --- Reflector.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'Reflector.py') diff --git a/Reflector.py b/Reflector.py index 5263316..1665090 100644 --- a/Reflector.py +++ b/Reflector.py @@ -35,6 +35,7 @@ import os import pipes import queue import re +import shlex import socket import subprocess import sys @@ -61,7 +62,7 @@ MIRRORLIST_ENTRY_FORMAT = "Server = " + MIRROR_URL_FORMAT + "\n" DEFAULT_CONNECTION_TIMEOUT = 5 DEFAULT_CACHE_TIMEOUT = 300 -DEFAULT_N_THREADS = 5 +DEFAULT_N_THREADS = os.cpu_count() SORT_TYPES = { 'age': 'last server synchronization', @@ -638,8 +639,6 @@ def add_arguments(parser): ''' Add reflector arguments to the argument parser. ''' - parser = argparse.ArgumentParser(description='retrieve and filter a list of the latest Arch Linux mirrors') - parser.add_argument( '--connection-timeout', type=int, metavar='n', default=DEFAULT_CONNECTION_TIMEOUT, help='The number of seconds to wait before a connection times out. Default: %(default)s' @@ -678,7 +677,7 @@ def add_arguments(parser): parser.add_argument( '--threads', type=int, metavar='n', default=DEFAULT_N_THREADS, - help='The maximum number of threads to use when rating mirrors. Default: %(default)s' + help='The maximum number of threads to use when rating mirrors. Keep in mind that this may skew your results if your connection is saturated. Default: %(default)s (number of detected CPUs)' ) parser.add_argument( @@ -703,7 +702,7 @@ def add_arguments(parser): filters.add_argument( '-c', '--country', dest='countries', action='append', metavar='', - help='Match one of the given countries (case-sensitive). Use "--list-countries" to see which are available.' + help='Match one of the given countries (case-sensitive). Multiple countries may be selected using commas (e.g. "France,Germany") or by passing this option multiple times. Use "--list-countries" to see which are available.' ) filters.add_argument( @@ -738,7 +737,7 @@ def add_arguments(parser): filters.add_argument( '-p', '--protocol', dest='protocols', action='append', metavar='', - help='Match one of the given protocols, e.g. "http", "ftp".' + help='Match one of the given protocols, e.g. "https" or "ftp". Multiple protocols may be selected using commas (e.g. "https,http") or by passing this option multiple times.' ) filters.add_argument( @@ -764,15 +763,40 @@ def add_arguments(parser): return parser +def split_list_args(args): + ''' + Split comma-separated list arguments into separate list elements. + ''' + if not args: + return + for arg in args: + yield from arg.split(',') + + +class MyArgumentParser(argparse.ArgumentParser): + ''' + Custom argument parser to support a more readable format in argument files. + ''' + def convert_arg_line_to_args(self, arg_line): + # Support comments and blank lines. + # content = arg_line.strip() + # if not content or content.startswith('#'): + # return list() + return shlex.split(arg_line, comments=True) + + def parse_args(args=None): ''' Parse command-line arguments. ''' - parser = argparse.ArgumentParser( - description='retrieve and filter a list of the latest Arch Linux mirrors' + parser = MyArgumentParser( + description='retrieve and filter a list of the latest Arch Linux mirrors', + fromfile_prefix_chars='@' ) parser = add_arguments(parser) options = parser.parse_args(args) + for list_arg in ('countries', 'protocols'): + setattr(options, list_arg, list(split_list_args(getattr(options, list_arg)))) return options -- cgit v1.2.3-70-g09d2