import re
from io import StringIO
import subprocess
+import collections
from salomeContextUtils import SalomeContextException #@UnresolvedImport
logging.basicConfig()
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
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:[] }
# 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
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):
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):
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)
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