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 file
222 # if windows supposed '.bat', if linux supposed '.sh'
223 # but user set extension script file in his pyconf as he wants, no obligation.
224 script = prod_info.compil_script
225 script_name = os.path.basename(script)
226 if script == script_name:
227 # Only a name is given. Search in the default directory
228 script_path = src.find_file_in_lpath(script_name, config.PATHS.PRODUCTPATH, "compil_scripts")
230 msg = _("Compilation script not found: %s") % script_name
231 DBG.tofix(msg, config.PATHS.PRODUCTPATH, True) # say where searched
232 raise src.SatException(msg)
233 prod_info.compil_script = script_path
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 # just as warning, problem later...
242 DBG.tofix("Compilation script is not execute mode file", prod_info.compil_script, True)
244 # Get the full paths of all the patches
245 if product_has_patches(prod_info):
248 for patch in prod_info.patches:
250 # If only a filename, then search for the patch in the PRODUCTPATH
251 if os.path.basename(patch_path) == patch_path:
252 # Search in the PRODUCTPATH/patches
253 patch_path = src.find_file_in_lpath(patch,
254 config.PATHS.PRODUCTPATH,
257 msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
258 "\n" % {"patch_name" : patch,
259 "prod_name" : prod_info.name})
260 raise src.SatException(msg)
261 patches.append(patch_path)
263 DBG.tofix("problem in prod_info.patches", prod_info)
264 prod_info.patches = patches
266 # Get the full paths of the environment scripts
267 if product_has_env_script(prod_info):
268 env_script_path = prod_info.environ.env_script
269 # If only a filename, then search for the environment script
270 # in the PRODUCTPATH/env_scripts
271 if os.path.basename(env_script_path) == env_script_path:
272 # Search in the PRODUCTPATH/env_scripts
273 env_script_path = src.find_file_in_lpath(
274 prod_info.environ.env_script,
275 config.PATHS.PRODUCTPATH,
277 if not env_script_path:
278 msg = _("Environment script %(env_name)s for %(prod_name)s not "
279 "found.\n" % {"env_name" : env_script_path,
280 "prod_name" : prod_info.name})
281 raise src.SatException(msg)
283 prod_info.environ.env_script = env_script_path
286 # The variable with_install_dir is at false only for internal use
287 # of the function get_install_dir
289 # Save the install_dir key if there is any
290 if "install_dir" in prod_info and not "install_dir_save" in prod_info:
291 prod_info.install_dir_save = prod_info.install_dir
293 # if it is not the first time the install_dir is computed, it means
294 # that install_dir_save exists and it has to be taken into account.
295 if "install_dir_save" in prod_info:
296 prod_info.install_dir = prod_info.install_dir_save
298 # Set the install_dir key
299 prod_info.install_dir = get_install_dir(config, base, version, prod_info)
303 def get_product_section(config, product_name, version, section=None):
304 """Get the product description from the configuration
306 :param config Config: The global configuration
307 :param product_name str: The product name
308 :param version str: The version of the product
309 :param section str: The searched section (if not None, the section is
311 :return: The product description
315 # if section is not None, try to get the corresponding section
317 if section not in config.PRODUCTS[product_name]:
319 # returns specific information for the given version
320 prod_info = config.PRODUCTS[product_name][section]
321 prod_info.section = section
322 prod_info.from_file = config.PRODUCTS[product_name].from_file
325 # If it exists, get the information of the product_version
326 if "version_" + version in config.PRODUCTS[product_name]:
327 # returns specific information for the given version
328 prod_info = config.PRODUCTS[product_name]["version_" + version]
329 prod_info.section = "version_" + version
330 prod_info.from_file = config.PRODUCTS[product_name].from_file
333 # Else, check if there is a description for multiple versions
334 l_section_name = config.PRODUCTS[product_name].keys()
335 l_section_ranges = [section_name for section_name in l_section_name
336 if VERSION_DELIMITER in section_name]
337 for section_range in l_section_ranges:
338 minimum, maximum = section_range.split(VERSION_DELIMITER)
339 if (src.only_numbers(version) >= src.only_numbers(minimum)
340 and src.only_numbers(version) <= src.only_numbers(maximum)):
341 # returns specific information for the versions
342 prod_info = config.PRODUCTS[product_name][section_range]
343 prod_info.section = section_range
344 prod_info.from_file = config.PRODUCTS[product_name].from_file
347 # Else, get the standard informations
348 if "default" in config.PRODUCTS[product_name]:
349 # returns the generic information (given version not found)
350 prod_info = config.PRODUCTS[product_name].default
351 prod_info.section = "default"
352 prod_info.from_file = config.PRODUCTS[product_name].from_file
355 # if noting was found, return None
358 def get_install_dir(config, base, version, prod_info):
359 """Compute the installation directory of a given product
361 :param config Config: The global configuration
362 :param base str: This corresponds to the value given by user in its
363 application.pyconf for the specific product. If "yes", the
364 user wants the product to be in base. If "no", he wants the
365 product to be in the application workdir
366 :param version str: The version of the product
367 :param product_info Config: The configuration specific to
370 :return: The path of the product installation
375 if (("install_dir" in prod_info and prod_info.install_dir == "base")
378 if (base == "no" or ("no_base" in config.APPLICATION
379 and config.APPLICATION.no_base == "yes")):
383 install_dir = get_base_install_dir(config, prod_info, version)
385 if "install_dir" not in prod_info or prod_info.install_dir == "base":
386 # Set it to the default value (in application directory)
387 install_dir = os.path.join(config.APPLICATION.workdir,
391 install_dir = prod_info.install_dir
395 def get_base_install_dir(config, prod_info, version):
396 """Compute the installation directory of a product in base
398 :param config Config: The global configuration
399 :param product_info Config: The configuration specific to
401 :param version str: The version of the product
402 :return: The path of the product installation
405 base_path = src.get_base_path(config)
406 prod_dir = os.path.join(base_path, prod_info.name + "-" + version)
407 if not os.path.exists(prod_dir):
408 return os.path.join(prod_dir, "config-1")
410 exists, install_dir = check_config_exists(config, prod_dir, prod_info)
414 # Find the first config-<i> directory that is available in the product
419 install_dir = os.path.join(prod_dir, "config-%i" % label)
420 if os.path.exists(install_dir):
427 def check_config_exists(config, prod_dir, prod_info):
429 Verify that the installation directory of a product in a base exists
430 Check all the config-<i> directory and verify the sat-config.pyconf file
433 :param config Config: The global configuration
434 :param prod_dir str: The product installation directory path
436 :param product_info Config: The configuration specific to
438 :return: True or false is the installation is found or not
439 and if it is found, the path of the found installation
440 :rtype: (boolean, str)
442 # check if the directories or files of the directory corresponds to the
443 # directory installation of the product
444 l_dir_and_files = os.listdir(prod_dir)
445 for dir_or_file in l_dir_and_files:
446 oExpr = re.compile(config_expression)
447 if not(oExpr.search(dir_or_file)):
448 # not config-<i>, not interesting
450 # check if there is the file sat-config.pyconf file in the installation
452 config_file = os.path.join(prod_dir, dir_or_file, src.CONFIG_FILENAME)
453 if not os.path.exists(config_file):
456 # If there is no dependency, it is the right path
457 if len(prod_info.depend)==0:
458 compile_cfg = src.pyconf.Config(config_file)
459 if len(compile_cfg) == 0:
460 return True, os.path.join(prod_dir, dir_or_file)
463 # check if there is the config described in the file corresponds the
464 # dependencies of the product
465 config_corresponds = True
466 compile_cfg = src.pyconf.Config(config_file)
467 for prod_dep in prod_info.depend:
468 # if the dependency is not in the config,
469 # the config does not correspond
470 if prod_dep not in compile_cfg:
471 config_corresponds = False
474 prod_dep_info = get_product_config(config, prod_dep, False)
475 # If the version of the dependency does not correspond,
476 # the config does not correspond
477 if prod_dep_info.version != compile_cfg[prod_dep]:
478 config_corresponds = False
481 for prod_name in compile_cfg:
482 if prod_name not in prod_info.depend:
483 config_corresponds = False
486 if config_corresponds:
487 return True, os.path.join(prod_dir, dir_or_file)
493 def get_products_infos(lproducts, config):
494 """Get the specific configuration of a list of products
496 :param lproducts List: The list of product names
497 :param config Config: The global configuration
498 :return: the list of tuples
499 (product name, specific configuration of the product)
500 :rtype: [(str, Config)]
503 # Loop on product names
504 for prod in lproducts:
505 # Get the specific configuration of the product
506 prod_info = get_product_config(config, prod)
507 if prod_info is not None:
508 products_infos.append((prod, prod_info))
510 msg = _("The %s product has no definition "
511 "in the configuration.") % prod
512 raise src.SatException(msg)
513 return products_infos
515 def get_product_dependencies(config, product_info):
517 Get recursively the list of products that are
518 in the product_info dependencies
520 :param config Config: The global configuration
521 :param product_info Config: The configuration specific to
523 :return: the list of products in dependence
526 if "depend" not in product_info or product_info.depend == []:
529 for prod in product_info.depend:
530 if prod == product_info.name:
534 prod_info = get_product_config(config, prod)
535 dep_prod = get_product_dependencies(config, prod_info)
536 for prod_in_dep in dep_prod:
537 if prod_in_dep not in res:
538 res.append(prod_in_dep)
541 def check_installation(product_info):
543 Verify if a product is well installed. Checks install directory presence
544 and some additional files if it is defined in the config
546 :param product_info Config: The configuration specific to
548 :return: True if it is well installed
551 if not product_compiles(product_info):
553 install_dir = product_info.install_dir
554 if not os.path.exists(install_dir):
556 if ("present_files" in product_info and
557 "install" in product_info.present_files):
558 for file_relative_path in product_info.present_files.install:
559 file_path = os.path.join(install_dir, file_relative_path)
560 if not os.path.exists(file_path):
564 def check_source(product_info):
565 """Verify if a sources of product is preset. Checks source directory presence
567 :param product_info Config: The configuration specific to
569 :return: True if it is well installed
572 DBG.write("check_source product_info", product_info)
573 source_dir = product_info.source_dir
574 if not os.path.exists(source_dir):
576 if ("present_files" in product_info and
577 "source" in product_info.present_files):
578 for file_relative_path in product_info.present_files.source:
579 file_path = os.path.join(source_dir, file_relative_path)
580 if not os.path.exists(file_path):
584 def product_is_salome(product_info):
585 """Know if a product is a SALOME module
587 :param product_info Config: The configuration specific to
589 :return: True if the product is a SALOME module, else False
592 return ("properties" in product_info and
593 "is_SALOME_module" in product_info.properties and
594 product_info.properties.is_SALOME_module == "yes")
596 def product_is_fixed(product_info):
597 """Know if a product is fixed
599 :param product_info Config: The configuration specific to
601 :return: True if the product is fixed, else False
604 get_src = product_info.get_source
605 return get_src.lower() == 'fixed'
607 def product_is_native(product_info):
608 """Know if a product is native
610 :param product_info Config: The configuration specific to
612 :return: True if the product is native, else False
615 get_src = product_info.get_source
616 return get_src.lower() == 'native'
618 def product_is_dev(product_info):
619 """Know if a product is in dev mode
621 :param product_info Config: The configuration specific to
623 :return: True if the product is in dev mode, else False
626 dev = product_info.dev
627 res = (dev.lower() == 'yes')
628 DBG.write('product_is_dev %s' % product_info.name, res)
629 # if product_info.name == "XDATA": return True #test #10569
632 def product_is_debug(product_info):
633 """Know if a product is in debug mode
635 :param product_info Config: The configuration specific to
637 :return: True if the product is in debug mode, else False
640 debug = product_info.debug
641 return debug.lower() == 'yes'
643 def product_is_verbose(product_info):
644 """Know if a product is in verbose mode
646 :param product_info Config: The configuration specific to
648 :return: True if the product is in verbose mode, else False
651 verbose = product_info.verbose
652 return verbose.lower() == 'yes'
654 def product_is_autotools(product_info):
655 """Know if a product is compiled using the autotools
657 :param product_info Config: The configuration specific to
659 :return: True if the product is autotools, else False
662 build_src = product_info.build_source
663 return build_src.lower() == 'autotools'
665 def product_is_cmake(product_info):
666 """Know if a product is compiled using the cmake
668 :param product_info Config: The configuration specific to
670 :return: True if the product is cmake, else False
673 build_src = product_info.build_source
674 return build_src.lower() == 'cmake'
676 def product_is_vcs(product_info):
677 """Know if a product is download using git, svn or cvs (not archive)
679 :param product_info Config: The configuration specific to
681 :return: True if the product is vcs, else False
684 return product_info.get_source in AVAILABLE_VCS
686 def product_is_smesh_plugin(product_info):
687 """Know if a product is a SMESH plugin
689 :param product_info Config: The configuration specific to
691 :return: True if the product is a SMESH plugin, else False
694 return ("properties" in product_info and
695 "smesh_plugin" in product_info.properties and
696 product_info.properties.smesh_plugin == "yes")
698 def product_is_cpp(product_info):
699 """Know if a product is cpp
701 :param product_info Config: The configuration specific to
703 :return: True if the product is a cpp, else False
706 return ("properties" in product_info and
707 "cpp" in product_info.properties and
708 product_info.properties.cpp == "yes")
710 def product_compiles(product_info):
712 Know if a product compiles or not
713 (some products do not have a compilation procedure)
715 :param product_info Config: The configuration specific to
717 :return: True if the product compiles, else False
720 return not("properties" in product_info and
721 "compilation" in product_info.properties and
722 product_info.properties.compilation == "no")
724 def product_has_script(product_info):
725 """Know if a product has a compilation script
727 :param product_info Config: The configuration specific to
729 :return: True if the product it has a compilation script, else False
732 if "build_source" not in product_info:
735 build_src = product_info.build_source
736 return build_src.lower() == 'script'
738 def product_has_env_script(product_info):
739 """Know if a product has an environment script
741 :param product_info Config: The configuration specific to
743 :return: True if the product it has an environment script, else False
746 return "environ" in product_info and "env_script" in product_info.environ
748 def product_has_patches(product_info):
749 """Know if a product has one or more patches
751 :param product_info Config: The configuration specific to
753 :return: True if the product has one or more patches
756 res = ( "patches" in product_info and len(product_info.patches) > 0 )
757 DBG.write('product_has_patches %s' % product_info.name, res)
758 # if product_info.name == "XDATA": return True #test #10569
761 def product_has_logo(product_info):
762 """Know if a product has a logo (YACSGEN generate)
764 :param product_info Config: The configuration specific to
766 :return: The path of the logo if the product has a logo, else False
769 if ("properties" in product_info and
770 "logo" in product_info.properties):
771 return product_info.properties.logo
775 def product_has_salome_gui(product_info):
776 """Know if a product has a SALOME gui
778 :param product_info Config: The configuration specific to
780 :return: True if the product has a SALOME gui, else False
783 return ("properties" in product_info and
784 "has_salome_gui" in product_info.properties and
785 product_info.properties.has_salome_gui == "yes")
787 def product_is_mpi(product_info):
788 """Know if a product has openmpi in its dependencies
790 :param product_info Config: The configuration specific to
792 :return: True if the product has openmpi inits dependencies
795 return "openmpi" in product_info.depend
797 def product_is_generated(product_info):
798 """Know if a product is generated (YACSGEN)
800 :param product_info Config: The configuration specific to
802 :return: True if the product is generated
805 return ("properties" in product_info and
806 "generate" in product_info.properties and
807 product_info.properties.generate == "yes")
809 def get_product_components(product_info):
810 """Get the component list to generate with the product
812 :param product_info Config: The configuration specific to
814 :return: The list of names of the components
818 if not product_is_generated(product_info):
822 if "component_name" in product_info:
823 compo_list = product_info.component_name
825 if isinstance(compo_list, str):
826 compo_list = [ compo_list ]