Salome HOME
sat #20490 : bug faux positif de sat prepare si le tag n'existe pas, ajout d'infos...
[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-" # trace product depends version(s)
35 PRODUCT_FILENAME = "sat-product-" # 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         if 'base' in config.APPLICATION:
75             base = config.APPLICATION.base
76
77     # special case for which only the product name is mentionned 
78     if isinstance(version, bool):
79         version = config.APPLICATION.tag
80
81     if isinstance(version, src.pyconf.Mapping):
82         dic_version = version
83         # Get the version/tag
84         if not 'tag' in dic_version:
85             version = config.APPLICATION.tag
86         else:
87             version = dic_version.tag
88         
89         # Get the debug if any
90         if 'debug' in dic_version:
91             debug = dic_version.debug
92         elif 'debug' in config.APPLICATION:
93             debug = config.APPLICATION.debug
94         
95         # Get the verbose if any
96         if 'verbose' in dic_version:
97             verbose = dic_version.verbose
98         elif 'verbose' in config.APPLICATION:
99             verbose = config.APPLICATION.verbose
100         
101         # Get the dev if any
102         if 'dev' in dic_version:
103             dev = dic_version.dev
104         elif 'dev' in config.APPLICATION:
105             dev = config.APPLICATION.dev
106         
107         # Get the hpc if any
108         if 'hpc' in dic_version:
109             hpc = dic_version.hpc
110         elif 'hpc' in config.APPLICATION:
111             hpc = config.APPLICATION.hpc
112
113         # Get the base if any
114         if 'base' in dic_version:
115             base = dic_version.base
116         elif 'base' in config.APPLICATION:
117             base = config.APPLICATION.base
118
119         # Get the section if any
120         if 'section' in dic_version:
121             section = dic_version.section
122     
123     # this case occur when version is overwritten, cf sat # 8897
124     if isinstance(version, dict): 
125         dic_version = version
126         # Get the version/tag
127         if not 'tag' in dic_version:
128             version = config.APPLICATION.tag
129         else:
130             version = dic_version["tag"]
131         
132         # Get the debug if any
133         if 'debug' in dic_version:
134             debug = dic_version["debug"]
135         elif 'debug' in config.APPLICATION:
136             debug = config.APPLICATION.debug
137         
138         # Get the verbose if any
139         if 'verbose' in dic_version:
140             verbose = dic_version["verbose"]
141         elif 'verbose' in config.APPLICATION:
142             verbose = config.APPLICATION.verbose
143         
144         # Get the dev if any
145         if 'dev' in dic_version:
146             dev = dic_version["dev"]
147         elif 'dev' in config.APPLICATION:
148             dev = config.APPLICATION.dev
149         
150         # Get the hpc if any
151         if 'hpc' in dic_version:
152             hpc = dic_version.hpc
153         elif 'hpc' in config.APPLICATION:
154             hpc = config.APPLICATION.hpc
155
156         # Get the base if any
157         if 'base' in dic_version:
158             base = dic_version["base"]
159
160         # Get the section if any
161         if 'section' in dic_version:
162             section = dic_version['section']
163
164     vv = version
165     # substitute some character with _ in order to get the correct definition
166     # in config.PRODUCTS. This is done because the pyconf tool does not handle
167     # the . and - characters 
168     for c in ".-": vv = vv.replace(c, "_")
169
170     prod_info = None
171     if product_name in config.PRODUCTS:
172         # Search for the product description in the configuration
173         prod_info = get_product_section(config, product_name, vv, section)
174         
175         # get salomeTool version
176         prod_info.sat_version = src.get_salometool_version(config)
177
178         # merge opt_depend in depend
179         if prod_info is not None and 'opt_depend' in prod_info:
180             for depend in prod_info.opt_depend:
181                 if depend in config.APPLICATION.products:
182                     prod_info.depend.append(depend,'')
183         
184
185         # In case of a product get with a vcs, 
186         # put the tag (equal to the version)
187         if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
188             
189             if prod_info.get_source == 'git':
190                 prod_info.git_info.tag = version
191             
192             if prod_info.get_source == 'svn':
193                 prod_info.svn_info.tag = version
194             
195             if prod_info.get_source == 'cvs':
196                 prod_info.cvs_info.tag = version
197         
198         # In case of a fixed product, 
199         # define the install_dir (equal to the version)
200         if prod_info is not None and \
201            (os.path.isdir(version) or version.startswith("/")):
202            # we consider a (fixed) path  existing paths; 
203            # or paths starting with '/' (the objective is to print a correct 
204            # message to the user in case of non existing path.)
205             prod_info.install_dir = version
206             prod_info.get_source = "fixed"
207         
208         # Check if the product is defined as native in the application
209         if prod_info is not None:
210             if version == "native":
211                 prod_info.get_source = "native"
212             elif prod_info.get_source == "native":
213                 msg = _("The product %(prod)s has version %(ver)s but is "
214                         "declared as native in its definition" %
215                         { 'prod': prod_info.name, 'ver': version})
216                 raise src.SatException(msg)
217
218     # If there is no definition but the product is declared as native,
219     # construct a new definition containing only the get_source key
220     if prod_info is None and version == "native":
221         prod_info = src.pyconf.Config()
222         prod_info.name = product_name
223         prod_info.get_source = "native"
224
225     # If there is no definition but the product is fixed,
226     # construct a new definition containing only the product name
227     if prod_info is None and os.path.isdir(version):
228         prod_info = src.pyconf.Config()
229         prod_info.name = product_name
230         prod_info.get_source = "fixed"
231         prod_info.addMapping("environ", src.pyconf.Mapping(prod_info), "")
232
233
234     # If prod_info is still None, it means that there is no product definition
235     # in the config. The user has to provide it.
236     if prod_info is None:
237         prod_pyconf_path = src.find_file_in_lpath(product_name + ".pyconf",
238                                                   config.PATHS.PRODUCTPATH)
239         if not prod_pyconf_path:
240             msg = _("""\
241 No definition found for the product %(1)s.
242 Please create a %(1)s.pyconf file somewhere in:
243   %(2)s""") % {
244   "1": product_name,
245   "2": PP.pformat(config.PATHS.PRODUCTPATH) }
246         else:
247             msg = _("""\
248 No definition corresponding to the version %(1)s was found in the file:
249   %(2)s.
250 Please add a section in it.""") % {"1" : vv, "2" : prod_pyconf_path}
251         raise src.SatException(msg)
252     
253     # Set the debug, dev and version keys
254     prod_info.debug = debug
255     prod_info.verbose = verbose
256     prod_info.dev = dev
257     prod_info.hpc = hpc
258     prod_info.version = version
259     if base != 'maybe':
260         prod_info.base = base
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         elif "archive_prefix" in prod_info.archive_info:
271             arch_name = prod_info.archive_info.archive_prefix + "-" + version + ".tar.gz"
272         else:
273             # standard name
274             arch_name = product_name + "-" + version + ".tar.gz"
275
276         arch_path = src.find_file_in_lpath(arch_name,
277                                            config.PATHS.ARCHIVEPATH)
278         if not arch_path:
279             # arch_path is not found. It may generate an error in sat source,
280             #                         unless the archive is found in ftp serveur
281             prod_info.archive_info.archive_name = arch_name #without path
282         else:
283             prod_info.archive_info.archive_name = arch_path
284
285         
286     # If the product compiles with a script, check the script existence
287     # and if it is executable
288     if product_has_script(prod_info):
289         # Check the compil_script key existence
290         if "compil_script" not in prod_info:
291             msg = _("""\
292 No compilation script found for the product %s.
293 Please provide a 'compil_script' key in its definition.""") % product_name
294             raise src.SatException(msg)
295         
296         # Get the path of the script file
297         # if windows supposed '.bat', if linux supposed '.sh'
298         # but user set extension script file in his pyconf as he wants, no obligation.
299         script = prod_info.compil_script
300         script_name = os.path.basename(script)
301         if script == script_name:
302             # Only a name is given. Search in the default directory
303             script_path = src.find_file_in_lpath(script_name, config.PATHS.PRODUCTPATH, "compil_scripts")
304             if not script_path:
305                 msg = _("Compilation script %s not found in") % script_name
306                 DBG.tofix(msg, config.PATHS.PRODUCTPATH, True) # say where searched
307                 script_path = "%s_(Not_Found_by_Sat!!)" % script_name
308             prod_info.compil_script = script_path
309
310        
311         # Check that the script is executable
312         if os.path.exists(prod_info.compil_script) and not os.access(prod_info.compil_script, os.X_OK):
313             DBG.tofix("Compilation script  file is not in 'execute mode'", prod_info.compil_script, True)
314     
315     # Get the full paths of all the patches
316     if product_has_patches(prod_info):
317         patches = []
318         try:
319           for patch in prod_info.patches:
320               patch_path = patch
321               # If only a filename, then search for the patch in the PRODUCTPATH
322               if os.path.basename(patch_path) == patch_path:
323                   # Search in the PRODUCTPATH/patches
324                   patch_path = src.find_file_in_lpath(patch,
325                                                       config.PATHS.PRODUCTPATH,
326                                                       "patches")
327                   if not patch_path:
328                       msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
329                               "\n" % {"patch_name" : patch,
330                                        "prod_name" : prod_info.name}) 
331                       raise src.SatException(msg)
332               patches.append(patch_path)
333         except:
334           DBG.tofix("problem in prod_info.patches", prod_info)
335         prod_info.patches = patches
336
337     # Get the full paths of the environment scripts
338     if product_has_env_script(prod_info):
339         env_script_path = prod_info.environ.env_script
340         # If only a filename, then search for the environment script 
341         # in the PRODUCTPATH/env_scripts
342         if os.path.basename(env_script_path) == env_script_path:
343             # Search in the PRODUCTPATH/env_scripts
344             env_script_path = src.find_file_in_lpath(
345                                             prod_info.environ.env_script,
346                                             config.PATHS.PRODUCTPATH,
347                                             "env_scripts")
348             if not env_script_path:
349                 msg = _("Environment script %(env_name)s for %(prod_name)s not "
350                         "found.\n" % {"env_name" : env_script_path,
351                                        "prod_name" : prod_info.name}) 
352                 raise src.SatException(msg)
353
354         prod_info.environ.env_script = env_script_path
355     
356     if with_install_dir: 
357         # The variable with_install_dir is at false only for internal use 
358         # of the function get_install_dir
359         
360         # Save the install_dir key if there is any
361         if "install_dir" in prod_info and not "install_dir_save" in prod_info:
362             prod_info.install_dir_save = prod_info.install_dir
363         
364         # if it is not the first time the install_dir is computed, it means
365         # that install_dir_save exists and it has to be taken into account.
366         if "install_dir_save" in prod_info:
367             prod_info.install_dir = prod_info.install_dir_save
368         
369         # Set the install_dir key
370         prod_info.install_dir = get_install_dir(config, version, prod_info)
371                 
372     return prod_info
373
374 def get_product_section(config, product_name, version, section=None):
375     """Build the product description from the configuration
376     
377     :param config Config: The global configuration
378     :param product_name str: The product name
379     :param version str: The version of the product as 'V8_4_0', or else.
380     :param section str: The searched section (if not None, the section is 
381                         explicitly given
382     :return: The product description
383     :rtype: Config
384     """
385
386
387     #get product definition and determine if the incremental definition mode is activated
388     aProd = config.PRODUCTS[product_name]
389     if "default" in aProd and\
390        "properties" in aProd.default and\
391        "incremental" in aProd.default.properties and\
392        aProd.default.properties.incremental == "yes":
393         # in this (new) mode the definition of the product is given by the default section
394         # and is incremented by others.
395         is_incr=True
396     else:
397         # in this (historic) mode the definition of the product is given by a full unique section
398         is_incr=False
399
400     # decode version number
401     try:
402       versionMMP = VMMP.MinorMajorPatch(version)
403     except: # example setuptools raise "minor in major_minor_patch is not integer: '0_6c11'"
404       versionMMP = None
405
406     # if a section is explicitely specified we select it
407     if section:
408         if section not in aProd:
409             pi=None
410         # returns specific information for the given version
411         pi = aProd[section]
412         pi.section = section
413         pi.from_file = aProd.from_file
414
415     # If it exists, get the information of the product_version
416     # ex: 'version_V6_6_0' as salome version classical syntax
417     elif "version_" + version in aProd:
418         # returns specific information for the given version
419         pi = aProd["version_" + version]
420         pi.section = "version_" + version
421         pi.from_file = aProd.from_file
422
423     # Else, check if there is a description for multiple versions
424     else:
425         l_section_names = aProd.keys()
426         l_section_ranges = []
427         tagged = []
428         for name in l_section_names:
429           aRange = VMMP.getRange_majorMinorPatch(name)
430           if aRange is not None:
431             l_section_ranges.append((name, aRange))
432
433         if versionMMP is not None and len(l_section_ranges) > 0:
434           for name, (vmin, vmax) in l_section_ranges:
435             if versionMMP >= vmin and versionMMP <= vmax:
436               tagged.append((name, [vmin, vmax]))
437
438         if len(tagged) > 1:
439           DBG.write("multiple version ranges tagged for '%s', fix it" % version,
440                          PP.pformat(tagged))
441           pi=None
442         elif len(tagged) == 1: # ok
443           name, (vmin, vmax) = tagged[0]
444           pi = aProd[name]
445           pi.section = name
446           pi.from_file = aProd.from_file
447
448         # Else, get the standard informations
449         elif "default" in aProd:
450             # returns the generic information (given version not found)
451             pi = aProd.default
452             pi.section = "default"
453             pi.from_file = aProd.from_file
454         else:
455             pi=None
456
457     if is_incr:
458         # If the definition is incremental, we take the default section
459         # and then complete it with other sections : 
460         #   - default_win
461         #   - the selected section (pi)
462         #   - the selected _win section
463         prod_info=aProd["default"]
464         prod_info.from_file = aProd.from_file
465         prod_info.section = "default"
466         if src.architecture.is_windows() and "default_win" in aProd:
467             for key in aProd["default_win"]:
468                 prod_info[key]=aProd["default_win"][key]
469         if pi!=None and pi.section!="default":
470             # update prod_info with incremental definition contained in pi
471             for key in pi:
472                 prod_info[key]=pi[key]
473             win_section=pi.section+"_win"
474             if src.architecture.is_windows() and win_section in aProd:
475                 for key in aProd[win_section]:
476                     prod_info[key]=aProd[win_section][key]
477     else:
478         prod_info=pi
479
480     #DBG.write("product info returned for product %s with version %s and section %s" %\
481     #          (product_name, version, section), prod_info)
482     return prod_info
483     
484 def get_install_dir(config, version, prod_info):
485     """Compute the installation directory of a given product 
486     
487     :param config Config: The global configuration
488     :param base str: This corresponds to the value given by user in its 
489                      application.pyconf for the specific product. If "yes", the
490                      user wants the product to be in base. If "no", he wants the
491                      product to be in the application workdir
492     :param version str: The version of the product
493     :param product_info Config: The configuration specific to 
494                                the product
495     
496     :return: The path of the product installation
497     :rtype: str
498     """
499     install_dir = ""
500     in_base = False
501     # prod_info.base : corresponds to what is specified in application pyconf (either from the global key base, or from a product dict)
502     # prod_info.install_dir : corresponds to what is specified in product pyconf (usually "base" for prerequisites)
503     if ( ("install_dir" in prod_info and prod_info.install_dir == "base") # product is declared as base in its own config 
504                                       or ("base" in prod_info  and prod_info.base != "no") ):  # product is declared as base in the application
505         # a product goes in base if install_dir is set to base, or if product was declared based in application pyconf
506         in_base = True
507
508     # check desactivation of base at application level
509     if ( "base" in prod_info  and prod_info.base == "no"):
510         in_base = False
511
512     if in_base:
513         install_dir = get_base_install_dir(config, prod_info, version)
514     else:
515         if "install_dir" not in prod_info or prod_info.install_dir == "base":
516             # Set it to the default value (in application directory)
517             if ( src.appli_test_property(config,"single_install_dir", "yes") and 
518                  src.product.product_test_property(prod_info,"single_install_dir", "yes")):
519                 # when single_install_dir mode is activated in tha application
520                 # we use config.INTERNAL.config.single_install_dir for products 
521                 # having single_install_dir property
522                 install_dir = os.path.join(config.APPLICATION.workdir,
523                                            config.INTERNAL.config.install_dir,
524                                            config.INTERNAL.config.single_install_dir)
525             elif ( src.appli_test_property(config,"pip", "yes") and 
526                    src.product.product_test_property(prod_info,"pip", "yes") and
527                    src.appli_test_property(config,"pip_install_dir", "python") ):
528                 # when pip mode is activated in the application
529                 # and product is pip, and pip_install_dir is set to python 
530                 # we assume python in installed in install_dir/Python
531                 install_dir = os.path.join(config.APPLICATION.workdir,
532                                            config.INTERNAL.config.install_dir,
533                                            "Python")   
534             else:
535                 install_dir = os.path.join(config.APPLICATION.workdir,
536                                            config.INTERNAL.config.install_dir,
537                                            prod_info.name)
538         else:
539             install_dir = prod_info.install_dir
540
541     return install_dir
542
543 def get_base_install_dir(config, prod_info, version):
544     """Compute the installation directory of a product in base 
545     
546     :param config Config: The global configuration
547     :param product_info Config: The configuration specific to 
548                                the product
549     :param version str: The version of the product    
550     :param base str: This corresponds to the value given by user in its 
551                      application.pyconf for the specific product. If "yes", the
552                      user wants the product to be in base. If "no", he wants the
553                      product to be in the application workdir.
554                      if it contains something else, is is interpreted as the name 
555                      of a base we build for module load.
556     :return: The path of the product installation
557     :rtype: str
558     """    
559     
560     # get rid of / to avoid create subdirectories cf sat #18546
561     version_wslash=version.replace("/", "_") 
562
563     if ( src.appli_test_property(config,"pip", "yes") and 
564          src.product.product_test_property(prod_info,"pip", "yes") and
565          src.appli_test_property(config,"pip_install_dir", "python") ):
566          # when pip mode is activated in the application
567          # and product is pip, and pip_install_dir is set to python 
568         python_info=get_product_config(config, "Python")
569         return python_info.install_dir
570
571     base_path = src.get_base_path(config) 
572     if "base" in prod_info and prod_info.base != "no" and prod_info.base != "yes":
573         # we are in the case of a named base
574         prod_dir = os.path.join(base_path, "apps", prod_info.base, prod_info.name, version_wslash)
575         return prod_dir
576     
577     prod_dir = os.path.join(base_path, prod_info.name + "-" + version_wslash)
578     if not os.path.exists(prod_dir):
579         return os.path.join(prod_dir, "config-1")
580     
581     exists, install_dir = check_config_exists(config, prod_dir, prod_info)
582     if exists:
583         return install_dir
584     
585     # Find the first config-<i> directory that is available in the product
586     # directory
587     found = False 
588     label = 1
589     while not found:
590         install_dir = os.path.join(prod_dir, "config-%i" % label)
591         if os.path.exists(install_dir):
592             label+=1
593         else:
594             found = True
595             
596     return install_dir
597
598 def add_compile_config_file(p_info, config):
599     '''Execute the proper configuration command(s)
600        in the product build directory.
601
602     :param p_info Config: The specific config of the product
603     :param config Config: The global configuration
604     '''
605     # Create the compile config
606     # DBG.write("add_compile_config_file", p_info, True)
607     res = src.pyconf.Config()
608     res.addMapping(p_info.name, src.pyconf.Mapping(res), "")
609     res[p_info.name]= p_info.version
610
611     depprod=[]
612     for d in p_info.depend:
613         depprod.append(d)
614     if "build_depend" in p_info:
615         for d in p_info.build_depend:
616             depprod.append(d)
617     for prod_name in depprod:
618       if prod_name not in res:
619         res.addMapping(prod_name, src.pyconf.Mapping(res), "")
620       prod_dep_info = src.product.get_product_config(config, prod_name, False)
621       res[prod_name] = prod_dep_info.version
622     # Write it in the install directory of the product
623     # This file is for automatic reading/checking
624     # see check_config_exists method
625     afilename = CONFIG_FILENAME + p_info.name + ".pyconf"
626     aFile = os.path.join(p_info.install_dir, afilename)
627     with open(aFile, 'w') as f:
628       res.__save__(f)
629
630     # this file is not mandatory, is for human eye reading
631     afilename = PRODUCT_FILENAME + p_info.name + ".pyconf"
632     aFile = os.path.join(p_info.install_dir, afilename)
633     try:
634       with open(aFile, 'w') as f:
635         p_info.__save__(f, evaluated=True) # evaluated expressions mode
636     except:
637       # sometime some information cannot be evaluated.
638       # for example, in the context of non VCS archives, information on git server is not available.
639       DBG.write("Warning : sat was not able to evaluate and write down some information in file %s" % aFile)
640   
641
642 def check_config_exists(config, prod_dir, prod_info, verbose=False):
643     """\
644     Verify that the installation directory of a product in a base exists.
645     Check all the config-<i>/sat-config.py files found for correspondence
646     with current config and prod_info depend-version-tags
647     
648     :param config Config: The global configuration
649     :param prod_dir str: The product installation directory path 
650                          (without config-<i>)
651     :param product_info Config: The configuration specific to 
652                                the product
653     :return: True or false is the installation is found or not 
654              and if it is found, the path of the found installation
655     :rtype: (boolean, str)
656     """
657     # check if the directories or files of the directory corresponds to the
658     # directory installation of the product
659     if os.path.isdir(prod_dir):
660       l_dir_and_files = os.listdir(prod_dir)
661     else:
662       raise Exception("Inexisting directory '%s'" % prod_dir)
663
664     DBG.write("check_config_exists 000",  (prod_dir, l_dir_and_files), verbose)
665     DBG.write("check_config_exists 111",  prod_info, verbose)
666
667     depend_all=[]
668     if "depend" in prod_info:
669         for d in prod_info.depend:
670             depend_all.append(d)
671     if "build_depend" in prod_info:
672         for d in prod_info.build_depend:
673             depend_all.append(d)
674     for dir_or_file in l_dir_and_files:
675         oExpr = re.compile(config_expression)
676         if not(oExpr.search(dir_or_file)):
677             # in mode BASE, not config-<i>, not interesting
678             # DBG.write("not interesting", dir_or_file, True)
679             continue
680         # check if there is the file sat-config.pyconf file in the installation
681         # directory    
682         afilename = CONFIG_FILENAME + prod_info.name + ".pyconf"
683         config_file = os.path.join(prod_dir, dir_or_file, afilename)
684         DBG.write("check_config_exists 222", config_file, verbose)
685         if not os.path.exists(config_file):
686             continue
687         
688         # check if there is the config described in the file corresponds the 
689         # dependencies of the product
690         config_corresponds = True    
691         compile_cfg = src.pyconf.Config(config_file)
692         for prod_dep in depend_all:
693             # if the dependency is not in the config, 
694             # the config does not correspond
695             if prod_dep not in compile_cfg:
696                 config_corresponds = False
697                 break
698             else:
699                 prod_dep_info = get_product_config(config, prod_dep, False)
700                 # If the version of the dependency does not correspond, 
701                 # the config does not correspond
702                 if prod_dep_info.version != compile_cfg[prod_dep]:
703                     config_corresponds = False
704                     break
705
706         if config_corresponds:
707           for prod_name in compile_cfg:
708             # assume new compatibility with prod_name in sat-config.pyconf files
709             if prod_name == prod_info.name:
710               if prod_info.version == compile_cfg[prod_name]:
711                 DBG.write("check_config_exists OK 333", compile_cfg, verbose)
712                 pass
713               else: # no correspondence with newer with prod_name sat-config.pyconf files
714                 config_corresponds = False
715                 break
716             else:
717               # as old compatibility without prod_name sat-config.pyconf files
718               if prod_name not in depend_all:
719                 # here there is an unexpected depend in an old compilation
720                 config_corresponds = False
721                 break
722         
723         if config_corresponds: # returns (and stops) at first correspondence found
724             DBG.write("check_config_exists OK 444", dir_or_file, verbose)
725             return True, os.path.join(prod_dir, dir_or_file)
726
727     # no correspondence found
728     return False, None
729             
730             
731     
732 def get_products_infos(lproducts, config):
733     """Get the specific configuration of a list of products
734     
735     :param lproducts List: The list of product names
736     :param config Config: The global configuration
737     :return: the list of tuples 
738              (product name, specific configuration of the product)
739     :rtype: [(str, Config)]
740     """
741     products_infos = []
742     # Loop on product names
743     for prod in lproducts:       
744         # Get the specific configuration of the product
745         prod_info = get_product_config(config, prod)
746         if prod_info is not None:
747             products_infos.append((prod, prod_info))
748         else:
749             msg = _("The %s product has no definition in the configuration.") % prod
750             raise src.SatException(msg)
751     return products_infos
752
753
754 def get_products_list(options, cfg, logger):
755     """
756     method that gives the product list with their informations from
757     configuration regarding the passed options.
758
759     :param options Options: The Options instance that stores the commands arguments
760     :param cfg Config: The global configuration
761     :param logger Logger: The logger instance to use for the display and logging
762     :return: The list of (product name, product_informations).
763     :rtype: List
764     """
765     # Get the products to be prepared, regarding the options
766     if options.products is None:
767         # No options, get all products sources
768         products = cfg.APPLICATION.products
769     else:
770         # if option --products, check that all products of the command line
771         # are present in the application.
772         """products = options.products
773         for p in products:
774             if p not in cfg.APPLICATION.products:
775                 raise src.SatException(_("Product %(product)s "
776                             "not defined in application %(application)s") %
777                         { 'product': p, 'application': cfg.VARS.application} )"""
778
779         products = src.getProductNames(cfg, options.products, logger)
780
781     # Construct the list of tuple containing
782     # the products name and their definition
783     resAll = src.product.get_products_infos(products, cfg)
784
785     # if the property option was passed, filter the list
786     if options.properties: # existing properties
787       ok = []
788       ko = []
789       res =[]
790       prop, value = options.properties # for example 'is_SALOME_module', 'yes'
791       for p_name, p_info in resAll:
792         try:
793           if p_info.properties[prop] == value:
794             res.append((p_name, p_info))
795             ok.append(p_name)
796           else:
797             ko.append(p_name)
798         except:
799           ko.append(p_name)
800
801       if len(ok) != len(resAll):
802         logger.trace("on properties %s\n products accepted:\n %s\n products rejected:\n %s\n" %
803                        (options.properties, PP.pformat(sorted(ok)), PP.pformat(sorted(ko))))
804       else:
805         logger.warning("properties %s\n seems useless with no products rejected" %
806                        (options.properties))
807     else:
808       res = resAll # not existing properties as all accepted
809
810     return res
811
812
813 def get_product_dependencies(config, product_info):
814     """\
815     Get recursively the list of products that are 
816     in the product_info dependencies
817     
818     :param config Config: The global configuration
819     :param product_info Config: The configuration specific to 
820                                the product
821     :return: the list of products in dependence
822     :rtype: list
823     """
824     depend_all=[]
825     if "depend" in product_info:
826         for d in product_info.depend:
827             depend_all.append(d)
828     if "build_depend" in product_info:
829         for d in product_info.build_depend:
830             depend_all.append(d)
831
832     if len(depend_all) == 0:
833         return []
834
835     res = []
836     for prod in depend_all:
837         if prod == product_info.name:
838             continue
839         if prod not in res:
840             res.append(prod)
841         prod_info = get_product_config(config, prod)
842         dep_prod = get_product_dependencies(config, prod_info)
843         for prod_in_dep in dep_prod:
844             if prod_in_dep not in res:
845                 res.append(prod_in_dep)
846     return res
847
848 def check_installation(config, product_info):
849     """\
850     Verify if a product is well installed. Checks install directory presence
851     and some additional files if it is defined in the config 
852     
853     :param product_info Config: The configuration specific to 
854                                the product
855     :return: True if it is well installed
856     :rtype: boolean
857     """
858     # don't check products that are not compiled
859     if not product_compiles(product_info):
860         return True
861
862     if product_is_native(product_info):
863         # check a system product
864         check_cmd=src.system.get_pkg_check_cmd(config.VARS.dist_name)
865         run_pkg,build_pkg=src.product.check_system_dep(check_cmd, product_info)
866         build_dep_ko=[]
867         for pkg in build_pkg:
868             if "KO" in build_pkg[pkg]:
869                build_dep_ko.append(pkg) 
870         if build_dep_ko:
871               # the product is not installed : display message and return error status
872               msg="Please install them with %s before compiling salome" % check_cmd[0]
873               print("\nmissing compile time dependencies : ")
874               for md in build_dep_ko: 
875                   print(md)
876               print(msg)
877               return False
878         else:
879             return True    
880
881     install_dir = product_info.install_dir
882     if src.product.product_is_fixed(product_info):
883         # we check directly the install dir only for fixed products
884         # (there is no pyconf file in that case)
885         if not os.path.exists(install_dir):
886             return False
887     else:
888         filename = CONFIG_FILENAME + product_info.name + ".pyconf"
889         if not os.path.exists(os.path.join(install_dir, filename)): 
890             return False
891
892     # check extra files if specified in present_files.install section
893     if ("present_files" in product_info and 
894         "install" in product_info.present_files):
895         for file_relative_path in product_info.present_files.install:
896             file_path = os.path.join(install_dir, file_relative_path)
897             if not os.path.exists(file_path):
898                 return False
899     return True
900
901 def check_source(product_info):
902     """Verify if a sources of product is preset. Checks source directory presence
903     
904     :param product_info Config: The configuration specific to 
905                                the product
906     :return: True if it is well installed
907     :rtype: boolean
908     """
909     source_dir = product_info.source_dir
910     if not os.path.exists(source_dir):
911         return False
912     if ("present_files" in product_info and 
913         "source" in product_info.present_files):
914         for file_relative_path in product_info.present_files.source:
915             file_path = os.path.join(source_dir, file_relative_path)
916             if not os.path.exists(file_path):
917                 return False
918     return True
919
920 def product_is_salome(product_info):
921     """Know if a product is a SALOME module
922     
923     :param product_info Config: The configuration specific to 
924                                the product
925     :return: True if the product is a SALOME module, else False
926     :rtype: boolean
927     """
928     return ("properties" in product_info and
929             "is_SALOME_module" in product_info.properties and
930             product_info.properties.is_SALOME_module == "yes")
931
932 def product_is_fixed(product_info):
933     """Know if a product is fixed
934     
935     :param product_info Config: The configuration specific to 
936                                the product
937     :return: True if the product is fixed, else False
938     :rtype: boolean
939     """
940     get_src = product_info.get_source
941     return get_src.lower() == 'fixed'
942
943 def product_is_native(product_info):
944     """Know if a product is native
945     
946     :param product_info Config: The configuration specific to 
947                                the product
948     :return: True if the product is native, else False
949     :rtype: boolean
950     """
951     get_src = product_info.get_source
952     return get_src.lower() == 'native'
953
954 def product_is_dev(product_info):
955     """Know if a product is in dev mode
956     
957     :param product_info Config: The configuration specific to 
958                                the product
959     :return: True if the product is in dev mode, else False
960     :rtype: boolean
961     """
962     dev = product_info.dev
963     res = (dev.lower() == 'yes')
964     DBG.write('product_is_dev %s' % product_info.name, res)
965     # if product_info.name == "XDATA": return True #test #10569
966     return res
967
968 def product_is_hpc(product_info):
969     """Know if a product is in hpc mode
970     
971     :param product_info Config: The configuration specific to 
972                                the product
973     :return: True if the product is in hpc mode, else False
974     :rtype: boolean
975     """
976     hpc = product_info.hpc
977     res = (hpc.lower() == 'yes')
978     return res
979
980 def product_is_debug(product_info):
981     """Know if a product is in debug mode
982     
983     :param product_info Config: The configuration specific to 
984                                the product
985     :return: True if the product is in debug mode, else False
986     :rtype: boolean
987     """
988     debug = product_info.debug
989     return debug.lower() == 'yes'
990
991 def product_is_verbose(product_info):
992     """Know if a product is in verbose mode
993     
994     :param product_info Config: The configuration specific to 
995                                the product
996     :return: True if the product is in verbose mode, else False
997     :rtype: boolean
998     """
999     verbose = product_info.verbose
1000     return verbose.lower() == 'yes'
1001
1002 def product_is_autotools(product_info):
1003     """Know if a product is compiled using the autotools
1004     
1005     :param product_info Config: The configuration specific to 
1006                                the product
1007     :return: True if the product is autotools, else False
1008     :rtype: boolean
1009     """
1010     build_src = product_info.build_source
1011     return build_src.lower() == 'autotools'
1012
1013 def product_is_cmake(product_info):
1014     """Know if a product is compiled using the cmake
1015     
1016     :param product_info Config: The configuration specific to 
1017                                the product
1018     :return: True if the product is cmake, else False
1019     :rtype: boolean
1020     """
1021     build_src = product_info.build_source
1022     return build_src.lower() == 'cmake'
1023
1024 def product_is_vcs(product_info):
1025     """Know if a product is download using git, svn or cvs (not archive)
1026     
1027     :param product_info Config: The configuration specific to 
1028                                the product
1029     :return: True if the product is vcs, else False
1030     :rtype: boolean
1031     """
1032     return product_info.get_source in AVAILABLE_VCS
1033
1034 def product_is_smesh_plugin(product_info):
1035     """Know if a product is a SMESH plugin
1036     
1037     :param product_info Config: The configuration specific to 
1038                                the product
1039     :return: True if the product is a SMESH plugin, else False
1040     :rtype: boolean
1041     """
1042     return ("properties" in product_info and
1043             "smesh_plugin" in product_info.properties and
1044             product_info.properties.smesh_plugin == "yes")
1045
1046 def product_is_cpp(product_info):
1047     """Know if a product is cpp
1048     
1049     :param product_info Config: The configuration specific to 
1050                                the product
1051     :return: True if the product is a cpp, else False
1052     :rtype: boolean
1053     """
1054     return ("properties" in product_info and
1055             "cpp" in product_info.properties and
1056             product_info.properties.cpp == "yes")
1057
1058 def product_compiles(product_info):
1059     """\
1060     Know if a product compiles or not 
1061     (some products do not have a compilation procedure)
1062     
1063     :param product_info Config: The configuration specific to 
1064                                the product
1065     :return: True if the product compiles, else False
1066     :rtype: boolean
1067     """
1068     return not("properties" in product_info and
1069             "compilation" in product_info.properties and
1070             product_info.properties.compilation == "no")
1071
1072 def product_has_script(product_info):
1073     """Know if a product has a compilation script
1074     
1075     :param product_info Config: The configuration specific to 
1076                                the product
1077     :return: True if the product it has a compilation script, else False
1078     :rtype: boolean
1079     """
1080     if "build_source" not in product_info:
1081         # Native case
1082         return False
1083     build_src = product_info.build_source
1084     return build_src.lower() == 'script'
1085
1086 def product_has_env_script(product_info):
1087     """Know if a product has an environment script
1088     
1089     :param product_info Config: The configuration specific to 
1090                                the product
1091     :return: True if the product it has an environment script, else False
1092     :rtype: boolean
1093     """
1094     return "environ" in product_info and "env_script" in product_info.environ
1095
1096 def product_has_patches(product_info):
1097     """Know if a product has one or more patches
1098     
1099     :param product_info Config: The configuration specific to 
1100                                the product
1101     :return: True if the product has one or more patches
1102     :rtype: boolean
1103     """   
1104     res = ( "patches" in product_info and len(product_info.patches) > 0 )
1105     return res
1106
1107 def product_has_logo(product_info):
1108     """Know if a product has a logo (YACSGEN generate)
1109     
1110     :param product_info Config: The configuration specific to 
1111                                the product
1112     :return: The path of the logo if the product has a logo, else False
1113     :rtype: Str
1114     """
1115     if ("properties" in product_info and
1116             "logo" in product_info.properties):
1117         return product_info.properties.logo
1118     else:
1119         return False
1120
1121 def product_has_licence(product_info, path):
1122     """Find out if a product has a licence
1123     
1124     :param product_info Config: The configuration specific to the product
1125     :param path Str: The path where to search for the licence
1126     :return: The name of the licence file (the complete path if it is found in the path, else the name, else False
1127     :rtype: Str
1128     """
1129     if ("properties" in product_info and
1130             "licence" in product_info.properties):
1131         licence_name = product_info.properties.licence
1132         if len(path) > 0:
1133             # search for licence_name in path
1134             # a- consolidate the path into one signe string licence_path
1135             licence_path=path[0]
1136             for lpath in path[1:]:
1137                 licence_path=licence_path+":"+lpath
1138             licence_path_list=licence_path.split(":")
1139             licence_fullname = src.find_file_in_lpath(licence_name, licence_path_list)
1140             if licence_fullname:
1141                 return licence_fullname
1142
1143         # if the search of licence in path failed, we return its name (not the full path) 
1144         return licence_name
1145
1146     else:
1147         return False  # product has no licence
1148
1149 def product_has_salome_gui(product_info):
1150     """Know if a product has a SALOME gui
1151     
1152     :param product_info Config: The configuration specific to 
1153                                the product
1154     :return: True if the product has a SALOME gui, else False
1155     :rtype: Boolean
1156     """
1157     return ("properties" in product_info and
1158             "has_salome_gui" in product_info.properties and
1159             product_info.properties.has_salome_gui == "yes")
1160
1161 def product_is_mpi(product_info):
1162     """Know if a product has openmpi in its dependencies
1163     
1164     :param product_info Config: The configuration specific to 
1165                                the product
1166     :return: True if the product has openmpi inits dependencies
1167     :rtype: boolean
1168     """
1169     return "openmpi" in product_info.depend
1170
1171 def product_is_generated(product_info):
1172     """Know if a product is generated (YACSGEN)
1173     
1174     :param product_info Config: The configuration specific to 
1175                                the product
1176     :return: True if the product is generated
1177     :rtype: boolean
1178     """
1179     return ("properties" in product_info and
1180             "generate" in product_info.properties and
1181             product_info.properties.generate == "yes")
1182
1183 def product_is_compile_time(product_info):
1184     """Know if a product is only used at compile time
1185     
1186     :param product_info Config: The configuration specific to 
1187                                the product
1188     :return: True if the product is only used at compile time
1189     :rtype: boolean
1190     """
1191     return ("properties" in product_info and
1192             "compile_time" in product_info.properties and
1193             product_info.properties.compile_time == "yes")
1194
1195 def product_is_compile_and_runtime(product_info):
1196     """Know if a product is only used at compile time
1197     
1198     :param product_info Config: The configuration specific to 
1199                                the product
1200     :return: True if the product is only used at compile time
1201     :rtype: boolean
1202     """
1203     return ("properties" in product_info and
1204             "compile_and_runtime" in product_info.properties and
1205             product_info.properties.compile_and_runtime == "yes")
1206
1207
1208
1209 def product_test_property(product_info, property_name, property_value):
1210     """Generic function to test if a product has a property set to a value
1211     
1212     :param product_info Config: The configuration specific to 
1213                                the product
1214     :param property_name : The name of the property to check
1215     :param property_value : The value of the property to test
1216     :return: True if the product has the property and the property is set to property_value
1217     :rtype: boolean
1218     """
1219     # first check if product has the property
1220     if not ("properties" in product_info and
1221             property_name in product_info.properties):
1222         return False
1223   
1224     # then check to the property is set to property_value
1225     eval_expression = 'product_info.properties.%s == "%s"' % (property_name,property_value)
1226     result = eval(eval_expression)
1227     return result
1228
1229 def check_system_dep(check_cmd, product_info):
1230     """Search for system dependencies, check if installed
1231     :param check_cmd Config: The command to use for checking (rpm/apt)
1232     :param product_info Config: The configuration specific to the product
1233     :rtype: two dictionnaries for runtime and compile time dependencies with text status
1234     """
1235     runtime_dep={}
1236     build_dep={}
1237     if "system_info" in product_info:
1238         if check_cmd[0]=="rpm":
1239             if "rpm" in product_info.system_info:
1240                 for pkg in product_info.system_info.rpm:
1241                     runtime_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
1242             if "rpm_dev" in product_info.system_info:
1243                 for pkg in product_info.system_info.rpm_dev:
1244                     build_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
1245         if check_cmd[0]=="apt":
1246             if "apt" in product_info.system_info:
1247                 for pkg in product_info.system_info.apt:
1248                     runtime_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
1249             if "apt_dev" in product_info.system_info:
1250                 for pkg in product_info.system_info.apt_dev:
1251                     build_dep[pkg]=src.system.check_system_pkg(check_cmd,pkg)
1252     return runtime_dep,build_dep
1253
1254
1255 def get_product_components(product_info):
1256     """Get the component list to generate with the product
1257     
1258     :param product_info Config: The configuration specific to 
1259                                the product
1260     :return: The list of names of the components
1261     :rtype: List
1262     
1263     """
1264     if not product_is_generated(product_info):
1265         return []
1266     
1267     compo_list = []
1268     if "component_name" in product_info:
1269         compo_list = product_info.component_name
1270     
1271         if isinstance(compo_list, str):
1272             compo_list = [ compo_list ]
1273
1274     return compo_list
1275 def product_is_wheel(product_info):
1276     """ tells whether a product is a wheel
1277     
1278     :param product_info Config: The configuration specific to 
1279                                the product
1280     :return: True if the product has a wheel, else False
1281     :rtype: Boolean
1282     """
1283     return ("properties" in product_info and
1284             "is_wheel" in product_info.properties and
1285             product_info.properties.is_wheel == "yes")
1286