3 # Copyright (C) 2010-2013 CEA/DEN
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.
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.
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
26 import src.debug as DBG
32 Class to manage the environment context
34 def __init__(self, environ=None):
35 """Initialization. If the environ argument is passed, the environment
36 will be add to it, else it is the external environment.
40 if environ is not None:
41 self.environ = environ
43 self.environ = os.environ
46 """easy non exhaustive quick resume for debug print"""
47 return "%s(\n%s\n)" % (self.__class__.__name__, PP.pformat(self.environ))
49 def _expandvars(self, value):
51 replace some $VARIABLE into its actual value in the environment
53 :param value str: the string to be replaced
54 :return: the replaced variable
58 # The string.Template class is a string class
59 # for supporting $-substitutions
60 zt = string.Template(value)
62 value = zt.substitute(self.environ)
63 except KeyError as exc:
64 raise src.SatException(_("Missing definition "
65 "in environment: %s") % str(exc))
68 def append_value(self, key, value, sep=os.pathsep):
70 append value to key using sep,
71 if value contains ":" or ";" then raise error
73 :param key str: the environment variable to append
74 :param value str: the value to append to key
75 :param sep str: the separator string
77 # check that value so no contain the system separator
79 if separator in value:
80 raise Exception("Environ append key '%s' value '%s' contains forbidden character '%s'" % (key, value, separator))
82 # check if the key is already in the environment
83 if key in self.environ:
84 value_list = self.environ[key].split(sep)
85 # Check if the value is already in the key value or not
86 if not value in value_list:
87 value_list.append(value)
89 value_list.append(value_list.pop(value_list.index(value)))
90 self.set(key, sep.join(value_list))
94 def append(self, key, value, sep=os.pathsep):
96 Same as append_value but the value argument can be a list
98 :param key str: the environment variable to append
99 :param value str or list: the value(s) to append to key
100 :param sep str: the separator string
102 if isinstance(value, list):
104 self.append_value(key, v, sep)
106 self.append_value(key, value, sep)
108 def prepend_value(self, key, value, sep=os.pathsep):
110 prepend value to key using sep,
111 if value contains ":" or ";" then raise error
113 :param key str: the environment variable to prepend
114 :param value str: the value to prepend to key
115 :param sep str: the separator string
117 # check that value so no contain the system separator
119 if separator in value:
120 raise Exception("Environ append key '%s' value '%s' contains forbidden character '%s'" % (key, value, separator))
122 # check if the key is already in the environment
123 if key in self.environ:
124 value_list = self.environ[key].split(sep)
125 if not value in value_list:
126 value_list.insert(0, value)
128 value_list.insert(0, value_list.pop(value_list.index(value)))
129 self.set(key, sep.join(value_list))
133 def prepend(self, key, value, sep=os.pathsep):
135 Same as prepend_value but the value argument can be a list
137 :param key str: the environment variable to prepend
138 :param value str or list: the value(s) to prepend to key
139 :param sep str: the separator string
141 if isinstance(value, list):
142 for v in reversed(value): # prepend list, first item at last to stay first
143 self.prepend_value(key, v, sep)
145 self.prepend_value(key, value, sep)
147 def is_defined(self, key):
149 Check if the key exists in the environment
151 :param key str: the environment variable to check
153 return key in self.environ.keys()
155 def set(self, key, value):
157 Set the environment variable "key" to value "value"
159 :param key str: the environment variable to set
160 :param value str: the value
162 self.environ[key] = self._expandvars(value)
166 Get the value of the environment variable "key"
168 :param key str: the environment variable
170 if key in self.environ:
171 return self.environ[key]
175 def get_value(self, key):
177 Get the value of the environment variable "key"
178 This method is added for API compatibility with FileEnviron class
180 :param key str: the environment variable
188 Class to manage the environment of SALOME.
195 enable_simple_env_script = True):
199 :param cfg Config: the global config
200 :param environ Environ: the Environ instance where
201 to store the environment variables
202 :param forBuild bool: If true, it is a launch environment,
204 :param for_package str: If not None, produce a relative environment
205 designed for a package.
207 self.environ = environ
209 self.forBuild = forBuild
210 self.for_package = for_package
211 self.enable_simple_env_script = enable_simple_env_script
213 self.has_python = False
216 """easy almost exhaustive quick resume for debug print"""
218 "environ" : self.environ,
219 "forBuild" : self.forBuild,
220 "for_package" : self.for_package,
222 return "%s(\n%s\n)" % (self.__class__.__name__, PP.pformat(res))
224 def append(self, key, value, sep=os.pathsep):
226 append value to key using sep
228 :param key str: the environment variable to append
229 :param value str: the value to append to key
230 :param sep str: the separator string
232 return self.environ.append(key, value, sep)
234 def prepend(self, key, value, sep=os.pathsep):
236 prepend value to key using sep
238 :param key str: the environment variable to prepend
239 :param value str: the value to prepend to key
240 :param sep str: the separator string
242 return self.environ.prepend(key, value, sep)
244 def is_defined(self, key):
246 Check if the key exists in the environment
248 :param key str: the environment variable to check
250 return self.environ.is_defined(key)
254 Get the value of the environment variable "key"
256 :param key str: the environment variable
258 return self.environ.get(key)
260 def get_value(self, key):
262 Get the real value of the environment variable "key"
263 This method is added for API compatibility with FileEnviron class
265 :param key str: the environment variable
267 if key in self.environ:
268 return self.environ[key]
272 def set(self, key, value):
274 Set the environment variable "key" to value "value"
276 :param key str: the environment variable to set
277 :param value str: the value
279 # check if value needs to be evaluated
280 if value is not None and value.startswith("`") and value.endswith("`"):
281 res = subprocess.Popen("echo %s" % value,
283 stdout=subprocess.PIPE).communicate()
284 value = res[0].strip()
286 return self.environ.set(key, value)
290 Write the environment to out
292 :param out file: the stream where to write the environment
294 for k in self.environ.environ.keys():
299 out.write("%s=%s\n" % (k, value))
301 def add_line(self, nb_line):
303 Add empty lines to the out stream (in case of file generation)
305 :param nb_line int: the number of empty lines to add
307 if 'add_line' in dir(self.environ):
308 self.environ.add_line(nb_line)
310 def add_comment(self, comment):
312 Add a commentary to the out stream (in case of file generation)
314 :param comment str: the commentary to add
316 if 'add_comment' in dir(self.environ):
317 self.environ.add_comment(comment)
319 def add_warning(self, warning):
321 Add a warning to the out stream (in case of file generation)
323 :param warning str: the warning to add
325 if 'add_warning' in dir(self.environ):
326 self.environ.add_warning(warning)
330 Add a final instruction in the out file (in case of file generation)
332 :param required bool: Do nothing if required is False
334 if 'finish' in dir(self.environ):
335 self.environ.add_line(1)
337 # self.environ.add_comment("clean all the path")
338 self.environ.finish()
340 def set_python_libdirs(self):
341 """Set some generic variables for python library paths"""
342 ver = self.get('PYTHON_VERSION')
343 self.set('PYTHON_LIBDIR', os.path.join('lib',
346 self.python_lib = self.get('PYTHON_LIBDIR')
347 self.has_python = True
349 def get_names(self, lProducts):
351 Get the products name to add in SALOME_MODULES environment variable
352 It is the name of the product, except in the case where the is a
353 component name. And it has to be in SALOME_MODULES variable only
354 if the product has the property has_salome_hui = "yes"
356 :param lProducts list: List of products to potentially add
358 lProdHasGui = [p for p in lProducts if 'properties' in
359 src.product.get_product_config(self.cfg, p) and
361 src.product.get_product_config(self.cfg, p).properties and
362 src.product.get_product_config(self.cfg,
363 p).properties.has_salome_gui=='yes']
365 for ProdName in lProdHasGui:
366 pi = src.product.get_product_config(self.cfg, ProdName)
367 if 'component_name' in pi:
368 lProdName.append(pi.component_name)
370 lProdName.append(ProdName)
373 def set_application_env(self, logger):
375 Sets the environment defined in the APPLICATION file.
377 :param logger Logger: The logger instance to display messages
381 self.set("PRODUCT_ROOT_DIR", "out_dir_Path")
383 self.cfg.APPLICATION.environ.PRODUCT_ROOT_DIR = src.pyconf.Reference(self.cfg, src.pyconf.DOLLAR, "workdir")
385 # these sensitive variables are reset to avoid bad environment interactions
387 self.add_comment("reset these sensitive variables to avoid bad environment interactions")
388 self.add_comment("comment these to lines if you wish a different behaviour")
389 self.set("LD_LIBRARY_PATH", "")
390 self.set("PYTHONPATH", "")
393 # Set the variables defined in the "environ" section
394 if 'environ' in self.cfg.APPLICATION:
395 # we write PRODUCT environment it in order to conform to
397 self.add_comment("PRODUCT environment")
398 self.load_cfg_environment(self.cfg.APPLICATION.environ)
399 if self.forBuild and "build" in self.cfg.APPLICATION.environ:
400 self.load_cfg_environment(self.cfg.APPLICATION.environ.build)
401 if not self.forBuild and "launch" in self.cfg.APPLICATION.environ:
402 self.load_cfg_environment(self.cfg.APPLICATION.environ.launch)
406 def set_salome_minimal_product_env(self, product_info, logger):
408 Sets the minimal environment for a SALOME product.
409 xxx_ROOT_DIR and xxx_SRC_DIR
411 :param product_info Config: The product description
412 :param logger Logger: The logger instance to display messages
415 DBG.write("set_salome_minimal_product_env", product_info)
416 root_dir = product_info.name + "_ROOT_DIR"
417 if not self.is_defined(root_dir):
418 if 'install_dir' in product_info and product_info.install_dir:
419 self.set(root_dir, product_info.install_dir)
420 elif not self.silent:
421 logger.write(" " + _("No install_dir for product %s\n") %
422 product_info.name, 5)
424 source_in_package = src.get_property_in_product_cfg(product_info,
425 "sources_in_package")
426 if not self.for_package or source_in_package == "yes":
427 # set source dir, unless no source dir
428 if not src.product.product_is_fixed(product_info):
429 src_dir = product_info.name + "_SRC_DIR"
430 if not self.is_defined(src_dir):
431 if not self.for_package:
432 self.set(src_dir, product_info.source_dir)
434 self.set(src_dir, os.path.join("out_dir_Path",
436 os.path.basename(product_info.source_dir)))
438 def set_salome_generic_product_env(self, pi):
440 Sets the generic environment for a SALOME product.
442 :param pi Config: The product description
444 # Construct XXX_ROOT_DIR
445 env_root_dir = self.get(pi.name + "_ROOT_DIR")
446 l_binpath_libpath = []
448 # create additional ROOT_DIR for CPP components
449 if 'component_name' in pi:
450 compo_name = pi.component_name
451 if compo_name + "CPP" == pi.name:
452 compo_root_dir = compo_name + "_ROOT_DIR"
453 envcompo_root_dir = os.path.join(
454 self.cfg.TOOLS.common.install_root, compo_name )
455 self.set(compo_root_dir , envcompo_root_dir)
456 bin_path = os.path.join(envcompo_root_dir, 'bin', 'salome')
457 lib_path = os.path.join(envcompo_root_dir, 'lib', 'salome')
458 l_binpath_libpath.append( (bin_path, lib_path) )
461 if src.get_property_in_product_cfg(pi, "fhs"):
462 lib_path = os.path.join(env_root_dir, 'lib')
463 bin_path = os.path.join(env_root_dir, 'bin')
465 # if the application doesn't include python, we don't need these two lines
466 pylib_path = os.path.join(env_root_dir, self.python_lib)
468 lib_path = os.path.join(env_root_dir, 'lib', 'salome')
469 bin_path = os.path.join(env_root_dir, 'bin', 'salome')
471 # if the application doesn't include python, we don't need these two lines
472 pylib_path = os.path.join(env_root_dir, self.python_lib, 'salome')
474 # Construct the paths to prepend to PATH and LD_LIBRARY_PATH and
476 l_binpath_libpath.append( (bin_path, lib_path) )
478 for bin_path, lib_path in l_binpath_libpath:
479 if not self.forBuild:
480 self.prepend('PATH', bin_path)
481 if src.architecture.is_windows():
482 self.prepend('PATH', lib_path)
484 self.prepend('LD_LIBRARY_PATH', lib_path)
486 l = [ bin_path, lib_path ]
487 if not src.product.product_is_wheel(pi):
490 self.prepend('PYTHONPATH', l)
492 def set_cpp_env(self, product_info):
494 Sets the generic environment for a SALOME cpp product.
496 :param product_info Config: The product description
498 # Construct XXX_ROOT_DIR
499 env_root_dir = self.get(product_info.name + "_ROOT_DIR")
500 l_binpath_libpath = []
502 # Construct the paths to prepend to PATH and LD_LIBRARY_PATH and
504 bin_path = os.path.join(env_root_dir, 'bin')
505 lib_path = os.path.join(env_root_dir, 'lib')
506 l_binpath_libpath.append( (bin_path, lib_path) )
508 for bin_path, lib_path in l_binpath_libpath:
509 if not self.forBuild:
510 self.prepend('PATH', bin_path)
511 if src.architecture.is_windows():
512 self.prepend('PATH', lib_path)
514 self.prepend('LD_LIBRARY_PATH', lib_path)
516 l = [ bin_path, lib_path ]
518 l.append(os.path.join(env_root_dir, self.python_lib))
519 self.prepend('PYTHONPATH', l)
521 def load_cfg_environment(self, cfg_env):
523 Loads environment defined in cfg_env
525 :param cfg_env Config: A config containing an environment
527 # Loop on cfg_env values
528 for env_def in cfg_env:
529 val = cfg_env[env_def]
531 # if it is env_script, do not do anything (reserved keyword)
532 if env_def == "env_script":
535 # if it is a dict, do not do anything
536 if isinstance(val, src.pyconf.Mapping):
539 # if it is a list, loop on its values
540 if isinstance(val, src.pyconf.Sequence):
541 # transform into list of strings
547 # "_" means that the value must be prepended
548 if env_def.startswith("_"):
549 # separator exception for PV_PLUGIN_PATH
550 if env_def[1:] == 'PV_PLUGIN_PATH':
551 self.prepend(env_def[1:], val, ';')
553 self.prepend(env_def[1:], val)
554 elif env_def.endswith("_"):
555 # separator exception for PV_PLUGIN_PATH
556 if env_def[:-1] == 'PV_PLUGIN_PATH':
557 self.append(env_def[:-1], val, ';')
559 self.append(env_def[:-1], val)
561 self.set(env_def, val)
563 def set_a_product(self, product, logger):
565 Sets the environment of a product.
567 :param product str: The product name
568 :param logger Logger: The logger instance to display messages
571 # Get the informations corresponding to the product
572 pi = src.product.get_product_config(self.cfg, product)
574 # skip compile time products at run time
575 if not self.forBuild:
576 if src.product.product_is_compile_time(pi):
579 # skip pip products when pip is activated and installation is done in python
580 #if (src.appli_test_property(self.cfg,"pip", "yes") and
581 # src.product.product_test_property(pi,"pip", "yes") and
582 # src.appli_test_property(self.cfg,"pip_install_dir", "python") ):
585 # skip mesa products (if any) at run time,
586 # unless use_mesa property was activated
587 if not self.forBuild:
588 if not ("APPLICATION" in self.cfg and
589 "properties" in self.cfg.APPLICATION and
590 "use_mesa" in self.cfg.APPLICATION.properties and
591 self.cfg.APPLICATION.properties.use_mesa == "yes") :
592 if ("properties" in pi and
593 "is_mesa" in pi.properties and
594 pi.properties.is_mesa == "yes") :
595 logger.write(_("Skip mesa product %s\n") % pi.name, 4)
600 pi.install_dir = os.path.join(
603 os.path.basename(pi.install_dir))
606 logger.write(_("Setting environment for %s\n") % product, 4)
609 self.add_comment('setting environ for ' + product)
611 # Do not define environment if the product is native
612 if src.product.product_is_native(pi):
613 if src.product.product_has_env_script(pi):
614 self.run_env_script(pi, native=True)
617 # Set an additional environment for SALOME products
618 if src.product.product_is_salome(pi):
619 # set environment using definition of the product
620 self.set_salome_minimal_product_env(pi, logger)
621 self.set_salome_generic_product_env(pi)
623 # use variable LICENCE_FILE to communicate the licence file name to the environment script
624 licence_file_name = src.product.product_has_licence(pi, self.cfg.PATHS.LICENCEPATH)
625 if licence_file_name:
626 logger.write("licence file found for product %s : %s\n" % (pi.name, licence_file_name), 5)
627 self.set("LICENCE_FILE", licence_file_name)
629 if src.product.product_is_cpp(pi):
630 # set a specific environment for cpp modules
631 self.set_salome_minimal_product_env(pi, logger)
634 if src.product.product_is_generated(pi):
635 if "component_name" in pi:
636 # hack the source and install directories in order to point
637 # on the generated product source install directories
638 install_dir_save = pi.install_dir
639 source_dir_save = pi.source_dir
641 pi.install_dir = os.path.join(self.cfg.APPLICATION.workdir,
642 self.cfg.INTERNAL.config.install_dir,
645 pi.install_dir = os.path.join("out_dir_Path",
648 pi.source_dir = os.path.join(self.cfg.APPLICATION.workdir,
651 pi.name = pi.component_name
652 self.set_salome_minimal_product_env(pi, logger)
653 self.set_salome_generic_product_env(pi)
655 # Put original values
656 pi.install_dir = install_dir_save
657 pi.source_dir = source_dir_save
660 # Put the environment define in the configuration of the product
662 self.load_cfg_environment(pi.environ)
663 if self.forBuild and "build" in pi.environ:
664 self.load_cfg_environment(pi.environ.build)
665 if not self.forBuild and "launch" in pi.environ:
666 self.load_cfg_environment(pi.environ.launch)
667 # if product_info defines a env_scripts, load it
668 if 'env_script' in pi.environ:
669 self.run_env_script(pi, logger)
674 def run_env_script(self, product_info, logger=None, native=False):
676 Runs an environment script.
678 :param product_info Config: The product description
679 :param logger Logger: The logger instance to display messages
680 :param native Boolean: if True load set_native_env instead of set_env
682 env_script = product_info.environ.env_script
683 # Check that the script exists
684 if not os.path.exists(env_script):
685 raise src.SatException(_("Environment script not found: %s") %
688 if not self.silent and logger is not None:
689 logger.write(" ** load %s\n" % env_script, 4)
691 # import the script and run the set_env function
694 pyproduct = imp.load_source(product_info.name + "_env_script",
697 if self.forBuild and "set_env_build" in dir(pyproduct):
698 pyproduct.set_env_build(self,
699 product_info.install_dir,
700 product_info.version)
701 elif (not self.forBuild) and "set_env_launch" in dir(pyproduct):
702 pyproduct.set_env_launch(self,
703 product_info.install_dir,
704 product_info.version)
706 # at least this one is mandatory,
707 # if set_env_build and set_env_build are not defined
708 pyproduct.set_env(self,
709 product_info.install_dir,
710 product_info.version)
712 # not mandatory, if set_nativ_env not defined, we do nothing
713 if "set_nativ_env" in dir(pyproduct):
714 pyproduct.set_nativ_env(self)
716 __, exceptionValue, exceptionTraceback = sys.exc_info()
717 print(exceptionValue)
719 traceback.print_tb(exceptionTraceback)
720 traceback.print_exc()
723 def set_products(self, logger, src_root=None):
725 Sets the environment for all the products.
727 :param logger Logger: The logger instance to display messages
728 :param src_root src: the application working directory
731 self.add_comment('setting environ for all products')
733 # Make sure that the python lib dirs are set after python
734 if "Python" in self.cfg.APPLICATION.products:
735 self.set_a_product("Python", logger)
736 self.set_python_libdirs()
738 # The loop on the products
739 for product in self.cfg.APPLICATION.products.keys():
740 if product == "Python":
742 self.set_a_product(product, logger)
744 def set_full_environ(self, logger, env_info):
746 Sets the full environment for products
747 specified in env_info dictionary.
749 :param logger Logger: The logger instance to display messages
750 :param env_info list: the list of products
752 DBG.write("set_full_environ for", env_info)
753 # DBG.write("set_full_environ config", self.cfg.APPLICATION.environ, True)
755 # set product environ
756 self.set_application_env(logger)
758 if "Python" in env_info:
759 self.set_a_product("Python", logger)
760 self.set_python_libdirs()
763 for product in env_info:
764 if product == "Python":
766 self.set_a_product(product, logger)
770 Class to dump the environment to a file.
772 def __init__(self, config, logger, out_dir, src_root, env_info=None):
776 :param cfg Config: the global config
777 :param logger Logger: The logger instance to display messages
778 :param out_dir str: The directory path where t put the output files
779 :param src_root str: The application working directory
780 :param env_info str: The list of products to add in the files.
784 self.out_dir = out_dir
785 self.src_root= src_root
787 self.env_info = env_info
789 def write_env_file(self,
795 additional_env = {}):
797 Create an environment file.
799 :param filename str: the file path
800 :param forBuild bool: if true, the build environment
801 :param shell str: the type of file wanted (.sh, .bat)
802 :param for_package bool: if true do specific stuff for required for packages
803 :param no_path_init bool: if true generate a environ file that do not reinitialise paths
804 :param additional_env dict: contains sat_ prefixed variables to help the génération,
805 and also variables to add in the environment.
806 :return: The path to the generated file
810 self.logger.write(_("Create environment file %s\n") %
811 src.printcolors.printcLabel(filename), 3)
813 # create then env object
814 env_file = open(os.path.join(self.out_dir, filename), "w")
816 # we duplicate additional_env, and transmit it to fileEnviron, which will use its sat_ prefixed variables.
817 # the other variables of additional_env are added to the environement file at the end of this function.
818 salome_env = copy.deepcopy(additional_env)
819 file_environ = src.fileEnviron.get_file_environ(env_file,
821 src.environment.Environ(salome_env))
823 # specify we don't want to reinitialise paths
824 # path will keep the inherited value, which will be appended with new values.
825 file_environ.set_no_init_path()
827 env = SalomeEnviron(self.config,
830 for_package=for_package)
832 env.silent = self.silent
834 # Set the environment
835 if self.env_info is not None:
836 env.set_full_environ(self.logger, self.env_info)
838 # set env from the APPLICATION
839 env.set_application_env(self.logger)
841 # The list of products to launch
842 lProductsName = env.get_names(self.config.APPLICATION.products.keys())
843 env.set( "SALOME_MODULES", ','.join(lProductsName))
846 env.set_products(self.logger,
847 src_root=self.src_root)
849 # Add the additional environment if it is not empty
850 if len(additional_env) != 0:
852 env.add_comment("[APPLI variables]")
853 for variable in additional_env:
854 if not variable.startswith("sat_"):
855 # by convention variables starting with sat_ are used to transfer information,
856 # not to be written in env
857 env.set(variable, additional_env[variable])
859 # finalise the writing and close the file
868 Definition of a Shell.
870 def __init__(self, name, extension):
874 :param name str: the shell name
875 :param extension str: the shell extension
878 self.extension = extension
880 def load_environment(config, build, logger):
882 Loads the environment (used to run the tests, for example).
884 :param config Config: the global config
885 :param build bool: build environement if True
886 :param logger Logger: The logger instance to display messages
888 environ = SalomeEnviron(config, Environ(os.environ), build)
889 environ.set_application_env(logger)
890 environ.set_products(logger)