From 561c8837aca34ed658cc6fbe04cf2f8778d824c4 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Tue, 17 Nov 2020 10:26:52 +0100 Subject: [PATCH] Add 'SALOME DEFAULT VALUES' section to the configuration file. This section can be used to replace the default values used by Salome, without replacing a value defined by the user in the environment. Variables that are already defined in the environment are not modified when declared in this section. --- bin/parseConfigFile.py | 17 ++++++++++++++--- bin/salomeContext.py | 18 +++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/bin/parseConfigFile.py b/bin/parseConfigFile.py index 69c3e2acd..4c2905574 100644 --- a/bin/parseConfigFile.py +++ b/bin/parseConfigFile.py @@ -23,6 +23,7 @@ import logging import re from io import StringIO import subprocess +import collections from salomeContextUtils import SalomeContextException #@UnresolvedImport logging.basicConfig() @@ -30,7 +31,8 @@ logConfigParser = logging.getLogger(__name__) ADD_TO_PREFIX = 'ADD_TO_' UNSET_KEYWORD = 'UNSET' - +# variables defined in this section are set only if not already defined +DEFAULT_VARS_SECTION_NAME = 'SALOME DEFAULT VALUES' def _expandSystemVariables(key, val): expandedVal = os.path.expandvars(val) # expand environment variables @@ -193,6 +195,7 @@ def __processConfigFile(config, reserved = None, filename="UNKNOWN FILENAME"): reserved = [] unsetVariables = [] outputVariables = [] + defaultValues = [] # Get raw items for each section, and make some processing for environment variables management reservedKeys = [ADD_TO_PREFIX+str(x) for x in reserved] # produce [ 'ADD_TO_reserved_1', 'ADD_TO_reserved_2', ..., ADD_TO_reserved_n ] reservedValues = dict([str(i),[]] for i in reserved) # create a dictionary in which keys are the 'ADD_TO_reserved_i' and associated values are empty lists: { 'reserved_1':[], 'reserved_2':[], ..., reserved_n:[] } @@ -217,7 +220,10 @@ def __processConfigFile(config, reserved = None, filename="UNKNOWN FILENAME"): # remove left&right spaces on each element vals = [v.strip(' \t\n\r') for v in vals] else: - outputVariables.append((key, expandedVal)) + if DEFAULT_VARS_SECTION_NAME == section.upper(): + defaultValues.append((key, expandedVal)) + else: + outputVariables.append((key, expandedVal)) pass pass # end if key pass # end for key,val @@ -230,7 +236,12 @@ def __processConfigFile(config, reserved = None, filename="UNKNOWN FILENAME"): vals = list(set(vals)) outVars.append((var, ','.join(vals))) - return unsetVariables, outVars, reservedValues + ConfigInfo = collections.namedtuple("ConfigInfo", + ["unsetVariables", + "outputVariables", + "reservedValues", + "defaultValues"]) + return ConfigInfo(unsetVariables, outVars, reservedValues, defaultValues) # def _trimColons(var): diff --git a/bin/salomeContext.py b/bin/salomeContext.py index 689411316..99fcd113a 100755 --- a/bin/salomeContext.py +++ b/bin/salomeContext.py @@ -183,6 +183,14 @@ class SalomeContext: os.environ[name] = value # + def setDefaultValue(self, name, value): + """ Set environment variable only if it is undefined.""" + env = os.getenv(name, '') + if not env: + value = os.path.expandvars(value) # expand environment variables + self.getLogger().debug("Set environment variable: %s=%s", name, value) + os.environ[name] = value + """Unset environment variable""" def unsetVariable(self, name): if os.environ.has_key(name): @@ -287,7 +295,11 @@ class SalomeContext: if reserved is None: reserved = [] try: - unsetVars, configVars, reservedDict = parseConfigFile(filename, reserved) + configInfo = parseConfigFile(filename, reserved) + unsetVars = configInfo.unsetVariables + configVars = configInfo.outputVariables + reservedDict = configInfo.reservedValues + defaultValues = configInfo.defaultValues except SalomeContextException as e: msg = "%s"%e self.getLogger().error(msg) @@ -312,6 +324,10 @@ class SalomeContext: self.setVariable(key, val, overwrite=True) pass + for key,val in defaultValues: + self.setDefaultValue(key, val) + pass + pythonpath = os.getenv('PYTHONPATH','').split(os.pathsep) pythonpath = [ os.path.realpath(x) for x in pythonpath ] sys.path[:0] = pythonpath -- 2.39.2