diff --git a/utils.py b/utils.py index 15399fe249253ef0b01b62c9009792002ed50872..cd8440150be0c2aa373baf817efdf49ed41f9e9d 100644 --- a/utils.py +++ b/utils.py @@ -20,6 +20,8 @@ DEFAULT_CONF_PATH = "global-conf.sh" AUTO_CONF_PATH = "auto-generated-conf.sh" CONF_PATH = "conf.sh" +_conf_cache = None + def log(text: str, error: bool = False): """Output log message to stout or stderr. @@ -27,7 +29,7 @@ def log(text: str, error: bool = False): :param text: Message to log :type text: str :param error: Wether it should output to stderr or not, defaults to False - :param error: bool, optional + :type error: bool, optional """ fo = sys.stderr if error else sys.stdout @@ -93,9 +95,9 @@ def exec_cmd(cmd: Any, log_output: bool = True, get_output: bool = True) -> tupl :param cmd: Command to run :type cmd: Any :param log_output: Wether to log output or not, defaults to True - :param log_output: bool, optional + :type log_output: bool, optional :param get_output: Wether to return output or not, defaults to True - :param get_output: bool, optional + :type get_output: bool, optional :return: Return code and output :rtype: tuple """ @@ -133,7 +135,7 @@ def check_cmd(cmd: Any, log_output: bool = False) -> int: :param cmd: Command to execute :type cmd: Any :param log_output: Wether to log output or not, defaults to False - :param log_output: bool, optional + :type log_output: bool, optional :return: Return code :rtype: int """ @@ -192,6 +194,8 @@ def load_conf() -> dict: log("Perhaps you forget to change the configuration.") log("Path of configuration file: %s" % str(Path(base_dir, CONF_PATH))) log("Perhaps you want to quit this script to change the configuration?\n") + global _conf_cache + _conf_cache = conf return conf @@ -201,14 +205,16 @@ def get_conf(name: str, default: str = None) -> str: :param name: Parameter name :type name: str :param default: Default parameter value, defaults to None - :param default: str, optional + :type default: str, optional :return: Parameter value :rtype: str """ - conf = load_conf() + global _conf_cache + if _conf_cache is None: + load_conf() - return conf.get(name, default) + return _conf_cache.get(name, default) def set_conf(key: str, value: str, override: bool = False) -> bool: @@ -219,7 +225,7 @@ def set_conf(key: str, value: str, override: bool = False) -> bool: :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 + :type override: bool, optional :return: True if the option have changed, False otherwise :rtype: bool """ @@ -238,15 +244,18 @@ def set_conf(key: str, value: str, override: bool = False) -> bool: conf = regex.sub("{}='{}'".format(key.upper(), str(value)), conf) with open(conf_path, "w") as conf_fh: conf_fh.write(conf) - return True + success = True elif not match: # add option with open(conf_path, "a") as conf_fh: conf_fh.write("\n{}='{}'\n".format(key.upper(), str(value))) - return True + success = True else: # no match or no override - return False + success = False + # reload conf + load_conf() + return success def run_commands(cmds: list): @@ -404,11 +413,11 @@ def mkcert( :param domains: Domains for which the certificates will be self-signed :type domains: list :param ecc: Wether to use Elliptic Curve cryptography or not, defaults to True, if Fasle RSA is used - :param ecc: bool, optional + :type ecc: bool, optional :param days: Validity lifetime of the certificate, defaults to 3650 - :param days: int, optional + :type days: int, optional :param config_tpl: OpenSSL config file template, defaults to OPENSSL_CONFIG_TEMPLATE - :param config_tpl: str, optional + :type config_tpl: str, optional """ # create certs dir