Salome HOME
Pour résoudre un conflit entre les branches master et Windows.
[tools/sat.git] / src / environment.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  CEA/DEN
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
19 import os
20 import subprocess
21 import string
22 import sys
23
24 import src
25
26 class Environ:
27     '''Class to manage the environment context
28     '''
29     def __init__(self, environ=None):
30         '''Initialization. If the environ argument is passed, the environment
31            will be add to it, else it is the external environment.
32            
33         :param environ dict:  
34         '''
35         if environ is not None:
36             self.environ = environ
37         else:
38             self.environ = os.environ
39
40     def __repr__(self):
41         """easy non exhaustive quick resume for debug print
42         """
43         res={}
44         res["environ"]=self.environ
45         return self.__class__.__name__ + str(res)[0:-1] + " ...etc...}"
46
47     def _expandvars(self, value):
48         '''replace some $VARIABLE into its actual value in the environment
49         
50         :param value str: the string to be replaced
51         :return: the replaced variable
52         :rtype: str
53         '''
54         if "$" in value:
55             # The string.Template class is a string class 
56             # for supporting $-substitutions
57             zt = string.Template(value)
58             try:
59                 value = zt.substitute(self.environ)
60             except KeyError as exc:
61                 raise src.SatException(_("Missing definition "
62                                          "in environment: %s") % str(exc))
63         return value
64
65     def append_value(self, key, value, sep=os.pathsep):
66         '''append value to key using sep
67         
68         :param key str: the environment variable to append
69         :param value str: the value to append to key
70         :param sep str: the separator string
71         '''
72         # check if the key is already in the environment
73         if key in self.environ:
74             value_list = self.environ[key].split(sep)
75             # Check if the value is already in the key value or not
76             if not value in value_list:
77                 value_list.append(value)
78             else:
79                 value_list.append(value_list.pop(value_list.index(value)))
80             self.set(key, sep.join(value_list))
81         else:
82             self.set(key, value)
83
84     def append(self, key, value, sep=os.pathsep):
85         '''Same as append_value but the value argument can be a list
86         
87         :param key str: the environment variable to append
88         :param value str or list: the value(s) to append to key
89         :param sep str: the separator string
90         '''
91         if isinstance(value, list):
92             for v in value:
93                 self.append_value(key, v, sep)
94         else:
95             self.append_value(key, value, sep)
96
97     def prepend_value(self, key, value, sep=os.pathsep):
98         '''prepend value to key using sep
99         
100         :param key str: the environment variable to prepend
101         :param value str: the value to prepend to key
102         :param sep str: the separator string
103         '''
104         if key in self.environ:
105             value_list = self.environ[key].split(sep)
106             if not value in value_list:
107                 value_list.insert(0, value)
108             else:
109                 value_list.insert(0, value_list.pop(value_list.index(value)))
110             self.set(key, sep.join(value_list))
111         else:
112             self.set(key, value)
113
114     def prepend(self, key, value, sep=os.pathsep):
115         '''Same as prepend_value but the value argument can be a list
116         
117         :param key str: the environment variable to prepend
118         :param value str or list: the value(s) to prepend to key
119         :param sep str: the separator string
120         '''
121         if isinstance(value, list):
122             for v in value:
123                 self.prepend_value(key, v, sep)
124         else:
125             self.prepend_value(key, value, sep)
126
127     def is_defined(self, key):
128         '''Check if the key exists in the environment
129         
130         :param key str: the environment variable to check
131         '''
132         return key in self.environ.keys()
133
134     def set(self, key, value):
135         '''Set the environment variable "key" to value "value"
136         
137         :param key str: the environment variable to set
138         :param value str: the value
139         '''
140         self.environ[key] = self._expandvars(value)
141
142     def get(self, key):
143         '''Get the value of the environment variable "key"
144         
145         :param key str: the environment variable
146         '''
147         if key in self.environ:
148             return self.environ[key]
149         else:
150             return ""
151
152     def command_value(self, key, command):
153         '''Get the value given by the system command "command" 
154            and put it in the environment variable key
155         
156         :param key str: the environment variable
157         :param command str: the command to execute
158         '''
159         value = subprocess.Popen(command,
160                                  shell=True,
161                                  stdout=subprocess.PIPE,
162                                  env=self.environ).communicate()[0]
163         self.environ[key] = value
164
165
166 class SalomeEnviron:
167     """Class to manage the environment of SALOME.
168     """
169
170     def __init__(self,
171                  cfg,
172                  environ,
173                  forBuild=False,
174                  for_package=None,
175                  enable_simple_env_script = True):
176         '''Initialization.
177
178         :param cfg Config: the global config
179         :param environ Environ: the Environ instance where 
180                                 to store the environment variables
181         :param forBuild bool: If true, it is a launch environment, 
182                               else a build one
183         :param for_package str: If not None, produce a relative environment 
184                                 designed for a package. 
185         '''
186         self.environ = environ
187         self.cfg = cfg
188         self.forBuild = forBuild
189         self.for_package = for_package
190         self.enable_simple_env_script = enable_simple_env_script
191         self.silent = False
192
193     def __repr__(self):
194         """easy non exhaustive quick resume for debug print"""
195         res={}
196         res["environ"]=str(self.environ)
197         res["forBuild"]=self.forBuild
198         return self.__class__.__name__ + str(res)[0:-1] + " ...etc...}"
199
200     def append(self, key, value, sep=os.pathsep):
201         '''append value to key using sep
202         
203         :param key str: the environment variable to append
204         :param value str: the value to append to key
205         :param sep str: the separator string
206         '''
207         return self.environ.append(key, value, sep)
208
209     def prepend(self, key, value, sep=os.pathsep):
210         '''prepend value to key using sep
211         
212         :param key str: the environment variable to prepend
213         :param value str: the value to prepend to key
214         :param sep str: the separator string
215         '''
216         return self.environ.prepend(key, value, sep)
217
218     def is_defined(self, key):
219         '''Check if the key exists in the environment
220         
221         :param key str: the environment variable to check
222         '''
223         return self.environ.is_defined(key)
224
225     def get(self, key):
226         '''Get the value of the environment variable "key"
227         
228         :param key str: the environment variable
229         '''
230         return self.environ.get(key)
231
232     def set(self, key, value):
233         '''Set the environment variable "key" to value "value"
234         
235         :param key str: the environment variable to set
236         :param value str: the value
237         '''
238         # check if value needs to be evaluated
239         if value is not None and value.startswith("`") and value.endswith("`"):
240             res = subprocess.Popen("echo %s" % value,
241                                    shell=True,
242                                    stdout=subprocess.PIPE).communicate()
243             value = res[0].strip()
244
245         return self.environ.set(key, value)
246
247     def dump(self, out):
248         """Write the environment to out
249         
250         :param out file: the stream where to write the environment
251         """
252         for k in self.environ.environ.keys():
253             try:
254                 value = self.get(k)
255             except:
256                 value = "?"
257             out.write("%s=%s\n" % (k, value))
258
259     def add_line(self, nb_line):
260         """Add empty lines to the out stream (in case of file generation)
261         
262         :param nb_line int: the number of empty lines to add
263         """
264         if 'add_line' in dir(self.environ):
265             self.environ.add_line(nb_line)
266
267     def add_comment(self, comment):
268         """Add a commentary to the out stream (in case of file generation)
269         
270         :param comment str: the commentary to add
271         """
272         if 'add_comment' in dir(self.environ):
273             self.environ.add_comment(comment)
274
275     def add_warning(self, warning):
276         """Add a warning to the out stream (in case of file generation)
277         
278         :param warning str: the warning to add
279         """
280         if 'add_warning' in dir(self.environ):
281             self.environ.add_warning(warning)
282
283     def finish(self, required):
284         """Add a final instruction in the out file (in case of file generation)
285         
286         :param required bool: Do nothing if required is False
287         """
288         if 'finish' in dir(self.environ):
289             self.environ.add_line(1)
290             self.environ.add_comment("clean all the path")
291             self.environ.finish(required)
292
293     def set_python_libdirs(self):
294         """Set some generic variables for python library paths
295         """
296         ver = self.get('PYTHON_VERSION')
297         self.set('PYTHON_LIBDIR0', os.path.join('lib',
298                                                 'python' + ver,
299                                                 'site-packages'))
300         self.set('PYTHON_LIBDIR1', os.path.join('lib64',
301                                                 'python' + ver,
302                                                 'site-packages'))
303           
304         self.python_lib0 = self.get('PYTHON_LIBDIR0')
305         self.python_lib1 = self.get('PYTHON_LIBDIR1')
306
307     def get_names(self, lProducts):
308         """Get the products name to add in SALOME_MODULES environment variable
309            It is the name of the product, except in the case where the is a 
310            component name. And it has to be in SALOME_MODULES variable only 
311            if the product has the property has_salome_hui = "yes"
312         
313         :param lProducts list: List of products to potentially add
314         """
315         lProdHasGui = [p for p in lProducts if 'properties' in 
316             src.product.get_product_config(self.cfg, p) and
317             'has_salome_gui' in 
318             src.product.get_product_config(self.cfg, p).properties and
319             src.product.get_product_config(self.cfg,
320                                            p).properties.has_salome_gui=='yes']
321         lProdName = []
322         for ProdName in lProdHasGui:
323             pi = src.product.get_product_config(self.cfg, ProdName)
324             if 'component_name' in pi:
325                 lProdName.append(pi.component_name)
326             else:
327                 lProdName.append(ProdName)
328         return lProdName
329
330     def set_application_env(self, logger):
331         """Sets the environment defined in the APPLICATION file.
332         
333         :param logger Logger: The logger instance to display messages
334         """
335         
336         # Set the variables defined in the "environ" section
337         if 'environ' in self.cfg.APPLICATION:
338             self.add_comment("APPLICATION environment")
339             for p in self.cfg.APPLICATION.environ:
340                 val = self.cfg.APPLICATION.environ[p]
341                 # "_" means that the value must be prepended
342                 if p.startswith("_"):
343                     # separator exception for PV_PLUGIN_PATH
344                     if p[1:] == 'PV_PLUGIN_PATH':
345                         self.prepend(p[1:], val, ';')
346                     else:
347                         self.prepend(p[1:], val)
348                 elif p.endswith("_"):
349                     # separator exception for PV_PLUGIN_PATH
350                     if p[:-1] == 'PV_PLUGIN_PATH':
351                         self.append(p[:-1], val, ';')
352                     else:
353                         self.append(p[:-1], val)
354                 else:
355                     self.set(p, val)
356             self.add_line(1)
357
358         # If there is an "environ_script" section, load the scripts
359         if 'environ_script' in self.cfg.APPLICATION:
360             for pscript in self.cfg.APPLICATION.environ_script:
361                 self.add_comment("script %s" % pscript)
362                 sname = pscript.replace(" ", "_")
363                 self.run_env_script("APPLICATION_%s" % sname,
364                                 self.cfg.APPLICATION.environ_script[pscript],
365                                 logger)
366                 self.add_line(1)
367         
368         # If there is profile (SALOME), then define additional variables
369         if ('profile' in self.cfg.APPLICATION and 
370                                     "product" in self.cfg.APPLICATION.profile):
371             profile_product = self.cfg.APPLICATION.profile.product
372             product_info_profile = src.product.get_product_config(self.cfg,
373                                                             profile_product)
374             profile_share_salome = os.path.join(product_info_profile.install_dir,
375                                                 "share",
376                                                 "salome" )
377             self.set( "SUITRoot", profile_share_salome )
378             self.set( "SalomeAppConfig",
379                       os.path.join(profile_share_salome,
380                                    "resources",
381                                    profile_product.lower() ) )
382         
383
384     def set_salome_minimal_product_env(self, product_info, logger):
385         """Sets the minimal environment for a SALOME product.
386            xxx_ROOT_DIR and xxx_SRC_DIR
387         
388         :param product_info Config: The product description
389         :param logger Logger: The logger instance to display messages        
390         """
391         # set root dir
392         root_dir = product_info.name + "_ROOT_DIR"
393         if not self.is_defined(root_dir):
394             if 'install_dir' in product_info and product_info.install_dir:
395                 self.set(root_dir, product_info.install_dir)
396             elif not self.silent:
397                 logger.write("  " + _("No install_dir for product %s\n") %
398                               product_info.name, 5)
399         
400         if not self.for_package:
401             # set source dir, unless no source dir
402             if not src.product.product_is_fixed(product_info):
403                 src_dir = product_info.name + "_SRC_DIR"
404                 if not self.is_defined(src_dir):
405                     self.set(src_dir, product_info.source_dir)
406
407     def set_salome_generic_product_env(self, pi):
408         """Sets the generic environment for a SALOME product.
409         
410         :param pi Config: The product description
411         """
412         # Construct XXX_ROOT_DIR
413         env_root_dir = self.get(pi.name + "_ROOT_DIR")
414         l_binpath_libpath = []
415
416         # create additional ROOT_DIR for CPP components
417         if 'component_name' in pi:
418             compo_name = pi.component_name
419             if compo_name + "CPP" == pi.name:
420                 compo_root_dir = compo_name + "_ROOT_DIR"
421                 envcompo_root_dir = os.path.join(
422                             self.cfg.TOOLS.common.install_root, compo_name )
423                 self.set(compo_root_dir ,  envcompo_root_dir)
424                 bin_path = os.path.join(envcompo_root_dir, 'bin', 'salome')
425                 lib_path = os.path.join(envcompo_root_dir, 'lib', 'salome')
426                 l_binpath_libpath.append( (bin_path, lib_path) )
427
428         appliname = 'salome'
429         if (src.get_cfg_param(pi, 'product_type', 'SALOME').upper() 
430                             not in [ "SALOME", "SMESH_PLUGIN", "SAMPLE" ]):
431             appliname = ''
432
433         # Construct the paths to prepend to PATH and LD_LIBRARY_PATH and 
434         # PYTHONPATH
435         bin_path = os.path.join(env_root_dir, 'bin', appliname)
436         lib_path = os.path.join(env_root_dir, 'lib', appliname)
437         l_binpath_libpath.append( (bin_path, lib_path) )
438
439         for bin_path, lib_path in l_binpath_libpath:
440             if not self.forBuild:
441                 self.prepend('PATH', bin_path)
442                 if src.architecture.is_windows():
443                     self.prepend('PATH', lib_path)
444                 else :
445                     self.prepend('LD_LIBRARY_PATH', lib_path)
446
447             l = [ bin_path, lib_path,
448                   os.path.join(env_root_dir, self.python_lib0, appliname),
449                   os.path.join(env_root_dir, self.python_lib1, appliname)
450                 ]
451             self.prepend('PYTHONPATH', l)
452
453     def set_cpp_env(self, product_info):
454         """Sets the generic environment for a SALOME cpp product.
455         
456         :param product_info Config: The product description
457         """
458         # Construct XXX_ROOT_DIR
459         env_root_dir = self.get(product_info.name + "_ROOT_DIR")
460         l_binpath_libpath = []
461
462         # Construct the paths to prepend to PATH and LD_LIBRARY_PATH and 
463         # PYTHONPATH
464         bin_path = os.path.join(env_root_dir, 'bin')
465         lib_path = os.path.join(env_root_dir, 'lib')
466         l_binpath_libpath.append( (bin_path, lib_path) )
467
468         for bin_path, lib_path in l_binpath_libpath:
469             if not self.forBuild:
470                 self.prepend('PATH', bin_path)
471                 if src.architecture.is_windows():
472                     self.prepend('PATH', lib_path)
473                 else :
474                     self.prepend('LD_LIBRARY_PATH', lib_path)
475
476             l = [ bin_path, lib_path,
477                   os.path.join(env_root_dir, self.python_lib0),
478                   os.path.join(env_root_dir, self.python_lib1)
479                 ]
480             self.prepend('PYTHONPATH', l)
481
482     def load_cfg_environment(self, cfg_env):
483         """Loads environment defined in cfg_env 
484         
485         :param cfg_env Config: A config containing an environment    
486         """
487         # Loop on cfg_env values
488         for env_def in cfg_env:
489             val = cfg_env[env_def]
490             
491             # if it is env_script, do not do anything (reserved keyword)
492             if env_def == "env_script":
493                 continue
494             
495             # if it is a dict, do not do anything
496             if isinstance(val, src.pyconf.Mapping):
497                 continue
498
499             # if it is a list, loop on its values
500             if isinstance(val, src.pyconf.Sequence):
501                 # transform into list of strings
502                 l_val = []
503                 for item in val:
504                     l_val.append(item)
505                 val = l_val
506
507             # "_" means that the value must be prepended
508             if env_def.startswith("_"):
509                 # separator exception for PV_PLUGIN_PATH
510                 if env_def[1:] == 'PV_PLUGIN_PATH':
511                     self.prepend(env_def[1:], val, ';')
512                 else:
513                     self.prepend(env_def[1:], val)
514             elif env_def.endswith("_"):
515                 # separator exception for PV_PLUGIN_PATH
516                 if env_def[:-1] == 'PV_PLUGIN_PATH':
517                     self.append(env_def[:-1], val, ';')
518                 else:
519                     self.append(env_def[:-1], val)
520             else:
521                 self.set(env_def, val)
522
523     def set_a_product(self, product, logger):
524         """Sets the environment of a product. 
525         
526         :param product str: The product name
527         :param logger Logger: The logger instance to display messages
528         """
529
530         # Get the informations corresponding to the product
531         pi = src.product.get_product_config(self.cfg, product)
532         
533         if self.for_package:
534             pi.install_dir = os.path.join("out_dir_Path",
535                                           self.for_package,
536                                           pi.name)
537
538         if not self.silent:
539             logger.write(_("Setting environment for %s\n") % product, 4)
540
541         self.add_line(1)
542         self.add_comment('setting environ for ' + product)
543             
544         # Do not define environment if the product is native
545         if src.product.product_is_native(pi):
546             if src.product.product_has_env_script(pi):
547                 self.run_env_script(pi, native=True)
548             return
549                
550         # Set an additional environment for SALOME products
551         if src.product.product_is_salome(pi):
552             # set environment using definition of the product
553             self.set_salome_minimal_product_env(pi, logger)
554             self.set_salome_generic_product_env(pi)
555         
556         if src.product.product_is_cpp(pi):
557             # set a specific environment for cpp modules
558             self.set_salome_minimal_product_env(pi, logger)
559             self.set_cpp_env(pi)
560             
561             if src.product.product_is_generated(pi):
562                 if "component_name" in pi:
563                     # hack the source and install directories in order to point  
564                     # on the generated product source install directories
565                     install_dir_save = pi.install_dir
566                     source_dir_save = pi.source_dir
567                     name_save = pi.name
568                     pi.install_dir = os.path.join(self.cfg.APPLICATION.workdir,
569                                                   "INSTALL",
570                                                   pi.component_name)
571                     pi.source_dir = os.path.join(self.cfg.APPLICATION.workdir,
572                                                   "GENERATED",
573                                                   pi.component_name)
574                     pi.name = pi.component_name
575                     self.set_salome_minimal_product_env(pi, logger)
576                     self.set_salome_generic_product_env(pi)
577                     
578                     # Put original values
579                     pi.install_dir = install_dir_save
580                     pi.source_dir = source_dir_save
581                     pi.name = name_save
582         
583         # Put the environment define in the configuration of the product
584         if "environ" in pi:
585             self.load_cfg_environment(pi.environ)
586             if self.forBuild and "build" in pi.environ:
587                 self.load_cfg_environment(pi.environ.build)
588             if not self.forBuild and "launch" in pi.environ:
589                 self.load_cfg_environment(pi.environ.launch)
590             # if product_info defines a env_scripts, load it
591             if 'env_script' in pi.environ:
592                 self.run_env_script(pi, logger)
593
594         
595             
596
597     def run_env_script(self, product_info, logger=None, native=False):
598         """Runs an environment script. 
599         
600         :param product_info Config: The product description
601         :param logger Logger: The logger instance to display messages
602         :param native Boolean: if True load set_native_env instead of set_env
603         """
604         env_script = product_info.environ.env_script
605         # Check that the script exists
606         if not os.path.exists(env_script):
607             raise src.SatException(_("Environment script not found: %s") % 
608                                    env_script)
609
610         if not self.silent and logger is not None:
611             logger.write("  ** load %s\n" % env_script, 4)
612
613         # import the script and run the set_env function
614         try:
615             import imp
616             pyproduct = imp.load_source(product_info.name + "_env_script",
617                                         env_script)
618             if not native:
619                 pyproduct.set_env(self,
620                                   product_info.install_dir,
621                                   product_info.version)
622             else:
623                 if "set_nativ_env" in dir(pyproduct):
624                     pyproduct.set_nativ_env(self)
625         except:
626             __, exceptionValue, exceptionTraceback = sys.exc_info()
627             print(exceptionValue)
628             import traceback
629             traceback.print_tb(exceptionTraceback)
630             traceback.print_exc()
631
632     def run_simple_env_script(self, script_path, logger=None):
633         """Runs an environment script. Same as run_env_script, but with a 
634            script path as parameter.
635         
636         :param script_path str: a path to an environment script
637         :param logger Logger: The logger instance to display messages
638         """
639         if not self.enable_simple_env_script:
640             return
641         # Check that the script exists
642         if not os.path.exists(script_path):
643             raise src.SatException(_("Environment script not found: %s") % 
644                                    script_path)
645
646         if not self.silent and logger is not None:
647             logger.write("  ** load %s\n" % script_path, 4)
648
649         script_basename = os.path.basename(script_path)
650         if script_basename.endswith(".py"):
651             script_basename = script_basename[:-len(".py")]
652
653         # import the script and run the set_env function
654         try:
655             import imp
656             pyproduct = imp.load_source(script_basename + "_env_script",
657                                         script_path)
658             pyproduct.load_env(self)
659         except:
660             __, exceptionValue, exceptionTraceback = sys.exc_info()
661             print(exceptionValue)
662             import traceback
663             traceback.print_tb(exceptionTraceback)
664             traceback.print_exc()
665
666     def set_products(self, logger, src_root=None):
667         """Sets the environment for all the products. 
668         
669         :param logger Logger: The logger instance to display messages
670         :param src_root src: the application working directory
671         """
672         self.add_line(1)
673         self.add_comment('setting environ for all products')
674
675         # Set the application working directory
676         if src_root is None:
677             src_root = self.cfg.APPLICATION.workdir
678         self.set('SRC_ROOT', src_root)
679
680         # SALOME variables
681         appli_name = "APPLI"
682         if "APPLI" in self.cfg and "application_name" in self.cfg.APPLI:
683             appli_name = self.cfg.APPLI.application_name
684         self.set("SALOME_APPLI_ROOT",
685                  os.path.join(self.cfg.APPLICATION.workdir, appli_name))
686
687         # Make sure that the python lib dirs are set after python
688         if "Python" in self.cfg.APPLICATION.products:
689             self.set_a_product("Python", logger)
690             self.set_python_libdirs()
691
692         # The loop on the products
693         for product in self.cfg.APPLICATION.products.keys():
694             if product == "Python":
695                 continue
696             self.set_a_product(product, logger)
697             self.finish(False)
698  
699     def set_full_environ(self, logger, env_info):
700         """Sets the full environment for products 
701            specified in env_info dictionary. 
702         
703         :param logger Logger: The logger instance to display messages
704         :param env_info list: the list of products
705         """
706         # set product environ
707         self.set_application_env(logger)
708
709         self.set_python_libdirs()
710
711         # set products        
712         for product in env_info:
713             self.set_a_product(product, logger)
714
715 class FileEnvWriter:
716     """Class to dump the environment to a file.
717     """
718     def __init__(self, config, logger, out_dir, src_root, env_info=None):
719         '''Initialization.
720
721         :param cfg Config: the global config
722         :param logger Logger: The logger instance to display messages
723         :param out_dir str: The directory path where t put the output files
724         :param src_root str: The application working directory
725         :param env_info str: The list of products to add in the files.
726         '''
727         self.config = config
728         self.logger = logger
729         self.out_dir = out_dir
730         self.src_root= src_root
731         self.silent = True
732         self.env_info = env_info
733
734     def write_env_file(self, filename, forBuild, shell, for_package = None):
735         """Create an environment file.
736         
737         :param filename str: the file path
738         :param forBuild bool: if true, the build environment
739         :param shell str: the type of file wanted (.sh, .bat)
740         :return: The path to the generated file
741         :rtype: str
742         """
743         if not self.silent:
744             self.logger.write(_("Create environment file %s\n") % 
745                               src.printcolors.printcLabel(filename), 3)
746
747         # create then env object
748         env_file = open(os.path.join(self.out_dir, filename), "w")
749         tmp = src.fileEnviron.get_file_environ(env_file,
750                                                shell,
751                                                {})
752         env = SalomeEnviron(self.config, tmp, forBuild, for_package=for_package)
753         env.silent = self.silent
754
755         # Set the environment
756         if self.env_info is not None:
757             env.set_full_environ(self.logger, self.env_info)
758         else:
759             # set env from the APPLICATION
760             env.set_application_env(self.logger)
761             
762             # The list of products to launch
763             lProductsName = env.get_names(self.config.APPLICATION.products.keys())
764             env.set( "SALOME_MODULES",    ','.join(lProductsName))
765             
766             # set the products
767             env.set_products(self.logger,
768                             src_root=self.src_root)
769
770         # add cleanup and close
771         env.finish(True)
772         env_file.close()
773
774         return env_file.name
775    
776     def write_cfgForPy_file(self,
777                             filename,
778                             additional_env = {},
779                             for_package = None,
780                             with_commercial = True):
781         """Append to current opened aFile a cfgForPy 
782            environment (SALOME python launcher).
783            
784         :param filename str: the file path
785         :param additional_env dict: a dictionary of additional variables 
786                                     to add to the environment
787         :param for_package str: If not None, produce a relative environment 
788                                 designed for a package. 
789         """
790         if not self.silent:
791             self.logger.write(_("Create configuration file %s\n") % 
792                               src.printcolors.printcLabel(filename.name), 3)
793
794         # create then env object
795         tmp = src.fileEnviron.get_file_environ(filename, 
796                                                "cfgForPy", 
797                                                {})
798         # environment for launch
799         env = SalomeEnviron(self.config,
800                             tmp,
801                             forBuild=False,
802                             for_package=for_package,
803                             enable_simple_env_script = with_commercial)
804         env.silent = self.silent
805
806         if self.env_info is not None:
807             env.set_full_environ(self.logger, self.env_info)
808         else:
809             # set env from PRODUCT
810             env.set_application_env(self.logger)
811
812             # The list of products to launch
813             lProductsName = env.get_names(self.config.APPLICATION.products.keys())
814             env.set( "SALOME_MODULES",    ','.join(lProductsName))
815
816             # set the products
817             env.set_products(self.logger,
818                             src_root=self.src_root)
819
820         # Add the additional environment if it is not empty
821         if len(additional_env) != 0:
822             for variable in additional_env:
823                 env.set(variable, additional_env[variable])
824
825         # add cleanup and close
826         env.finish(True)
827
828 class Shell:
829     """Definition of a Shell.
830     """
831     def __init__(self, name, extension):
832         '''Initialization.
833
834         :param name str: the shell name
835         :param extension str: the shell extension
836         '''
837         self.name = name
838         self.extension = extension
839
840 def load_environment(config, build, logger):
841     """Loads the environment (used to run the tests, for example).
842     
843     :param config Config: the global config
844     :param build bool: build environement if True
845     :param logger Logger: The logger instance to display messages
846     """
847     environ = SalomeEnviron(config, Environ(os.environ), build)
848     environ.set_application_env(logger)
849     environ.set_products(logger)
850     environ.finish(True)