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
202 prod_info.archive_info.archive_name = arch_path
205 # If the product compiles with a script, check the script existence
206 # and if it is executable
207 if product_has_script(prod_info):
208 # Check the compil_script key existence
209 if "compil_script" not in prod_info:
211 No compilation script found for the product %s.
212 Please provide a 'compil_script' key in its definition.""") % product_name
213 raise src.SatException(msg)
215 # Get the path of the script
216 script = prod_info.compil_script
217 script_name = os.path.basename(script)
218 if script == script_name:
219 # Only a name is given. Search in the default directory
220 script_path = src.find_file_in_lpath(script_name,
221 config.PATHS.PRODUCTPATH,
224 raise src.SatException(
225 _("Compilation script not found: %s") % script_name)
226 prod_info.compil_script = script_path
227 if src.architecture.is_windows():
228 prod_info.compil_script = prod_info.compil_script[:-len(".sh")] + ".bat"
230 # Check that the script is executable
231 if not os.access(prod_info.compil_script, os.X_OK):
232 #raise src.SatException(
233 # _("Compilation script cannot be executed: %s") %
234 # prod_info.compil_script)
235 DBG.tofix("Compilation script cannot be executed:", prod_info.compil_script)
237 # Get the full paths of all the patches
238 if product_has_patches(prod_info):
241 for patch in prod_info.patches:
243 # If only a filename, then search for the patch in the PRODUCTPATH
244 if os.path.basename(patch_path) == patch_path:
245 # Search in the PRODUCTPATH/patches
246 patch_path = src.find_file_in_lpath(patch,
247 config.PATHS.PRODUCTPATH,
250 msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
251 "\n" % {"patch_name" : patch,
252 "prod_name" : prod_info.name})
253 raise src.SatException(msg)
254 patches.append(patch_path)
256 DBG.tofix("problem in prod_info.patches", prod_info)
257 prod_info.patches = patches
259 # Get the full paths of the environment scripts
260 if product_has_env_script(prod_info):
261 env_script_path = prod_info.environ.env_script
262 # If only a filename, then search for the environment script
263 # in the PRODUCTPATH/env_scripts
264 if os.path.basename(env_script_path) == env_script_path:
265 # Search in the PRODUCTPATH/env_scripts
266 env_script_path = src.find_file_in_lpath(
267 prod_info.environ.env_script,
268 config.PATHS.PRODUCTPATH,
270 if not env_script_path:
271 msg = _("Environment script %(env_name)s for %(prod_name)s not "
272 "found.\n" % {"env_name" : env_script_path,
273 "prod_name" : prod_info.name})
274 raise src.SatException(msg)
276 prod_info.environ.env_script = env_script_path
279 # The variable with_install_dir is at false only for internal use
280 # of the function get_install_dir
282 # Save the install_dir key if there is any
283 if "install_dir" in prod_info and not "install_dir_save" in prod_info:
284 prod_info.install_dir_save = prod_info.install_dir
286 # if it is not the first time the install_dir is computed, it means
287 # that install_dir_save exists and it has to be taken into account.
288 if "install_dir_save" in prod_info:
289 prod_info.install_dir = prod_info.install_dir_save
291 # Set the install_dir key
292 prod_info.install_dir = get_install_dir(config, base, version, prod_info)
296 def get_product_section(config, product_name, version, section=None):
297 """Get the product description from the configuration
299 :param config Config: The global configuration
300 :param product_name str: The product name
301 :param version str: The version of the product
302 :param section str: The searched section (if not None, the section is
304 :return: The product description
308 # if section is not None, try to get the corresponding section
310 if section not in config.PRODUCTS[product_name]:
312 # returns specific information for the given version
313 prod_info = config.PRODUCTS[product_name][section]
314 prod_info.section = section
315 prod_info.from_file = config.PRODUCTS[product_name].from_file
318 # If it exists, get the information of the product_version
319 if "version_" + version in config.PRODUCTS[product_name]:
320 # returns specific information for the given version
321 prod_info = config.PRODUCTS[product_name]["version_" + version]
322 prod_info.section = "version_" + version
323 prod_info.from_file = config.PRODUCTS[product_name].from_file
326 # Else, check if there is a description for multiple versions
327 l_section_name = config.PRODUCTS[product_name].keys()
328 l_section_ranges = [section_name for section_name in l_section_name
329 if VERSION_DELIMITER in section_name]
330 for section_range in l_section_ranges:
331 minimum, maximum = section_range.split(VERSION_DELIMITER)
332 if (src.only_numbers(version) >= src.only_numbers(minimum)
333 and src.only_numbers(version) <= src.only_numbers(maximum)):
334 # returns specific information for the versions
335 prod_info = config.PRODUCTS[product_name][section_range]
336 prod_info.section = section_range
337 prod_info.from_file = config.PRODUCTS[product_name].from_file
340 # Else, get the standard informations
341 if "default" in config.PRODUCTS[product_name]:
342 # returns the generic information (given version not found)
343 prod_info = config.PRODUCTS[product_name].default
344 prod_info.section = "default"
345 prod_info.from_file = config.PRODUCTS[product_name].from_file
348 # if noting was found, return None
351 def get_install_dir(config, base, version, prod_info):
352 """Compute the installation directory of a given product
354 :param config Config: The global configuration
355 :param base str: This corresponds to the value given by user in its
356 application.pyconf for the specific product. If "yes", the
357 user wants the product to be in base. If "no", he wants the
358 product to be in the application workdir
359 :param version str: The version of the product
360 :param product_info Config: The configuration specific to
363 :return: The path of the product installation
368 if (("install_dir" in prod_info and prod_info.install_dir == "base")
371 if (base == "no" or ("no_base" in config.APPLICATION
372 and config.APPLICATION.no_base == "yes")):
376 install_dir = get_base_install_dir(config, prod_info, version)
378 if "install_dir" not in prod_info or prod_info.install_dir == "base":
379 # Set it to the default value (in application directory)
380 install_dir = os.path.join(config.APPLICATION.workdir,
384 install_dir = prod_info.install_dir
388 def get_base_install_dir(config, prod_info, version):
389 """Compute the installation directory of a product in base
391 :param config Config: The global configuration
392 :param product_info Config: The configuration specific to
394 :param version str: The version of the product
395 :return: The path of the product installation
398 base_path = src.get_base_path(config)
399 prod_dir = os.path.join(base_path, prod_info.name + "-" + version)
400 if not os.path.exists(prod_dir):
401 return os.path.join(prod_dir, "config-1")
403 exists, install_dir = check_config_exists(config, prod_dir, prod_info)
407 # Find the first config-<i> directory that is available in the product
412 install_dir = os.path.join(prod_dir, "config-%i" % label)
413 if os.path.exists(install_dir):
420 def check_config_exists(config, prod_dir, prod_info):
422 Verify that the installation directory of a product in a base exists
423 Check all the config-<i> directory and verify the sat-config.pyconf file
426 :param config Config: The global configuration
427 :param prod_dir str: The product installation directory path
429 :param product_info Config: The configuration specific to
431 :return: True or false is the installation is found or not
432 and if it is found, the path of the found installation
433 :rtype: (boolean, str)
435 # check if the directories or files of the directory corresponds to the
436 # directory installation of the product
437 l_dir_and_files = os.listdir(prod_dir)
438 for dir_or_file in l_dir_and_files:
439 oExpr = re.compile(config_expression)
440 if not(oExpr.search(dir_or_file)):
441 # not config-<i>, not interesting
443 # check if there is the file sat-config.pyconf file in the installation
445 config_file = os.path.join(prod_dir, dir_or_file, src.CONFIG_FILENAME)
446 if not os.path.exists(config_file):
449 # If there is no dependency, it is the right path
450 if len(prod_info.depend)==0:
451 compile_cfg = src.pyconf.Config(config_file)
452 if len(compile_cfg) == 0:
453 return True, os.path.join(prod_dir, dir_or_file)
456 # check if there is the config described in the file corresponds the
457 # dependencies of the product
458 config_corresponds = True
459 compile_cfg = src.pyconf.Config(config_file)
460 for prod_dep in prod_info.depend:
461 # if the dependency is not in the config,
462 # the config does not correspond
463 if prod_dep not in compile_cfg:
464 config_corresponds = False
467 prod_dep_info = get_product_config(config, prod_dep, False)
468 # If the version of the dependency does not correspond,
469 # the config does not correspond
470 if prod_dep_info.version != compile_cfg[prod_dep]:
471 config_corresponds = False
474 for prod_name in compile_cfg:
475 if prod_name not in prod_info.depend:
476 config_corresponds = False
479 if config_corresponds:
480 return True, os.path.join(prod_dir, dir_or_file)
486 def get_products_infos(lproducts, config):
487 """Get the specific configuration of a list of products
489 :param lproducts List: The list of product names
490 :param config Config: The global configuration
491 :return: the list of tuples
492 (product name, specific configuration of the product)
493 :rtype: [(str, Config)]
496 # Loop on product names
497 for prod in lproducts:
498 # Get the specific configuration of the product
499 prod_info = get_product_config(config, prod)
500 if prod_info is not None:
501 products_infos.append((prod, prod_info))
503 msg = _("The %s product has no definition "
504 "in the configuration.") % prod
505 raise src.SatException(msg)
506 return products_infos
508 def get_product_dependencies(config, product_info):
510 Get recursively the list of products that are
511 in the product_info dependencies
513 :param config Config: The global configuration
514 :param product_info Config: The configuration specific to
516 :return: the list of products in dependence
519 if "depend" not in product_info or product_info.depend == []:
522 for prod in product_info.depend:
523 if prod == product_info.name:
527 prod_info = get_product_config(config, prod)
528 dep_prod = get_product_dependencies(config, prod_info)
529 for prod_in_dep in dep_prod:
530 if prod_in_dep not in res:
531 res.append(prod_in_dep)
534 def check_installation(product_info):
536 Verify if a product is well installed. Checks install directory presence
537 and some additional files if it is defined in the config
539 :param product_info Config: The configuration specific to
541 :return: True if it is well installed
544 if not product_compiles(product_info):
546 install_dir = product_info.install_dir
547 if not os.path.exists(install_dir):
549 if ("present_files" in product_info and
550 "install" in product_info.present_files):
551 for file_relative_path in product_info.present_files.install:
552 file_path = os.path.join(install_dir, file_relative_path)
553 if not os.path.exists(file_path):
557 def check_source(product_info):
558 """Verify if a sources of product is preset. Checks source directory presence
560 :param product_info Config: The configuration specific to
562 :return: True if it is well installed
565 DBG.write("check_source product_info", product_info)
566 source_dir = product_info.source_dir
567 if not os.path.exists(source_dir):
569 if ("present_files" in product_info and
570 "source" in product_info.present_files):
571 for file_relative_path in product_info.present_files.source:
572 file_path = os.path.join(source_dir, file_relative_path)
573 if not os.path.exists(file_path):
577 def product_is_salome(product_info):
578 """Know if a product is a SALOME module
580 :param product_info Config: The configuration specific to
582 :return: True if the product is a SALOME module, else False
585 return ("properties" in product_info and
586 "is_SALOME_module" in product_info.properties and
587 product_info.properties.is_SALOME_module == "yes")
589 def product_is_fixed(product_info):
590 """Know if a product is fixed
592 :param product_info Config: The configuration specific to
594 :return: True if the product is fixed, else False
597 get_src = product_info.get_source
598 return get_src.lower() == 'fixed'
600 def product_is_native(product_info):
601 """Know if a product is native
603 :param product_info Config: The configuration specific to
605 :return: True if the product is native, else False
608 get_src = product_info.get_source
609 return get_src.lower() == 'native'
611 def product_is_dev(product_info):
612 """Know if a product is in dev mode
614 :param product_info Config: The configuration specific to
616 :return: True if the product is in dev mode, else False
619 dev = product_info.dev
620 res = (dev.lower() == 'yes')
621 DBG.write('product_is_dev %s' % product_info.name, res)
622 # if product_info.name == "XDATA": return True #test #10569
625 def product_is_debug(product_info):
626 """Know if a product is in debug mode
628 :param product_info Config: The configuration specific to
630 :return: True if the product is in debug mode, else False
633 debug = product_info.debug
634 return debug.lower() == 'yes'
636 def product_is_autotools(product_info):
637 """Know if a product is compiled using the autotools
639 :param product_info Config: The configuration specific to
641 :return: True if the product is autotools, else False
644 build_src = product_info.build_source
645 return build_src.lower() == 'autotools'
647 def product_is_cmake(product_info):
648 """Know if a product is compiled using the cmake
650 :param product_info Config: The configuration specific to
652 :return: True if the product is cmake, else False
655 build_src = product_info.build_source
656 return build_src.lower() == 'cmake'
658 def product_is_vcs(product_info):
659 """Know if a product is download using git, svn or cvs (not archive)
661 :param product_info Config: The configuration specific to
663 :return: True if the product is vcs, else False
666 return product_info.get_source in AVAILABLE_VCS
668 def product_is_smesh_plugin(product_info):
669 """Know if a product is a SMESH plugin
671 :param product_info Config: The configuration specific to
673 :return: True if the product is a SMESH plugin, else False
676 return ("properties" in product_info and
677 "smesh_plugin" in product_info.properties and
678 product_info.properties.smesh_plugin == "yes")
680 def product_is_cpp(product_info):
681 """Know if a product is cpp
683 :param product_info Config: The configuration specific to
685 :return: True if the product is a cpp, else False
688 return ("properties" in product_info and
689 "cpp" in product_info.properties and
690 product_info.properties.cpp == "yes")
692 def product_compiles(product_info):
694 Know if a product compiles or not
695 (some products do not have a compilation procedure)
697 :param product_info Config: The configuration specific to
699 :return: True if the product compiles, else False
702 return not("properties" in product_info and
703 "compilation" in product_info.properties and
704 product_info.properties.compilation == "no")
706 def product_has_script(product_info):
707 """Know if a product has a compilation script
709 :param product_info Config: The configuration specific to
711 :return: True if the product it has a compilation script, else False
714 if "build_source" not in product_info:
717 build_src = product_info.build_source
718 return build_src.lower() == 'script'
720 def product_has_env_script(product_info):
721 """Know if a product has an environment script
723 :param product_info Config: The configuration specific to
725 :return: True if the product it has an environment script, else False
728 return "environ" in product_info and "env_script" in product_info.environ
730 def product_has_patches(product_info):
731 """Know if a product has one or more patches
733 :param product_info Config: The configuration specific to
735 :return: True if the product has one or more patches
738 res = ( "patches" in product_info and len(product_info.patches) > 0 )
739 DBG.write('product_has_patches %s' % product_info.name, res)
740 # if product_info.name == "XDATA": return True #test #10569
743 def product_has_logo(product_info):
744 """Know if a product has a logo (YACSGEN generate)
746 :param product_info Config: The configuration specific to
748 :return: The path of the logo if the product has a logo, else False
751 if ("properties" in product_info and
752 "logo" in product_info.properties):
753 return product_info.properties.logo
757 def product_has_salome_gui(product_info):
758 """Know if a product has a SALOME gui
760 :param product_info Config: The configuration specific to
762 :return: True if the product has a SALOME gui, else False
765 return ("properties" in product_info and
766 "has_salome_gui" in product_info.properties and
767 product_info.properties.has_salome_gui == "yes")
769 def product_is_mpi(product_info):
770 """Know if a product has openmpi in its dependencies
772 :param product_info Config: The configuration specific to
774 :return: True if the product has openmpi inits dependencies
777 return "openmpi" in product_info.depend
779 def product_is_generated(product_info):
780 """Know if a product is generated (YACSGEN)
782 :param product_info Config: The configuration specific to
784 :return: True if the product is generated
787 return ("properties" in product_info and
788 "generate" in product_info.properties and
789 product_info.properties.generate == "yes")
791 def get_product_components(product_info):
792 """Get the component list to generate with the product
794 :param product_info Config: The configuration specific to
796 :return: The list of names of the components
800 if not product_is_generated(product_info):
804 if "component_name" in product_info:
805 compo_list = product_info.component_name
807 if isinstance(compo_list, str):
808 compo_list = [ compo_list ]