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 msg = _("No definition found for the product %s\n"
136 "Please create a %s.pyconf file." % (product_name, product_name))
137 raise src.SatException(msg)
139 # Set the debug, dev and version keys
140 prod_info.debug = debug
142 prod_info.version = version
144 # Set the archive_info if the product is get in archive mode
145 if prod_info.get_source == "archive":
146 if not "archive_info" in prod_info:
147 prod_info.addMapping("archive_info",
148 src.pyconf.Mapping(prod_info),
150 if "archive_name" not in prod_info.archive_info:
151 arch_name = product_name + "-" + version + ".tar.gz"
152 arch_path = src.find_file_in_lpath(arch_name,
153 config.PATHS.ARCHIVEPATH)
155 msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
156 "\n" % {"arch_name" : arch_name,
157 "prod_name" : prod_info.name})
158 raise src.SatException(msg)
159 prod_info.archive_info.archive_name = arch_path
161 if (os.path.basename(prod_info.archive_info.archive_name) ==
162 prod_info.archive_info.archive_name):
163 arch_name = prod_info.archive_info.archive_name
164 arch_path = src.find_file_in_lpath(
166 config.PATHS.ARCHIVEPATH)
168 msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
169 "\n" % {"arch_name" : arch_name,
170 "prod_name" : prod_info.name})
171 raise src.SatException(msg)
172 prod_info.archive_info.archive_name = arch_path
174 # If the product compiles with a script, check the script existence
175 # and if it is executable
176 if product_has_script(prod_info):
177 # Check the compil_script key existence
178 if "compil_script" not in prod_info:
179 msg = _("No compilation script found for the product %s\n"
180 "Please provide a \"compil_script\" key in its definition."
182 raise src.SatException(msg)
184 # Get the path of the script
185 script = prod_info.compil_script
186 script_name = os.path.basename(script)
187 if script == script_name:
188 # Only a name is given. Search in the default directory
189 script_path = src.find_file_in_lpath(script_name,
190 config.PATHS.PRODUCTPATH,
193 raise src.SatException(_("Compilation script not found: %s") %
195 prod_info.compil_script = script_path
197 # Check that the script is executable
198 if not os.access(prod_info.compil_script, os.X_OK):
199 raise src.SatException(
200 _("Compilation script cannot be executed: %s") %
201 prod_info.compil_script)
203 # Get the full paths of all the patches
204 if product_has_patches(prod_info):
206 for patch in prod_info.patches:
208 # If only a filename, then search for the patch in the PRODUCTPATH
209 if os.path.basename(patch_path) == patch_path:
210 # Search in the PRODUCTPATH/patches
211 patch_path = src.find_file_in_lpath(patch,
212 config.PATHS.PRODUCTPATH,
215 msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
216 "\n" % {"patch_name" : patch,
217 "prod_name" : prod_info.name})
218 raise src.SatException(msg)
219 patches.append(patch_path)
220 prod_info.patches = patches
222 # Get the full paths of the environment scripts
223 if product_has_env_script(prod_info):
224 env_script_path = prod_info.environ.env_script
225 # If only a filename, then search for the environment script
226 # in the PRODUCTPATH/env_scripts
227 if os.path.basename(env_script_path) == env_script_path:
228 # Search in the PRODUCTPATH/env_scripts
229 env_script_path = src.find_file_in_lpath(
230 prod_info.environ.env_script,
231 config.PATHS.PRODUCTPATH,
233 if not env_script_path:
234 msg = _("Environment script %(env_name)s for %(prod_name)s not "
235 "found.\n" % {"env_name" : env_script_path,
236 "prod_name" : prod_info.name})
237 raise src.SatException(msg)
239 prod_info.environ.env_script = env_script_path
242 # The variable with_install_dir is at false only for internal use
243 # of the function get_install_dir
245 # Set the install_dir key
246 prod_info.install_dir = get_install_dir(config, base, version, prod_info)
250 def get_product_section(config, product_name, version, section=None):
251 '''Get the product description from the configuration
253 :param config Config: The global configuration
254 :param product_name str: The product name
255 :param version str: The version of the product
256 :param section str: The searched section (if not None, the section is
258 :return: The product description
262 # if section is not None, try to get the corresponding section
264 if section not in config.PRODUCTS[product_name]:
266 # returns specific information for the given version
267 prod_info = config.PRODUCTS[product_name][section]
268 prod_info.section = section
271 # If it exists, get the information of the product_version
272 if "version_" + version in config.PRODUCTS[product_name]:
273 # returns specific information for the given version
274 prod_info = config.PRODUCTS[product_name]["version_" + version]
275 prod_info.section = "version_" + version
278 # Else, check if there is a description for multiple versions
279 l_section_name = config.PRODUCTS[product_name].keys()
280 l_section_ranges = [section_name for section_name in l_section_name
281 if VERSION_DELIMITER in section_name]
282 for section_range in l_section_ranges:
283 minimum, maximum = section_range.split(VERSION_DELIMITER)
284 if (src.only_numbers(version) >= src.only_numbers(minimum)
285 and src.only_numbers(version) <= src.only_numbers(maximum)):
286 # returns specific information for the versions
287 prod_info = config.PRODUCTS[product_name][section_range]
288 prod_info.section = section_range
291 # Else, get the standard informations
292 if "default" in config.PRODUCTS[product_name]:
293 # returns the generic information (given version not found)
294 prod_info = config.PRODUCTS[product_name].default
295 prod_info.section = "default"
298 # if noting was found, return None
301 def get_install_dir(config, base, version, prod_info):
302 '''Compute the installation directory of a given product
304 :param config Config: The global configuration
305 :param base str: This corresponds to the value given by user in its
306 application.pyconf for the specific product. If "yes", the
307 user wants the product to be in base. If "no", he wants the
308 product to be in the application workdir
309 :param version str: The version of the product
310 :param product_info Config: The configuration specific to
313 :return: The path of the product installation
318 if (("install_dir" in prod_info and prod_info.install_dir == "base")
321 if (base == "no" or ("no_base" in config.APPLICATION
322 and config.APPLICATION.no_base == "yes")):
326 install_dir = get_base_install_dir(config, prod_info, version)
328 if "install_dir" not in prod_info or prod_info.install_dir == "base":
329 # Set it to the default value (in application directory)
330 install_dir = os.path.join(config.APPLICATION.workdir,
334 install_dir = prod_info.install_dir
338 def get_base_install_dir(config, prod_info, version):
339 '''Compute the installation directory of a product in base
341 :param config Config: The global configuration
342 :param product_info Config: The configuration specific to
344 :param version str: The version of the product
345 :return: The path of the product installation
348 base_path = src.get_base_path(config)
349 prod_dir = os.path.join(base_path, prod_info.name + "-" + version)
350 if not os.path.exists(prod_dir):
351 return os.path.join(prod_dir, "config-1")
353 exists, install_dir = check_config_exists(config, prod_dir, prod_info)
357 # Find the first config-<i> directory that is available in the product
362 install_dir = os.path.join(prod_dir, "config-%i" % label)
363 if os.path.exists(install_dir):
370 def check_config_exists(config, prod_dir, prod_info):
371 '''Verify that the installation directory of a product in a base exists
372 Check all the config-<i> directory and verify the sat-config.pyconf file
375 :param config Config: The global configuration
376 :param prod_dir str: The product installation directory path
378 :param product_info Config: The configuration specific to
380 :return: True or false is the installation is found or not
381 and if it is found, the path of the found installation
382 :rtype: (boolean, str)
384 # check if the directories or files of the directory corresponds to the
385 # directory installation of the product
386 l_dir_and_files = os.listdir(prod_dir)
387 for dir_or_file in l_dir_and_files:
388 oExpr = re.compile(config_expression)
389 if not(oExpr.search(dir_or_file)):
390 # not config-<i>, not interesting
392 # check if there is the file sat-config.pyconf file in the installation
394 config_file = os.path.join(prod_dir, dir_or_file, src.CONFIG_FILENAME)
395 if not os.path.exists(config_file):
398 # If there is no dependency, it is the right path
399 if len(prod_info.depend)==0:
400 return True, os.path.join(prod_dir, dir_or_file)
402 # check if there is the config described in the file corresponds the
403 # dependencies of the product
404 config_corresponds = True
405 compile_cfg = src.pyconf.Config(config_file)
406 for prod_dep in prod_info.depend:
407 # if the dependency is not in the config,
408 # the config does not correspond
409 if prod_dep not in compile_cfg:
410 config_corresponds = False
413 prod_dep_info = get_product_config(config, prod_dep, False)
414 # If the version of the dependency does not correspond,
415 # the config does not correspond
416 if prod_dep_info.version != compile_cfg[prod_dep]:
417 config_corresponds = False
419 if config_corresponds:
420 return True, os.path.join(prod_dir, dir_or_file)
426 def get_products_infos(lproducts, config):
427 '''Get the specific configuration of a list of products
429 :param lproducts List: The list of product names
430 :param config Config: The global configuration
431 :return: the list of tuples
432 (product name, specific configuration of the product)
433 :rtype: [(str, Config)]
436 # Loop on product names
437 for prod in lproducts:
438 # Get the specific configuration of the product
439 prod_info = get_product_config(config, prod)
440 if prod_info is not None:
441 products_infos.append((prod, prod_info))
443 msg = _("The %s product has no definition "
444 "in the configuration.") % prod
445 raise src.SatException(msg)
446 return products_infos
448 def get_product_dependencies(config, product_info):
449 '''Get recursively the list of products that are
450 in the product_info dependencies
452 :param config Config: The global configuration
453 :param product_info Config: The configuration specific to
455 :return: the list of products in dependence
458 if "depend" not in product_info or product_info.depend == []:
461 for prod in product_info.depend:
462 if prod == product_info.name:
466 prod_info = get_product_config(config, prod)
467 dep_prod = get_product_dependencies(config, prod_info)
468 for prod_in_dep in dep_prod:
469 if prod_in_dep not in res:
470 res.append(prod_in_dep)
473 def check_installation(product_info):
474 '''Verify if a product is well installed. Checks install directory presence
475 and some additional files if it is defined in the config
477 :param product_info Config: The configuration specific to
479 :return: True if it is well installed
482 if not product_compiles(product_info):
484 install_dir = product_info.install_dir
485 if not os.path.exists(install_dir):
487 if ("present_files" in product_info and
488 "install" in product_info.present_files):
489 for file_relative_path in product_info.present_files.install:
490 file_path = os.path.join(install_dir, file_relative_path)
491 if not os.path.exists(file_path):
495 def product_is_sample(product_info):
496 '''Know if a product has the sample type
498 :param product_info Config: The configuration specific to
500 :return: True if the product has the sample type, else False
503 if 'type' in product_info:
504 ptype = product_info.type
505 return ptype.lower() == 'sample'
509 def product_is_salome(product_info):
510 '''Know if a product is of type salome
512 :param product_info Config: The configuration specific to
514 :return: True if the product is salome, else False
517 if 'type' in product_info:
518 ptype = product_info.type
519 return ptype.lower() == 'salome'
523 def product_is_fixed(product_info):
524 '''Know if a product is fixed
526 :param product_info Config: The configuration specific to
528 :return: True if the product is fixed, else False
531 get_src = product_info.get_source
532 return get_src.lower() == 'fixed'
534 def product_is_native(product_info):
535 '''Know if a product is native
537 :param product_info Config: The configuration specific to
539 :return: True if the product is native, else False
542 get_src = product_info.get_source
543 return get_src.lower() == 'native'
545 def product_is_dev(product_info):
546 '''Know if a product is in dev mode
548 :param product_info Config: The configuration specific to
550 :return: True if the product is in dev mode, else False
553 dev = product_info.dev
554 return dev.lower() == 'yes'
556 def product_is_debug(product_info):
557 '''Know if a product is in debug mode
559 :param product_info Config: The configuration specific to
561 :return: True if the product is in debug mode, else False
564 debug = product_info.debug
565 return debug.lower() == 'yes'
567 def product_is_autotools(product_info):
568 '''Know if a product is compiled using the autotools
570 :param product_info Config: The configuration specific to
572 :return: True if the product is autotools, else False
575 build_src = product_info.build_source
576 return build_src.lower() == 'autotools'
578 def product_is_cmake(product_info):
579 '''Know if a product is compiled using the cmake
581 :param product_info Config: The configuration specific to
583 :return: True if the product is cmake, else False
586 build_src = product_info.build_source
587 return build_src.lower() == 'cmake'
589 def product_is_vcs(product_info):
590 '''Know if a product is download using git, svn or cvs (not archive)
592 :param product_info Config: The configuration specific to
594 :return: True if the product is vcs, else False
597 return product_info.get_source in AVAILABLE_VCS
599 def product_is_SALOME(product_info):
600 '''Know if a product is a SALOME module
602 :param product_info Config: The configuration specific to
604 :return: True if the product is a SALOME module, else False
607 return ("properties" in product_info and
608 "is_SALOME_module" in product_info.properties and
609 product_info.properties.is_SALOME_module == "yes")
611 def product_is_smesh_plugin(product_info):
612 '''Know if a product is a SMESH plugin
614 :param product_info Config: The configuration specific to
616 :return: True if the product is a SMESH plugin, else False
619 return ("properties" in product_info and
620 "smesh_plugin" in product_info.properties and
621 product_info.properties.smesh_plugin == "yes")
623 def product_is_cpp(product_info):
624 '''Know if a product is cpp
626 :param product_info Config: The configuration specific to
628 :return: True if the product is a cpp, else False
631 return ("properties" in product_info and
632 "cpp" in product_info.properties and
633 product_info.properties.cpp == "yes")
635 def product_compiles(product_info):
636 '''Know if a product compiles or not (some products do not have a
637 compilation procedure)
639 :param product_info Config: The configuration specific to
641 :return: True if the product compiles, else False
644 return not("properties" in product_info and
645 "compilation" in product_info.properties and
646 product_info.properties.compilation == "no")
648 def product_has_script(product_info):
649 '''Know if a product has a compilation script
651 :param product_info Config: The configuration specific to
653 :return: True if the product it has a compilation script, else False
656 if "build_source" not in product_info:
659 build_src = product_info.build_source
660 return build_src.lower() == 'script'
662 def product_has_env_script(product_info):
663 '''Know if a product has an environment script
665 :param product_info Config: The configuration specific to
667 :return: True if the product it has an environment script, else False
670 return "environ" in product_info and "env_script" in product_info.environ
672 def product_has_patches(product_info):
673 '''Know if a product has one or more patches
675 :param product_info Config: The configuration specific to
677 :return: True if the product has one or more patches
680 return "patches" in product_info and len(product_info.patches) > 0
682 def product_has_logo(product_info):
683 '''Know if a product has a logo (YACSGEN generate)
685 :param product_info Config: The configuration specific to
687 :return: The path of the logo if the product has a logo, else False
690 if ("properties" in product_info and
691 "logo" in product_info.properties):
692 return product_info.properties.logo
696 def product_has_salome_gui(product_info):
697 '''Know if a product has a SALOME gui
699 :param product_info Config: The configuration specific to
701 :return: True if the product has a SALOME gui, else False
704 return ("properties" in product_info and
705 "has_salome_gui" in product_info.properties and
706 product_info.properties.has_salome_gui == "yes")
708 def product_is_mpi(product_info):
709 '''Know if a product has openmpi in its dependencies
711 :param product_info Config: The configuration specific to
713 :return: True if the product has openmpi inits dependencies
716 return "openmpi" in product_info.depend
718 def product_is_generated(product_info):
719 '''Know if a product is generated (YACSGEN)
721 :param product_info Config: The configuration specific to
723 :return: True if the product is generated
726 return ("properties" in product_info and
727 "generate" in product_info.properties and
728 product_info.properties.generate == "yes")
730 def get_product_components(product_info):
731 '''Get the component list to generate with the product
733 :param product_info Config: The configuration specific to
735 :return: The list of names of the components
739 if not product_is_generated(product_info):
743 if "component_name" in product_info:
744 compo_list = product_info.component_name
746 if isinstance(compo_list, str):
747 compo_list = [ compo_list ]