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