From add9eded5f9a59b3b44b6effeaf70e30184bb32a Mon Sep 17 00:00:00 2001 From: fhdk <fh@uex.dk> Date: Sat, 25 Feb 2017 16:28:03 +0100 Subject: [PATCH] setting pacman-mirrors.conf dynamically (reverted from commit e0a94c0d157f5491bed8865ae2e86d1d499dac77) --- pacman_mirrors/configfn.py | 7 +-- pacman_mirrors/configuration.py | 2 +- pacman_mirrors/pacman_mirrors.py | 18 +++++--- tests/test_command_line_parse.py | 36 +++++---------- tests/test_default_config.py | 75 +++++++++++++++----------------- tests/test_httpfn.py | 8 +--- tests/test_pacman_mirrors.py | 6 +-- 7 files changed, 65 insertions(+), 87 deletions(-) diff --git a/pacman_mirrors/configfn.py b/pacman_mirrors/configfn.py index 619d9099..a941a967 100644 --- a/pacman_mirrors/configfn.py +++ b/pacman_mirrors/configfn.py @@ -20,20 +20,21 @@ """Pacman-Mirrors Configuration Functions""" from . import txt -from .configuration import MIRROR_DIR, MIRROR_FILE, MIRROR_LIST +from .configuration import CONFIG_FILE, MIRROR_DIR, MIRROR_FILE, MIRROR_LIST class ConfigFn: """Configuration Functions""" @staticmethod - def build_config(configfile): + def build_config(): """Get config informations""" # initialising defaults # information which can differ from these defaults # is fetched from config file config = { "branch": "stable", + "config_file": CONFIG_FILE, "method": "rank", "mirror_dir": MIRROR_DIR, "mirror_file": MIRROR_FILE, @@ -43,7 +44,7 @@ class ConfigFn: } try: # read configuration from file - with open(configfile) as conf: + with open(config["config_file"]) as conf: for line in conf: line = line.strip() if line.startswith("#") or "=" not in line: diff --git a/pacman_mirrors/configuration.py b/pacman_mirrors/configuration.py index bd6d1ec8..3c8278fa 100644 --- a/pacman_mirrors/configuration.py +++ b/pacman_mirrors/configuration.py @@ -21,7 +21,7 @@ # this is for runing in dev environment # TODO: CHANGE BELOW IN PRODUCTION -DEVELOPMENT = "" +DEVELOPMENT = "dev" DESCRIPTION = "unstable development" if DEVELOPMENT: # http constants diff --git a/pacman_mirrors/pacman_mirrors.py b/pacman_mirrors/pacman_mirrors.py index f936470d..6487b1db 100644 --- a/pacman_mirrors/pacman_mirrors.py +++ b/pacman_mirrors/pacman_mirrors.py @@ -32,7 +32,7 @@ from pacman_mirrors import __version__ from random import shuffle # CHANGE CONTENT IN configuration from .configuration import DEVELOPMENT, DESCRIPTION -from .configuration import CONFIG_FILE, CUSTOM_FILE, MIRROR_DIR +from .configuration import CUSTOM_FILE, MIRROR_DIR from .configfn import ConfigFn from .custom_help_formatter import CustomHelpFormatter from .customfn import CustomFn @@ -60,7 +60,7 @@ class PacmanMirrors: def __init__(self): """Init""" - self.configfile = CONFIG_FILE + self.config = {} self.custom = False self.fasttrack = None self.geoip = False @@ -118,6 +118,7 @@ class PacmanMirrors: parser.add_argument("-q", "--quiet", action="store_true", help=txt.HLP_ARG_QUIET) + # TODO: experimental arguments parser.add_argument("-f", "--fasttrack", type=int, metavar=txt.DIGIT, @@ -309,6 +310,7 @@ class PacmanMirrors: def load_all_mirrors(self): """Load mirrors""" + MiscFn.debug("ENTER: load_all_mirrors", "config['only_country']", self.config["only_country"]) if self.config["only_country"] == ["all"]: self.disable_custom_config() @@ -374,7 +376,7 @@ class PacmanMirrors: def run(self): """Run""" FileFn.dir_must_exist(MIRROR_DIR) - self.config = ConfigFn.build_config(self.configfile) + self.config = ConfigFn.build_config() self.command_line_parse() self.network = HttpFn.update_mirrors() self.load_all_mirrors() @@ -388,10 +390,12 @@ class PacmanMirrors: else: self.build_common_mirror_list() - print("{}.:! Pacman-Mirrors {} - {} {}".format(txt.YS, - __version__, - DESCRIPTION, - txt.CE)) + # # TODO: Eventually remove in production + if DEVELOPMENT: + print("{}.:! Pacman-Mirrors {} - {} {}".format(txt.YS, + __version__, + DESCRIPTION, + txt.CE)) if __name__ == "__main__": diff --git a/tests/test_command_line_parse.py b/tests/test_command_line_parse.py index 766b9a24..5144404b 100644 --- a/tests/test_command_line_parse.py +++ b/tests/test_command_line_parse.py @@ -28,8 +28,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-b", "unstable"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.config["branch"] == "unstable" @@ -41,8 +40,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-b", "testing"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.config["branch"] == "testing" @@ -54,8 +52,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-m", "random"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.config["method"] == "random" @@ -67,8 +64,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-d", "/another/dir/"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.config["mirror_dir"] == "/another/dir/" @@ -80,8 +76,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-o", "/another/list"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.config["mirror_list"] == "/another/list" @@ -93,8 +88,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-c", "France,Germany"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.config["only_country"] == ["France", "Germany"] @@ -106,8 +100,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-c Denmark"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.custom is True @@ -119,8 +112,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "--geoip"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.geoip is True @@ -132,8 +124,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-f 5"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.fasttrack == 5 @@ -145,8 +136,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-i"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.interactive is True @@ -158,8 +148,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-t 5"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.max_wait_time == 5 @@ -171,8 +160,7 @@ class TestCommandLineParse(unittest.TestCase): ["pacman-mirrors", "-q"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() app.command_line_parse() assert app.quiet is True diff --git a/tests/test_default_config.py b/tests/test_default_config.py index 0e8f9f3b..4e2ab4d3 100644 --- a/tests/test_default_config.py +++ b/tests/test_default_config.py @@ -28,8 +28,7 @@ class TestDefaultConfig(unittest.TestCase): ["pacman-mirrors", "-g"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() assert app.config["branch"] == "stable" @patch("os.getuid") @@ -40,46 +39,41 @@ class TestDefaultConfig(unittest.TestCase): ["pacman-mirrors", "-g"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() assert app.config["method"] == "rank" - # @patch("os.getuid") - # def test_default_mirrordir(self, mock_os_getuid): - # """TEST: config[mirror_dir] = mock/""" - # mock_os_getuid.return_value = 0 - # with unittest.mock.patch("sys.argv", - # ["pacman-mirrors", - # "-g"]): - # app = PacmanMirrors() - # app.configfile = "conf/pacman-mirrors.conf" - # app.config = ConfigFn.build_config(app.configfile) - # assert app.config["mirror_dir"] == "mock/var/" + @patch("os.getuid") + def test_default_mirrordir(self, mock_os_getuid): + """TEST: config[mirror_dir] = mock/""" + mock_os_getuid.return_value = 0 + with unittest.mock.patch("sys.argv", + ["pacman-mirrors", + "-g"]): + app = PacmanMirrors() + app.config = ConfigFn.build_config() + assert app.config["mirror_dir"] == "mock/var/" - # @patch("os.getuid") - # def test_default_mirrorfile(self, mock_os_getuid): - # """TEST: config[mirror_file] = mock/mirrors.json""" - # mock_os_getuid.return_value = 0 - # with unittest.mock.patch("sys.argv", - # ["pacman-mirrors", - # "-g"]): - # app = PacmanMirrors() - # app.configfile = "conf/pacman-mirrors.conf" - # app.config = ConfigFn.build_config(app.configfile) - # assert app.config["mirror_file"] == "mock/var/mirrors.json" + @patch("os.getuid") + def test_default_mirrorfile(self, mock_os_getuid): + """TEST: config[mirror_file] = mock/mirrors.json""" + mock_os_getuid.return_value = 0 + with unittest.mock.patch("sys.argv", + ["pacman-mirrors", + "-g"]): + app = PacmanMirrors() + app.config = ConfigFn.build_config() + assert app.config["mirror_file"] == "mock/var/mirrors.json" - # @patch("os.getuid") - # def test_default_mirrorlist(self, mock_os_getuid): - # """TEST: config[mirror_list] = mock/mirrorlist""" - # mock_os_getuid.return_value = 0 - # with unittest.mock.patch("sys.argv", - # ["pacman-mirrors", - # "-g"]): - # app = PacmanMirrors() - # app.configfile = "conf/pacman-mirrors.conf" - # app.config = ConfigFn.build_config(app.configfile) - # app.config["mirror_list"] = "mock/etc/mirrorlist" - # assert app.config["mirror_list"] == "mock/etc/mirrorlist" + @patch("os.getuid") + def test_default_mirrorlist(self, mock_os_getuid): + """TEST: config[mirror_list] = mock/mirrorlist""" + mock_os_getuid.return_value = 0 + with unittest.mock.patch("sys.argv", + ["pacman-mirrors", + "-g"]): + app = PacmanMirrors() + app.config = ConfigFn.build_config() + assert app.config["mirror_list"] == "mock/etc/mirrorlist" @patch("os.getuid") def test_default_noupdate(self, mock_os_getuid): @@ -89,8 +83,7 @@ class TestDefaultConfig(unittest.TestCase): ["pacman-mirrors", "-g"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) + app.config = ConfigFn.build_config() assert app.config["no_update"] is False # @patch("os.getuid") @@ -102,7 +95,7 @@ class TestDefaultConfig(unittest.TestCase): # "-g"]): # app = PacmanMirrors() # app.config = {} - # app.config = ConfigFn.build_config(app.configfile) + # app.config = ConfigFn.build_config() # assert app.config["only_country"] == [] def tearDown(self): diff --git a/tests/test_httpfn.py b/tests/test_httpfn.py index 6ad67e30..7d942736 100644 --- a/tests/test_httpfn.py +++ b/tests/test_httpfn.py @@ -31,9 +31,7 @@ class TestHttpFn(unittest.TestCase): ["pacman-mirrors", "--geoip"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) - app.config["mirror_file"] = "mock/var/mirrors.json" + app.config = ConfigFn.build_config() app.command_line_parse() app.load_all_mirrors() assert app.selected_countries == "France" @@ -49,9 +47,7 @@ class TestHttpFn(unittest.TestCase): "-g", "--geoip"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) - app.config["mirror_file"] = "mock/var/mirrors.json" + app.config = ConfigFn.build_config() app.command_line_parse() app.load_all_mirrors() assert app.selected_countries == app.mirrors.countrylist diff --git a/tests/test_pacman_mirrors.py b/tests/test_pacman_mirrors.py index 4999d541..a598452f 100644 --- a/tests/test_pacman_mirrors.py +++ b/tests/test_pacman_mirrors.py @@ -31,11 +31,7 @@ class TestPacmanMirrors(unittest.TestCase): "-qc", "all", "-m", "random"]): app = PacmanMirrors() - app.configfile = "conf/pacman-mirrors.conf" - app.config = ConfigFn.build_config(app.configfile) - app.config["mirror_dir"] = "mock/var/" - app.config["mirror_file"] = "mock/var/mirrors.json" - app.config["mirror_list"] = "mock/etc/mirrorlist" + app.config = ConfigFn.build_config() app.command_line_parse() FileFn.dir_must_exist(app.config["mirror_dir"]) app.network = HttpFn.update_mirrors() -- GitLab