Skip to content
Snippets Groups Projects
Commit ddeb3e65 authored by FH's avatar FH
Browse files

refactor objective testability

Since travis builds are prone to fail if pacman-mirrors.conf is not found
 It has been made possible to change location by adding it to internal config
 It can then be changed on-the-fly to fit travis build (reverted from commit 9e9f32d5)
parent 625f1b3e
No related branches found
No related tags found
No related merge requests found
......@@ -20,7 +20,7 @@
"""Pacman-Mirrors Configuration Functions"""
from . import txt
from .configuration import CONFIG_FILE, CUSTOM_FILE, MIRROR_DIR, MIRROR_FILE, MIRROR_LIST
from .configuration import CONFIG_FILE, MIRROR_DIR, MIRROR_FILE, MIRROR_LIST
class ConfigFn:
......@@ -34,8 +34,6 @@ class ConfigFn:
# is fetched from config file
config = {
"branch": "stable",
"config_file": CONFIG_FILE,
"custom_file": CUSTOM_FILE,
"method": "rank",
"mirror_dir": MIRROR_DIR,
"mirror_file": MIRROR_FILE,
......@@ -45,7 +43,7 @@ class ConfigFn:
}
try:
# read configuration from file
with open(config["config_file"]) as conf:
with open(CONFIG_FILE) as conf:
for line in conf:
line = line.strip()
if line.startswith("#") or "=" not in line:
......
......@@ -56,44 +56,44 @@ class CustomFn:
CustomHelper.cleanup()
@staticmethod
def modify_config(configfile, customfile, onlycountry, custom=False):
def modify_config(onlycountry, custom=False):
"""Modify configuration"""
if not custom:
if os.path.isfile(customfile):
os.remove(customfile)
CustomFn.write_custom_config(configfile, onlycountry, custom)
if os.path.isfile(CUSTOM_FILE):
os.remove(CUSTOM_FILE)
CustomFn.write_custom_config(CONFIG_FILE, onlycountry, custom)
@staticmethod
def write_custom_config(configfile, selectedcountries, custom=False):
def write_custom_config(filename, selection, custom=False):
"""Writes the configuration to file
:param configfile:
:param selectedcountries:
:param filename:
:param selection:
:param custom:
"""
if custom:
if selectedcountries == ["Custom"]:
selectedcountries = "OnlyCountry = Custom\n"
if selection == ["Custom"]:
new_config = "OnlyCountry = Custom\n"
else:
selectedcountries = "OnlyCountry = {list}\n".format(
list=",".join(selectedcountries))
new_config = "OnlyCountry = {list}\n".format(
list=",".join(selection))
else:
selectedcountries = "# OnlyCountry = \n"
new_config = "# OnlyCountry = \n"
try:
with open(
configfile) as cnf, tempfile.NamedTemporaryFile(
filename) as cnf, tempfile.NamedTemporaryFile(
"w+t", dir=os.path.dirname(
configfile), delete=False) as tmp:
filename), delete=False) as tmp:
replaced = False
for line in cnf:
if "OnlyCountry" in line:
tmp.write(selectedcountries)
tmp.write(new_config)
replaced = True
else:
tmp.write("{}".format(line))
if not replaced:
tmp.write(selectedcountries)
os.replace(tmp.name, configfile)
os.chmod(configfile, 0o644)
tmp.write(new_config)
os.replace(tmp.name, filename)
os.chmod(filename, 0o644)
except OSError as err:
print(".: {} {}: {}: {}".format(txt.ERR_CLR, txt.CANNOT_READ_FILE,
err.filename, err.strerror))
......
......@@ -23,6 +23,7 @@ import os
import datetime
from .configuration import FALLBACK, MIRROR_FILE, REPO_ARCH, STATUS_FILE
from .jsonfn import JsonFn
from .miscfn import MiscFn
from . import txt
......
......@@ -200,14 +200,10 @@ class PacmanMirrors:
worklist,
quiet=self.quiet)
if self.custom or self.config["only_country"] != self.mirrors.mirrorlist:
CustomFn.modify_config(self.config["config_file"],
self.config["custom_file"],
self.config["only_country"],
CustomFn.modify_config(self.config["only_country"],
custom=True)
else:
CustomFn.modify_config(self.config["config_file"],
self.config["custom_file"],
self.config["only_country"])
CustomFn.modify_config(self.config["only_country"])
def build_fasttrack_mirror_list(self, number):
"""Fast-track the mirrorlist by aggressive sorting"""
......@@ -297,12 +293,9 @@ class PacmanMirrors:
worklist,
custom=True,
quiet=self.quiet)
# always use "Custom" when interactive mode
# always use "Custom" from interactive
self.config["only_country"] = ["Custom"]
CustomFn.modify_config(self.config["config_file"],
self.config["custom_file"],
self.config["only_country"],
custom=True)
CustomFn.modify_config(self.config["only_country"], custom=True)
print(".: {} {}: {}".format(txt.INF_CLR,
txt.MIRROR_LIST_SAVED,
CUSTOM_FILE))
......
......@@ -29,7 +29,6 @@ class TestCommandLineParse(unittest.TestCase):
"-b", "unstable"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.config["branch"] == "unstable"
......@@ -42,7 +41,6 @@ class TestCommandLineParse(unittest.TestCase):
"-b", "testing"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.config["branch"] == "testing"
......@@ -55,7 +53,6 @@ class TestCommandLineParse(unittest.TestCase):
"-m", "random"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.config["method"] == "random"
......@@ -68,7 +65,6 @@ class TestCommandLineParse(unittest.TestCase):
"-d", "/another/dir/"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.config["mirror_dir"] == "/another/dir/"
......@@ -81,7 +77,6 @@ class TestCommandLineParse(unittest.TestCase):
"-o", "/another/list"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.config["mirror_list"] == "/another/list"
......@@ -94,7 +89,6 @@ class TestCommandLineParse(unittest.TestCase):
"-c", "France,Germany"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.config["only_country"] == ["France", "Germany"]
......@@ -107,7 +101,6 @@ class TestCommandLineParse(unittest.TestCase):
"-c Denmark"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.custom is True
......@@ -120,7 +113,6 @@ class TestCommandLineParse(unittest.TestCase):
"--geoip"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.geoip is True
......@@ -133,7 +125,6 @@ class TestCommandLineParse(unittest.TestCase):
"-f 5"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.fasttrack == 5
......@@ -146,7 +137,6 @@ class TestCommandLineParse(unittest.TestCase):
"-i"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.interactive is True
......@@ -159,7 +149,6 @@ class TestCommandLineParse(unittest.TestCase):
"-t 5"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.max_wait_time == 5
......@@ -172,7 +161,6 @@ class TestCommandLineParse(unittest.TestCase):
"-q"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
assert app.quiet is True
......
......@@ -29,7 +29,6 @@ class TestDefaultConfig(unittest.TestCase):
"-g"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
assert app.config["branch"] == "stable"
@patch("os.getuid")
......@@ -41,7 +40,6 @@ class TestDefaultConfig(unittest.TestCase):
"-g"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
assert app.config["method"] == "rank"
@patch("os.getuid")
......@@ -53,7 +51,6 @@ class TestDefaultConfig(unittest.TestCase):
"-g"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
assert app.config["mirror_dir"] == "mock/var/"
@patch("os.getuid")
......@@ -65,7 +62,6 @@ class TestDefaultConfig(unittest.TestCase):
"-g"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
assert app.config["mirror_file"] == "mock/var/mirrors.json"
@patch("os.getuid")
......@@ -77,7 +73,6 @@ class TestDefaultConfig(unittest.TestCase):
"-g"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
assert app.config["mirror_list"] == "mock/etc/mirrorlist"
@patch("os.getuid")
......@@ -89,7 +84,6 @@ class TestDefaultConfig(unittest.TestCase):
"-g"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
assert app.config["no_update"] is False
# @patch("os.getuid")
......
......@@ -32,7 +32,6 @@ class TestGeoip(unittest.TestCase):
"--geoip"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
app.load_all_mirrors()
assert app.selected_countries == "France"
......@@ -49,7 +48,6 @@ class TestGeoip(unittest.TestCase):
"--geoip"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
app.load_all_mirrors()
assert app.selected_countries == app.mirrors.countrylist
......
......@@ -31,7 +31,6 @@ class TestPacmanMirrors(unittest.TestCase):
"-m", "random"]):
app = PacmanMirrors()
app.config = ConfigFn.build_config()
app.config["config_file"] = "conf/pacman-mirrors.conf"
app.command_line_parse()
app.network = HttpFn.update_mirrors()
app.load_all_mirrors()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment