Salome HOME
Deal with ref count server side.
[modules/kernel.git] / bin / parseConfigFile.py
index 7d60b22095195aa1439ef8cc98385a3cd93fcbf4..3054ed1f341eeb2154d3280befda32627afe1970 100644 (file)
@@ -23,6 +23,7 @@ import logging
 import re
 from io import StringIO
 import subprocess
+from salomeContextUtils import SalomeContextException
 
 logging.basicConfig()
 logConfigParser = logging.getLogger(__name__)
@@ -31,6 +32,17 @@ ADD_TO_PREFIX = 'ADD_TO_'
 UNSET_KEYWORD = 'UNSET'
 
 
+def _expandSystemVariables(key, val):
+  expandedVal = os.path.expandvars(val) # expand environment variables
+  # Search for not expanded variables (i.e. non-existing environment variables)
+  pattern = re.compile('\${ ( [^}]* ) }', re.VERBOSE) # string enclosed in ${ and }
+  expandedVal = pattern.sub(r'', expandedVal) # remove matching patterns
+
+  if not "DLIM8VAR" in key: # special case: DISTENE licence key can contain double clons (::)
+    expandedVal = _trimColons(expandedVal)
+  return expandedVal
+#
+
 # :TRICKY: So ugly solution...
 class MultiOptSafeConfigParser(ConfigParser.SafeConfigParser):
   def __init__(self):
@@ -86,7 +98,7 @@ class MultiOptSafeConfigParser(ConfigParser.SafeConfigParser):
           optname = None
         # no section header in the file?
         elif cursect is None:
-          raise MissingSectionHeaderError(fpname, lineno, line)
+          raise ConfigParser.MissingSectionHeaderError(fpname, lineno, line)
         # an option line?
         else:
           mo = self.OPTCRE.match(line)
@@ -103,6 +115,13 @@ class MultiOptSafeConfigParser(ConfigParser.SafeConfigParser):
                 if pos != -1 and optval[pos-1].isspace():
                   optval = optval[:pos]
               optval = optval.strip()
+              # ADD THESE LINES
+              splittedComments = optval.split('#')
+              s = _expandSystemVariables(optname, splittedComments[0])
+              optval = s.strip().strip("'").strip('"')
+              #if len(splittedComments) > 1:
+              #  optval += " #" + " ".join(splittedComments[1:])
+              # END OF ADD
               # allow empty values
               if optval == '""':
                 optval = ''
@@ -157,7 +176,11 @@ def parseConfigFile(filename, reserved = []):
     logConfigParser.error("No section found in file: %s"%(filename))
     return []
 
-  return __processConfigFile(config, reserved, filename)
+  try:
+    return __processConfigFile(config, reserved, filename)
+  except ConfigParser.InterpolationMissingOptionError, e:
+    msg = "A variable may be undefined in SALOME context file: %s\nParser error is: %s\n"%(filename, e)
+    raise SalomeContextException(msg)
 #
 
 def __processConfigFile(config, reserved = [], filename="UNKNOWN FILENAME"):
@@ -181,13 +204,7 @@ def __processConfigFile(config, reserved = [], filename="UNKNOWN FILENAME"):
       elif key == UNSET_KEYWORD:
         unsetVariables += val.replace(',', ' ').split()
       else:
-        expandedVal = os.path.expandvars(val) # expand environment variables
-        # Search for not expanded variables (i.e. non-existing environment variables)
-        pattern = re.compile('\${ ( [^}]* ) }', re.VERBOSE) # string enclosed in ${ and }
-        expandedVal = pattern.sub(r'', expandedVal) # remove matching patterns
-        # Trim colons
-        if not "DLIM8VAR" in key: # special case: DISTENE licence key can contain double clons (::)
-          expandedVal = _trimColons(expandedVal)
+        expandedVal = _expandSystemVariables(key, val)
 
         if key in reservedKeys:
           shortKey = key[len(ADD_TO_PREFIX):]
@@ -202,7 +219,14 @@ def __processConfigFile(config, reserved = [], filename="UNKNOWN FILENAME"):
       pass # end for key,val
     pass # end for section
 
-  return unsetVariables, outputVariables, reservedValues
+  # remove duplicate values
+  outVars = []
+  for (var, values) in outputVariables:
+    vals = values.split(',')
+    vals = list(set(vals))
+    outVars.append((var, ','.join(vals)))
+
+  return unsetVariables, outVars, reservedValues
 #
 
 def _trimColons(var):
@@ -272,8 +296,6 @@ class EnvFileConverter(object):
           line = ADD_TO_PREFIX + k + ": " + value
       # Update list of variable names
       # :TODO: define excludeBlock variable (similar to exclude) and provide method to extend it
-      if line.startswith("LOGNAME="):
-        return "\n"
       if "cleandup()" in line:
         print "WARNING: parseConfigFile.py: skip cleandup and look for '# PRODUCT environment'"
         while True: