X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FfileEnviron.py;h=3fe41c87945290dc5524abfb57bf98f015cc91e8;hb=28cebd157f9d39920d0480232e7c361716ca45bb;hp=5b293ffcb8ed948cfae504ba6d6b3d1ccc9f3a83;hpb=28b1fb5c2f0491c4d881d0ccc3b8912fa41018ca;p=tools%2Fsat.git diff --git a/src/fileEnviron.py b/src/fileEnviron.py index 5b293ff..3fe41c8 100644 --- a/src/fileEnviron.py +++ b/src/fileEnviron.py @@ -33,6 +33,8 @@ def get_file_environ(output, shell, environ=None): environ=src.environment.Environ({}) if shell == "bash": return BashFileEnviron(output, environ) + if shell == "tcl": + return TclFileEnviron(output, environ) if shell == "bat": return BatFileEnviron(output, environ) if shell == "cfgForPy": @@ -128,7 +130,6 @@ class FileEnviron(object): value_list = self.environ.get(key).split(sep) if self.environ._expandvars(value) in value_list: do_append=False # value is already in key path : we don't append it again - #print "\nCNC append_value value is already set!! key=%s, val=%s value_list=%s\n" % (key,self.environ._expandvars(value), value_list) if do_append: self.environ.append_value(key, value,sep) @@ -168,7 +169,6 @@ class FileEnviron(object): exp_val=self.environ._expandvars(value) if exp_val in value_list: do_not_prepend=True - #print "\nCNC prepend_value value is already set!! key=%s, val=%s value_list=%s\n" % (key,exp_val, value_list) if not do_not_prepend: self.environ.prepend_value(key, value,sep) self.set(key, value + sep + self.get(key)) @@ -210,7 +210,10 @@ class FileEnviron(object): :param key str: the environment variable """ - return '${%s}' % key + if src.architecture.is_windows(): + return '%' + key + '%' + else: + return '${%s}' % key def get_value(self, key): """Get the real value of the environment variable "key" @@ -237,12 +240,79 @@ class FileEnviron(object): def value_filter(self, value): res=value - # on windows platform, replace / by \ - if src.architecture.is_windows(): - res = value.replace("/","\\") return res +class TclFileEnviron(FileEnviron): + """\ + Class for tcl shell. + """ + def __init__(self, output, environ=None): + """Initialization + + :param output file: the output file stream. + :param environ dict: a potential additional environment. + """ + self._do_init(output, environ) + self.output.write(tcl_header.replace("", + self.environ.get("sat_product_name"))) + self.output.write("\nset software %s\n" % self.environ.get("sat_product_name") ) + self.output.write("set version %s\n" % self.environ.get("sat_product_version") ) + root=os.path.join(self.environ.get("sat_product_base_path"), + "apps", + self.environ.get("sat_product_base_name"), + "$software", + "$version") + self.output.write("set root %s\n" % root) + modules_to_load=self.environ.get("sat_product_load_depend") + if len(modules_to_load)>0: + # write module load commands for product dependencies + self.output.write("\n") + for module_to_load in modules_to_load.split(";"): + self.output.write(module_to_load+"\n") + + def set(self, key, value): + """Set the environment variable "key" to value "value" + + :param key str: the environment variable to set + :param value str: the value + """ + self.output.write('setenv %s "%s"\n' % (key, value)) + self.environ.set(key, value) + + def get(self, key): + """\ + Get the value of the environment variable "key" + + :param key str: the environment variable + """ + return self.environ.get(key) + + def append_value(self, key, value, sep=os.pathsep): + """append value to key using sep + + :param key str: the environment variable to append + :param value str: the value to append to key + :param sep str: the separator string + """ + if sep==os.pathsep: + self.output.write('append-path %s %s\n' % (key, value)) + else: + self.output.write('append-path --delim=\%c %s %s\n' % (sep, key, value)) + + def prepend_value(self, key, value, sep=os.pathsep): + """prepend value to key using sep + + :param key str: the environment variable to prepend + :param value str: the value to prepend to key + :param sep str: the separator string + """ + if sep==os.pathsep: + self.output.write('prepend-path %s %s\n' % (key, value)) + else: + self.output.write('prepend-path --delim=\%c %s %s\n' % (sep, key, value)) + + class BashFileEnviron(FileEnviron): """\ Class for bash shell. @@ -388,7 +458,6 @@ class LauncherFileEnviron(FileEnviron): self._do_init(output, environ) self.python_version=self.environ.get("sat_python_version") self.bin_kernel_root_dir=self.environ.get("sat_bin_kernel_install_dir") - self.app_root_dir=self.environ.get("sat_app_root_dir") # four whitespaces for first indentation in a python script self.indent=" " @@ -403,6 +472,9 @@ class LauncherFileEnviron(FileEnviron): launcher_header=launcher_header2 else: launcher_header=launcher_header3 + # in case of Windows OS, Python scripts are not executable. PyExe ? + if src.architecture.is_windows(): + launcher_header = launcher_header.replace("#! /usr/bin/env python3",'') self.output.write(launcher_header\ .replace("BIN_KERNEL_INSTALL_DIR", self.bin_kernel_root_dir)) @@ -432,16 +504,45 @@ class LauncherFileEnviron(FileEnviron): """ self.output.write('# "WARNING %s"\n' % warning) - def append_value(self, key, value, sep=":"): + def append_value(self, key, value, sep=os.pathsep): """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 key str: the environment variable to prepend + :param value str: the value to prepend to key :param sep str: the separator string """ - # append is not defined in context api - self.prepend_value(key, value) + # check that value so no contain the system separator + separator=os.pathsep + msg="LauncherFileEnviron append key '%s' value '%s' contains forbidden character '%s'" + if separator in value: + raise Exception(msg % (key, value, separator)) + + if (self.init_path and (not self.environ.is_defined(key))): + # reinitialisation mode set to true (the default) + # for the first occurrence of key, we set it. + # therefore key will not be inherited from environment + self.set(key, value) + return + + # in all other cases we use append (except if value is already the key + do_append=True + if self.environ.is_defined(key): + value_list = self.environ.get(key).split(sep) + # rem : value cannot be expanded (unlike bash/bat case) - but it doesn't matter. + if value in value_list: + do_append=False # value is already in key path : we don't append it again + + if do_append: + self.environ.append_value(key, value,sep) # register value in self.environ + if key in self.specialKeys.keys(): + #for these special keys we use the specific salomeContext function + self.output.write(self.begin+'addTo%s(r"%s")\n' % + (self.specialKeys[key], self.value_filter(value))) + else: + # else we use the general salomeContext addToVariable function + self.output.write(self.indent+'appendPath(r"%s", r"%s",separator="%s")\n' + % (key, self.value_filter(value), sep)) def append(self, key, value, sep=":"): """Same as append_value but the value argument can be a list @@ -523,7 +624,7 @@ class LauncherFileEnviron(FileEnviron): def add_comment(self, comment): - # Special comment in case of the distène licence + # Special comment in case of the DISTENE licence if comment=="DISTENE license": self.output.write(self.indent+ "#"+ @@ -619,9 +720,24 @@ rem The following variables are used only in case of a sat package set out_dir_Path=%~dp0 """ +tcl_header="""\ +#%Module -*- tcl -*- +# +# module for use with 'environment-modules' package +# +""" bash_header="""\ #!/bin/bash +if [ "$BASH" = "" ] +then + # check that the user is not using another shell + echo + echo "Warning! SALOME environment not initialized" + echo "You must run this script in a bash shell." + echo "As you are using another shell. Please first run: bash" + echo +fi ########################################################################## # # This line is used only in case of a sat package @@ -654,7 +770,7 @@ 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' ] # to get salomeContext + sys.path[:0] = [ r'BIN_KERNEL_INSTALL_DIR' ] # to get salomeContext # define folder to store omniorb config (initially in virtual application folder) try: @@ -679,9 +795,9 @@ def _showDoc(modules): if os.path.isfile(docfile): out, err = subprocess.Popen(["xdg-open", docfile]).communicate() else: - print "Online documentation is not accessible for module:", module + print ("Online documentation is not accessible for module:", module) else: - print module+"_ROOT_DIR not found!" + print (module+"_ROOT_DIR not found!") def main(args): # Identify application path then locate configuration files @@ -692,11 +808,6 @@ def main(args): 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: @@ -733,7 +844,7 @@ 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' ] + sys.path[:0] = [ r'BIN_KERNEL_INSTALL_DIR' ] # define folder to store omniorb config (initially in virtual application folder) try: @@ -806,6 +917,18 @@ launcher_tail_py2="""\ logging.getLogger("salome").error(e) sys.exit(1) # +# salomeContext only prepend variables, we use our own appendPath when required +def appendPath(name, value, separator=os.pathsep): + if value == '': + return + + value = os.path.expandvars(value) # expand environment variables + env = os.getenv(name, None) + if env is None: + os.environ[name] = value + else: + os.environ[name] = env + separator + value + if __name__ == "__main__": args = sys.argv[1:] @@ -826,7 +949,19 @@ launcher_tail_py3="""\ import logging logging.getLogger("salome").error(e) sys.exit(1) -# + +# salomeContext only prepend variables, we use our own appendPath when required +def appendPath(name, value, separator=os.pathsep): + if value == '': + return + + value = os.path.expandvars(value) # expand environment variables + env = os.getenv(name, None) + if env is None: + os.environ[name] = value + else: + os.environ[name] = env + separator + value + if __name__ == "__main__": args = sys.argv[1:]