From: aguerre Date: Mon, 14 Oct 2013 11:55:48 +0000 (+0000) Subject: introduce addToEnviron method in API X-Git-Tag: BR_hydro_v_0_3_1~81 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=3f7e7dc30dba928be85bd26cca6aca813596e798;p=modules%2Fkernel.git introduce addToEnviron method in API --- diff --git a/bin/appliskel/salome b/bin/appliskel/salome index 41e0883dc..0f402bf7d 100755 --- a/bin/appliskel/salome +++ b/bin/appliskel/salome @@ -62,19 +62,6 @@ def main(args): # runner.addToPythonPath('mypythonpath') # runner.setEnviron('myvarname', 'value') - kernel_root_dir = os.getenv("KERNEL_ROOT_DIR") - if kernel_root_dir: - runner.addToLdLibraryPath(os.path.join(kernel_root_dir, "lib/salome")) - - gui_root_dir = os.getenv("GUI_ROOT_DIR") - if gui_root_dir: - runner.addToLdLibraryPath(os.path.join(gui_root_dir, "lib/salome")) - - paravis_root_dir = os.getenv("PARAVIS_ROOT_DIR") - if paravis_root_dir: - runner.addToLdLibraryPath(os.path.join(paravis_root_dir, "lib/salome")) - - # Start SALOME, parsing command line arguments runner.go(args) print 'Thank you for using SALOME!' diff --git a/bin/parseConfigFile.py b/bin/parseConfigFile.py index 3511bbe97..8dd21189f 100644 --- a/bin/parseConfigFile.py +++ b/bin/parseConfigFile.py @@ -138,10 +138,10 @@ def parseConfigFile(filename, reserved = []): 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 @@ -261,16 +261,28 @@ class EnvFileConverter(object): 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) @@ -278,10 +290,16 @@ class EnvFileConverter(object): 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 @@ -309,10 +327,8 @@ class EnvFileConverter(object): # # 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' diff --git a/bin/salomeRunner.py b/bin/salomeRunner.py index 512f40730..c80c4f316 100644 --- a/bin/salomeRunner.py +++ b/bin/salomeRunner.py @@ -58,17 +58,18 @@ class SalomeRunner: 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 @@ -90,27 +91,17 @@ class SalomeRunner: """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""" @@ -134,6 +125,20 @@ class SalomeRunner: 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 # ################################### @@ -197,8 +202,8 @@ class SalomeRunner: 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: @@ -208,7 +213,7 @@ class SalomeRunner: 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: @@ -218,34 +223,6 @@ class SalomeRunner: 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