X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FfileEnviron.py;h=ab79916b5a030c22e9223a32d0d453fd44410260;hb=dd1ed832b516b72b6022734fbf1aacd93906c72c;hp=44963f54f8cb4257dad9f1589176b6ec14f023e0;hpb=a239fbf51d506c6afdd8d5dd11592cb52486f2dd;p=tools%2Fsat.git diff --git a/src/fileEnviron.py b/src/fileEnviron.py index 44963f5..ab79916 100644 --- a/src/fileEnviron.py +++ b/src/fileEnviron.py @@ -25,9 +25,6 @@ bat_header="""\ rem The following variables are used only in case of a sat package set out_dir_Path=%~dp0 -set PRODUCT_OUT_DIR=%out_dir_Path% -set prereq_install_Path=%out_dir_Path%\PREREQUISITES\INSTALL -set prereq_build_Path=%out_dir_Path%\PREREQUISITES\BUILD """ @@ -70,10 +67,8 @@ out_var=`cleandup $xenv $2` export $1=$out_var } -# The 3 following variables are used only in case of a sat package +# This line is used only in case of a sat package export out_dir_Path=$(cd $(dirname ${BASH_SOURCE[0]});pwd) -export PRODUCT_OUT_DIR=${out_dir_Path} -export PRODUCT_ROOT_DIR=${PRODUCT_OUT_DIR} ########################################################################### """ @@ -178,12 +173,16 @@ class FileEnviron(object): def append_value(self, key, value, sep=os.pathsep): """\ - append value to key using sep - + append value to key using sep, + if value contains ":" or ";" then raise error + :param key str: the environment variable to append :param value str: the value to append to key :param sep str: the separator string """ + for c in [";", ":"]: # windows or linux path separators + if c in value: + raise Exception("FileEnviron append key '%s' value '%s' contains forbidden character '%s'" % (key, value, c)) self.set(key, self.get(key) + sep + value) if (key, sep) not in self.toclean: self.toclean.append((key, sep)) @@ -197,18 +196,23 @@ class FileEnviron(object): :param sep str: the separator string """ if isinstance(value, list): - self.append_value(key, sep.join(value), sep) + for v in value: + self.append_value(key, v, sep) else: self.append_value(key, value, sep) def prepend_value(self, key, value, sep=os.pathsep): """\ - prepend value to key using sep + prepend value to key using sep, + if value contains ":" or ";" then raise error :param key str: the environment variable to prepend :param value str: the value to prepend to key :param sep str: the separator string """ + for c in [";", ":"]: # windows or linux path separators + if c in value: + raise Exception("FileEnviron prepend key '%s' value '%s' contains forbidden character '%s'" % (key, value, c)) self.set(key, value + sep + self.get(key)) if (key, sep) not in self.toclean: self.toclean.append((key, sep)) @@ -222,7 +226,8 @@ class FileEnviron(object): :param sep str: the separator string """ if isinstance(value, list): - self.prepend_value(key, sep.join(value), sep) + for v in reversed(value): # prepend list, first item at last to stay first + self.prepend_value(key, v, sep) else: self.prepend_value(key, value, sep) @@ -251,6 +256,13 @@ class FileEnviron(object): """ return '${%s}' % key + def get_value(self, key): + """Get the real value of the environment variable "key" + It can help env scripts + :param key str: the environment variable + """ + return self.environ[key] + def command_value(self, key, command): """\ Get the value given by the system command "command" @@ -536,12 +548,16 @@ class LauncherFileEnviron: self.output.write('# "WARNING %s"\n' % warning) def append_value(self, key, value, sep=":"): - """append value to key using sep + """append value to key using sep, + if value contains ":" or ";" then raise error :param key str: the environment variable to append :param value str: the value to append to key :param sep str: the separator string """ + for c in [";", ":"]: # windows or linux path separators + if c in value: + raise Exception("LauncherFileEnviron append key '%s' value '%s' contains forbidden character '%s'" % (key, value, c)) if self.is_defined(key) : self.add(key, value) else : @@ -555,17 +571,22 @@ class LauncherFileEnviron: :param sep str: the separator string """ if isinstance(value, list): - self.append_value(key, sep.join(value), sep) + for v in value: + self.append_value(key, v, sep) else: self.append_value(key, value, sep) def prepend_value(self, key, value, sep=":"): - """prepend value to key using sep + """prepend value to key using sep, + if value contains ":" or ";" then raise error :param key str: the environment variable to prepend :param value str: the value to prepend to key :param sep str: the separator string """ + for c in [";", ":"]: # windows or linux path separators + if c in value: + raise Exception("LauncherFileEnviron prepend key '%s' value '%s' contains forbidden character '%s'" % (key, value, c)) if self.is_defined(key) : self.add(key, value) else : @@ -579,7 +600,8 @@ class LauncherFileEnviron: :param sep str: the separator string """ if isinstance(value, list): - self.prepend_value(key, sep.join(value), sep) + for v in value: + self.prepend_value(key, v, sep) else: self.prepend_value(key, value, sep) @@ -608,6 +630,13 @@ class LauncherFileEnviron: (key, self.change_to_launcher(value))) self.environ[key] = value + def get_value(self, key): + """Get the real value of the environment variable "key", not ${key} + It can help env scripts + :param key str: the environment variable + """ + return self.environ[key] + def add(self, key, value): """prepend value to key using sep @@ -709,7 +738,7 @@ class ScreenEnviron(FileEnviron): src.printcolors.printcInfo(name), sign, value)) def is_defined(self, name): - return self.defined.has_key(name) + return name in self.defined def get(self, name): return "${%s}" % name @@ -738,7 +767,7 @@ class ScreenEnviron(FileEnviron): # The SALOME launcher template withProfile = """\ - #! /usr/bin/env python +#! /usr/bin/env python ################################################################ # WARNING: this file is automatically generated by SalomeTools # @@ -747,11 +776,12 @@ withProfile = """\ import os import sys +import subprocess # Add the pwdPath to able to run the launcher after unpacking a package # Used only in case of a salomeTools package -out_dir_Path=os.path.abspath(os.path.dirname(__file__)) +out_dir_Path=os.path.dirname(os.path.realpath(__file__)) # Preliminary work to initialize path to SALOME Python modules def __initialize(): @@ -763,11 +793,29 @@ def __initialize(): try: from salomeContextUtils import setOmniOrbUserPath setOmniOrbUserPath() - except Exception, e: - print e + except Exception as e: + print(e) sys.exit(1) # End of preliminary work +# salome doc only works for virtual applications. Therefore we overwrite it with this function +def _showDoc(modules): + for module in modules: + modulePath = os.getenv(module+"_ROOT_DIR") + if modulePath != None: + baseDir = os.path.join(modulePath, "share", "doc", "salome") + docfile = os.path.join(baseDir, "gui", module.upper(), "index.html") + if not os.path.isfile(docfile): + docfile = os.path.join(baseDir, "tui", module.upper(), "index.html") + if not os.path.isfile(docfile): + docfile = os.path.join(baseDir, "dev", module.upper(), "index.html") + if os.path.isfile(docfile): + out, err = subprocess.Popen(["xdg-open", docfile]).communicate() + else: + print "Online documentation is not accessible for module:", module + else: + print module+"_ROOT_DIR not found!" + def main(args): # Identify application path then locate configuration files __initialize() @@ -798,15 +846,15 @@ def main(args): # Logger level error context.getLogger().setLevel(40) - context.setVariable(r"PRODUCT_ROOT_DIR", out_dir_Path, overwrite=True) # here your local standalone environment - # Start SALOME, parsing command line arguments - context.runSalome(args) - #print 'Thank you for using SALOME!' + if len(args) >1 and args[0]=='doc': + _showDoc(args[1:]) + return - # Logger level info - context.getLogger().setLevel(20) + # Start SALOME, parsing command line arguments + out, err, status = context.runSalome(args) + sys.exit(status) except SalomeContextException, e: import logging @@ -844,3 +892,129 @@ if __name__ == "__main__": # """ +withProfile3 = """\ +#! /usr/bin/env python3 + +################################################################ +# WARNING: this file is automatically generated by SalomeTools # +# WARNING: and so could be overwritten at any time. # +################################################################ + +import os +import sys +import subprocess + + +# Add the pwdPath to able to run the launcher after unpacking a package +# Used only in case of a salomeTools package +out_dir_Path=os.path.dirname(os.path.realpath(__file__)) + +# Preliminary work to initialize path to SALOME Python modules +def __initialize(): + + sys.path[:0] = [ 'BIN_KERNEL_INSTALL_DIR' ] + os.environ['ABSOLUTE_APPLI_PATH'] = 'KERNEL_INSTALL_DIR' + + # define folder to store omniorb config (initially in virtual application folder) + try: + from salomeContextUtils import setOmniOrbUserPath + setOmniOrbUserPath() + except Exception as e: + print(e) + sys.exit(1) +# End of preliminary work + +# salome doc only works for virtual applications. Therefore we overwrite it with this function +def _showDoc(modules): + for module in modules: + modulePath = os.getenv(module+"_ROOT_DIR") + if modulePath != None: + baseDir = os.path.join(modulePath, "share", "doc", "salome") + docfile = os.path.join(baseDir, "gui", module.upper(), "index.html") + if not os.path.isfile(docfile): + docfile = os.path.join(baseDir, "tui", module.upper(), "index.html") + if not os.path.isfile(docfile): + docfile = os.path.join(baseDir, "dev", module.upper(), "index.html") + if os.path.isfile(docfile): + out, err = subprocess.Popen(["xdg-open", docfile]).communicate() + else: + print("Online documentation is not accessible for module:", module) + else: + print(module+"_ROOT_DIR not found!") + +def main(args): + # Identify application path then locate configuration files + __initialize() + + if args == ['--help']: + from salomeContext import usage + usage() + sys.exit(0) + + #from salomeContextUtils import getConfigFileNames + #configFileNames, args, unexisting = getConfigFileNames( args, checkExistence=True ) + #if len(unexisting) > 0: + # print("ERROR: unexisting configuration file(s): " + ', '.join(unexisting)) + # sys.exit(1) + + # Create a SalomeContext which parses configFileNames to initialize environment + try: + from salomeContext import SalomeContext, SalomeContextException + SalomeContext.addToSpecial=addToSpecial + context = SalomeContext(None) + + # Here set specific variables, if needed + # context.addToPath('mypath') + # context.addToLdLibraryPath('myldlibrarypath') + # context.addToPythonPath('mypythonpath') + # context.setVariable('myvarname', 'value') + + # Logger level error + context.getLogger().setLevel(40) + + # here your local standalone environment + + if len(args) >1 and args[0]=='doc': + _showDoc(args[1:]) + return + + # Start SALOME, parsing command line arguments + out, err, status = context.runSalome(args) + sys.exit(status) + + except SalomeContextException as e: + import logging + logging.getLogger("salome").error(e) + sys.exit(1) +# +def addToSpecial(self, name, value, pathSep=None): + # add special dangerous cases: TCLLIBPATH PV_PLUGIN_PATH etc... + # http://computer-programming-forum.com/57-tcl/1dfddc136afccb94.htm + # TCLLIBPATH: Tcl treats the contents of that variable as a list. Be happy, for you can now use drive letters on windows. + if value == '': + return + + specialBlanksKeys=["TCLLIBPATH", "TKLIBPATH"] + specialSemicolonKeys=["PV_PLUGIN_PATH"] + res=os.pathsep + if name in specialBlanksKeys: res=" " + if name in specialSemicolonKeys: res=";" + + if pathSep==None: + sep=res + else: + sep=pathSep + 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 + sep + env #explicitely or not special path separator ?whitespace, semicolon? + +if __name__ == "__main__": + args = sys.argv[1:] + main(args) +# +""" +