Salome HOME
sat #8581 : ajout pour chaque produit d'une information sur le tag du projet et de...
[tools/sat.git] / src / product.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2012  CEA/DEN
4 #
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.
9 #
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.
14 #
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
19 """\
20 In this file are implemented the methods 
21 relative to the product notion of salomeTools
22 """
23
24 import os
25 import re
26 import pprint as PP
27
28 import src
29 import src.debug as DBG
30 import src.versionMinorMajorPatch as VMMP
31
32 AVAILABLE_VCS = ['git', 'svn', 'cvs']
33
34 CONFIG_FILENAME = "sat-config.pyconf" # trace product depends version(s)
35 PRODUCT_FILENAME = "sat-product.pyconf" # trace product compile config
36 config_expression = "^config-\d+$"
37
38 def get_product_config(config, product_name, with_install_dir=True):
39     """Get the specific configuration of a product from the global configuration
40     
41     :param config Config: The global configuration
42     :param product_name str: The name of the product
43     :param with_install_dir boolean: If false, do not provide an install 
44                                      directory (at false only for internal use 
45                                      of the function check_config_exists)
46     :return: the specific configuration of the product
47     :rtype: Config
48     """
49
50     # Get the version of the product from the application definition
51     version = config.APPLICATION.products[product_name]
52     
53     # Define debug and dev modes
54     # Get the tag if a dictionary is given in APPLICATION.products for the
55     # current product 
56     debug = 'no'
57     dev = 'no'
58     hpc = 'no'
59     verbose = 'no'
60     base = 'maybe'
61     section = None
62
63     # if no version, then take the default one defined in the application
64     if isinstance(version, bool) or isinstance(version, str): 
65         # in this case tag is mandatory, not debug, verbose, dev
66         if 'debug' in config.APPLICATION:
67             debug = config.APPLICATION.debug
68         if 'verbose' in config.APPLICATION:
69             verbose = config.APPLICATION.verbose
70         if 'dev' in config.APPLICATION:
71             dev = config.APPLICATION.dev
72         if 'hpc' in config.APPLICATION:
73             hpc = config.APPLICATION.hpc
74
75     # special case for which only the product name is mentionned 
76     if isinstance(version, bool):
77         version = config.APPLICATION.tag
78
79     if isinstance(version, src.pyconf.Mapping):
80         dic_version = version
81         # Get the version/tag
82         if not 'tag' in dic_version:
83             version = config.APPLICATION.tag
84         else:
85             version = dic_version.tag
86         
87         # Get the debug if any
88         if 'debug' in dic_version:
89             debug = dic_version.debug
90         elif 'debug' in config.APPLICATION:
91             debug = config.APPLICATION.debug
92         
93         # Get the verbose if any
94         if 'verbose' in dic_version:
95             verbose = dic_version.verbose
96         elif 'verbose' in config.APPLICATION:
97             verbose = config.APPLICATION.verbose
98         
99         # Get the dev if any
100         if 'dev' in dic_version:
101             dev = dic_version.dev
102         elif 'dev' in config.APPLICATION:
103             dev = config.APPLICATION.dev
104         
105         # Get the hpc if any
106         if 'hpc' in dic_version:
107             hpc = dic_version.hpc
108         elif 'hpc' in config.APPLICATION:
109             hpc = config.APPLICATION.hpc
110
111         # Get the base if any
112         if 'base' in dic_version:
113             base = dic_version.base
114
115         # Get the section if any
116         if 'section' in dic_version:
117             section = dic_version.section
118     
119     # this case occur when version is overwritten, cf sat # 8897
120     if isinstance(version, dict): 
121         dic_version = version
122         # Get the version/tag
123         if not 'tag' in dic_version:
124             version = config.APPLICATION.tag
125         else:
126             version = dic_version["tag"]
127         
128         # Get the debug if any
129         if 'debug' in dic_version:
130             debug = dic_version["debug"]
131         elif 'debug' in config.APPLICATION:
132             debug = config.APPLICATION.debug
133         
134         # Get the verbose if any
135         if 'verbose' in dic_version:
136             verbose = dic_version["verbose"]
137         elif 'verbose' in config.APPLICATION:
138             verbose = config.APPLICATION.verbose
139         
140         # Get the dev if any
141         if 'dev' in dic_version:
142             dev = dic_version["dev"]
143         elif 'dev' in config.APPLICATION:
144             dev = config.APPLICATION.dev
145         
146         # Get the hpc if any
147         if 'hpc' in dic_version:
148             hpc = dic_version.hpc
149         elif 'hpc' in config.APPLICATION:
150             hpc = config.APPLICATION.hpc
151
152         # Get the base if any
153         if 'base' in dic_version:
154             base = dic_version["base"]
155
156         # Get the section if any
157         if 'section' in dic_version:
158             section = dic_version['section']
159
160     vv = version
161     # substitute some character with _ in order to get the correct definition
162     # in config.PRODUCTS. This is done because the pyconf tool does not handle
163     # the . and - characters 
164     for c in ".-": vv = vv.replace(c, "_")
165
166     prod_info = None
167     if product_name in config.PRODUCTS:
168         # Search for the product description in the configuration
169         prod_info = get_product_section(config, product_name, vv, section)
170         
171         # get salomeTool version
172         prod_info.sat_version = src.get_salometool_version(config)
173
174         # get the product project git tag, if any
175         product_project_git_tag = src.system.git_describe(os.path.dirname(prod_info.from_file))
176         if product_project_git_tag:
177             prod_info.sat_project_tag = product_project_git_tag
178         else:
179             prod_info.sat_project_tag = "unknown"
180
181         # merge opt_depend in depend
182         if prod_info is not None and 'opt_depend' in prod_info:
183             for depend in prod_info.opt_depend:
184                 if depend in config.APPLICATION.products:
185                     prod_info.depend.append(depend,'')
186         
187         # In case of a product get with a vcs, 
188         # put the tag (equal to the version)
189         if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
190             
191             if prod_info.get_source == 'git':
192                 prod_info.git_info.tag = version
193             
194             if prod_info.get_source == 'svn':
195                 prod_info.svn_info.tag = version
196             
197             if prod_info.get_source == 'cvs':
198                 prod_info.cvs_info.tag = version
199         
200         # In case of a fixed product, 
201         # define the install_dir (equal to the version)
202         if prod_info is not None and \
203            (os.path.isdir(version) or version.startswith("/")):
204            # we consider a (fixed) path  existing paths; 
205            # or paths starting with '/' (the objective is to print a correct 
206            # message to the user in case of non existing path.)
207             prod_info.install_dir = version
208             prod_info.get_source = "fixed"
209         
210         # Check if the product is defined as native in the application
211         if prod_info is not None:
212             if version == "native":
213                 prod_info.get_source = "native"
214             elif prod_info.get_source == "native":
215                 msg = _("The product %(prod)s has version %(ver)s but is "
216                         "declared as native in its definition" %
217                         { 'prod': prod_info.name, 'ver': version})
218                 raise src.SatException(msg)
219
220     # If there is no definition but the product is declared as native,
221     # construct a new definition containing only the get_source key
222     if prod_info is None and version == "native":
223         prod_info = src.pyconf.Config()
224         prod_info.name = product_name
225         prod_info.get_source = "native"
226
227     # If there is no definition but the product is fixed,
228     # construct a new definition containing only the product name
229     if prod_info is None and os.path.isdir(version):
230         prod_info = src.pyconf.Config()
231         prod_info.name = product_name
232         prod_info.get_source = "fixed"
233         prod_info.addMapping("environ", src.pyconf.Mapping(prod_info), "")
234
235
236     # If prod_info is still None, it means that there is no product definition
237     # in the config. The user has to provide it.
238     if prod_info is None:
239         prod_pyconf_path = src.find_file_in_lpath(product_name + ".pyconf",
240                                                   config.PATHS.PRODUCTPATH)
241         if not prod_pyconf_path:
242             msg = _("""\
243 No definition found for the product %(1)s.
244 Please create a %(1)s.pyconf file somewhere in:
245   %(2)s""") % {
246   "1": product_name,
247   "2": PP.pformat(config.PATHS.PRODUCTPATH) }
248         else:
249             msg = _("""\
250 No definition corresponding to the version %(1)s was found in the file:
251   %(2)s.
252 Please add a section in it.""") % {"1" : vv, "2" : prod_pyconf_path}
253         raise src.SatException(msg)
254     
255     # Set the debug, dev and version keys
256     prod_info.debug = debug
257     prod_info.verbose = verbose
258     prod_info.dev = dev
259     prod_info.hpc = hpc
260     prod_info.version = version
261
262     # Set the archive_info if the product is get in archive mode
263     if prod_info.get_source == "archive":
264         if not "archive_info" in prod_info:
265             prod_info.addMapping("archive_info",
266                                  src.pyconf.Mapping(prod_info),
267                                  "")
268         if "archive_name" in prod_info.archive_info: 
269             arch_name = prod_info.archive_info.archive_name
270         else:
271             # standard name
272             arch_name = product_name + "-" + version + ".tar.gz"
273
274         arch_path = src.find_file_in_lpath(arch_name,
275                                            config.PATHS.ARCHIVEPATH)
276         if not arch_path:
277             # arch_path is not found. It may generate an error in sat source,
278             #                         unless the archive is found in ftp serveur
279             msg = _("Archive %(1)s for %(2)s not found in config.PATHS.ARCHIVEPATH") % \
280                    {"1" : arch_name, "2" : prod_info.name}
281             DBG.tofix(msg, config.PATHS.ARCHIVEPATH)
282             prod_info.archive_info.archive_name = arch_name #without path
283         else:
284             prod_info.archive_info.archive_name = arch_path
285
286         
287     # If the product compiles with a script, check the script existence
288     # and if it is executable
289     if product_has_script(prod_info):
290         # Check the compil_script key existence
291         if "compil_script" not in prod_info:
292             msg = _("""\
293 No compilation script found for the product %s.
294 Please provide a 'compil_script' key in its definition.""") % product_name
295             raise src.SatException(msg)
296         
297         # Get the path of the script file
298         # if windows supposed '.bat', if linux supposed '.sh'
299         # but user set extension script file in his pyconf as he wants, no obligation.
300         script = prod_info.compil_script
301         script_name = os.path.basename(script)
302         if script == script_name:
303             # Only a name is given. Search in the default directory
304             script_path = src.find_file_in_lpath(script_name, config.PATHS.PRODUCTPATH, "compil_scripts")
305             if not script_path:
306                 msg = _("Compilation script %s not found in") % script_name
307                 DBG.tofix(msg, config.PATHS.PRODUCTPATH, True) # say where searched
308                 script_path = "%s_(Not_Found_by_Sat!!)" % script_name
309             prod_info.compil_script = script_path
310
311        
312         # Check that the script is executable
313         if os.path.exists(prod_info.compil_script) and not os.access(prod_info.compil_script, os.X_OK):
314             DBG.tofix("Compilation script  file is not in 'execute mode'", prod_info.compil_script, True)
315     
316     # Get the full paths of all the patches
317     if product_has_patches(prod_info):
318         patches = []
319         try:
320           for patch in prod_info.patches:
321               patch_path = patch
322               # If only a filename, then search for the patch in the PRODUCTPATH
323               if os.path.basename(patch_path) == patch_path:
324                   # Search in the PRODUCTPATH/patches
325                   patch_path = src.find_file_in_lpath(patch,
326                                                       config.PATHS.PRODUCTPATH,
327                                                       "patches")
328                   if not patch_path:
329                       msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
330                               "\n" % {"patch_name" : patch,
331                                        "prod_name" : prod_info.name}) 
332                       raise src.SatException(msg)
333               patches.append(patch_path)
334         except:
335           DBG.tofix("problem in prod_info.patches", prod_info)
336         prod_info.patches = patches
337
338     # Get the full paths of the environment scripts
339     if product_has_env_script(prod_info):
340         env_script_path = prod_info.environ.env_script
341         # If only a filename, then search for the environment script 
342         # in the PRODUCTPATH/env_scripts
343         if os.path.basename(env_script_path) == env_script_path:
344             # Search in the PRODUCTPATH/env_scripts
345             env_script_path = src.find_file_in_lpath(
346                                             prod_info.environ.env_script,
347                                             config.PATHS.PRODUCTPATH,
348                                             "env_scripts")
349             if not env_script_path:
350                 msg = _("Environment script %(env_name)s for %(prod_name)s not "
351                         "found.\n" % {"env_name" : env_script_path,
352                                        "prod_name" : prod_info.name}) 
353                 raise src.SatException(msg)
354
355         prod_info.environ.env_script = env_script_path
356     
357     if with_install_dir: 
358         # The variable with_install_dir is at false only for internal use 
359         # of the function get_install_dir
360         
361         # Save the install_dir key if there is any
362         if "install_dir" in prod_info and not "install_dir_save" in prod_info:
363             prod_info.install_dir_save = prod_info.install_dir
364         
365         # if it is not the first time the install_dir is computed, it means
366         # that install_dir_save exists and it has to be taken into account.
367         if "install_dir_save" in prod_info:
368             prod_info.install_dir = prod_info.install_dir_save
369         
370         # Set the install_dir key
371         prod_info.install_dir = get_install_dir(config, base, version, prod_info)
372                 
373     return prod_info
374
375 def get_product_section(config, product_name, version, section=None, verbose=False):
376     """Get the product description from the configuration
377     
378     :param config Config: The global configuration
379     :param product_name str: The product name
380     :param version str: The version of the product as 'V8_4_0', or else.
381     :param section str: The searched section (if not None, the section is 
382                         explicitly given
383     :return: The product description
384     :rtype: Config
385     """
386
387     # if section is not None, try to get the corresponding section
388     aProd = config.PRODUCTS[product_name]
389     try:
390       versionMMP = VMMP.MinorMajorPatch(version)
391     except: # example setuptools raise "minor in major_minor_patch is not integer: '0_6c11'"
392       versionMMP = None
393     DBG.write("get_product_section for product %s '%s' as version '%s'" % (product_name, version, versionMMP),
394               (section, aProd.keys()), verbose)
395     # DBG.write("yoo1", aProd, True)
396     if section:
397         if section not in aProd:
398             return None
399         # returns specific information for the given version
400         prod_info = aProd[section]
401         prod_info.section = section
402         prod_info.from_file = aProd.from_file
403         return prod_info
404
405     # If it exists, get the information of the product_version
406     # ex: 'version_V6_6_0' as salome version classical syntax
407     if "version_" + version in aProd:
408         DBG.write("found section for version_" + version, "", verbose)
409         # returns specific information for the given version
410         prod_info = aProd["version_" + version]
411         prod_info.section = "version_" + version
412         prod_info.from_file = aProd.from_file
413         return prod_info
414
415     # Else, check if there is a description for multiple versions
416     l_section_names = aProd.keys()
417     l_section_ranges = []
418     tagged = []
419     for name in l_section_names:
420       # DBG.write("name", name,True)
421       aRange = VMMP.getRange_majorMinorPatch(name)
422       if aRange is not None:
423         DBG.write("found version range for section '%s'" % name, aRange, verbose)
424         l_section_ranges.append((name, aRange))
425
426     if versionMMP is not None and len(l_section_ranges) > 0:
427       for name, (vmin, vmax) in l_section_ranges:
428         if versionMMP >= vmin and versionMMP <= vmax:
429           tagged.append((name, [vmin, vmax]))
430
431     if len(tagged) > 1:
432       DBG.write("multiple version ranges tagged for '%s', fix it" % version,
433                      PP.pformat(tagged), True)
434       return None
435     if len(tagged) == 1: # ok
436       DBG.write("one version range tagged for '%s'" % version,
437                    PP.pformat(tagged), verbose)
438       name, (vmin, vmax) = tagged[0]
439       prod_info = aProd[name]
440       prod_info.section = name
441       prod_info.from_file = aProd.from_file
442       return prod_info
443
444     # Else, get the standard informations
445     if "default" in aProd:
446         # returns the generic information (given version not found)
447         prod_info = aProd.default
448         DBG.write("default tagged for '%s'" % version, prod_info, verbose)
449         prod_info.section = "default"
450         prod_info.from_file = aProd.from_file
451         return prod_info
452     
453     # if noting was found, return None
454     return None
455     
456 def get_install_dir(config, base, version, prod_info):
457     """Compute the installation directory of a given product 
458     
459     :param config Config: The global configuration
460     :param base str: This corresponds to the value given by user in its 
461                      application.pyconf for the specific product. If "yes", the
462                      user wants the product to be in base. If "no", he wants the
463                      product to be in the application workdir
464     :param version str: The version of the product
465     :param product_info Config: The configuration specific to 
466                                the product
467     
468     :return: The path of the product installation
469     :rtype: str
470     """
471     install_dir = ""
472     in_base = False
473     if (("install_dir" in prod_info and prod_info.install_dir == "base") 
474                                                             or base == "yes"):
475         in_base = True
476     if (base == "no" or ("no_base" in config.APPLICATION 
477                          and config.APPLICATION.no_base == "yes")):
478         in_base = False
479     
480     if in_base:
481         install_dir = get_base_install_dir(config, prod_info, version)
482     else:
483         if "install_dir" not in prod_info or prod_info.install_dir == "base":
484             # Set it to the default value (in application directory)
485             install_dir = os.path.join(config.APPLICATION.workdir,
486                                                 "INSTALL",
487                                                 prod_info.name)
488         else:
489             install_dir = prod_info.install_dir
490
491     return install_dir
492
493 def get_base_install_dir(config, prod_info, version):
494     """Compute the installation directory of a product in base 
495     
496     :param config Config: The global configuration
497     :param product_info Config: The configuration specific to 
498                                the product
499     :param version str: The version of the product    
500     :return: The path of the product installation
501     :rtype: str
502     """    
503     base_path = src.get_base_path(config) 
504     prod_dir = os.path.join(base_path, prod_info.name + "-" + version)
505     if not os.path.exists(prod_dir):
506         return os.path.join(prod_dir, "config-1")
507     
508     exists, install_dir = check_config_exists(config, prod_dir, prod_info)
509     if exists:
510         return install_dir
511     
512     # Find the first config-<i> directory that is available in the product
513     # directory
514     found = False 
515     label = 1
516     while not found:
517         install_dir = os.path.join(prod_dir, "config-%i" % label)
518         if os.path.exists(install_dir):
519             label+=1
520         else:
521             found = True
522             
523     return install_dir
524
525 def add_compile_config_file(p_info, config):
526     '''Execute the proper configuration command(s)
527        in the product build directory.
528
529     :param p_info Config: The specific config of the product
530     :param config Config: The global configuration
531     '''
532     # Create the compile config
533     # DBG.write("add_compile_config_file", p_info, True)
534     res = src.pyconf.Config()
535     res.addMapping(p_info.name, src.pyconf.Mapping(res), "")
536     res[p_info.name]= p_info.version
537
538     for prod_name in p_info.depend:
539       if prod_name not in res:
540         res.addMapping(prod_name, src.pyconf.Mapping(res), "")
541       prod_dep_info = src.product.get_product_config(config, prod_name, False)
542       res[prod_name] = prod_dep_info.version
543     # Write it in the install directory of the product
544     # This file is for automatic reading/checking
545     # see check_config_exists method
546     aFile = os.path.join(p_info.install_dir, CONFIG_FILENAME)
547     with open(aFile, 'w') as f:
548       res.__save__(f)
549
550     # this file is not mandatory, is for human eye reading
551     aFile = os.path.join(p_info.install_dir, PRODUCT_FILENAME)
552     try:
553       with open(aFile, 'w') as f:
554         p_info.__save__(f, evaluated=True) # evaluated expressions mode
555     except:
556       DBG.write("cannot evaluate product info - problem in file %s" % aFile, p_info, True)
557       # write DBG mode, as no problem if evaluation not possible
558       msg = """\
559 # Some informations cannot be evaluated.
560 # for example:
561 # In the context of non VCS archives, information on git server is not available.
562   
563 """
564       with open(aFile, 'w') as f:
565         f.write(msg)
566         f.write(DBG.getStrConfigDbg(p_info))
567
568 def check_config_exists(config, prod_dir, prod_info, verbose=False):
569     """\
570     Verify that the installation directory of a product in a base exists.
571     Check all the config-<i>/sat-config.py files found for correspondence
572     with current config and prod_info depend-version-tags
573     
574     :param config Config: The global configuration
575     :param prod_dir str: The product installation directory path 
576                          (without config-<i>)
577     :param product_info Config: The configuration specific to 
578                                the product
579     :return: True or false is the installation is found or not 
580              and if it is found, the path of the found installation
581     :rtype: (boolean, str)
582     """
583     # check if the directories or files of the directory corresponds to the
584     # directory installation of the product
585     if os.path.isdir(prod_dir):
586       l_dir_and_files = os.listdir(prod_dir)
587     else:
588       raise Exception("Inexisting directory '%s'" % prod_dir)
589
590     DBG.write("check_config_exists 000",  (prod_dir, l_dir_and_files), verbose)
591     DBG.write("check_config_exists 111",  prod_info, verbose)
592
593     for dir_or_file in l_dir_and_files:
594         oExpr = re.compile(config_expression)
595         if not(oExpr.search(dir_or_file)):
596             # in mode BASE, not config-<i>, not interesting
597             # DBG.write("not interesting", dir_or_file, True)
598             continue
599         # check if there is the file sat-config.pyconf file in the installation
600         # directory    
601         config_file = os.path.join(prod_dir, dir_or_file, CONFIG_FILENAME)
602         DBG.write("check_config_exists 222", config_file, verbose)
603         if not os.path.exists(config_file):
604             continue
605         
606         # check if there is the config described in the file corresponds the 
607         # dependencies of the product
608         config_corresponds = True    
609         compile_cfg = src.pyconf.Config(config_file)
610         for prod_dep in prod_info.depend:
611             # if the dependency is not in the config, 
612             # the config does not correspond
613             if prod_dep not in compile_cfg:
614                 config_corresponds = False
615                 break
616             else:
617                 prod_dep_info = get_product_config(config, prod_dep, False)
618                 # If the version of the dependency does not correspond, 
619                 # the config does not correspond
620                 if prod_dep_info.version != compile_cfg[prod_dep]:
621                     config_corresponds = False
622                     break
623
624         if config_corresponds:
625           for prod_name in compile_cfg:
626             # assume new compatibility with prod_name in sat-config.pyconf files
627             if prod_name == prod_info.name:
628               if prod_info.version == compile_cfg[prod_name]:
629                 DBG.write("check_config_exists OK 333", compile_cfg, verbose)
630                 pass
631               else: # no correspondence with newer with prod_name sat-config.pyconf files
632                 config_corresponds = False
633                 break
634             else:
635               # as old compatibility without prod_name sat-config.pyconf files
636               if prod_name not in prod_info.depend:
637                 # here there is an unexpected depend in an old compilation
638                 config_corresponds = False
639                 break
640         
641         if config_corresponds: # returns (and stops) at first correspondence found
642             DBG.write("check_config_exists OK 444", dir_or_file, verbose)
643             return True, os.path.join(prod_dir, dir_or_file)
644
645     # no correspondence found
646     return False, None
647             
648             
649     
650 def get_products_infos(lproducts, config):
651     """Get the specific configuration of a list of products
652     
653     :param lproducts List: The list of product names
654     :param config Config: The global configuration
655     :return: the list of tuples 
656              (product name, specific configuration of the product)
657     :rtype: [(str, Config)]
658     """
659     products_infos = []
660     # Loop on product names
661     for prod in lproducts:       
662         # Get the specific configuration of the product
663         prod_info = get_product_config(config, prod)
664         if prod_info is not None:
665             products_infos.append((prod, prod_info))
666         else:
667             msg = _("The %s product has no definition in the configuration.") % prod
668             raise src.SatException(msg)
669     return products_infos
670
671
672 def get_products_list(options, cfg, logger):
673     """
674     method that gives the product list with their informations from
675     configuration regarding the passed options.
676
677     :param options Options: The Options instance that stores the commands arguments
678     :param cfg Config: The global configuration
679     :param logger Logger: The logger instance to use for the display and logging
680     :return: The list of (product name, product_informations).
681     :rtype: List
682     """
683     # Get the products to be prepared, regarding the options
684     if options.products is None:
685         # No options, get all products sources
686         products = cfg.APPLICATION.products
687     else:
688         # if option --products, check that all products of the command line
689         # are present in the application.
690         """products = options.products
691         for p in products:
692             if p not in cfg.APPLICATION.products:
693                 raise src.SatException(_("Product %(product)s "
694                             "not defined in application %(application)s") %
695                         { 'product': p, 'application': cfg.VARS.application} )"""
696
697         products = src.getProductNames(cfg, options.products, logger)
698
699     # Construct the list of tuple containing
700     # the products name and their definition
701     resAll = src.product.get_products_infos(products, cfg)
702
703     # if the property option was passed, filter the list
704     if options.properties: # existing properties
705       ok = []
706       ko = []
707       res =[]
708       prop, value = options.properties # for example 'is_SALOME_module', 'yes'
709       for p_name, p_info in resAll:
710         try:
711           if p_info.properties[prop] == value:
712             res.append((p_name, p_info))
713             ok.append(p_name)
714           else:
715             ko.append(p_name)
716         except:
717           ok.append(p_name)
718
719       if len(ok) != len(resAll):
720         logger.trace("on properties %s\n products accepted:\n %s\n products rejected:\n %s\n" %
721                        (options.properties, PP.pformat(sorted(ok)), PP.pformat(sorted(ko))))
722       else:
723         logger.warning("properties %s\n seems useless with no products rejected" %
724                        (options.properties))
725     else:
726       res = resAll # not existing properties as all accepted
727
728     return res
729
730
731 def get_product_dependencies(config, product_info):
732     """\
733     Get recursively the list of products that are 
734     in the product_info dependencies
735     
736     :param config Config: The global configuration
737     :param product_info Config: The configuration specific to 
738                                the product
739     :return: the list of products in dependence
740     :rtype: list
741     """
742     if "depend" not in product_info or product_info.depend == []:
743         return []
744     res = []
745     for prod in product_info.depend:
746         if prod == product_info.name:
747             continue
748         if prod not in res:
749             res.append(prod)
750         prod_info = get_product_config(config, prod)
751         dep_prod = get_product_dependencies(config, prod_info)
752         for prod_in_dep in dep_prod:
753             if prod_in_dep not in res:
754                 res.append(prod_in_dep)
755     return res
756
757 def check_installation(product_info):
758     """\
759     Verify if a product is well installed. Checks install directory presence
760     and some additional files if it is defined in the config 
761     
762     :param product_info Config: The configuration specific to 
763                                the product
764     :return: True if it is well installed
765     :rtype: boolean
766     """
767     if not product_compiles(product_info):
768         return True
769     install_dir = product_info.install_dir
770     if not os.path.exists(install_dir):
771         return False
772     if ("present_files" in product_info and 
773         "install" in product_info.present_files):
774         for file_relative_path in product_info.present_files.install:
775             file_path = os.path.join(install_dir, file_relative_path)
776             if not os.path.exists(file_path):
777                 return False
778     return True
779
780 def check_source(product_info):
781     """Verify if a sources of product is preset. Checks source directory presence
782     
783     :param product_info Config: The configuration specific to 
784                                the product
785     :return: True if it is well installed
786     :rtype: boolean
787     """
788     DBG.write("check_source product_info", product_info)
789     source_dir = product_info.source_dir
790     if not os.path.exists(source_dir):
791         return False
792     if ("present_files" in product_info and 
793         "source" in product_info.present_files):
794         for file_relative_path in product_info.present_files.source:
795             file_path = os.path.join(source_dir, file_relative_path)
796             if not os.path.exists(file_path):
797                 return False
798     return True
799
800 def product_is_salome(product_info):
801     """Know if a product is a SALOME module
802     
803     :param product_info Config: The configuration specific to 
804                                the product
805     :return: True if the product is a SALOME module, else False
806     :rtype: boolean
807     """
808     return ("properties" in product_info and
809             "is_SALOME_module" in product_info.properties and
810             product_info.properties.is_SALOME_module == "yes")
811
812 def product_is_fixed(product_info):
813     """Know if a product is fixed
814     
815     :param product_info Config: The configuration specific to 
816                                the product
817     :return: True if the product is fixed, else False
818     :rtype: boolean
819     """
820     get_src = product_info.get_source
821     return get_src.lower() == 'fixed'
822
823 def product_is_native(product_info):
824     """Know if a product is native
825     
826     :param product_info Config: The configuration specific to 
827                                the product
828     :return: True if the product is native, else False
829     :rtype: boolean
830     """
831     get_src = product_info.get_source
832     return get_src.lower() == 'native'
833
834 def product_is_dev(product_info):
835     """Know if a product is in dev mode
836     
837     :param product_info Config: The configuration specific to 
838                                the product
839     :return: True if the product is in dev mode, else False
840     :rtype: boolean
841     """
842     dev = product_info.dev
843     res = (dev.lower() == 'yes')
844     DBG.write('product_is_dev %s' % product_info.name, res)
845     # if product_info.name == "XDATA": return True #test #10569
846     return res
847
848 def product_is_hpc(product_info):
849     """Know if a product is in hpc mode
850     
851     :param product_info Config: The configuration specific to 
852                                the product
853     :return: True if the product is in hpc mode, else False
854     :rtype: boolean
855     """
856     hpc = product_info.hpc
857     res = (hpc.lower() == 'yes')
858     return res
859
860 def product_is_debug(product_info):
861     """Know if a product is in debug mode
862     
863     :param product_info Config: The configuration specific to 
864                                the product
865     :return: True if the product is in debug mode, else False
866     :rtype: boolean
867     """
868     debug = product_info.debug
869     return debug.lower() == 'yes'
870
871 def product_is_verbose(product_info):
872     """Know if a product is in verbose mode
873     
874     :param product_info Config: The configuration specific to 
875                                the product
876     :return: True if the product is in verbose mode, else False
877     :rtype: boolean
878     """
879     verbose = product_info.verbose
880     return verbose.lower() == 'yes'
881
882 def product_is_autotools(product_info):
883     """Know if a product is compiled using the autotools
884     
885     :param product_info Config: The configuration specific to 
886                                the product
887     :return: True if the product is autotools, else False
888     :rtype: boolean
889     """
890     build_src = product_info.build_source
891     return build_src.lower() == 'autotools'
892
893 def product_is_cmake(product_info):
894     """Know if a product is compiled using the cmake
895     
896     :param product_info Config: The configuration specific to 
897                                the product
898     :return: True if the product is cmake, else False
899     :rtype: boolean
900     """
901     build_src = product_info.build_source
902     return build_src.lower() == 'cmake'
903
904 def product_is_vcs(product_info):
905     """Know if a product is download using git, svn or cvs (not archive)
906     
907     :param product_info Config: The configuration specific to 
908                                the product
909     :return: True if the product is vcs, else False
910     :rtype: boolean
911     """
912     return product_info.get_source in AVAILABLE_VCS
913
914 def product_is_smesh_plugin(product_info):
915     """Know if a product is a SMESH plugin
916     
917     :param product_info Config: The configuration specific to 
918                                the product
919     :return: True if the product is a SMESH plugin, else False
920     :rtype: boolean
921     """
922     return ("properties" in product_info and
923             "smesh_plugin" in product_info.properties and
924             product_info.properties.smesh_plugin == "yes")
925
926 def product_is_cpp(product_info):
927     """Know if a product is cpp
928     
929     :param product_info Config: The configuration specific to 
930                                the product
931     :return: True if the product is a cpp, else False
932     :rtype: boolean
933     """
934     return ("properties" in product_info and
935             "cpp" in product_info.properties and
936             product_info.properties.cpp == "yes")
937
938 def product_compiles(product_info):
939     """\
940     Know if a product compiles or not 
941     (some products do not have a compilation procedure)
942     
943     :param product_info Config: The configuration specific to 
944                                the product
945     :return: True if the product compiles, else False
946     :rtype: boolean
947     """
948     return not("properties" in product_info and
949             "compilation" in product_info.properties and
950             product_info.properties.compilation == "no")
951
952 def product_has_script(product_info):
953     """Know if a product has a compilation script
954     
955     :param product_info Config: The configuration specific to 
956                                the product
957     :return: True if the product it has a compilation script, else False
958     :rtype: boolean
959     """
960     if "build_source" not in product_info:
961         # Native case
962         return False
963     build_src = product_info.build_source
964     return build_src.lower() == 'script'
965
966 def product_has_env_script(product_info):
967     """Know if a product has an environment script
968     
969     :param product_info Config: The configuration specific to 
970                                the product
971     :return: True if the product it has an environment script, else False
972     :rtype: boolean
973     """
974     return "environ" in product_info and "env_script" in product_info.environ
975
976 def product_has_patches(product_info):
977     """Know if a product has one or more patches
978     
979     :param product_info Config: The configuration specific to 
980                                the product
981     :return: True if the product has one or more patches
982     :rtype: boolean
983     """   
984     res = ( "patches" in product_info and len(product_info.patches) > 0 )
985     DBG.write('product_has_patches %s' % product_info.name, res)
986     # if product_info.name == "XDATA": return True #test #10569
987     return res
988
989 def product_has_logo(product_info):
990     """Know if a product has a logo (YACSGEN generate)
991     
992     :param product_info Config: The configuration specific to 
993                                the product
994     :return: The path of the logo if the product has a logo, else False
995     :rtype: Str
996     """
997     if ("properties" in product_info and
998             "logo" in product_info.properties):
999         return product_info.properties.logo
1000     else:
1001         return False
1002
1003 def product_has_licence(product_info, path):
1004     """Find out if a product has a licence
1005     
1006     :param product_info Config: The configuration specific to the product
1007     :param path Str: The path where to search for the licence
1008     :return: The name of the licence file (the complete path if it is found in the path, else the name, else False
1009     :rtype: Str
1010     """
1011     if ("properties" in product_info and
1012             "licence" in product_info.properties):
1013         licence_name = product_info.properties.licence
1014         if len(path) > 0:
1015             # search for licence_name in path
1016             # a- consolidate the path into one signe string licence_path
1017             licence_path=path[0]
1018             for lpath in path[1:]:
1019                 licence_path=licence_path+":"+lpath
1020             licence_path_list=licence_path.split(":")
1021             licence_fullname = src.find_file_in_lpath(licence_name, licence_path_list)
1022             if licence_fullname:
1023                 return licence_fullname
1024
1025         # if the search of licence in path failed, we return its name (not the full path) 
1026         return licence_name
1027
1028     else:
1029         return False  # product has no licence
1030
1031 def product_has_salome_gui(product_info):
1032     """Know if a product has a SALOME gui
1033     
1034     :param product_info Config: The configuration specific to 
1035                                the product
1036     :return: True if the product has a SALOME gui, else False
1037     :rtype: Boolean
1038     """
1039     return ("properties" in product_info and
1040             "has_salome_gui" in product_info.properties and
1041             product_info.properties.has_salome_gui == "yes")
1042
1043 def product_is_mpi(product_info):
1044     """Know if a product has openmpi in its dependencies
1045     
1046     :param product_info Config: The configuration specific to 
1047                                the product
1048     :return: True if the product has openmpi inits dependencies
1049     :rtype: boolean
1050     """
1051     return "openmpi" in product_info.depend
1052
1053 def product_is_generated(product_info):
1054     """Know if a product is generated (YACSGEN)
1055     
1056     :param product_info Config: The configuration specific to 
1057                                the product
1058     :return: True if the product is generated
1059     :rtype: boolean
1060     """
1061     return ("properties" in product_info and
1062             "generate" in product_info.properties and
1063             product_info.properties.generate == "yes")
1064
1065 def get_product_components(product_info):
1066     """Get the component list to generate with the product
1067     
1068     :param product_info Config: The configuration specific to 
1069                                the product
1070     :return: The list of names of the components
1071     :rtype: List
1072     
1073     """
1074     if not product_is_generated(product_info):
1075         return []
1076     
1077     compo_list = []
1078     if "component_name" in product_info:
1079         compo_list = product_info.component_name
1080     
1081         if isinstance(compo_list, str):
1082             compo_list = [ compo_list ]
1083
1084     return compo_list