Salome HOME
add the possibility to bypass the reinitialisation of paths with a dedicated variable...
[tools/sat.git] / src / fileEnviron.py
index 0e4036761c5477109da8e49f1b1f2e8252b4b258..dbf0938545537fbe2e056f8434032a343a34c19e 100644 (file)
 import os
 import pprint as PP
 import src.debug as DBG
-
-bat_header="""\
-@echo off
-
-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
-"""
-
-
-bash_header="""\
-#!/bin/bash
-##########################################################################
-#
-#### cleandup ###
-# cleanup a path (first parameter) from duplicated entries;
-# second parameter is the separator
-cleandup() {
-out_var=`echo $1 | awk -v sep=$2 '{                      \\
-     na = split($1,a,sep);                               \\
-     k=0;                                                \\
-     for(i=0;i<=na;i++) {                                \\
-       found=0;                                          \\
-       for(j=0;j<k;j++) {                                \\
-         if(a[i]==aa[j])                                 \\
-         {                                               \\
-           found=1;                                      \\
-           break;                                        \\
-         };                                              \\
-       };                                                \\
-       if(found==0) {                                    \\
-         aa[k++]=a[i];                                   \\
-       };                                                \\
-     };                                                  \\
-     ORS=sep;                                            \\
-     for(i=0;i<k;i++) {                                  \\
-       print aa[i];                                      \\
-     }                                                   \\
-   }' | sed -e 's|\\(.*\\)$1|\\1|g' -e 's|^[:;]||' -e 's|[:;]$||'`
-echo $out_var
-}
-### clean ###
-clean ()
-{
-xenv=`printenv $1`
-out_var=`cleandup $xenv $2`
-export $1=$out_var
-}
-
-# The 3 following variables are 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}
-
-###########################################################################
-"""
-
-cfg_header="""\
-[SALOME Configuration]
-"""
-
-Launcher_header="""\
-# a generated SALOME Configuration file using python syntax
-"""
+import src.architecture
+import src.environment
 
 def get_file_environ(output, shell, environ=None):
     """Instantiate correct FileEnvironment sub-class.
@@ -93,8 +29,12 @@ def get_file_environ(output, shell, environ=None):
     :param shell str: the type of shell syntax to use.
     :param environ dict: a potential additional environment.
     """
+    if 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":
@@ -112,7 +52,7 @@ class FileEnviron(object):
         Initialization
         
         :param output file: the output file stream.
-        :param environ dict: a potential additional environment.
+        :param environ dict: SalomeEnviron.
         """
         self._do_init(output, environ)
 
@@ -134,15 +74,11 @@ class FileEnviron(object):
         :param environ dict: a potential additional environment.
         """
         self.output = output
-        self.toclean = []
+        self.init_path=True # by default we initialise all paths, except PATH
         if environ is not None:
-            #if str(type(environ)) == "<type 'instance'>":
-            if id(environ) == id(os.environ):
-               DBG.tofix("set %s environ as python os.environ, are you sure it is safe ?" % self.__class__.__name__, True)
             self.environ = environ
         else:
-            DBG.tofix("set %s environ as COPY of python os.environ, are you sure it is safe ?" % self.__class__.__name__, True)
-            self.environ = dict(os.environ) #make a copy cvw 180320
+            self.environ = src.environment.Environ({})
 
     def add_line(self, number):
         """\
@@ -185,12 +121,19 @@ class FileEnviron(object):
         :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))
+        # check that value so no contain the system separator
+        separator=os.pathsep
+        if separator in value:
+            raise Exception("FileEnviron append key '%s' value '%s' contains forbidden character '%s'" % (key, value, separator))
+        do_append=True
+        if self.environ.is_defined(key):
+            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
+            
+        if do_append:
+            self.environ.append_value(key, value,sep)
+            self.set(key, self.get(key) + sep + value)
 
     def append(self, key, value, sep=os.pathsep):
         """\
@@ -215,12 +158,20 @@ class FileEnviron(object):
         :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))
+        # check that value so no contain the system separator
+        separator=os.pathsep
+        if separator in value:
+            raise Exception("FileEnviron append key '%s' value '%s' contains forbidden character '%s'" % (key, value, separator))
+
+        do_not_prepend=False
+        if self.environ.is_defined(key):
+            value_list = self.environ.get(key).split(sep)
+            exp_val=self.environ._expandvars(value)
+            if exp_val in value_list:
+                do_not_prepend=True
+        if not do_not_prepend:
+            self.environ.prepend_value(key, value,sep)
+            self.set(key, value + sep + self.get(key))
 
     def prepend(self, key, value, sep=os.pathsep):
         """\
@@ -242,7 +193,7 @@ class FileEnviron(object):
         
         :param key str: the environment variable to check
         """
-        return (key in self.environ)
+        return self.environ.is_defined(key)
 
     def set(self, key, value):
         """\
@@ -259,37 +210,109 @@ 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"
         It can help env scripts
         :param key str: the environment variable
         """
-        return self.environ[key]
+        return self.environ.get_value(key)
+
+    def finish(self):
+        """Add a final instruction in the out file (in case of file generation)
+        
+        :param required bool: Do nothing if required is False
+        """
+        return
 
-    def command_value(self, key, command):
+    def set_no_init_path(self):
+        """Set the no initialisation mode for all paths.
+           By default only PATH is not reinitialised. All others paths are
+           (LD_LIBRARY_PATH, PYTHONPATH, ...)
+           After the call to these function ALL PATHS ARE NOT REINITIALISED.
+           There initial value is inherited from the environment
+        """
+        self.init_path=False
+
+    def value_filter(self, value):
+        res=value
+        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("<module_name>",
+                                             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 given by the system command "command" 
-        and put it in the environment variable key.
-        Has to be overwritten in the derived classes
-        This can be seen as a virtual method
+        Get the value of the environment variable "key"
         
         :param key str: the environment variable
-        :param command str: the command to execute
         """
-        raise NotImplementedError("command_value is not implement "
-                                  "for this shell!")
+        return self.environ.get(key)
 
-    def finish(self, required=True):
-        """Add a final instruction in the out file (in case of file generation)
+    def append_value(self, key, value, sep=os.pathsep):
+        """append value to key using sep
         
-        :param required bool: Do nothing if required is False
+        :param key str: the environment variable to append
+        :param value str: the value to append to key
+        :param sep str: the separator string
         """
-        for (key, sep) in self.toclean:
-            if sep != ' ':
-                self.output.write('clean %s "%s"\n' % (key, sep))
+        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.
@@ -310,28 +333,9 @@ class BashFileEnviron(FileEnviron):
         :param value str: the value
         """
         self.output.write('export %s="%s"\n' % (key, value))
-        self.environ[key] = value
-
-    def command_value(self, key, command):
-        """\
-        Get the value given by the system command "command" 
-        and put it in the environment variable key.
-        Has to be overwritten in the derived classes
-        This can be seen as a virtual method
+        self.environ.set(key, value)
         
-        :param key str: the environment variable
-        :param command str: the command to execute
-        """
-        self.output.write('export %s=$(%s)\n' % (key, command))
 
-    def finish(self, required=True):
-        """Add a final instruction in the out file (in case of file generation)
-        
-        :param required bool: Do nothing if required is False
-        """
-        if not required:
-            return
-        FileEnviron.finish(self, required)
         
 class BatFileEnviron(FileEnviron):
     """\
@@ -366,30 +370,9 @@ class BatFileEnviron(FileEnviron):
         :param key str: the environment variable to set
         :param value str: the value
         """
-        self.output.write('set %s=%s\n' % (key, value))
-        self.environ[key] = value
-
-    def command_value(self, key, command):
-        """\
-        Get the value given by the system command "command" 
-        and put it in the environment variable key.
-        Has to be overwritten in the derived classes
-        This can be seen as a virtual method
-        
-        :param key str: the environment variable
-        :param command str: the command to execute
-        """
-        self.output.write('%s > tmp.txt\n' % (command))
-        self.output.write('set /p %s =< tmp.txt\n' % (key))
+        self.output.write('set %s=%s\n' % (key, self.value_filter(value)))
+        self.environ.set(key, value)
 
-    def finish(self, required=True):
-        """\
-        Add a final instruction in the out file (in case of file generation)
-        In the particular windows case, do nothing
-        
-        :param required bool: Do nothing if required is False
-        """
-        return
 
 class ContextFileEnviron(FileEnviron):
     """Class for a salome context configuration file.
@@ -410,7 +393,7 @@ class ContextFileEnviron(FileEnviron):
         :param value str: the value
         """
         self.output.write('%s="%s"\n' % (key, value))
-        self.environ[key] = value
+        self.environ.set(key, value)
 
     def get(self, key):
         """Get the value of the environment variable "key"
@@ -419,19 +402,6 @@ class ContextFileEnviron(FileEnviron):
         """
         return '%({0})s'.format(key)
 
-    def command_value(self, key, command):
-        """\
-        Get the value given by the system command "command" 
-        and put it in the environment variable key.
-        Has to be overwritten in the derived classes
-        This can be seen as a virtual method
-        
-        :param key str: the environment variable
-        :param command str: the command to execute
-        """
-        raise NotImplementedError("command_value is not implement "
-                                  "for salome context files!")
-
     def add_echo(self, text):
         """Add a comment
         
@@ -453,7 +423,16 @@ class ContextFileEnviron(FileEnviron):
         :param value str: the value to prepend to key
         :param sep str: the separator string
         """
-        self.output.write('ADD_TO_%s: %s\n' % (key, value))
+        do_append=True
+        if self.environ.is_defined(key):
+            value_list = self.environ.get(key).split(sep)
+            #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)
+            self.output.write('ADD_TO_%s: %s\n' % (key, value))
 
     def append_value(self, key, value, sep=os.pathsep):
         """append value to key using sep
@@ -464,30 +443,8 @@ class ContextFileEnviron(FileEnviron):
         """
         self.prepend_value(key, value)
 
-    def finish(self, required=True):
-        """Add a final instruction in the out file (in case of file generation)
-        
-        :param required bool: Do nothing if required is False
-        """
-        return
 
-def special_path_separator(name):
-    """\
-    TCLLIBPATH, TKLIBPATH, PV_PLUGIN_PATH environments variables need
-    some exotic path separator.
-    This function gives the separator regarding the name of the variable
-    to append or prepend.
-       
-    :param name str: The name of the variable to find the separator
-    """
-    special_blanks_keys=["TCLLIBPATH", "TKLIBPATH"]
-    special_semicolon_keys=["PV_PLUGIN_PATH"]
-    res=os.pathsep
-    if name in special_blanks_keys: res=" "
-    if name in special_semicolon_keys: res=";"
-    return res
-
-class LauncherFileEnviron:
+class LauncherFileEnviron(FileEnviron):
     """\
     Class to generate a launcher file script 
     (in python syntax) SalomeContext API
@@ -498,45 +455,44 @@ class LauncherFileEnviron:
         :param output file: the output file stream.
         :param environ dict: a potential additional environment.
         """
-        self.output = output
-        self.toclean = []
-        if environ is not None:
-            self.environ = environ
-        else:
-            self.environ = os.environ
-        # Initialize some variables
-        if not "PATH" in self.environ.keys():
-            self.environ["PATH"]=""
-        if not "LD_LIBRARY_PATH" in self.environ.keys():
-            self.environ["LD_LIBRARY_PATH"]=""
-        if not "PYTHONPATH" in self.environ.keys():
-            self.environ["PYTHONPATH"]=""
-        if not "TCLLIBPATH" in self.environ.keys():
-            self.environ["TCLLIBPATH"]=""
-        if not "TKLIBPATH" in self.environ.keys():
-            self.environ["TKLIBPATH"]=""
+        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")
 
         # four whitespaces for first indentation in a python script
         self.indent="    "
         self.prefix="context."
         self.setVarEnv="setVariable"
-        
         self.begin=self.indent+self.prefix
-        self.output.write(Launcher_header)
+
+        # write the begining of launcher file.
+        # choose the template version corresponding to python version 
+        # and substitute BIN_KERNEL_INSTALL_DIR (the path to salomeContext.py)
+        if self.python_version == 2:
+            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))
+
+        # for these path, we use specialired functions in salomeContext api
         self.specialKeys={"PATH": "Path",
                           "LD_LIBRARY_PATH": "LdLibraryPath",
                           "PYTHONPATH": "PythonPath"}
 
-    def change_to_launcher(self, value):
-        res=value
-        return res
+        # we do not want to reinitialise PATH.
+        # for that we make sure PATH is in self.environ
+        # and therefore we will not use setVariable for PATH
+        if not self.environ.is_defined("PATH"):
+            self.environ.set("PATH","")
 
-    def add_line(self, number):
-        """Add some empty lines in the launcher file
-        
-        :param number int: the number of lines to add
-        """
-        self.output.write("\n" * number)
+        if self.init_path:
+            self.output.write('\n'+self.indent)
+            self.add_echo("Modifier cette variable pour ne pas réinitialiser les PATHS")
+            self.output.write(self.indent+'reinitialise_paths=True\n\n')
 
     def add_echo(self, text):
         """Add a comment
@@ -552,21 +508,50 @@ class LauncherFileEnviron:
         """
         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
         """
-        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 :
+        # 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))
+
+        is_key_defined=self.environ.is_defined(key)
+        conditional_reinit=False
+        if (self.init_path and (not is_key_defined)):
+            # 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.output.write(self.indent+'if reinitialise_paths:\n'+self.indent)
             self.set(key, value)
+            self.output.write(self.indent+'else:\n'+self.indent)
+            conditional_reinit=True # in this case do not register value in self.environ a second time
+
+        # in all other cases we use append (except if value is already the key
+        do_append=True
+        if is_key_defined:
+            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:
+            if not conditional_reinit:
+                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
@@ -581,7 +566,7 @@ class LauncherFileEnviron:
         else:
             self.append_value(key, value, sep)
 
-    def prepend_value(self, key, value, sep=":"):
+    def prepend_value(self, key, value, sep=os.pathsep):
         """prepend value to key using sep,
         if value contains ":" or ";" then raise error
         
@@ -589,13 +574,43 @@ class LauncherFileEnviron:
         :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 :
+        # 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))
+
+        is_key_defined=self.environ.is_defined(key)
+        conditional_reinit=False
+        if (self.init_path and (not is_key_defined)):
+            # 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.output.write(self.indent+'if reinitialise_paths:\n'+self.indent)
             self.set(key, value)
+            self.output.write(self.indent+'else:\n'+self.indent)
+            conditional_reinit=True # in this case do not register value in self.environ a second time
+
+        # in all other cases we use append (except if value is already the key
+        do_append=True
+        if is_key_defined:
+            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:
+            if not conditional_reinit:
+                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.begin+'addToVariable(r"%s", r"%s",separator="%s")\n' 
+                                  % (key, self.value_filter(value), sep))
+            
 
     def prepend(self, key, value, sep=":"):
         """Same as prepend_value but the value argument can be a list
@@ -610,19 +625,6 @@ class LauncherFileEnviron:
         else:
             self.prepend_value(key, value, sep)
 
-    def is_defined(self, key):
-        """Check if the key exists in the environment
-        
-        :param key str: the environment variable to check
-        """
-        return key in self.environ.keys()
-
-    def get(self, key):
-        """Get the value of the environment variable "key"
-        
-        :param key str: the environment variable
-        """
-        return '${%s}' % key
 
     def set(self, key, value):
         """Set the environment variable "key" to value "value"
@@ -632,75 +634,25 @@ class LauncherFileEnviron:
         """
         self.output.write(self.begin+self.setVarEnv+
                           '(r"%s", r"%s", overwrite=True)\n' % 
-                          (key, self.change_to_launcher(value)))
-        self.environ[key] = value
+                          (key, self.value_filter(value)))
+        self.environ.set(key,value)
     
-    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 add(self, key, value):
-        """prepend value to key using sep
-        
-        :param key str: the environment variable to prepend
-        :param value str: the value to prepend to key
-        """     
-        if key in self.specialKeys.keys():
-            self.output.write(self.begin+'addTo%s(r"%s")\n' % 
-                              (self.specialKeys[key],
-                               self.change_to_launcher(value)))
-            self.environ[key]+=":"+value
-            return
-        sep=special_path_separator(key)
-        self.output.write(self.indent+
-                          '#temporary solution!!! have to be defined in API a '
-                          '?dangerous? addToSpecial(r"%s", r"%s")\n' % 
-                          (key, value))
-        #pathsep not precised because do not know future os launch?
-        self.output.write(self.begin+'addToSpecial(r"%s", r"%s")\n' 
-                          % (key, self.change_to_launcher(value)))
-        self.environ[key]+=sep+value #here yes we know os for current execution
-
-    def command_value(self, key, command):
-        """\
-        Get the value given by the system command "command" 
-        and put it in the environment variable key.
-        
-        :param key str: the environment variable
-        :param command str: the command to execute
-        """
-        self.output.write(self.indent+'#`%s`\n' % command)
-
-        import shlex, subprocess
-        args = shlex.split(command)
-        res=subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-        out, __ = res.communicate()
-        self.output.write(self.begin+
-                          self.setVarEnv+
-                          '(r"%s", r"%s", overwrite=True)\n' % (key, out))
 
     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+
                               "#"+
                               self.prefix+
                               self.setVarEnv+
                               '(r"%s", r"%s", overwrite=True)\n' % 
-                              ('DISTENE_LICENSE_FILE', 
-                               self.change_to_launcher(
-                                            'Use global envvar: DLIM8VAR')))
+                              ('DISTENE_LICENSE_FILE', 'Use global envvar: DLIM8VAR'))
             self.output.write(self.indent+
                               "#"+
                               self.prefix+
                               self.setVarEnv+
                               '(r"%s", r"%s", overwrite=True)\n' % 
-                              ('DLIM8VAR', 
-                               self.change_to_launcher(
-                                                '<your licence>')))
+                              ('DLIM8VAR', '<your licence>'))
             return
         if "setting environ for" in comment:
             self.output.write(self.indent+"#[%s]\n" % 
@@ -709,13 +661,18 @@ class LauncherFileEnviron:
 
         self.output.write(self.indent+"# %s\n" % comment)
 
-    def finish(self, required=True):
+    def finish(self):
         """\
         Add a final instruction in the out file (in case of file generation)
         In the particular launcher case, do nothing
         
         :param required bool: Do nothing if required is False
         """
+        if self.python_version == 2:
+            launcher_tail=launcher_tail_py2
+        else:
+            launcher_tail=launcher_tail_py3
+        self.output.write(launcher_tail)
         return
 
 class ScreenEnviron(FileEnviron):
@@ -764,14 +721,51 @@ class ScreenEnviron(FileEnviron):
         value = self.get(name) + sep + value
         self.write("append", name, value)
 
-    def command_value(self, key, command):
-        pass
-
     def run_env_script(self, module, script):
         self.write("load", script, "", sign="")
 
-# The SALOME launcher template 
-withProfile =  """\
+
+#
+#  Headers
+#
+bat_header="""\
+@echo off
+
+rem The following variables are used only in case of a sat package
+set out_dir_Path=%~dp0
+"""
+
+tcl_header="""\
+#%Module -*- tcl -*-
+#
+# <module_name> 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
+export out_dir_Path=$(cd $(dirname ${BASH_SOURCE[0]});pwd)
+
+###########################################################################
+"""
+
+cfg_header="""\
+[SALOME Configuration]
+"""
+
+launcher_header2="""\
 #! /usr/bin/env python
 
 ################################################################
@@ -782,7 +776,7 @@ withProfile =  """\
 import os
 import sys
 import subprocess
-
+import os.path
 
 # Add the pwdPath to able to run the launcher after unpacking a package
 # Used only in case of a salomeTools package
@@ -791,8 +785,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' ]
-  os.environ['ABSOLUTE_APPLI_PATH'] = 'KERNEL_INSTALL_DIR'
+  sys.path[:0] = [ r'BIN_KERNEL_INSTALL_DIR' ]  # to get salomeContext
   
   # define folder to store omniorb config (initially in virtual application folder)
   try:
@@ -817,9 +810,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
@@ -830,16 +823,10 @@ 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:
     from salomeContext import SalomeContext, SalomeContextException
-    SalomeContext.addToSpecial=addToSpecial
     context = SalomeContext(None)
     
     # Here set specific variables, if needed
@@ -850,55 +837,9 @@ 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
-
-    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, 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)
-#
 """
-    
-withProfile3 =  """\
+
+launcher_header3="""\
 #! /usr/bin/env python3
 
 ################################################################
@@ -909,7 +850,7 @@ withProfile3 =  """\
 import os
 import sys
 import subprocess
-
+import os.path
 
 # Add the pwdPath to able to run the launcher after unpacking a package
 # Used only in case of a salomeTools package
@@ -918,8 +859,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' ]
-  os.environ['ABSOLUTE_APPLI_PATH'] = 'KERNEL_INSTALL_DIR'
+  sys.path[:0] = [ r'BIN_KERNEL_INSTALL_DIR' ]
   
   # define folder to store omniorb config (initially in virtual application folder)
   try:
@@ -966,7 +906,6 @@ def main(args):
   # 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
@@ -977,10 +916,30 @@ def main(args):
 
     # Logger level error
     context.getLogger().setLevel(40)
+"""
+
+launcher_tail_py2="""\
+    #[hook to integrate in launcher additionnal user modules]
+    
+    # Load all files extra.env.d/*.py and call the module's init routine]
+
+    extradir=out_dir_Path + r"/extra.env.d"
 
-    context.setVariable(r"PRODUCT_ROOT_DIR", out_dir_Path, overwrite=True)
-    # here your local standalone environment
+    if os.path.exists(extradir):
+        import imp
+        sys.path.insert(0, os.path.join(os.getcwd(), extradir))
+        for filename in sorted(
+            filter(lambda x: os.path.isfile(os.path.join(extradir, x)),
+                   os.listdir(extradir))):
 
+            if filename.endswith(".py"):
+                f = os.path.join(extradir, filename)
+                module_name = os.path.splitext(os.path.basename(f))[0]
+                fp, path, desc = imp.find_module(module_name)
+                module = imp.load_module(module_name, fp, path, desc)
+                module.init(context, out_dir_Path)
+
+    #[manage salome doc command]
     if len(args) >1 and args[0]=='doc':
         _showDoc(args[1:])
         return
@@ -989,39 +948,82 @@ def main(args):
     out, err, status = context.runSalome(args)
     sys.exit(status)
 
-  except SalomeContextException as e:
+  except SalomeContextException, 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?
+# 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:]
   main(args)
 #
 """
+
+launcher_tail_py3="""\
+    #[hook to integrate in launcher additionnal user modules]
     
+    # Load all files extra.env.d/*.py and call the module's init routine]
+
+    extradir=out_dir_Path + r"/extra.env.d"
+
+    if os.path.exists(extradir):
+        import imp
+        sys.path.insert(0, os.path.join(os.getcwd(), extradir))
+        for filename in sorted(
+            filter(lambda x: os.path.isfile(os.path.join(extradir, x)),
+                   os.listdir(extradir))):
+
+            if filename.endswith(".py"):
+                f = os.path.join(extradir, filename)
+                module_name = os.path.splitext(os.path.basename(f))[0]
+                fp, path, desc = imp.find_module(module_name)
+                module = imp.load_module(module_name, fp, path, desc)
+                module.init(context, out_dir_Path)
+
+    #[manage salome doc command]
+    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)
+# 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:]
+  main(args)
+#
+"""
+    
+