3 # Copyright (C) 2010-2012 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''In this file are implemented the methods
19 relative to the product notion of salomeTools
26 import src.debug as DBG
28 AVAILABLE_VCS = ['git', 'svn', 'cvs']
29 config_expression = "^config-\d+$"
30 VERSION_DELIMITER = "_to_"
32 def get_product_config(config, product_name, with_install_dir=True):
33 '''Get the specific configuration of a product from the global configuration
35 :param config Config: The global configuration
36 :param product_name str: The name of the product
37 :param with_install_dir boolean: If false, do not provide an install
38 directory (at false only for internal use
39 of the function check_config_exists)
40 :return: the specific configuration of the product
44 # Get the version of the product from the application definition
45 version = config.APPLICATION.products[product_name]
46 # if no version, then take the default one defined in the application
47 if isinstance(version, bool):
48 version = config.APPLICATION.tag
50 # Define debug and dev modes
51 # Get the tag if a dictionary is given in APPLICATION.products for the
57 if isinstance(version, src.pyconf.Mapping):
60 if not 'tag' in dic_version:
61 version = config.APPLICATION.tag
63 version = dic_version.tag
65 # Get the debug if any
66 if 'debug' in dic_version:
67 debug = dic_version.debug
70 if 'dev' in dic_version:
74 if 'base' in dic_version:
75 base = dic_version.base
77 # Get the section if any
78 if 'section' in dic_version:
79 section = dic_version.section
82 # substitute some character with _ in order to get the correct definition
83 # in config.PRODUCTS. This is done because the pyconf tool does not handle
84 # the . and - characters
85 for c in ".-": vv = vv.replace(c, "_")
88 if product_name in config.PRODUCTS:
89 # Search for the product description in the configuration
90 prod_info = get_product_section(config, product_name, vv, section)
92 # merge opt_depend in depend
93 if prod_info is not None and 'opt_depend' in prod_info:
94 for depend in prod_info.opt_depend:
95 if depend in config.APPLICATION.products:
96 prod_info.depend.append(depend,'')
98 # In case of a product get with a vcs,
99 # put the tag (equal to the version)
100 if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
102 if prod_info.get_source == 'git':
103 prod_info.git_info.tag = version
105 if prod_info.get_source == 'svn':
106 prod_info.svn_info.tag = version
108 if prod_info.get_source == 'cvs':
109 prod_info.cvs_info.tag = version
111 # In case of a fixed product,
112 # define the install_dir (equal to the version)
113 if prod_info is not None and os.path.isdir(version):
114 prod_info.install_dir = version
115 prod_info.get_source = "fixed"
117 # Check if the product is defined as native in the application
118 if prod_info is not None:
119 if version == "native":
120 prod_info.get_source = "native"
121 elif prod_info.get_source == "native":
122 msg = _("The product %(prod)s has version %(ver)s but is "
123 "declared as native in its definition" %
124 { 'prod': prod_info.name, 'ver': version})
125 raise src.SatException(msg)
127 # If there is no definition but the product is declared as native,
128 # construct a new definition containing only the get_source key
129 if prod_info is None and version == "native":
130 prod_info = src.pyconf.Config()
131 prod_info.name = product_name
132 prod_info.get_source = "native"
134 # If there is no definition but the product is fixed,
135 # construct a new definition containing only the product name
136 if prod_info is None and os.path.isdir(version):
137 prod_info = src.pyconf.Config()
138 prod_info.name = product_name
139 prod_info.get_source = "fixed"
140 prod_info.addMapping("environ", src.pyconf.Mapping(prod_info), "")
143 # If prod_info is still None, it means that there is no product definition
144 # in the config. The user has to provide it.
145 if prod_info is None:
146 prod_pyconf_path = src.find_file_in_lpath(product_name + ".pyconf",
147 config.PATHS.PRODUCTPATH)
148 if not prod_pyconf_path:
150 No definition found for the product %(1)s.
151 Please create a %(2)s.pyconf file somewhere in:
155 "3": config.PATHS.PRODUCTPATH }
158 No definition corresponding to the version %(1)s was found in the file:
160 Please add a section in it.""") % {"1" : vv, "2" : prod_pyconf_path}
161 raise src.SatException(msg)
163 # Set the debug, dev and version keys
164 prod_info.debug = debug
166 prod_info.version = version
168 # Set the archive_info if the product is get in archive mode
169 if prod_info.get_source == "archive":
170 if not "archive_info" in prod_info:
171 prod_info.addMapping("archive_info",
172 src.pyconf.Mapping(prod_info),
174 if "archive_name" not in prod_info.archive_info:
175 arch_name = product_name + "-" + version + ".tar.gz"
176 arch_path = src.find_file_in_lpath(arch_name,
177 config.PATHS.ARCHIVEPATH)
179 msg = _("Archive %(1)s for %(2)s not found in config.PATHS.ARCHIVEPATH") % \
180 {"1" : arch_name, "2" : prod_info.name}
181 DBG.tofix(msg, config.PATHS.ARCHIVEPATH)
182 prod_info.archive_info.archive_name = arch_name #without path
183 # raise src.SatException(msg) #may be a warning, continue #8646
185 prod_info.archive_info.archive_name = arch_path
187 if (os.path.basename(prod_info.archive_info.archive_name) ==
188 prod_info.archive_info.archive_name):
189 arch_name = prod_info.archive_info.archive_name
190 arch_path = src.find_file_in_lpath(
192 config.PATHS.ARCHIVEPATH)
194 msg = _("Archive %(1)s for %(2)s not found in config.PATHS.ARCHIVEPATH") % \
195 {"1" : arch_name, "2" : prod_info.name}
196 DBG.tofix(msg, config.PATHS.ARCHIVEPATH) #avoid 2 messages in compile
197 prod_info.archive_info.archive_name = arch_name #without path
198 # raise src.SatException(msg) #may be a warning, continue #8646
200 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):
419 '''Verify that the installation directory of a product in a base exists
420 Check all the config-<i> directory and verify the sat-config.pyconf file
423 :param config Config: The global configuration
424 :param prod_dir str: The product installation directory path
426 :param product_info Config: The configuration specific to
428 :return: True or false is the installation is found or not
429 and if it is found, the path of the found installation
430 :rtype: (boolean, str)
432 # check if the directories or files of the directory corresponds to the
433 # directory installation of the product
434 l_dir_and_files = os.listdir(prod_dir)
435 for dir_or_file in l_dir_and_files:
436 oExpr = re.compile(config_expression)
437 if not(oExpr.search(dir_or_file)):
438 # not config-<i>, not interesting
440 # check if there is the file sat-config.pyconf file in the installation
442 config_file = os.path.join(prod_dir, dir_or_file, src.CONFIG_FILENAME)
443 if not os.path.exists(config_file):
446 # If there is no dependency, it is the right path
447 if len(prod_info.depend)==0:
448 compile_cfg = src.pyconf.Config(config_file)
449 if len(compile_cfg) == 0:
450 return True, os.path.join(prod_dir, dir_or_file)
453 # check if there is the config described in the file corresponds the
454 # dependencies of the product
455 config_corresponds = True
456 compile_cfg = src.pyconf.Config(config_file)
457 for prod_dep in prod_info.depend:
458 # if the dependency is not in the config,
459 # the config does not correspond
460 if prod_dep not in compile_cfg:
461 config_corresponds = False
464 prod_dep_info = get_product_config(config, prod_dep, False)
465 # If the version of the dependency does not correspond,
466 # the config does not correspond
467 if prod_dep_info.version != compile_cfg[prod_dep]:
468 config_corresponds = False
471 for prod_name in compile_cfg:
472 if prod_name not in prod_info.depend:
473 config_corresponds = False
476 if config_corresponds:
477 return True, os.path.join(prod_dir, dir_or_file)
483 def get_products_infos(lproducts, config):
484 '''Get the specific configuration of a list of products
486 :param lproducts List: The list of product names
487 :param config Config: The global configuration
488 :return: the list of tuples
489 (product name, specific configuration of the product)
490 :rtype: [(str, Config)]
493 # Loop on product names
494 for prod in lproducts:
495 # Get the specific configuration of the product
496 prod_info = get_product_config(config, prod)
497 if prod_info is not None:
498 products_infos.append((prod, prod_info))
500 msg = _("The %s product has no definition "
501 "in the configuration.") % prod
502 raise src.SatException(msg)
503 return products_infos
505 def get_product_dependencies(config, product_info):
506 '''Get recursively the list of products that are
507 in the product_info dependencies
509 :param config Config: The global configuration
510 :param product_info Config: The configuration specific to
512 :return: the list of products in dependence
515 if "depend" not in product_info or product_info.depend == []:
518 for prod in product_info.depend:
519 if prod == product_info.name:
523 prod_info = get_product_config(config, prod)
524 dep_prod = get_product_dependencies(config, prod_info)
525 for prod_in_dep in dep_prod:
526 if prod_in_dep not in res:
527 res.append(prod_in_dep)
530 def check_installation(product_info):
531 '''Verify if a product is well installed. Checks install directory presence
532 and some additional files if it is defined in the config
534 :param product_info Config: The configuration specific to
536 :return: True if it is well installed
539 if not product_compiles(product_info):
541 install_dir = product_info.install_dir
542 if not os.path.exists(install_dir):
544 if ("present_files" in product_info and
545 "install" in product_info.present_files):
546 for file_relative_path in product_info.present_files.install:
547 file_path = os.path.join(install_dir, file_relative_path)
548 if not os.path.exists(file_path):
552 def check_source(product_info):
553 '''Verify if a sources of product is preset. Checks source directory presence
555 :param product_info Config: The configuration specific to
557 :return: True if it is well installed
560 DBG.write("check_source product_info", product_info)
561 source_dir = product_info.source_dir
562 if not os.path.exists(source_dir):
564 if ("present_files" in product_info and
565 "source" in product_info.present_files):
566 for file_relative_path in product_info.present_files.source:
567 file_path = os.path.join(source_dir, file_relative_path)
568 if not os.path.exists(file_path):
572 def product_is_salome(product_info):
573 '''Know if a product is a SALOME module
575 :param product_info Config: The configuration specific to
577 :return: True if the product is a SALOME module, else False
580 return ("properties" in product_info and
581 "is_SALOME_module" in product_info.properties and
582 product_info.properties.is_SALOME_module == "yes")
584 def product_is_fixed(product_info):
585 '''Know if a product is fixed
587 :param product_info Config: The configuration specific to
589 :return: True if the product is fixed, else False
592 get_src = product_info.get_source
593 return get_src.lower() == 'fixed'
595 def product_is_native(product_info):
596 '''Know if a product is native
598 :param product_info Config: The configuration specific to
600 :return: True if the product is native, else False
603 get_src = product_info.get_source
604 return get_src.lower() == 'native'
606 def product_is_dev(product_info):
607 '''Know if a product is in dev mode
609 :param product_info Config: The configuration specific to
611 :return: True if the product is in dev mode, else False
614 dev = product_info.dev
615 res = (dev.lower() == 'yes')
616 DBG.write('product_is_dev %s' % product_info.name, res)
617 # if product_info.name == "XDATA": return True #test #10569
620 def product_is_debug(product_info):
621 '''Know if a product is in debug mode
623 :param product_info Config: The configuration specific to
625 :return: True if the product is in debug mode, else False
628 debug = product_info.debug
629 return debug.lower() == 'yes'
631 def product_is_autotools(product_info):
632 '''Know if a product is compiled using the autotools
634 :param product_info Config: The configuration specific to
636 :return: True if the product is autotools, else False
639 build_src = product_info.build_source
640 return build_src.lower() == 'autotools'
642 def product_is_cmake(product_info):
643 '''Know if a product is compiled using the cmake
645 :param product_info Config: The configuration specific to
647 :return: True if the product is cmake, else False
650 build_src = product_info.build_source
651 return build_src.lower() == 'cmake'
653 def product_is_vcs(product_info):
654 '''Know if a product is download using git, svn or cvs (not archive)
656 :param product_info Config: The configuration specific to
658 :return: True if the product is vcs, else False
661 return product_info.get_source in AVAILABLE_VCS
663 def product_is_smesh_plugin(product_info):
664 '''Know if a product is a SMESH plugin
666 :param product_info Config: The configuration specific to
668 :return: True if the product is a SMESH plugin, else False
671 return ("properties" in product_info and
672 "smesh_plugin" in product_info.properties and
673 product_info.properties.smesh_plugin == "yes")
675 def product_is_cpp(product_info):
676 '''Know if a product is cpp
678 :param product_info Config: The configuration specific to
680 :return: True if the product is a cpp, else False
683 return ("properties" in product_info and
684 "cpp" in product_info.properties and
685 product_info.properties.cpp == "yes")
687 def product_compiles(product_info):
688 '''Know if a product compiles or not (some products do not have a
689 compilation procedure)
691 :param product_info Config: The configuration specific to
693 :return: True if the product compiles, else False
696 return not("properties" in product_info and
697 "compilation" in product_info.properties and
698 product_info.properties.compilation == "no")
700 def product_has_script(product_info):
701 '''Know if a product has a compilation script
703 :param product_info Config: The configuration specific to
705 :return: True if the product it has a compilation script, else False
708 if "build_source" not in product_info:
711 build_src = product_info.build_source
712 return build_src.lower() == 'script'
714 def product_has_env_script(product_info):
715 '''Know if a product has an environment script
717 :param product_info Config: The configuration specific to
719 :return: True if the product it has an environment script, else False
722 return "environ" in product_info and "env_script" in product_info.environ
724 def product_has_patches(product_info):
725 '''Know if a product has one or more patches
727 :param product_info Config: The configuration specific to
729 :return: True if the product has one or more patches
732 res = ( "patches" in product_info and len(product_info.patches) > 0 )
733 DBG.write('product_has_patches %s' % product_info.name, res)
734 # if product_info.name == "XDATA": return True #test #10569
737 def product_has_logo(product_info):
738 '''Know if a product has a logo (YACSGEN generate)
740 :param product_info Config: The configuration specific to
742 :return: The path of the logo if the product has a logo, else False
745 if ("properties" in product_info and
746 "logo" in product_info.properties):
747 return product_info.properties.logo
751 def product_has_salome_gui(product_info):
752 '''Know if a product has a SALOME gui
754 :param product_info Config: The configuration specific to
756 :return: True if the product has a SALOME gui, else False
759 return ("properties" in product_info and
760 "has_salome_gui" in product_info.properties and
761 product_info.properties.has_salome_gui == "yes")
763 def product_is_mpi(product_info):
764 '''Know if a product has openmpi in its dependencies
766 :param product_info Config: The configuration specific to
768 :return: True if the product has openmpi inits dependencies
771 return "openmpi" in product_info.depend
773 def product_is_generated(product_info):
774 '''Know if a product is generated (YACSGEN)
776 :param product_info Config: The configuration specific to
778 :return: True if the product is generated
781 return ("properties" in product_info and
782 "generate" in product_info.properties and
783 product_info.properties.generate == "yes")
785 def get_product_components(product_info):
786 '''Get the component list to generate with the product
788 :param product_info Config: The configuration specific to
790 :return: The list of names of the components
794 if not product_is_generated(product_info):
798 if "component_name" in product_info:
799 compo_list = product_info.component_name
801 if isinstance(compo_list, str):
802 compo_list = [ compo_list ]