3 # Copyright (C) 2010-2012 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
18 '''In this file are implemented the methods
19 relative to the product notion of salomeTools
27 AVAILABLE_VCS = ['git', 'svn', 'cvs']
28 config_expression = "^config-\d+$"
29 VERSION_DELIMITER = "_to_"
31 def get_product_config(config, product_name, with_install_dir=True):
32 '''Get the specific configuration of a product from the global configuration
34 :param config Config: The global configuration
35 :param product_name str: The name of the product
36 :param with_install_dir boolean: If false, do not provide an install
37 directory (at false only for internal use
38 of the function get_install_dir)
39 :return: the specific configuration of the product
43 # Get the version of the product from the application definition
44 version = config.APPLICATION.products[product_name]
45 # if no version, then take the default one defined in the application
46 if isinstance(version, bool):
47 version = config.APPLICATION.tag
49 # Define debug and dev modes
50 # Get the tag if a dictionary is given in APPLICATION.products for the
56 if isinstance(version, src.pyconf.Mapping):
59 if not 'tag' in dic_version:
60 version = config.APPLICATION.tag
62 version = dic_version.tag
64 # Get the debug if any
65 if 'debug' in dic_version:
66 debug = dic_version.debug
69 if 'dev' in dic_version:
73 if 'base' in dic_version:
74 base = dic_version.base
76 # Get the section if any
77 if 'section' in dic_version:
78 section = dic_version.section
81 # substitute some character with _ in order to get the correct definition
82 # in config.PRODUCTS. This is done because the pyconf tool does not handle
83 # the . and - characters
84 for c in ".-": vv = vv.replace(c, "_")
87 if product_name in config.PRODUCTS:
88 # Search for the product description in the configuration
89 prod_info = get_product_section(config, product_name, vv, section)
91 # merge opt_depend in depend
92 if prod_info is not None and 'opt_depend' in prod_info:
93 for depend in prod_info.opt_depend:
94 if depend in config.APPLICATION:
95 prod_info.depend.append(depend,'')
97 # In case of a product get with a vcs,
98 # put the tag (equal to the version)
99 if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
101 if prod_info.get_source == 'git':
102 prod_info.git_info.tag = version
104 if prod_info.get_source == 'svn':
105 prod_info.svn_info.tag = version
107 if prod_info.get_source == 'cvs':
108 prod_info.cvs_info.tag = version
110 # In case of a fixed product,
111 # define the install_dir (equal to the version)
112 if prod_info is not None and prod_info.get_source=="fixed":
113 prod_info.install_dir = version
115 # Check if the product is defined as native in the application
116 if prod_info is not None:
117 if version == "native":
118 prod_info.get_source = "native"
119 elif prod_info.get_source == "native":
120 msg = _("The product %(prod)s has version %(ver)s but is "
121 "declared as native in its definition" %
122 { 'prod': prod_info.name, 'ver': version})
123 raise src.SatException(msg)
125 # If there is no definition but the product is declared as native,
126 # construct a new definition containing only the get_source key
127 if prod_info is None and version == "native":
128 prod_info = src.pyconf.Config()
129 prod_info.name = product_name
130 prod_info.get_source = "native"
132 # If prod_info is still None, it means that there is no product definition
133 # in the config. The user has to provide it.
134 if prod_info is None:
135 prod_pyconf_path = src.find_file_in_lpath(product_name + ".pyconf",
136 config.PATHS.PRODUCTPATH)
137 if not prod_pyconf_path:
138 msg = _("No definition found for the product %s\n"
139 "Please create a %s.pyconf file." % (product_name, product_name))
141 msg = _("No definition corresponding to the version %(version)s was"
142 " found in the file %(prod_file_path)s.\nPlease add a "
143 "section in it." % {"version" : vv,
144 "prod_file_path" : prod_pyconf_path} )
145 raise src.SatException(msg)
147 # Set the debug, dev and version keys
148 prod_info.debug = debug
150 prod_info.version = version
152 # Set the archive_info if the product is get in archive mode
153 if prod_info.get_source == "archive":
154 if not "archive_info" in prod_info:
155 prod_info.addMapping("archive_info",
156 src.pyconf.Mapping(prod_info),
158 if "archive_name" not in prod_info.archive_info:
159 arch_name = product_name + "-" + version + ".tar.gz"
160 arch_path = src.find_file_in_lpath(arch_name,
161 config.PATHS.ARCHIVEPATH)
163 msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
164 "\n" % {"arch_name" : arch_name,
165 "prod_name" : prod_info.name})
166 raise src.SatException(msg)
167 prod_info.archive_info.archive_name = arch_path
169 if (os.path.basename(prod_info.archive_info.archive_name) ==
170 prod_info.archive_info.archive_name):
171 arch_name = prod_info.archive_info.archive_name
172 arch_path = src.find_file_in_lpath(
174 config.PATHS.ARCHIVEPATH)
176 msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
177 "\n" % {"arch_name" : arch_name,
178 "prod_name" : prod_info.name})
179 raise src.SatException(msg)
180 prod_info.archive_info.archive_name = arch_path
182 # If the product compiles with a script, check the script existence
183 # and if it is executable
184 if product_has_script(prod_info):
185 # Check the compil_script key existence
186 if "compil_script" not in prod_info:
187 msg = _("No compilation script found for the product %s\n"
188 "Please provide a \"compil_script\" key in its definition."
190 raise src.SatException(msg)
192 # Get the path of the script
193 script = prod_info.compil_script
194 script_name = os.path.basename(script)
195 if script == script_name:
196 # Only a name is given. Search in the default directory
197 script_path = src.find_file_in_lpath(script_name,
198 config.PATHS.PRODUCTPATH,
201 raise src.SatException(_("Compilation script not found: %s") %
203 prod_info.compil_script = script_path
205 # Check that the script is executable
206 if not os.access(prod_info.compil_script, os.X_OK):
207 raise src.SatException(
208 _("Compilation script cannot be executed: %s") %
209 prod_info.compil_script)
211 # Get the full paths of all the patches
212 if product_has_patches(prod_info):
214 for patch in prod_info.patches:
216 # If only a filename, then search for the patch in the PRODUCTPATH
217 if os.path.basename(patch_path) == patch_path:
218 # Search in the PRODUCTPATH/patches
219 patch_path = src.find_file_in_lpath(patch,
220 config.PATHS.PRODUCTPATH,
223 msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
224 "\n" % {"patch_name" : patch,
225 "prod_name" : prod_info.name})
226 raise src.SatException(msg)
227 patches.append(patch_path)
228 prod_info.patches = patches
230 # Get the full paths of the environment scripts
231 if product_has_env_script(prod_info):
232 env_script_path = prod_info.environ.env_script
233 # If only a filename, then search for the environment script
234 # in the PRODUCTPATH/env_scripts
235 if os.path.basename(env_script_path) == env_script_path:
236 # Search in the PRODUCTPATH/env_scripts
237 env_script_path = src.find_file_in_lpath(
238 prod_info.environ.env_script,
239 config.PATHS.PRODUCTPATH,
241 if not env_script_path:
242 msg = _("Environment script %(env_name)s for %(prod_name)s not "
243 "found.\n" % {"env_name" : env_script_path,
244 "prod_name" : prod_info.name})
245 raise src.SatException(msg)
247 prod_info.environ.env_script = env_script_path
250 # The variable with_install_dir is at false only for internal use
251 # of the function get_install_dir
253 # Set the install_dir key
254 prod_info.install_dir = get_install_dir(config, base, version, prod_info)
258 def get_product_section(config, product_name, version, section=None):
259 '''Get the product description from the configuration
261 :param config Config: The global configuration
262 :param product_name str: The product name
263 :param version str: The version of the product
264 :param section str: The searched section (if not None, the section is
266 :return: The product description
270 # if section is not None, try to get the corresponding section
272 if section not in config.PRODUCTS[product_name]:
274 # returns specific information for the given version
275 prod_info = config.PRODUCTS[product_name][section]
276 prod_info.section = section
279 # If it exists, get the information of the product_version
280 if "version_" + version in config.PRODUCTS[product_name]:
281 # returns specific information for the given version
282 prod_info = config.PRODUCTS[product_name]["version_" + version]
283 prod_info.section = "version_" + version
286 # Else, check if there is a description for multiple versions
287 l_section_name = config.PRODUCTS[product_name].keys()
288 l_section_ranges = [section_name for section_name in l_section_name
289 if VERSION_DELIMITER in section_name]
290 for section_range in l_section_ranges:
291 minimum, maximum = section_range.split(VERSION_DELIMITER)
292 if (src.only_numbers(version) >= src.only_numbers(minimum)
293 and src.only_numbers(version) <= src.only_numbers(maximum)):
294 # returns specific information for the versions
295 prod_info = config.PRODUCTS[product_name][section_range]
296 prod_info.section = section_range
299 # Else, get the standard informations
300 if "default" in config.PRODUCTS[product_name]:
301 # returns the generic information (given version not found)
302 prod_info = config.PRODUCTS[product_name].default
303 prod_info.section = "default"
306 # if noting was found, return None
309 def get_install_dir(config, base, version, prod_info):
310 '''Compute the installation directory of a given product
312 :param config Config: The global configuration
313 :param base str: This corresponds to the value given by user in its
314 application.pyconf for the specific product. If "yes", the
315 user wants the product to be in base. If "no", he wants the
316 product to be in the application workdir
317 :param version str: The version of the product
318 :param product_info Config: The configuration specific to
321 :return: The path of the product installation
326 if (("install_dir" in prod_info and prod_info.install_dir == "base")
329 if (base == "no" or ("no_base" in config.APPLICATION
330 and config.APPLICATION.no_base == "yes")):
334 install_dir = get_base_install_dir(config, prod_info, version)
336 if "install_dir" not in prod_info or prod_info.install_dir == "base":
337 # Set it to the default value (in application directory)
338 install_dir = os.path.join(config.APPLICATION.workdir,
342 install_dir = prod_info.install_dir
346 def get_base_install_dir(config, prod_info, version):
347 '''Compute the installation directory of a product in base
349 :param config Config: The global configuration
350 :param product_info Config: The configuration specific to
352 :param version str: The version of the product
353 :return: The path of the product installation
356 base_path = src.get_base_path(config)
357 prod_dir = os.path.join(base_path, prod_info.name + "-" + version)
358 if not os.path.exists(prod_dir):
359 return os.path.join(prod_dir, "config-1")
361 exists, install_dir = check_config_exists(config, prod_dir, prod_info)
365 # Find the first config-<i> directory that is available in the product
370 install_dir = os.path.join(prod_dir, "config-%i" % label)
371 if os.path.exists(install_dir):
378 def check_config_exists(config, prod_dir, prod_info):
379 '''Verify that the installation directory of a product in a base exists
380 Check all the config-<i> directory and verify the sat-config.pyconf file
383 :param config Config: The global configuration
384 :param prod_dir str: The product installation directory path
386 :param product_info Config: The configuration specific to
388 :return: True or false is the installation is found or not
389 and if it is found, the path of the found installation
390 :rtype: (boolean, str)
392 # check if the directories or files of the directory corresponds to the
393 # directory installation of the product
394 l_dir_and_files = os.listdir(prod_dir)
395 for dir_or_file in l_dir_and_files:
396 oExpr = re.compile(config_expression)
397 if not(oExpr.search(dir_or_file)):
398 # not config-<i>, not interesting
400 # check if there is the file sat-config.pyconf file in the installation
402 config_file = os.path.join(prod_dir, dir_or_file, src.CONFIG_FILENAME)
403 if not os.path.exists(config_file):
406 # If there is no dependency, it is the right path
407 if len(prod_info.depend)==0:
408 return True, os.path.join(prod_dir, dir_or_file)
410 # check if there is the config described in the file corresponds the
411 # dependencies of the product
412 config_corresponds = True
413 compile_cfg = src.pyconf.Config(config_file)
414 for prod_dep in prod_info.depend:
415 # if the dependency is not in the config,
416 # the config does not correspond
417 if prod_dep not in compile_cfg:
418 config_corresponds = False
421 prod_dep_info = get_product_config(config, prod_dep, False)
422 # If the version of the dependency does not correspond,
423 # the config does not correspond
424 if prod_dep_info.version != compile_cfg[prod_dep]:
425 config_corresponds = False
427 if config_corresponds:
428 return True, os.path.join(prod_dir, dir_or_file)
434 def get_products_infos(lproducts, config):
435 '''Get the specific configuration of a list of products
437 :param lproducts List: The list of product names
438 :param config Config: The global configuration
439 :return: the list of tuples
440 (product name, specific configuration of the product)
441 :rtype: [(str, Config)]
444 # Loop on product names
445 for prod in lproducts:
446 # Get the specific configuration of the product
447 prod_info = get_product_config(config, prod)
448 if prod_info is not None:
449 products_infos.append((prod, prod_info))
451 msg = _("The %s product has no definition "
452 "in the configuration.") % prod
453 raise src.SatException(msg)
454 return products_infos
456 def get_product_dependencies(config, product_info):
457 '''Get recursively the list of products that are
458 in the product_info dependencies
460 :param config Config: The global configuration
461 :param product_info Config: The configuration specific to
463 :return: the list of products in dependence
466 if "depend" not in product_info or product_info.depend == []:
469 for prod in product_info.depend:
470 if prod == product_info.name:
474 prod_info = get_product_config(config, prod)
475 dep_prod = get_product_dependencies(config, prod_info)
476 for prod_in_dep in dep_prod:
477 if prod_in_dep not in res:
478 res.append(prod_in_dep)
481 def check_installation(product_info):
482 '''Verify if a product is well installed. Checks install directory presence
483 and some additional files if it is defined in the config
485 :param product_info Config: The configuration specific to
487 :return: True if it is well installed
490 if not product_compiles(product_info):
492 install_dir = product_info.install_dir
493 if not os.path.exists(install_dir):
495 if ("present_files" in product_info and
496 "install" in product_info.present_files):
497 for file_relative_path in product_info.present_files.install:
498 file_path = os.path.join(install_dir, file_relative_path)
499 if not os.path.exists(file_path):
503 def product_is_sample(product_info):
504 '''Know if a product has the sample type
506 :param product_info Config: The configuration specific to
508 :return: True if the product has the sample type, else False
511 if 'type' in product_info:
512 ptype = product_info.type
513 return ptype.lower() == 'sample'
517 def product_is_salome(product_info):
518 '''Know if a product is of type salome
520 :param product_info Config: The configuration specific to
522 :return: True if the product is salome, else False
525 if 'type' in product_info:
526 ptype = product_info.type
527 return ptype.lower() == 'salome'
531 def product_is_fixed(product_info):
532 '''Know if a product is fixed
534 :param product_info Config: The configuration specific to
536 :return: True if the product is fixed, else False
539 get_src = product_info.get_source
540 return get_src.lower() == 'fixed'
542 def product_is_native(product_info):
543 '''Know if a product is native
545 :param product_info Config: The configuration specific to
547 :return: True if the product is native, else False
550 get_src = product_info.get_source
551 return get_src.lower() == 'native'
553 def product_is_dev(product_info):
554 '''Know if a product is in dev mode
556 :param product_info Config: The configuration specific to
558 :return: True if the product is in dev mode, else False
561 dev = product_info.dev
562 return dev.lower() == 'yes'
564 def product_is_debug(product_info):
565 '''Know if a product is in debug mode
567 :param product_info Config: The configuration specific to
569 :return: True if the product is in debug mode, else False
572 debug = product_info.debug
573 return debug.lower() == 'yes'
575 def product_is_autotools(product_info):
576 '''Know if a product is compiled using the autotools
578 :param product_info Config: The configuration specific to
580 :return: True if the product is autotools, else False
583 build_src = product_info.build_source
584 return build_src.lower() == 'autotools'
586 def product_is_cmake(product_info):
587 '''Know if a product is compiled using the cmake
589 :param product_info Config: The configuration specific to
591 :return: True if the product is cmake, else False
594 build_src = product_info.build_source
595 return build_src.lower() == 'cmake'
597 def product_is_vcs(product_info):
598 '''Know if a product is download using git, svn or cvs (not archive)
600 :param product_info Config: The configuration specific to
602 :return: True if the product is vcs, else False
605 return product_info.get_source in AVAILABLE_VCS
607 def product_is_SALOME(product_info):
608 '''Know if a product is a SALOME module
610 :param product_info Config: The configuration specific to
612 :return: True if the product is a SALOME module, else False
615 return ("properties" in product_info and
616 "is_SALOME_module" in product_info.properties and
617 product_info.properties.is_SALOME_module == "yes")
619 def product_is_smesh_plugin(product_info):
620 '''Know if a product is a SMESH plugin
622 :param product_info Config: The configuration specific to
624 :return: True if the product is a SMESH plugin, else False
627 return ("properties" in product_info and
628 "smesh_plugin" in product_info.properties and
629 product_info.properties.smesh_plugin == "yes")
631 def product_is_cpp(product_info):
632 '''Know if a product is cpp
634 :param product_info Config: The configuration specific to
636 :return: True if the product is a cpp, else False
639 return ("properties" in product_info and
640 "cpp" in product_info.properties and
641 product_info.properties.cpp == "yes")
643 def product_compiles(product_info):
644 '''Know if a product compiles or not (some products do not have a
645 compilation procedure)
647 :param product_info Config: The configuration specific to
649 :return: True if the product compiles, else False
652 return not("properties" in product_info and
653 "compilation" in product_info.properties and
654 product_info.properties.compilation == "no")
656 def product_has_script(product_info):
657 '''Know if a product has a compilation script
659 :param product_info Config: The configuration specific to
661 :return: True if the product it has a compilation script, else False
664 if "build_source" not in product_info:
667 build_src = product_info.build_source
668 return build_src.lower() == 'script'
670 def product_has_env_script(product_info):
671 '''Know if a product has an environment script
673 :param product_info Config: The configuration specific to
675 :return: True if the product it has an environment script, else False
678 return "environ" in product_info and "env_script" in product_info.environ
680 def product_has_patches(product_info):
681 '''Know if a product has one or more patches
683 :param product_info Config: The configuration specific to
685 :return: True if the product has one or more patches
688 return "patches" in product_info and len(product_info.patches) > 0
690 def product_has_logo(product_info):
691 '''Know if a product has a logo (YACSGEN generate)
693 :param product_info Config: The configuration specific to
695 :return: The path of the logo if the product has a logo, else False
698 if ("properties" in product_info and
699 "logo" in product_info.properties):
700 return product_info.properties.logo
704 def product_has_salome_gui(product_info):
705 '''Know if a product has a SALOME gui
707 :param product_info Config: The configuration specific to
709 :return: True if the product has a SALOME gui, else False
712 return ("properties" in product_info and
713 "has_salome_gui" in product_info.properties and
714 product_info.properties.has_salome_gui == "yes")
716 def product_is_mpi(product_info):
717 '''Know if a product has openmpi in its dependencies
719 :param product_info Config: The configuration specific to
721 :return: True if the product has openmpi inits dependencies
724 return "openmpi" in product_info.depend
726 def product_is_generated(product_info):
727 '''Know if a product is generated (YACSGEN)
729 :param product_info Config: The configuration specific to
731 :return: True if the product is generated
734 return ("properties" in product_info and
735 "generate" in product_info.properties and
736 product_info.properties.generate == "yes")
738 def get_product_components(product_info):
739 '''Get the component list to generate with the product
741 :param product_info Config: The configuration specific to
743 :return: The list of names of the components
747 if not product_is_generated(product_info):
751 if "component_name" in product_info:
752 compo_list = product_info.component_name
754 if isinstance(compo_list, str):
755 compo_list = [ compo_list ]