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

add new utils to write option in conf.sh | refs #27883

parent 4e858f63
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
from collections import OrderedDict from collections import OrderedDict
import os import os
from pathlib import Path from pathlib import Path
import re
import subprocess import subprocess
import sys import sys
from typing import Any from typing import Any
...@@ -210,6 +211,44 @@ def get_conf(name: str, default: str = None) -> str: ...@@ -210,6 +211,44 @@ def get_conf(name: str, default: str = None) -> str:
return conf.get(name, default) return conf.get(name, default)
def set_conf(key: str, value: str, override: bool = False) -> bool:
"""Write the given configuration option in `conf.sh`.
:param key: Option name
:type key: str
:param value: Option value
:type value: str
:param override: Wether to override the option if it already exists, defaults to False
:param override: bool, optional
:return: True if the option have changed, False otherwise
:rtype: bool
"""
state = False
# open conf.sh file
conf_fh = open(Path(__file__, CONF_PATH).resolve(), "w")
# get its content
conf = conf_fh.read()
# check if option already exists
regex = re.compile(r"^" + key.upper() + "=(.*)$", flags=re.M)
match = regex.search(conf)
if match and override:
conf = regex.sub("{}='{}'".format(key.upper(), str(value)), conf)
state = True
else:
# add option
conf = conf + "\n{}='{}'\n".format(key.upper(), str(value))
state = True
# write file
conf_fh.write(conf)
conf_fh.close()
return state
def run_commands(cmds: list): def run_commands(cmds: list):
"""Run a serie of successive commands. """Run a serie of successive commands.
......
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