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 # create bash product file
27 _bash_content = """PRODUCT_DIR=%s
28 if [[ "${ENV_FOR_LAUNCH}x" == "x" ]]
30 export ENV_FOR_LAUNCH=1
33 if [[ "${ENV_FOR_LAUNCH}" == "1" ]]
35 source $PRODUCT_DIR/env_launch.sh
37 source $PRODUCT_DIR/env_build.sh
41 # create batch product file
42 _batch_content = """set PRODUCT_DIR=%s
43 IF NOT DEFINED ENV_FOR_LAUNCH set ENV_FOR_LAUNCH=1
45 if "%%ENV_FOR_LAUNCH%%"=="1" (
46 %%PRODUCT_DIR%%\\env_launch.bat
48 %%PRODUCT_DIR%%\\env_build.bat
53 '''Class to manage the environment context
55 def __init__(self, environ=None):
56 '''Initialization. If the environ argument is passed, the environment
57 will be add to it, else it is the external environment.
61 if environ is not None:
62 self.environ = environ
64 self.environ = os.environ
67 """easy non exhaustive quick resume for debug print
70 res["environ"]=self.environ
71 return self.__class__.__name__ + str(res)[0:-1] + " ...etc...}"
73 def _expandvars(self, value):
74 '''replace some $VARIABLE into its actual value in the environment
76 :param value str: the string to be replaced
77 :return: the replaced variable
81 # The string.Template class is a string class
82 # for supporting $-substitutions
83 zt = string.Template(value)
85 value = zt.substitute(self.environ)
86 except KeyError as exc:
87 raise src.SatException(_("Missing definition "
88 "in environment: %s") % str(exc))
91 def append_value(self, key, value, sep=os.pathsep):
92 '''append value to key using sep
94 :param key str: the environment variable to append
95 :param value str: the value to append to key
96 :param sep str: the separator string
98 # check if the key is already in the environment
99 if key in self.environ:
100 value_list = self.environ[key].split(sep)
101 # Check if the value is already in the key value or not
102 if not value in value_list:
103 value_list.append(value)
105 value_list.append(value_list.pop(value_list.index(value)))
106 self.set(key, sep.join(value_list))
110 def append(self, key, value, sep=os.pathsep):
111 '''Same as append_value but the value argument can be a list
113 :param key str: the environment variable to append
114 :param value str or list: the value(s) to append to key
115 :param sep str: the separator string
117 if isinstance(value, list):
119 self.append_value(key, v, sep)
121 self.append_value(key, value, sep)
123 def prepend_value(self, key, value, sep=os.pathsep):
124 '''prepend value to key using sep
126 :param key str: the environment variable to prepend
127 :param value str: the value to prepend to key
128 :param sep str: the separator string
130 if key in self.environ:
131 value_list = self.environ[key].split(sep)
132 if not value in value_list:
133 value_list.insert(0, value)
135 value_list.insert(0, value_list.pop(value_list.index(value)))
136 self.set(key, sep.join(value_list))
140 def prepend(self, key, value, sep=os.pathsep):
141 '''Same as prepend_value but the value argument can be a list
143 :param key str: the environment variable to prepend
144 :param value str or list: the value(s) to prepend to key
145 :param sep str: the separator string
147 if isinstance(value, list):
149 self.prepend_value(key, v, sep)
151 self.prepend_value(key, value, sep)
153 def is_defined(self, key):
154 '''Check if the key exists in the environment
156 :param key str: the environment variable to check
158 return self.environ.has_key(key)
160 def set(self, key, value):
161 '''Set the environment variable "key" to value "value"
163 :param key str: the environment variable to set
164 :param value str: the value
166 self.environ[key] = self._expandvars(value)
169 '''Get the value of the environment variable "key"
171 :param key str: the environment variable
173 if key in self.environ:
174 return self.environ[key]
178 def command_value(self, key, command):
179 '''Get the value given by the system command "command"
180 and put it in the environment variable key
182 :param key str: the environment variable
183 :param command str: the command to execute
185 value = subprocess.Popen(command,
187 stdout=subprocess.PIPE,
188 env=self.environ).communicate()[0]
189 self.environ[key] = value
193 """Class to manage the environment of SALOME.
196 def __init__(self, cfg, environ, forBuild=False):
197 self.environ = environ
199 self.forBuild = forBuild
203 """easy non exhaustive quick resume for debug print"""
205 res["environ"]=str(self.environ)
206 res["forBuild"]=self.forBuild
207 return self.__class__.__name__ + str(res)[0:-1] + " ...etc...}"
209 def append(self, key, value, sep=os.pathsep):
210 return self.environ.append(key, value, sep)
212 def prepend(self, key, value, sep=os.pathsep):
213 return self.environ.prepend(key, value, sep)
215 def is_defined(self, key):
216 return self.environ.is_defined(key)
219 return self.environ.get(key)
221 def set(self, key, value):
222 # check if value needs to be evaluated
223 if value is not None and value.startswith("`") and value.endswith("`"):
224 res = subprocess.Popen("echo %s" % value, shell=True, stdout=subprocess.PIPE).communicate()
225 value = res[0].strip()
227 return self.environ.set(key, value)
230 """Write the environment to out"""
231 for k in self.environ.environ.keys():
236 out.write("%s=%s\n" % (k, value))
238 def add_line(self, nb_line):
239 if 'add_line' in dir(self.environ):
240 self.environ.add_line(nb_line)
242 def add_comment(self, comment):
243 if 'add_comment' in dir(self.environ):
244 self.environ.add_comment(comment)
246 def add_warning(self, warning):
247 if 'add_warning' in dir(self.environ):
248 self.environ.add_warning(warning)
250 def finish(self, required):
251 if 'finish' in dir(self.environ):
252 self.environ.add_line(1)
253 self.environ.add_comment("clean all the path")
254 self.environ.finish(required)
256 def list_version_4_prereq(self, prerequisite, logger):
258 for path in self.cfg.TOOLS.environ.prereq_install_dir:
259 if not os.path.exists(path):
261 prereqlist = os.listdir(path)
262 for prereq in prereqlist:
263 if prereq.split("-")[0] == prerequisite:
264 #logger.error(str(prereq) + "\n")
265 alist.append(str(prereq))
268 logger.write(_("Available prerequisites are:") + "\n\t%s\n" % '\n\t'.join(alist), 2)
270 def set_python_libdirs(self):
271 if src.architecture.is_windows():
272 # sysconfig.get_python_lib() does not return appropriate path on Windows
273 # clearly decide here once for windows
274 ver = self.get('PYTHON_VERSION')
275 self.set('PYTHON_LIBDIR0', os.path.join('lib', 'python' + ver, 'site-packages'))
276 self.set('PYTHON_LIBDIR1', os.path.join('lib64', 'python' + ver, 'site-packages'))
279 ver = self.get('PYTHON_VERSION')
280 self.set('PYTHON_LIBDIR0', os.path.join('lib', 'python' + ver, 'site-packages'))
281 self.set('PYTHON_LIBDIR1', os.path.join('lib64', 'python' + ver, 'site-packages'))
283 self.python_lib0 = self.get('PYTHON_LIBDIR0')
284 self.python_lib1 = self.get('PYTHON_LIBDIR1')
287 # Get the products name to add in SALOME_MODULES environment variable
288 # It is the name of the product, except in the case where the is a component name.
289 # And it has to be in SALOME_MODULES variable only if has_gui = "yes"
290 def getNames(self, lProducts):
291 lProdHasGui = [p for p in lProducts if 'has_gui' in src.product.get_product_config(self.cfg, p) and src.product.get_product_config(self.cfg, p).has_gui=='yes']
293 for ProdName in lProdHasGui:
294 pi = src.product.get_product_config(self.cfg, ProdName)
295 if 'component_name' in pi:
296 lProdName.append(pi.component_name)
298 lProdName.append(ProdName)
302 # Sets the environment defined in the PRODUCT file.
303 def set_application_env(self, logger):
304 if 'environ' in self.cfg.APPLICATION:
305 self.add_comment("APPLICATION environment")
306 for p in self.cfg.APPLICATION.environ:
307 self.set(p, self.cfg.APPLICATION.environ[p])
310 if 'environ_script' in self.cfg.APPLICATION:
311 for pscript in self.cfg.APPLICATION.environ_script:
312 self.add_comment("script %s" % pscript)
313 sname = pscript.replace(" ", "_")
314 self.run_env_script("APPLICATION_%s" % sname, self.cfg.APPLICATION.environ_script[pscript], logger)
317 if 'profile' in self.cfg.APPLICATION:
318 profile_product = self.cfg.APPLICATION.profile.product
319 product_info_profile = src.product.get_product_config(self.cfg, profile_product)
320 profile_share_salome = os.path.join( product_info_profile.install_dir, "share", "salome" )
321 self.set( "SUITRoot", profile_share_salome )
322 self.set( "SalomeAppConfig", os.path.join( profile_share_salome, "resources", profile_product.lower() ) )
324 # The list of products to launch
325 lProductsName = self.getNames(self.cfg.APPLICATION.products.keys())
327 self.set( "SALOME_MODULES", ','.join(lProductsName))
330 # Set xxx_ROOT_DIR and xxx_SRC_DIR.
331 def set_salome_minimal_product_env(self, product_info, logger, single_dir, cfgdic=None):
333 root_dir = product_info.name + "_ROOT_DIR"
334 indic = cfgdic is not None and root_dir in cfgdic
335 if not self.is_defined(root_dir) and not indic:
337 self.set(root_dir, os.path.join(self.get('INSTALL_ROOT'), 'SALOME'))
338 elif 'install_dir' in product_info and product_info.install_dir:
339 self.set(root_dir, product_info.install_dir)
340 elif not self.silent:
341 logger.write(" " + _("No install_dir for product %s\n") % product_info.name, 5)
343 # set source dir, unless the product is fixed (no source dir)
344 if not src.product.product_is_fixed(product_info):
345 src_dir = product_info.name + "_SRC_DIR"
346 indic = cfgdic is not None and src_dir in cfgdic
347 if not self.is_defined(src_dir) and not indic:
348 self.set(src_dir, product_info.source_dir)
350 def set_salome_generic_product_env(self, product):
351 pi = src.product.get_product_config(self.cfg, product)
352 env_root_dir = self.get(pi.name + "_ROOT_DIR")
353 l_binpath_libpath = []
355 # create additional ROOT_DIR for CPP components
356 if 'component_name' in pi:
357 compo_name = pi.component_name
358 if compo_name + "CPP" == product:
359 compo_root_dir = compo_name + "_ROOT_DIR"
360 envcompo_root_dir = os.path.join( self.cfg.TOOLS.common.install_root, compo_name )
361 self.set(compo_root_dir , envcompo_root_dir)
362 bin_path = os.path.join(envcompo_root_dir, 'bin', 'salome')
363 lib_path = os.path.join(envcompo_root_dir, 'lib', 'salome')
364 l_binpath_libpath.append( (bin_path, lib_path) )
367 if src.get_cfg_param(pi, 'product_type', 'SALOME').upper() not in [ "SALOME", "SMESH_PLUGIN", "SAMPLE" ]:
370 bin_path = os.path.join(env_root_dir, 'bin', appliname)
371 lib_path = os.path.join(env_root_dir, 'lib', appliname)
372 l_binpath_libpath.append( (bin_path, lib_path) )
374 for bin_path, lib_path in l_binpath_libpath:
375 if not self.forBuild:
376 self.prepend('PATH', bin_path)
377 if src.architecture.is_windows():
378 self.prepend('PATH', lib_path)
380 self.prepend('LD_LIBRARY_PATH', lib_path)
382 l = [ bin_path, lib_path,
383 os.path.join(env_root_dir, self.python_lib0, appliname),
384 os.path.join(env_root_dir, self.python_lib1, appliname)
386 self.prepend('PYTHONPATH', l)
389 # Loads environment define in the configuration.
390 def load_cfg_environment(self, cfg_env):
391 for env_def in cfg_env:
392 val = cfg_env[env_def]
393 if isinstance(val, src.pyconf.Mapping):
396 if isinstance(val, src.pyconf.Sequence):
397 # transform into list of strings
403 if env_def.startswith("_"):
404 # separator exception for PV_PLUGIN_PATH
405 if env_def[1:] == 'PV_PLUGIN_PATH':
406 self.prepend(env_def[1:], val, ';')
408 self.prepend(env_def[1:], val)
409 elif env_def.endswith("_"):
410 # separator exception for PV_PLUGIN_PATH
411 if env_def[:-1] == 'PV_PLUGIN_PATH':
412 self.prepend(env_def[:-1], val, ';')
414 self.prepend(env_def[:-1], val)
416 self.set(env_def, val)
419 # Sets the environment of a product.
420 def set_a_product(self, product, logger, single_dir):
423 logger.write(_("Setting environment for %s\n") % product, 4)
426 self.add_comment('setting environ for ' + product)
428 pi = src.product.get_product_config(self.cfg, product)
430 # Do not define environment if the product is native or fixed
431 if src.product.product_is_native(pi):
435 # set environment using definition of the product
436 self.set_salome_minimal_product_env(pi, logger, single_dir, pi.environ)
437 self.set_salome_generic_product_env(product)
438 self.load_cfg_environment(pi.environ)
439 if self.forBuild and "build" in pi.environ:
440 self.load_cfg_environment(pi.environ.build)
441 if not self.forBuild and "launch" in pi.environ:
442 self.load_cfg_environment(pi.environ.launch)
444 # no environment defined in config
445 self.set_salome_minimal_product_env(pi, logger, single_dir)
446 if 'install_dir' in pi :
447 self.set_salome_generic_product_env(product)
449 # if product_info defines a env_scripts load it
450 if 'env_script' in pi:
451 self.run_env_script(pi, logger)
455 # Runs an environment script.
456 def run_env_script(self, product_info, logger=None):
457 env_script = product_info.env_script
458 if not os.path.exists(product_info.env_script):
459 raise src.SatException(_("Environment script not found: %s") % env_script)
461 if not self.silent and logger is not None:
462 logger.write(" ** load %s\n" % product_info.env_script, 4)
466 pyproduct = imp.load_source(product_info.name + "_env_script", env_script)
467 pyproduct.set_env(self, product_info.install_dir, product_info.version)
469 __, exceptionValue, exceptionTraceback = sys.exc_info()
470 print(exceptionValue)
472 traceback.print_tb(exceptionTraceback)
473 traceback.print_exc()
476 # Sets the environment for all the products.
477 def set_products(self, logger, src_root=None, single_dir=False):
479 self.add_comment('setting environ for all products')
481 self.set_python_libdirs()
484 src_root = self.cfg.APPLICATION.workdir
485 self.set('SRC_ROOT', src_root)
488 if "APPLI" in self.cfg and "application_name" in self.cfg.APPLI:
489 appli_name = self.cfg.APPLI.application_name
490 self.set("SALOME_APPLI_ROOT", os.path.join(self.cfg.APPLICATION.workdir, appli_name))
493 single_dir = src.get_cfg_param(self.cfg.APPLICATION, "compil_in_single_dir", "no") == 'yes'
495 for product in src.get_cfg_param(self.cfg.APPLICATION, "imported_products", []):
496 self.set_a_product(product, logger, single_dir=single_dir)
499 for product in self.cfg.APPLICATION.products.keys():
500 self.set_a_product(product, logger, single_dir=single_dir)
505 # Sets the full environment for prerequisites and products specified in env_info dictionary.
506 def set_full_environ(self, logger, env_info):
507 # set product environ
508 self.set_application_env(logger)
511 install_root = os.path.join(self.cfg.APPLICATION.workdir, "INSTALL")
512 source_root = os.path.join(self.cfg.APPLICATION.workdir, "SOURCES")
513 self.set('INSTALL_ROOT', install_root)
514 self.set('SRC_ROOT', source_root)
515 self.set_python_libdirs()
517 single_dir = src.get_cfg_param(self.cfg.APPLICATION, "compil_in_single_dir", "no") == 'yes'
518 for product in env_info['products']:
519 self.set_a_product(product, logger, single_dir=single_dir)
522 # Class to dump the environment to a file.
524 def __init__(self, config, logger, out_dir, src_root, single_dir, env_info=None):
527 self.out_dir = out_dir
528 self.src_root= src_root
529 self.single_dir = single_dir
531 self.env_info = env_info
533 def write_env_file(self, filename, forBuild, shell):
534 """Create an environment file."""
536 self.logger.write(_("Create environment file %s\n") % src.printcolors.printcLabel(filename), 3)
538 # create then env object
539 env_file = open(os.path.join(self.out_dir, filename), "w")
540 tmp = src.fileEnviron.get_file_environ(env_file, shell, {}, self.config )
541 env = SalomeEnviron(self.config, tmp, forBuild)
542 env.silent = self.silent
544 if self.env_info is not None:
545 env.set_full_environ(self.logger, self.env_info)
547 # set env from PRODUCT
548 env.set_application_env(self.logger)
550 env.set_products(self.logger,
551 src_root=self.src_root, single_dir=self.single_dir)
553 # add cleanup and close
559 def write_product_file(self, filename, shell):
560 """Create a product file."""
562 self.logger.write(_("Create product file %s\n") % src.printcolors.printcLabel(filename), 3)
564 prod_file = open(os.path.join(self.out_dir, filename), "w")
566 content = _bash_content % self.out_dir
567 elif shell == "batch":
568 content = _batch_content % self.out_dir
570 raise src.SatException(_("Unknown shell: %s") % shell)
572 prod_file.write(content)
575 return prod_file.name
577 def write_cfgForPy_file(self, aFile, additional_env = {}):
578 """append to current opened aFile a cfgForPy environment (python syntax)."""
580 self.logger.write(_("Create configuration file %s\n") % src.printcolors.printcLabel(aFile.name), 3)
582 # create then env object
583 tmp = src.fileEnviron.get_file_environ(aFile, "cfgForPy", {}, self.config)
586 env = SalomeEnviron(self.config, tmp, forLaunch)
587 env.silent = self.silent
589 if self.env_info is not None:
590 env.set_full_environ(self.logger, self.env_info)
592 # set env from PRODUCT
593 env.set_application_env(self.logger)
594 # set the prerequisites
595 env.set_prerequisites(self.logger)
597 env.set_products(self.logger,
598 src_root=self.src_root, single_dir=self.single_dir)
600 if len(additional_env) != 0:
601 for variable in additional_env:
602 env.set(variable, additional_env[variable])
604 # add cleanup and close
608 # Definition of a Shell.
610 def __init__(self, name, extension):
612 self.extension = extension
615 # Loads the environment (used to run the tests).
616 def load_environment(config, build, logger):
617 environ = SalomeEnviron(config, Environ(os.environ), build)
618 environ.set_application_env(logger)
619 environ.set_products(logger)