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
20 In this file are implemented the methods
21 relative to the product notion of salomeTools
28 import src.debug as DBG
30 AVAILABLE_VCS = ['git', 'svn', 'cvs']
31 config_expression = "^config-\d+$"
32 VERSION_DELIMITER = "_to_"
34 def get_product_config(config, product_name, with_install_dir=True):
35 """Get the specific configuration of a product from the global configuration
37 :param config Config: The global configuration
38 :param product_name str: The name of the product
39 :param with_install_dir boolean: If false, do not provide an install
40 directory (at false only for internal use
41 of the function check_config_exists)
42 :return: the specific configuration of the product
46 # Get the version of the product from the application definition
47 version = config.APPLICATION.products[product_name]
48 # if no version, then take the default one defined in the application
49 if isinstance(version, bool):
50 version = config.APPLICATION.tag
52 # Define debug and dev modes
53 # Get the tag if a dictionary is given in APPLICATION.products for the
59 if isinstance(version, src.pyconf.Mapping):
62 if not 'tag' in dic_version:
63 version = config.APPLICATION.tag
65 version = dic_version.tag
67 # Get the debug if any
68 if 'debug' in dic_version:
69 debug = dic_version.debug
72 if 'dev' in dic_version:
76 if 'base' in dic_version:
77 base = dic_version.base
79 # Get the section if any
80 if 'section' in dic_version:
81 section = dic_version.section
84 # substitute some character with _ in order to get the correct definition
85 # in config.PRODUCTS. This is done because the pyconf tool does not handle
86 # the . and - characters
87 for c in ".-": vv = vv.replace(c, "_")
90 if product_name in config.PRODUCTS:
91 # Search for the product description in the configuration
92 prod_info = get_product_section(config, product_name, vv, section)
94 # merge opt_depend in depend
95 if prod_info is not None and 'opt_depend' in prod_info:
96 for depend in prod_info.opt_depend:
97 if depend in config.APPLICATION.products:
98 prod_info.depend.append(depend,'')
100 # In case of a product get with a vcs,
101 # put the tag (equal to the version)
102 if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
104 if prod_info.get_source == 'git':
105 prod_info.git_info.tag = version
107 if prod_info.get_source == 'svn':
108 prod_info.svn_info.tag = version
110 if prod_info.get_source == 'cvs':
111 prod_info.cvs_info.tag = version
113 # In case of a fixed product,
114 # define the install_dir (equal to the version)
115 if prod_info is not None and os.path.isdir(version):
116 prod_info.install_dir = version
117 prod_info.get_source = "fixed"
119 # Check if the product is defined as native in the application
120 if prod_info is not None:
121 if version == "native":
122 prod_info.get_source = "native"
123 elif prod_info.get_source == "native":
124 msg = _("The product %(prod)s has version %(ver)s but is "
125 "declared as native in its definition" %
126 { 'prod': prod_info.name, 'ver': version})
127 raise src.SatException(msg)
129 # If there is no definition but the product is declared as native,
130 # construct a new definition containing only the get_source key
131 if prod_info is None and version == "native":
132 prod_info = src.pyconf.Config()
133 prod_info.name = product_name
134 prod_info.get_source = "native"
136 # If there is no definition but the product is fixed,
137 # construct a new definition containing only the product name
138 if prod_info is None and os.path.isdir(version):
139 prod_info = src.pyconf.Config()
140 prod_info.name = product_name
141 prod_info.get_source = "fixed"
142 prod_info.addMapping("environ", src.pyconf.Mapping(prod_info), "")
145 # If prod_info is still None, it means that there is no product definition
146 # in the config. The user has to provide it.
147 if prod_info is None:
148 prod_pyconf_path = src.find_file_in_lpath(product_name + ".pyconf",
149 config.PATHS.PRODUCTPATH)
150 if not prod_pyconf_path:
152 No definition found for the product %(1)s.
153 Please create a %(2)s.pyconf file somewhere in:
157 "3": config.PATHS.PRODUCTPATH }
160 No definition corresponding to the version %(1)s was found in the file:
162 Please add a section in it.""") % {"1" : vv, "2" : prod_pyconf_path}
163 raise src.SatException(msg)
165 # Set the debug, dev and version keys
166 prod_info.debug = debug
168 prod_info.version = version
170 # Set the archive_info if the product is get in archive mode
171 if prod_info.get_source == "archive":
172 if not "archive_info" in prod_info:
173 prod_info.addMapping("archive_info",
174 src.pyconf.Mapping(prod_info),
176 if "archive_name" not in prod_info.archive_info:
177 arch_name = product_name + "-" + version + ".tar.gz"
178 arch_path = src.find_file_in_lpath(arch_name,
179 config.PATHS.ARCHIVEPATH)
181 msg = _("Archive %(1)s for %(2)s not found in config.PATHS.ARCHIVEPATH") % \
182 {"1" : arch_name, "2" : prod_info.name}
183 DBG.tofix(msg, config.PATHS.ARCHIVEPATH)
184 prod_info.archive_info.archive_name = arch_name #without path
185 # raise src.SatException(msg) #may be a warning, continue #8646
187 prod_info.archive_info.archive_name = arch_path
189 if (os.path.basename(prod_info.archive_info.archive_name) ==
190 prod_info.archive_info.archive_name):
191 arch_name = prod_info.archive_info.archive_name
192 arch_path = src.find_file_in_lpath(
194 config.PATHS.ARCHIVEPATH)
196 msg = _("Archive %(1)s for %(2)s not found in config.PATHS.ARCHIVEPATH") % \
197 {"1" : arch_name, "2" : prod_info.name}
198 DBG.tofix(msg, config.PATHS.ARCHIVEPATH) #avoid 2 messages in compile
199 prod_info.archive_info.archive_name = arch_name #without path
200 # raise src.SatException(msg) #may be a warning, continue #8646
201 prod_info.archive_info.archive_name = arch_path
203 # If the product compiles with a script, check the script existence
204 # and if it is executable
205 if product_has_script(prod_info):
206 # Check the compil_script key existence
207 if "compil_script" not in prod_info:
209 No compilation script found for the product %s.
210 Please provide a 'compil_script' key in its definition.""") % product_name
211 raise src.SatException(msg)
213 # Get the path of the script
214 script = prod_info.compil_script
215 script_name = os.path.basename(script)
216 if script == script_name:
217 # Only a name is given. Search in the default directory
218 script_path = src.find_file_in_lpath(script_name,
219 config.PATHS.PRODUCTPATH,
222 raise src.SatException(
223 _("Compilation script not found: %s") % script_name)
224 prod_info.compil_script = script_path
225 if src.architecture.is_windows():
226 prod_info.compil_script = prod_info.compil_script[:-len(".sh")] + ".bat"
228 # Check that the script is executable
229 if not os.access(prod_info.compil_script, os.X_OK):
230 #raise src.SatException(
231 # _("Compilation script cannot be executed: %s") %
232 # prod_info.compil_script)
233 DBG.tofix("Compilation script cannot be executed:", prod_info.compil_script)
235 # Get the full paths of all the patches
236 if product_has_patches(prod_info):
239 for patch in prod_info.patches:
241 # If only a filename, then search for the patch in the PRODUCTPATH
242 if os.path.basename(patch_path) == patch_path:
243 # Search in the PRODUCTPATH/patches
244 patch_path = src.find_file_in_lpath(patch,
245 config.PATHS.PRODUCTPATH,
248 msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
249 "\n" % {"patch_name" : patch,
250 "prod_name" : prod_info.name})
251 raise src.SatException(msg)
252 patches.append(patch_path)
254 DBG.tofix("problem in prod_info.patches", prod_info)
255 prod_info.patches = patches
257 # Get the full paths of the environment scripts
258 if product_has_env_script(prod_info):
259 env_script_path = prod_info.environ.env_script
260 # If only a filename, then search for the environment script
261 # in the PRODUCTPATH/env_scripts
262 if os.path.basename(env_script_path) == env_script_path:
263 # Search in the PRODUCTPATH/env_scripts
264 env_script_path = src.find_file_in_lpath(
265 prod_info.environ.env_script,
266 config.PATHS.PRODUCTPATH,
268 if not env_script_path:
269 msg = _("Environment script %(env_name)s for %(prod_name)s not "
270 "found.\n" % {"env_name" : env_script_path,
271 "prod_name" : prod_info.name})
272 raise src.SatException(msg)
274 prod_info.environ.env_script = env_script_path
277 # The variable with_install_dir is at false only for internal use
278 # of the function get_install_dir
280 # Save the install_dir key if there is any
281 if "install_dir" in prod_info and not "install_dir_save" in prod_info:
282 prod_info.install_dir_save = prod_info.install_dir
284 # if it is not the first time the install_dir is computed, it means
285 # that install_dir_save exists and it has to be taken into account.
286 if "install_dir_save" in prod_info:
287 prod_info.install_dir = prod_info.install_dir_save
289 # Set the install_dir key
290 prod_info.install_dir = get_install_dir(config, base, version, prod_info)
294 def get_product_section(config, product_name, version, section=None):
295 """Get the product description from the configuration
297 :param config Config: The global configuration
298 :param product_name str: The product name
299 :param version str: The version of the product
300 :param section str: The searched section (if not None, the section is
302 :return: The product description
306 # if section is not None, try to get the corresponding section
308 if section not in config.PRODUCTS[product_name]:
310 # returns specific information for the given version
311 prod_info = config.PRODUCTS[product_name][section]
312 prod_info.section = section
313 prod_info.from_file = config.PRODUCTS[product_name].from_file
316 # If it exists, get the information of the product_version
317 if "version_" + version in config.PRODUCTS[product_name]:
318 # returns specific information for the given version
319 prod_info = config.PRODUCTS[product_name]["version_" + version]
320 prod_info.section = "version_" + version
321 prod_info.from_file = config.PRODUCTS[product_name].from_file
324 # Else, check if there is a description for multiple versions
325 l_section_name = config.PRODUCTS[product_name].keys()
326 l_section_ranges = [section_name for section_name in l_section_name
327 if VERSION_DELIMITER in section_name]
328 for section_range in l_section_ranges:
329 minimum, maximum = section_range.split(VERSION_DELIMITER)
330 if (src.only_numbers(version) >= src.only_numbers(minimum)
331 and src.only_numbers(version) <= src.only_numbers(maximum)):
332 # returns specific information for the versions
333 prod_info = config.PRODUCTS[product_name][section_range]
334 prod_info.section = section_range
335 prod_info.from_file = config.PRODUCTS[product_name].from_file
338 # Else, get the standard informations
339 if "default" in config.PRODUCTS[product_name]:
340 # returns the generic information (given version not found)
341 prod_info = config.PRODUCTS[product_name].default
342 prod_info.section = "default"
343 prod_info.from_file = config.PRODUCTS[product_name].from_file
346 # if noting was found, return None
349 def get_install_dir(config, base, version, prod_info):
350 """Compute the installation directory of a given product
352 :param config Config: The global configuration
353 :param base str: This corresponds to the value given by user in its
354 application.pyconf for the specific product. If "yes", the
355 user wants the product to be in base. If "no", he wants the
356 product to be in the application workdir
357 :param version str: The version of the product
358 :param product_info Config: The configuration specific to
361 :return: The path of the product installation
366 if (("install_dir" in prod_info and prod_info.install_dir == "base")
369 if (base == "no" or ("no_base" in config.APPLICATION
370 and config.APPLICATION.no_base == "yes")):
374 install_dir = get_base_install_dir(config, prod_info, version)
376 if "install_dir" not in prod_info or prod_info.install_dir == "base":
377 # Set it to the default value (in application directory)
378 install_dir = os.path.join(config.APPLICATION.workdir,
382 install_dir = prod_info.install_dir
386 def get_base_install_dir(config, prod_info, version):
387 """Compute the installation directory of a product in base
389 :param config Config: The global configuration
390 :param product_info Config: The configuration specific to
392 :param version str: The version of the product
393 :return: The path of the product installation
396 base_path = src.get_base_path(config)
397 prod_dir = os.path.join(base_path, prod_info.name + "-" + version)
398 if not os.path.exists(prod_dir):
399 return os.path.join(prod_dir, "config-1")
401 exists, install_dir = check_config_exists(config, prod_dir, prod_info)
405 # Find the first config-<i> directory that is available in the product
410 install_dir = os.path.join(prod_dir, "config-%i" % label)
411 if os.path.exists(install_dir):
418 def check_config_exists(config, prod_dir, prod_info):
420 Verify that the installation directory of a product in a base exists
421 Check all the config-<i> directory and verify the sat-config.pyconf file
424 :param config Config: The global configuration
425 :param prod_dir str: The product installation directory path
427 :param product_info Config: The configuration specific to
429 :return: True or false is the installation is found or not
430 and if it is found, the path of the found installation
431 :rtype: (boolean, str)
433 # check if the directories or files of the directory corresponds to the
434 # directory installation of the product
435 l_dir_and_files = os.listdir(prod_dir)
436 for dir_or_file in l_dir_and_files:
437 oExpr = re.compile(config_expression)
438 if not(oExpr.search(dir_or_file)):
439 # not config-<i>, not interesting
441 # check if there is the file sat-config.pyconf file in the installation
443 config_file = os.path.join(prod_dir, dir_or_file, src.CONFIG_FILENAME)
444 if not os.path.exists(config_file):
447 # If there is no dependency, it is the right path
448 if len(prod_info.depend)==0:
449 compile_cfg = src.pyconf.Config(config_file)
450 if len(compile_cfg) == 0:
451 return True, os.path.join(prod_dir, dir_or_file)
454 # check if there is the config described in the file corresponds the
455 # dependencies of the product
456 config_corresponds = True
457 compile_cfg = src.pyconf.Config(config_file)
458 for prod_dep in prod_info.depend:
459 # if the dependency is not in the config,
460 # the config does not correspond
461 if prod_dep not in compile_cfg:
462 config_corresponds = False
465 prod_dep_info = get_product_config(config, prod_dep, False)
466 # If the version of the dependency does not correspond,
467 # the config does not correspond
468 if prod_dep_info.version != compile_cfg[prod_dep]:
469 config_corresponds = False
472 for prod_name in compile_cfg:
473 if prod_name not in prod_info.depend:
474 config_corresponds = False
477 if config_corresponds:
478 return True, os.path.join(prod_dir, dir_or_file)
484 def get_products_infos(lproducts, config):
485 """Get the specific configuration of a list of products
487 :param lproducts List: The list of product names
488 :param config Config: The global configuration
489 :return: the list of tuples
490 (product name, specific configuration of the product)
491 :rtype: [(str, Config)]
494 # Loop on product names
495 for prod in lproducts:
496 # Get the specific configuration of the product
497 prod_info = get_product_config(config, prod)
498 if prod_info is not None:
499 products_infos.append((prod, prod_info))
501 msg = _("The %s product has no definition "
502 "in the configuration.") % prod
503 raise src.SatException(msg)
504 return products_infos
506 def get_product_dependencies(config, product_info):
508 Get recursively the list of products that are
509 in the product_info dependencies
511 :param config Config: The global configuration
512 :param product_info Config: The configuration specific to
514 :return: the list of products in dependence
517 if "depend" not in product_info or product_info.depend == []:
520 for prod in product_info.depend:
521 if prod == product_info.name:
525 prod_info = get_product_config(config, prod)
526 dep_prod = get_product_dependencies(config, prod_info)
527 for prod_in_dep in dep_prod:
528 if prod_in_dep not in res:
529 res.append(prod_in_dep)
532 def check_installation(product_info):
534 Verify if a product is well installed. Checks install directory presence
535 and some additional files if it is defined in the config
537 :param product_info Config: The configuration specific to
539 :return: True if it is well installed
542 if not product_compiles(product_info):
544 install_dir = product_info.install_dir
545 if not os.path.exists(install_dir):
547 if ("present_files" in product_info and
548 "install" in product_info.present_files):
549 for file_relative_path in product_info.present_files.install:
550 file_path = os.path.join(install_dir, file_relative_path)
551 if not os.path.exists(file_path):
555 def check_source(product_info):
556 """Verify if a sources of product is preset. Checks source directory presence
558 :param product_info Config: The configuration specific to
560 :return: True if it is well installed
563 DBG.write("check_source product_info", product_info)
564 source_dir = product_info.source_dir
565 if not os.path.exists(source_dir):
567 if ("present_files" in product_info and
568 "source" in product_info.present_files):
569 for file_relative_path in product_info.present_files.source:
570 file_path = os.path.join(source_dir, file_relative_path)
571 if not os.path.exists(file_path):
575 def product_is_sample(product_info):
576 """Know if a product has the sample type
578 :param product_info Config: The configuration specific to
580 :return: True if the product has the sample type, else False
583 if 'type' in product_info:
584 ptype = product_info.type
585 return ptype.lower() == 'sample'
589 def product_is_salome(product_info):
590 """Know if a product is a SALOME module
592 :param product_info Config: The configuration specific to
594 :return: True if the product is a SALOME module, else False
597 return ("properties" in product_info and
598 "is_SALOME_module" in product_info.properties and
599 product_info.properties.is_SALOME_module == "yes")
601 def product_is_fixed(product_info):
602 """Know if a product is fixed
604 :param product_info Config: The configuration specific to
606 :return: True if the product is fixed, else False
609 get_src = product_info.get_source
610 return get_src.lower() == 'fixed'
612 def product_is_native(product_info):
613 """Know if a product is native
615 :param product_info Config: The configuration specific to
617 :return: True if the product is native, else False
620 get_src = product_info.get_source
621 return get_src.lower() == 'native'
623 def product_is_dev(product_info):
624 """Know if a product is in dev mode
626 :param product_info Config: The configuration specific to
628 :return: True if the product is in dev mode, else False
631 dev = product_info.dev
632 res = (dev.lower() == 'yes')
633 DBG.write('product_is_dev %s' % product_info.name, res)
634 # if product_info.name == "XDATA": return True #test #10569
637 def product_is_debug(product_info):
638 """Know if a product is in debug mode
640 :param product_info Config: The configuration specific to
642 :return: True if the product is in debug mode, else False
645 debug = product_info.debug
646 return debug.lower() == 'yes'
648 def product_is_autotools(product_info):
649 """Know if a product is compiled using the autotools
651 :param product_info Config: The configuration specific to
653 :return: True if the product is autotools, else False
656 build_src = product_info.build_source
657 return build_src.lower() == 'autotools'
659 def product_is_cmake(product_info):
660 """Know if a product is compiled using the cmake
662 :param product_info Config: The configuration specific to
664 :return: True if the product is cmake, else False
667 build_src = product_info.build_source
668 return build_src.lower() == 'cmake'
670 def product_is_vcs(product_info):
671 """Know if a product is download using git, svn or cvs (not archive)
673 :param product_info Config: The configuration specific to
675 :return: True if the product is vcs, else False
678 return product_info.get_source in AVAILABLE_VCS
680 def product_is_smesh_plugin(product_info):
681 """Know if a product is a SMESH plugin
683 :param product_info Config: The configuration specific to
685 :return: True if the product is a SMESH plugin, else False
688 return ("properties" in product_info and
689 "smesh_plugin" in product_info.properties and
690 product_info.properties.smesh_plugin == "yes")
692 def product_is_cpp(product_info):
693 """Know if a product is cpp
695 :param product_info Config: The configuration specific to
697 :return: True if the product is a cpp, else False
700 return ("properties" in product_info and
701 "cpp" in product_info.properties and
702 product_info.properties.cpp == "yes")
704 def product_compiles(product_info):
706 Know if a product compiles or not
707 (some products do not have a compilation procedure)
709 :param product_info Config: The configuration specific to
711 :return: True if the product compiles, else False
714 return not("properties" in product_info and
715 "compilation" in product_info.properties and
716 product_info.properties.compilation == "no")
718 def product_has_script(product_info):
719 """Know if a product has a compilation script
721 :param product_info Config: The configuration specific to
723 :return: True if the product it has a compilation script, else False
726 if "build_source" not in product_info:
729 build_src = product_info.build_source
730 return build_src.lower() == 'script'
732 def product_has_env_script(product_info):
733 """Know if a product has an environment script
735 :param product_info Config: The configuration specific to
737 :return: True if the product it has an environment script, else False
740 return "environ" in product_info and "env_script" in product_info.environ
742 def product_has_patches(product_info):
743 """Know if a product has one or more patches
745 :param product_info Config: The configuration specific to
747 :return: True if the product has one or more patches
750 res = ( "patches" in product_info and len(product_info.patches) > 0 )
751 DBG.write('product_has_patches %s' % product_info.name, res)
752 # if product_info.name == "XDATA": return True #test #10569
755 def product_has_logo(product_info):
756 """Know if a product has a logo (YACSGEN generate)
758 :param product_info Config: The configuration specific to
760 :return: The path of the logo if the product has a logo, else False
763 if ("properties" in product_info and
764 "logo" in product_info.properties):
765 return product_info.properties.logo
769 def product_has_salome_gui(product_info):
770 """Know if a product has a SALOME gui
772 :param product_info Config: The configuration specific to
774 :return: True if the product has a SALOME gui, else False
777 return ("properties" in product_info and
778 "has_salome_gui" in product_info.properties and
779 product_info.properties.has_salome_gui == "yes")
781 def product_is_mpi(product_info):
782 """Know if a product has openmpi in its dependencies
784 :param product_info Config: The configuration specific to
786 :return: True if the product has openmpi inits dependencies
789 return "openmpi" in product_info.depend
791 def product_is_generated(product_info):
792 """Know if a product is generated (YACSGEN)
794 :param product_info Config: The configuration specific to
796 :return: True if the product is generated
799 return ("properties" in product_info and
800 "generate" in product_info.properties and
801 product_info.properties.generate == "yes")
803 def get_product_components(product_info):
804 """Get the component list to generate with the product
806 :param product_info Config: The configuration specific to
808 :return: The list of names of the components
812 if not product_is_generated(product_info):
816 if "component_name" in product_info:
817 compo_list = product_info.component_name
819 if isinstance(compo_list, str):
820 compo_list = [ compo_list ]