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
60 if isinstance(version, src.pyconf.Mapping):
63 if not 'tag' in dic_version:
64 version = config.APPLICATION.tag
66 version = dic_version.tag
68 # Get the debug if any
69 if 'debug' in dic_version:
70 debug = dic_version.debug
72 # Get the verbose if any
73 if 'verbose' in dic_version:
74 verbose = dic_version.verbose
77 if 'dev' in dic_version:
81 if 'base' in dic_version:
82 base = dic_version.base
84 # Get the section if any
85 if 'section' in dic_version:
86 section = dic_version.section
89 # substitute some character with _ in order to get the correct definition
90 # in config.PRODUCTS. This is done because the pyconf tool does not handle
91 # the . and - characters
92 for c in ".-": vv = vv.replace(c, "_")
95 if product_name in config.PRODUCTS:
96 # Search for the product description in the configuration
97 prod_info = get_product_section(config, product_name, vv, section)
99 # merge opt_depend in depend
100 if prod_info is not None and 'opt_depend' in prod_info:
101 for depend in prod_info.opt_depend:
102 if depend in config.APPLICATION.products:
103 prod_info.depend.append(depend,'')
105 # In case of a product get with a vcs,
106 # put the tag (equal to the version)
107 if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
109 if prod_info.get_source == 'git':
110 prod_info.git_info.tag = version
112 if prod_info.get_source == 'svn':
113 prod_info.svn_info.tag = version
115 if prod_info.get_source == 'cvs':
116 prod_info.cvs_info.tag = version
118 # In case of a fixed product,
119 # define the install_dir (equal to the version)
120 if prod_info is not None and os.path.isdir(version):
121 prod_info.install_dir = version
122 prod_info.get_source = "fixed"
124 # Check if the product is defined as native in the application
125 if prod_info is not None:
126 if version == "native":
127 prod_info.get_source = "native"
128 elif prod_info.get_source == "native":
129 msg = _("The product %(prod)s has version %(ver)s but is "
130 "declared as native in its definition" %
131 { 'prod': prod_info.name, 'ver': version})
132 raise src.SatException(msg)
134 # If there is no definition but the product is declared as native,
135 # construct a new definition containing only the get_source key
136 if prod_info is None and version == "native":
137 prod_info = src.pyconf.Config()
138 prod_info.name = product_name
139 prod_info.get_source = "native"
141 # If there is no definition but the product is fixed,
142 # construct a new definition containing only the product name
143 if prod_info is None and os.path.isdir(version):
144 prod_info = src.pyconf.Config()
145 prod_info.name = product_name
146 prod_info.get_source = "fixed"
147 prod_info.addMapping("environ", src.pyconf.Mapping(prod_info), "")
150 # If prod_info is still None, it means that there is no product definition
151 # in the config. The user has to provide it.
152 if prod_info is None:
153 prod_pyconf_path = src.find_file_in_lpath(product_name + ".pyconf",
154 config.PATHS.PRODUCTPATH)
155 if not prod_pyconf_path:
157 No definition found for the product %(1)s.
158 Please create a %(2)s.pyconf file somewhere in:
162 "3": config.PATHS.PRODUCTPATH }
165 No definition corresponding to the version %(1)s was found in the file:
167 Please add a section in it.""") % {"1" : vv, "2" : prod_pyconf_path}
168 raise src.SatException(msg)
170 # Set the debug, dev and version keys
171 prod_info.debug = debug
172 prod_info.verbose = verbose
174 prod_info.version = version
176 # Set the archive_info if the product is get in archive mode
177 if prod_info.get_source == "archive":
178 if not "archive_info" in prod_info:
179 prod_info.addMapping("archive_info",
180 src.pyconf.Mapping(prod_info),
182 if "archive_name" not in prod_info.archive_info:
183 arch_name = product_name + "-" + version + ".tar.gz"
184 arch_path = src.find_file_in_lpath(arch_name,
185 config.PATHS.ARCHIVEPATH)
187 msg = _("Archive %(1)s for %(2)s not found in config.PATHS.ARCHIVEPATH") % \
188 {"1" : arch_name, "2" : prod_info.name}
189 DBG.tofix(msg, config.PATHS.ARCHIVEPATH)
190 prod_info.archive_info.archive_name = arch_name #without path
191 # raise src.SatException(msg) #may be a warning, continue #8646
193 prod_info.archive_info.archive_name = arch_path
195 if (os.path.basename(prod_info.archive_info.archive_name) ==
196 prod_info.archive_info.archive_name):
197 arch_name = prod_info.archive_info.archive_name
198 arch_path = src.find_file_in_lpath(
200 config.PATHS.ARCHIVEPATH)
202 msg = _("Archive %(1)s for %(2)s not found in config.PATHS.ARCHIVEPATH") % \
203 {"1" : arch_name, "2" : prod_info.name}
204 DBG.tofix(msg, config.PATHS.ARCHIVEPATH) #avoid 2 messages in compile
205 prod_info.archive_info.archive_name = arch_name #without path
206 # raise src.SatException(msg) #may be a warning, continue #8646
208 prod_info.archive_info.archive_name = arch_path
211 # If the product compiles with a script, check the script existence
212 # and if it is executable
213 if product_has_script(prod_info):
214 # Check the compil_script key existence
215 if "compil_script" not in prod_info:
217 No compilation script found for the product %s.
218 Please provide a 'compil_script' key in its definition.""") % product_name
219 raise src.SatException(msg)
221 # Get the path of the script
222 script = prod_info.compil_script
223 script_name = os.path.basename(script)
224 if script == script_name:
225 # Only a name is given. Search in the default directory
226 script_path = src.find_file_in_lpath(script_name,
227 config.PATHS.PRODUCTPATH,
230 raise src.SatException(
231 _("Compilation script not found: %s") % script_name)
232 prod_info.compil_script = script_path
233 if src.architecture.is_windows():
234 prod_info.compil_script = prod_info.compil_script[:-len(".sh")] + ".bat"
236 # Check that the script is executable
237 if not os.access(prod_info.compil_script, os.X_OK):
238 #raise src.SatException(
239 # _("Compilation script cannot be executed: %s") %
240 # prod_info.compil_script)
241 DBG.tofix("Compilation script cannot be executed:", prod_info.compil_script)
243 # Get the full paths of all the patches
244 if product_has_patches(prod_info):
247 for patch in prod_info.patches:
249 # If only a filename, then search for the patch in the PRODUCTPATH
250 if os.path.basename(patch_path) == patch_path:
251 # Search in the PRODUCTPATH/patches
252 patch_path = src.find_file_in_lpath(patch,
253 config.PATHS.PRODUCTPATH,
256 msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
257 "\n" % {"patch_name" : patch,
258 "prod_name" : prod_info.name})
259 raise src.SatException(msg)
260 patches.append(patch_path)
262 DBG.tofix("problem in prod_info.patches", prod_info)
263 prod_info.patches = patches
265 # Get the full paths of the environment scripts
266 if product_has_env_script(prod_info):
267 env_script_path = prod_info.environ.env_script
268 # If only a filename, then search for the environment script
269 # in the PRODUCTPATH/env_scripts
270 if os.path.basename(env_script_path) == env_script_path:
271 # Search in the PRODUCTPATH/env_scripts
272 env_script_path = src.find_file_in_lpath(
273 prod_info.environ.env_script,
274 config.PATHS.PRODUCTPATH,
276 if not env_script_path:
277 msg = _("Environment script %(env_name)s for %(prod_name)s not "
278 "found.\n" % {"env_name" : env_script_path,
279 "prod_name" : prod_info.name})
280 raise src.SatException(msg)
282 prod_info.environ.env_script = env_script_path
285 # The variable with_install_dir is at false only for internal use
286 # of the function get_install_dir
288 # Save the install_dir key if there is any
289 if "install_dir" in prod_info and not "install_dir_save" in prod_info:
290 prod_info.install_dir_save = prod_info.install_dir
292 # if it is not the first time the install_dir is computed, it means
293 # that install_dir_save exists and it has to be taken into account.
294 if "install_dir_save" in prod_info:
295 prod_info.install_dir = prod_info.install_dir_save
297 # Set the install_dir key
298 prod_info.install_dir = get_install_dir(config, base, version, prod_info)
302 def get_product_section(config, product_name, version, section=None):
303 """Get the product description from the configuration
305 :param config Config: The global configuration
306 :param product_name str: The product name
307 :param version str: The version of the product
308 :param section str: The searched section (if not None, the section is
310 :return: The product description
314 # if section is not None, try to get the corresponding section
316 if section not in config.PRODUCTS[product_name]:
318 # returns specific information for the given version
319 prod_info = config.PRODUCTS[product_name][section]
320 prod_info.section = section
321 prod_info.from_file = config.PRODUCTS[product_name].from_file
324 # If it exists, get the information of the product_version
325 if "version_" + version in config.PRODUCTS[product_name]:
326 # returns specific information for the given version
327 prod_info = config.PRODUCTS[product_name]["version_" + version]
328 prod_info.section = "version_" + version
329 prod_info.from_file = config.PRODUCTS[product_name].from_file
332 # Else, check if there is a description for multiple versions
333 l_section_name = config.PRODUCTS[product_name].keys()
334 l_section_ranges = [section_name for section_name in l_section_name
335 if VERSION_DELIMITER in section_name]
336 for section_range in l_section_ranges:
337 minimum, maximum = section_range.split(VERSION_DELIMITER)
338 if (src.only_numbers(version) >= src.only_numbers(minimum)
339 and src.only_numbers(version) <= src.only_numbers(maximum)):
340 # returns specific information for the versions
341 prod_info = config.PRODUCTS[product_name][section_range]
342 prod_info.section = section_range
343 prod_info.from_file = config.PRODUCTS[product_name].from_file
346 # Else, get the standard informations
347 if "default" in config.PRODUCTS[product_name]:
348 # returns the generic information (given version not found)
349 prod_info = config.PRODUCTS[product_name].default
350 prod_info.section = "default"
351 prod_info.from_file = config.PRODUCTS[product_name].from_file
354 # if noting was found, return None
357 def get_install_dir(config, base, version, prod_info):
358 """Compute the installation directory of a given product
360 :param config Config: The global configuration
361 :param base str: This corresponds to the value given by user in its
362 application.pyconf for the specific product. If "yes", the
363 user wants the product to be in base. If "no", he wants the
364 product to be in the application workdir
365 :param version str: The version of the product
366 :param product_info Config: The configuration specific to
369 :return: The path of the product installation
374 if (("install_dir" in prod_info and prod_info.install_dir == "base")
377 if (base == "no" or ("no_base" in config.APPLICATION
378 and config.APPLICATION.no_base == "yes")):
382 install_dir = get_base_install_dir(config, prod_info, version)
384 if "install_dir" not in prod_info or prod_info.install_dir == "base":
385 # Set it to the default value (in application directory)
386 install_dir = os.path.join(config.APPLICATION.workdir,
390 install_dir = prod_info.install_dir
394 def get_base_install_dir(config, prod_info, version):
395 """Compute the installation directory of a product in base
397 :param config Config: The global configuration
398 :param product_info Config: The configuration specific to
400 :param version str: The version of the product
401 :return: The path of the product installation
404 base_path = src.get_base_path(config)
405 prod_dir = os.path.join(base_path, prod_info.name + "-" + version)
406 if not os.path.exists(prod_dir):
407 return os.path.join(prod_dir, "config-1")
409 exists, install_dir = check_config_exists(config, prod_dir, prod_info)
413 # Find the first config-<i> directory that is available in the product
418 install_dir = os.path.join(prod_dir, "config-%i" % label)
419 if os.path.exists(install_dir):
426 def check_config_exists(config, prod_dir, prod_info):
428 Verify that the installation directory of a product in a base exists
429 Check all the config-<i> directory and verify the sat-config.pyconf file
432 :param config Config: The global configuration
433 :param prod_dir str: The product installation directory path
435 :param product_info Config: The configuration specific to
437 :return: True or false is the installation is found or not
438 and if it is found, the path of the found installation
439 :rtype: (boolean, str)
441 # check if the directories or files of the directory corresponds to the
442 # directory installation of the product
443 l_dir_and_files = os.listdir(prod_dir)
444 for dir_or_file in l_dir_and_files:
445 oExpr = re.compile(config_expression)
446 if not(oExpr.search(dir_or_file)):
447 # not config-<i>, not interesting
449 # check if there is the file sat-config.pyconf file in the installation
451 config_file = os.path.join(prod_dir, dir_or_file, src.CONFIG_FILENAME)
452 if not os.path.exists(config_file):
455 # If there is no dependency, it is the right path
456 if len(prod_info.depend)==0:
457 compile_cfg = src.pyconf.Config(config_file)
458 if len(compile_cfg) == 0:
459 return True, os.path.join(prod_dir, dir_or_file)
462 # check if there is the config described in the file corresponds the
463 # dependencies of the product
464 config_corresponds = True
465 compile_cfg = src.pyconf.Config(config_file)
466 for prod_dep in prod_info.depend:
467 # if the dependency is not in the config,
468 # the config does not correspond
469 if prod_dep not in compile_cfg:
470 config_corresponds = False
473 prod_dep_info = get_product_config(config, prod_dep, False)
474 # If the version of the dependency does not correspond,
475 # the config does not correspond
476 if prod_dep_info.version != compile_cfg[prod_dep]:
477 config_corresponds = False
480 for prod_name in compile_cfg:
481 if prod_name not in prod_info.depend:
482 config_corresponds = False
485 if config_corresponds:
486 return True, os.path.join(prod_dir, dir_or_file)
492 def get_products_infos(lproducts, config):
493 """Get the specific configuration of a list of products
495 :param lproducts List: The list of product names
496 :param config Config: The global configuration
497 :return: the list of tuples
498 (product name, specific configuration of the product)
499 :rtype: [(str, Config)]
502 # Loop on product names
503 for prod in lproducts:
504 # Get the specific configuration of the product
505 prod_info = get_product_config(config, prod)
506 if prod_info is not None:
507 products_infos.append((prod, prod_info))
509 msg = _("The %s product has no definition "
510 "in the configuration.") % prod
511 raise src.SatException(msg)
512 return products_infos
514 def get_product_dependencies(config, product_info):
516 Get recursively the list of products that are
517 in the product_info dependencies
519 :param config Config: The global configuration
520 :param product_info Config: The configuration specific to
522 :return: the list of products in dependence
525 if "depend" not in product_info or product_info.depend == []:
528 for prod in product_info.depend:
529 if prod == product_info.name:
533 prod_info = get_product_config(config, prod)
534 dep_prod = get_product_dependencies(config, prod_info)
535 for prod_in_dep in dep_prod:
536 if prod_in_dep not in res:
537 res.append(prod_in_dep)
540 def check_installation(product_info):
542 Verify if a product is well installed. Checks install directory presence
543 and some additional files if it is defined in the config
545 :param product_info Config: The configuration specific to
547 :return: True if it is well installed
550 if not product_compiles(product_info):
552 install_dir = product_info.install_dir
553 if not os.path.exists(install_dir):
555 if ("present_files" in product_info and
556 "install" in product_info.present_files):
557 for file_relative_path in product_info.present_files.install:
558 file_path = os.path.join(install_dir, file_relative_path)
559 if not os.path.exists(file_path):
563 def check_source(product_info):
564 """Verify if a sources of product is preset. Checks source directory presence
566 :param product_info Config: The configuration specific to
568 :return: True if it is well installed
571 DBG.write("check_source product_info", product_info)
572 source_dir = product_info.source_dir
573 if not os.path.exists(source_dir):
575 if ("present_files" in product_info and
576 "source" in product_info.present_files):
577 for file_relative_path in product_info.present_files.source:
578 file_path = os.path.join(source_dir, file_relative_path)
579 if not os.path.exists(file_path):
583 def product_is_salome(product_info):
584 """Know if a product is a SALOME module
586 :param product_info Config: The configuration specific to
588 :return: True if the product is a SALOME module, else False
591 return ("properties" in product_info and
592 "is_SALOME_module" in product_info.properties and
593 product_info.properties.is_SALOME_module == "yes")
595 def product_is_fixed(product_info):
596 """Know if a product is fixed
598 :param product_info Config: The configuration specific to
600 :return: True if the product is fixed, else False
603 get_src = product_info.get_source
604 return get_src.lower() == 'fixed'
606 def product_is_native(product_info):
607 """Know if a product is native
609 :param product_info Config: The configuration specific to
611 :return: True if the product is native, else False
614 get_src = product_info.get_source
615 return get_src.lower() == 'native'
617 def product_is_dev(product_info):
618 """Know if a product is in dev mode
620 :param product_info Config: The configuration specific to
622 :return: True if the product is in dev mode, else False
625 dev = product_info.dev
626 res = (dev.lower() == 'yes')
627 DBG.write('product_is_dev %s' % product_info.name, res)
628 # if product_info.name == "XDATA": return True #test #10569
631 def product_is_debug(product_info):
632 """Know if a product is in debug mode
634 :param product_info Config: The configuration specific to
636 :return: True if the product is in debug mode, else False
639 debug = product_info.debug
640 return debug.lower() == 'yes'
642 def product_is_verbose(product_info):
643 """Know if a product is in verbose mode
645 :param product_info Config: The configuration specific to
647 :return: True if the product is in verbose mode, else False
650 verbose = product_info.verbose
651 return verbose.lower() == 'yes'
653 def product_is_autotools(product_info):
654 """Know if a product is compiled using the autotools
656 :param product_info Config: The configuration specific to
658 :return: True if the product is autotools, else False
661 build_src = product_info.build_source
662 return build_src.lower() == 'autotools'
664 def product_is_cmake(product_info):
665 """Know if a product is compiled using the cmake
667 :param product_info Config: The configuration specific to
669 :return: True if the product is cmake, else False
672 build_src = product_info.build_source
673 return build_src.lower() == 'cmake'
675 def product_is_vcs(product_info):
676 """Know if a product is download using git, svn or cvs (not archive)
678 :param product_info Config: The configuration specific to
680 :return: True if the product is vcs, else False
683 return product_info.get_source in AVAILABLE_VCS
685 def product_is_smesh_plugin(product_info):
686 """Know if a product is a SMESH plugin
688 :param product_info Config: The configuration specific to
690 :return: True if the product is a SMESH plugin, else False
693 return ("properties" in product_info and
694 "smesh_plugin" in product_info.properties and
695 product_info.properties.smesh_plugin == "yes")
697 def product_is_cpp(product_info):
698 """Know if a product is cpp
700 :param product_info Config: The configuration specific to
702 :return: True if the product is a cpp, else False
705 return ("properties" in product_info and
706 "cpp" in product_info.properties and
707 product_info.properties.cpp == "yes")
709 def product_compiles(product_info):
711 Know if a product compiles or not
712 (some products do not have a compilation procedure)
714 :param product_info Config: The configuration specific to
716 :return: True if the product compiles, else False
719 return not("properties" in product_info and
720 "compilation" in product_info.properties and
721 product_info.properties.compilation == "no")
723 def product_has_script(product_info):
724 """Know if a product has a compilation script
726 :param product_info Config: The configuration specific to
728 :return: True if the product it has a compilation script, else False
731 if "build_source" not in product_info:
734 build_src = product_info.build_source
735 return build_src.lower() == 'script'
737 def product_has_env_script(product_info):
738 """Know if a product has an environment script
740 :param product_info Config: The configuration specific to
742 :return: True if the product it has an environment script, else False
745 return "environ" in product_info and "env_script" in product_info.environ
747 def product_has_patches(product_info):
748 """Know if a product has one or more patches
750 :param product_info Config: The configuration specific to
752 :return: True if the product has one or more patches
755 res = ( "patches" in product_info and len(product_info.patches) > 0 )
756 DBG.write('product_has_patches %s' % product_info.name, res)
757 # if product_info.name == "XDATA": return True #test #10569
760 def product_has_logo(product_info):
761 """Know if a product has a logo (YACSGEN generate)
763 :param product_info Config: The configuration specific to
765 :return: The path of the logo if the product has a logo, else False
768 if ("properties" in product_info and
769 "logo" in product_info.properties):
770 return product_info.properties.logo
774 def product_has_salome_gui(product_info):
775 """Know if a product has a SALOME gui
777 :param product_info Config: The configuration specific to
779 :return: True if the product has a SALOME gui, else False
782 return ("properties" in product_info and
783 "has_salome_gui" in product_info.properties and
784 product_info.properties.has_salome_gui == "yes")
786 def product_is_mpi(product_info):
787 """Know if a product has openmpi in its dependencies
789 :param product_info Config: The configuration specific to
791 :return: True if the product has openmpi inits dependencies
794 return "openmpi" in product_info.depend
796 def product_is_generated(product_info):
797 """Know if a product is generated (YACSGEN)
799 :param product_info Config: The configuration specific to
801 :return: True if the product is generated
804 return ("properties" in product_info and
805 "generate" in product_info.properties and
806 product_info.properties.generate == "yes")
808 def get_product_components(product_info):
809 """Get the component list to generate with the product
811 :param product_info Config: The configuration specific to
813 :return: The list of names of the components
817 if not product_is_generated(product_info):
821 if "component_name" in product_info:
822 compo_list = product_info.component_name
824 if isinstance(compo_list, str):
825 compo_list = [ compo_list ]