Skip to content
Snippets Groups Projects
Commit 04e2f97c authored by Frede H's avatar Frede H :speech_balloon:
Browse files

api implementation @udeved

parent 96659eb5
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,32 @@ from . import txt
from . import configuration as conf
def api_branch(branch, filename):
"""Write branch"""
branch = "Branch = {}".format(branch)
try:
with open(
filename) as cnf, tempfile.NamedTemporaryFile(
"w+t", dir=os.path.dirname(
filename), delete=False) as tmp:
replaced = False
for line in cnf:
if "Branch" in line:
tmp.write(branch)
replaced = True
else:
tmp.write("{}".format(line))
if not replaced:
tmp.write(branch)
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))
exit(1)
def build_config():
"""Get config informations
:returns: config, custom
......
......@@ -130,6 +130,16 @@ class PacmanMirrors:
parser.add_argument("--default",
action="store_true",
help=txt.HLP_ARG_DEFAULT)
# api arguments
parser.add_argument("-a/--api",
action="store_true")
parser.add_argument("--get-branch",
type=bool)
parser.add_argument("--set-branch",
type=bool)
parser.add_argument("--prefix",
type=str,
action="store_true")
args = parser.parse_args()
......@@ -196,6 +206,41 @@ class PacmanMirrors:
self.custom = False
self.config["only_country"] = []
if args.api:
if args.branch:
self.api_config(prefix=args.prefix,
set_branch=args.set_branch)
else:
self.api_config(get_branch=args.get_branch)
def api_config(self, prefix=None, set_branch=False, get_branch=False):
"""Api functions
:param prefix: prefix to the config paths
:param set_branch: writes branch to pacman-mirrors
:param get_branch: exit with -1 -2 -3
"""
if prefix:
if "$" in prefix:
prefix = os.environ.get(prefix)
self.config["config_file"] = prefix + self.config["config_file"]
self.config["custom_file"] = prefix + self.config["custom_file"]
self.config["fallback_file"] = prefix + self.config["fallback_file"]
self.config["mirror_dir"] = prefix + self.config["mirror_dir"]
self.config["mirror_file"] = prefix + self.config["mirror_file"]
self.config["mirror_list"] = prefix + self.config["mirror_list"]
self.config["status_file"] = prefix + self.config["status_file"]
if set_branch:
configfn.api_branch(self.config["branch"], self.config["config_file"])
if get_branch:
if self.config["branch"] == "stable":
exit(-1)
if self.config["branch"] == "testing":
exit(-2)
if self.config["branch"] == "unstable":
exit(-3)
def build_common_mirror_list(self):
"""Generate common mirrorlist"""
worklist = mirrorfn.filter_mirror_country(self.mirrors.mirrorlist,
......
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