logConfigParser.error("No section found in file: %s"%(filename))
return []
- return _processConfigFile(config, reserved, filename)
+ return __processConfigFile(config, reserved, filename)
#
-def _processConfigFile(config, reserved = [], filename="UNKNOWN FILENAME"):
+def __processConfigFile(config, reserved = [], filename="UNKNOWN FILENAME"):
# :TODO: may detect duplicated variables in the same section (raise a warning)
# or even duplicate sections
break
while "clean " in line[0:6]: #skip clean calls with ending ";" crash
line = self.fp.readline()
+ # Extract variable=value
if "=" in line:
try:
variable, value = line.split('=')
except: #avoid error for complicated sh line xx=`...=...`, but warning
print "WARNING: parseConfigFile.py: line with multiples '=' character are hazardous: '"+line+"'"
variable, value = line.split('=',1)
+ pass
+
+ # Self-extending variables that are not in reserved keywords
+ # Example: FOO=something:${FOO}
+ # In this case, remove the ${FOO} in value
+ if variable in value:
+ value = self._purgeValue(value, variable)
+ line = "%s=%s"%(variable,value)
+
self.allParsedVariableNames.append(variable)
- # Self-extending variables that are not in reserved keywords
- # Example: FOO=something:${FOO}
- # :TODO:
+ # End of extraction
+
+ if not line:
+ return line
+
#
# replace "${FOO}" and "$FOO" and ${FOO} and $FOO by %(FOO)s if FOO is
# defined in current file (i.e. it is not an external environment variable)
key = r'\$\{?'+k+'\}?'
pattern = re.compile(key, re.VERBOSE)
line = pattern.sub(r'%('+k+')s', line)
- # Remove quotes
- pattern = re.compile(r'\"', re.VERBOSE)
- line = pattern.sub(r'', line)
+ # Remove quotes (if line does not contain whitespaces)
+ try:
+ variable, value = line.split('=', 1)
+ except ValueError:
+ variable, value = line.split(':', 1)
+ if not ' ' in value.strip():
+ pattern = re.compile(r'\"', re.VERBOSE)
+ line = pattern.sub(r'', line)
#
+
# Replace `shell_command` by its result
def myrep(obj):
obj = re.sub('`', r'', obj.group(0)) # remove quotes
#
# Convert .sh environment file to configuration file format
-def convertEnvFileToConfigFile(envFilename, configFilename):
- #reserved=['PATH', 'LD_LIBRARY_PATH', 'PYTHONPATH']
+def convertEnvFileToConfigFile(envFilename, configFilename, reserved=[]):
logConfigParser.debug('convert env file %s to %s'%(envFilename, configFilename))
- reserved=['PATH', 'LD_LIBRARY_PATH', 'PYTHONPATH', 'MANPATH', 'R_LIBS', 'PV_PLUGIN_PATH', 'TCLLIBPATH', 'TKLIBPATH']
fileContents = open(envFilename, 'r').read()
pattern = re.compile('\n[\n]+', re.VERBOSE) # multiple '\n'
if len(configFileNames) == 0:
raise SalomeRunnerException("No configuration files given")
+ reserved=['PATH', 'LD_LIBRARY_PATH', 'PYTHONPATH', 'MANPATH', 'PV_PLUGIN_PATH']
for filename in configFileNames:
basename, extension = os.path.splitext(filename)
if extension == ".cfg":
- self.__setEnvironmentFromConfigFile(filename)
+ self.__setEnvironmentFromConfigFile(filename, reserved)
elif extension == ".sh":
#new convert procedures, temporary could be use not to be automatically deleted
#temp = tempfile.NamedTemporaryFile(suffix='.cfg', delete=False)
temp = tempfile.NamedTemporaryFile(suffix='.cfg')
try:
- convertEnvFileToConfigFile(filename, temp.name)
- self.__setEnvironmentFromConfigFile(temp.name)
+ convertEnvFileToConfigFile(filename, temp.name, reserved)
+ self.__setEnvironmentFromConfigFile(temp.name, reserved)
except ConfigParser.ParsingError, e:
self.getLogger().warning("Invalid token found when parsing file: %s\n"%(filename))
print e
"""Append value to PATH environment variable"""
def addToPath(self, value):
- self.__addToReserved('PATH', value)
+ self.addToEnviron('PATH', value)
#
"""Append value to LD_LIBRARY_PATH environment variable"""
def addToLdLibraryPath(self, value):
- self.__addToReserved('LD_LIBRARY_PATH', value)
+ self.addToEnviron('LD_LIBRARY_PATH', value)
#
"""Append value to PYTHONPATH environment variable"""
def addToPythonPath(self, value):
- self.__addToReserved('PYTHONPATH', value)
- #
-
- """Append value to TCLLIBPATH environment variable"""
- def addToTclLibPath(self, value):
- self.__addToReservedTclTk('TCLLIBPATH', value)
- #
-
- """Append value to TKLIBPATH environment variable"""
- def addToTkLibPath(self, value):
- self.__addToReservedTclTk('TKLIBPATH', value)
+ self.addToEnviron('PYTHONPATH', value)
#
"""Set environment variable to value"""
del os.environ[name]
#
+ """Append value to environment variable"""
+ def addToEnviron(self, name, value, separator=os.pathsep):
+ if value == '':
+ return
+
+ value = os.path.expandvars(value) # expand environment variables
+ self.getLogger().debug("Add to %s: %s", name, value)
+ env = os.getenv(name, None)
+ if env is None:
+ os.environ[name] = value
+ else:
+ os.environ[name] = value + separator + env
+ #
+
###################################
# This begins the private section #
###################################
sys.exit(1)
#
- def __setEnvironmentFromConfigFile(self, filename):
- unsetVars, configVars, reservedDict = parseConfigFile(filename, reserved=['PATH', 'LD_LIBRARY_PATH', 'PYTHONPATH'])
+ def __setEnvironmentFromConfigFile(self, filename, reserved=[]):
+ unsetVars, configVars, reservedDict = parseConfigFile(filename, reserved)
# unset variables
for var in unsetVars:
for reserved in reservedDict:
a = filter(None, reservedDict[reserved]) # remove empty elements
reformattedVals = ':'.join(a)
- self.__addToReserved(reserved, reformattedVals)
+ self.addToEnviron(reserved, reformattedVals)
pass
for key,val in configVars:
sys.path[:0] = os.getenv('PYTHONPATH','').split(':')
#
- def __addToReserved(self, name, value):
- if value == '':
- return
-
- value = os.path.expandvars(value) # expand environment variables
- self.getLogger().debug("Add to %s: %s", name, value)
- env = os.getenv(name, None)
- if env is None:
- os.environ[name] = value
- else:
- os.environ[name] = value + os.pathsep + env
- #
-
- def __addToReservedTclTk(self, name, value):
- if value == '':
- return
-
- value = os.path.expandvars(value) # expand environment variables
- self.getLogger().debug("Add to %s: %s", name, value)
- env = os.getenv(name, None)
- #http://computer-programming-forum.com/57-tcl/1dfddc136afccb94.htm
- #Tcl treats the contents of that variable as a list. Be happy, for you can now use drive letters on windows.
- if env is None:
- os.environ[name] = value
- else:
- os.environ[name] = value + " " + env #explicitely whitespace
- #
-
def _runAppli(self, args=[]):
# Initialize SALOME environment
sys.argv = ['runSalome'] + args