Skip to content
Snippets Groups Projects
Commit 3d77f065 authored by Nicolas KAROLAK's avatar Nicolas KAROLAK
Browse files

set_conf: fix read/write handling | refs #27883

parent e68faf97
No related branches found
No related tags found
No related merge requests found
......@@ -224,13 +224,11 @@ def set_conf(key: str, value: str, override: bool = False) -> bool:
:rtype: bool
"""
state = False
# open conf.sh file
base_dir = Path(__file__).resolve().parent
conf_fh = open(Path(base_dir, CONF_PATH).resolve(), "w+")
# get its content
conf = conf_fh.read()
conf_path = Path(base_dir, CONF_PATH).resolve()
# read conf.sh
with open(conf_path) as read_conf_fh:
conf = read_conf_fh.read()
# check if option already exists
regex = re.compile(r"^" + key.upper() + "=(.*)$", flags=re.M)
......@@ -238,17 +236,17 @@ def set_conf(key: str, value: str, override: bool = False) -> bool:
if match and override:
# override option
conf = regex.sub("{}='{}'".format(key.upper(), str(value)), conf)
state = True
with open(conf_path, "w") as conf_fh:
conf_fh.write(conf)
return True
elif not match:
# add option
conf = conf + "\n{}='{}'\n".format(key.upper(), str(value))
state = True
# write file
conf_fh.write(conf)
conf_fh.close()
return state
with open(conf_path, "a") as conf_fh:
conf_fh.write("\n{}='{}'\n".format(key.upper(), str(value)))
return True
else:
# no override
return False
def run_commands(cmds: list):
......
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