Salome HOME
be able to prepend an env value from APPLICATION.environ
[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 '''In this file are implemented the methods 
19    relative to the product notion of salomeTools
20 '''
21
22 import os
23 import re
24
25 import src
26
27 AVAILABLE_VCS = ['git', 'svn', 'cvs']
28 config_expression = "^config-\d+$"
29 VERSION_DELIMITER = "_to_"
30
31 def get_product_config(config, product_name, with_install_dir=True):
32     '''Get the specific configuration of a product from the global configuration
33     
34     :param config Config: The global configuration
35     :param product_name str: The name of the product
36     :param with_install_dir boolean: If false, do not provide an install 
37                                      directory (at false only for internal use 
38                                      of the function get_install_dir)
39     :return: the specific configuration of the product
40     :rtype: Config
41     '''
42     
43     # Get the version of the product from the application definition
44     version = config.APPLICATION.products[product_name]
45     # if no version, then take the default one defined in the application
46     if isinstance(version, bool): 
47         version = config.APPLICATION.tag      
48     
49     # Define debug and dev modes
50     # Get the tag if a dictionary is given in APPLICATION.products for the
51     # current product 
52     debug = 'no'
53     dev = 'no'
54     base = 'maybe'
55     section = None
56     if isinstance(version, src.pyconf.Mapping):
57         dic_version = version
58         # Get the version/tag
59         if not 'tag' in dic_version:
60             version = config.APPLICATION.tag
61         else:
62             version = dic_version.tag
63         
64         # Get the debug if any
65         if 'debug' in dic_version:
66             debug = dic_version.debug
67         
68         # Get the dev if any
69         if 'dev' in dic_version:
70             dev = dic_version.dev
71         
72         # Get the base if any
73         if 'base' in dic_version:
74             base = dic_version.base
75
76         # Get the section if any
77         if 'section' in dic_version:
78             section = dic_version.section
79     
80     vv = version
81     # substitute some character with _ in order to get the correct definition
82     # in config.PRODUCTS. This is done because the pyconf tool does not handle
83     # the . and - characters 
84     for c in ".-": vv = vv.replace(c, "_")
85     
86     prod_info = None
87     if product_name in config.PRODUCTS:
88         # Search for the product description in the configuration
89         prod_info = get_product_section(config, product_name, vv, section)
90         
91         # merge opt_depend in depend
92         if prod_info is not None and 'opt_depend' in prod_info:
93             for depend in prod_info.opt_depend:
94                 if depend in config.APPLICATION:
95                     prod_info.depend.append(depend,'')
96         
97         # In case of a product get with a vcs, 
98         # put the tag (equal to the version)
99         if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
100             
101             if prod_info.get_source == 'git':
102                 prod_info.git_info.tag = version
103             
104             if prod_info.get_source == 'svn':
105                 prod_info.svn_info.tag = version
106             
107             if prod_info.get_source == 'cvs':
108                 prod_info.cvs_info.tag = version
109         
110         # In case of a fixed product, 
111         # define the install_dir (equal to the version)
112         if prod_info is not None and prod_info.get_source=="fixed":
113             prod_info.install_dir = version
114         
115         # Check if the product is defined as native in the application
116         if prod_info is not None:
117             if version == "native":
118                 prod_info.get_source = "native"
119             elif prod_info.get_source == "native":
120                 msg = _("The product %(prod)s has version %(ver)s but is "
121                         "declared as native in its definition" %
122                         { 'prod': prod_info.name, 'ver': version})
123                 raise src.SatException(msg)
124
125     # If there is no definition but the product is declared as native,
126     # construct a new definition containing only the get_source key
127     if prod_info is None and version == "native":
128         prod_info = src.pyconf.Config()
129         prod_info.name = product_name
130         prod_info.get_source = "native"
131     
132     # If prod_info is still None, it means that there is no product definition
133     # in the config. The user has to provide it.
134     if prod_info is None:
135         msg = _("No definition found for the product %s\n"
136             "Please create a %s.pyconf file." % (product_name, product_name))
137         raise src.SatException(msg)
138     
139     # Set the debug, dev and version keys
140     prod_info.debug = debug
141     prod_info.dev = dev
142     prod_info.version = version
143     
144     # Set the archive_info if the product is get in archive mode
145     if prod_info.get_source == "archive":
146         if not "archive_info" in prod_info:
147             prod_info.addMapping("archive_info",
148                                  src.pyconf.Mapping(prod_info),
149                                  "")
150         if "archive_name" not in prod_info.archive_info: 
151             arch_name = product_name + "-" + version + ".tar.gz"
152             arch_path = src.find_file_in_lpath(arch_name,
153                                                config.PATHS.ARCHIVEPATH)
154             if not arch_path:
155                 msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
156                             "\n" % {"arch_name" : arch_name,
157                                      "prod_name" : prod_info.name}) 
158                 raise src.SatException(msg)
159             prod_info.archive_info.archive_name = arch_path
160         else:
161             if (os.path.basename(prod_info.archive_info.archive_name) == 
162                                         prod_info.archive_info.archive_name):
163                 arch_name = prod_info.archive_info.archive_name
164                 arch_path = src.find_file_in_lpath(
165                                             arch_name,
166                                             config.PATHS.ARCHIVEPATH)
167                 if not arch_path:
168                     msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
169                                 "\n" % {"arch_name" : arch_name,
170                                          "prod_name" : prod_info.name}) 
171                     raise src.SatException(msg)
172                 prod_info.archive_info.archive_name = arch_path
173         
174     # If the product compiles with a script, check the script existence
175     # and if it is executable
176     if product_has_script(prod_info):
177         # Check the compil_script key existence
178         if "compil_script" not in prod_info:
179             msg = _("No compilation script found for the product %s\n"
180                 "Please provide a \"compil_script\" key in its definition." 
181                 % (product_name))
182             raise src.SatException(msg)
183         
184         # Get the path of the script
185         script = prod_info.compil_script
186         script_name = os.path.basename(script)
187         if script == script_name:
188             # Only a name is given. Search in the default directory
189             script_path = src.find_file_in_lpath(script_name,
190                                                  config.PATHS.PRODUCTPATH,
191                                                  "compil_scripts")
192             if not script_path:
193                 raise src.SatException(_("Compilation script not found: %s") % 
194                                    script_name)
195             prod_info.compil_script = script_path
196        
197         # Check that the script is executable
198         if not os.access(prod_info.compil_script, os.X_OK):
199             raise src.SatException(
200                     _("Compilation script cannot be executed: %s") % 
201                     prod_info.compil_script)
202     
203     # Get the full paths of all the patches
204     if product_has_patches(prod_info):
205         patches = []
206         for patch in prod_info.patches:
207             patch_path = patch
208             # If only a filename, then search for the patch in the PRODUCTPATH
209             if os.path.basename(patch_path) == patch_path:
210                 # Search in the PRODUCTPATH/patches
211                 patch_path = src.find_file_in_lpath(patch,
212                                                     config.PATHS.PRODUCTPATH,
213                                                     "patches")
214                 if not patch_path:
215                     msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
216                             "\n" % {"patch_name" : patch,
217                                      "prod_name" : prod_info.name}) 
218                     raise src.SatException(msg)
219             patches.append(patch_path)
220         prod_info.patches = patches
221
222     # Get the full paths of the environment scripts
223     if product_has_env_script(prod_info):
224         env_script_path = prod_info.environ.env_script
225         # If only a filename, then search for the environment script 
226         # in the PRODUCTPATH/env_scripts
227         if os.path.basename(env_script_path) == env_script_path:
228             # Search in the PRODUCTPATH/env_scripts
229             env_script_path = src.find_file_in_lpath(
230                                             prod_info.environ.env_script,
231                                             config.PATHS.PRODUCTPATH,
232                                             "env_scripts")
233             if not env_script_path:
234                 msg = _("Environment script %(env_name)s for %(prod_name)s not "
235                         "found.\n" % {"env_name" : env_script_path,
236                                        "prod_name" : prod_info.name}) 
237                 raise src.SatException(msg)
238
239         prod_info.environ.env_script = env_script_path
240     
241     if with_install_dir: 
242         # The variable with_install_dir is at false only for internal use 
243         # of the function get_install_dir
244         
245         # Set the install_dir key
246         prod_info.install_dir = get_install_dir(config, base, version, prod_info)
247                 
248     return prod_info
249
250 def get_product_section(config, product_name, version, section=None):
251     '''Get the product description from the configuration
252     
253     :param config Config: The global configuration
254     :param product_name str: The product name
255     :param version str: The version of the product
256     :param section str: The searched section (if not None, the section is 
257                         explicitly given
258     :return: The product description
259     :rtype: Config
260     '''
261
262     # if section is not None, try to get the corresponding section
263     if section:
264         if section not in config.PRODUCTS[product_name]:
265             return None
266         # returns specific information for the given version
267         prod_info = config.PRODUCTS[product_name][section]
268         prod_info.section = section
269         return prod_info
270
271     # If it exists, get the information of the product_version
272     if "version_" + version in config.PRODUCTS[product_name]:
273         # returns specific information for the given version
274         prod_info = config.PRODUCTS[product_name]["version_" + version]
275         prod_info.section = "version_" + version
276         return prod_info
277     
278     # Else, check if there is a description for multiple versions
279     l_section_name = config.PRODUCTS[product_name].keys()
280     l_section_ranges = [section_name for section_name in l_section_name 
281                         if VERSION_DELIMITER in section_name]
282     for section_range in l_section_ranges:
283         minimum, maximum = section_range.split(VERSION_DELIMITER)
284         if (src.only_numbers(version) >= src.only_numbers(minimum)
285                     and src.only_numbers(version) <= src.only_numbers(maximum)):
286             # returns specific information for the versions
287             prod_info = config.PRODUCTS[product_name][section_range]
288             prod_info.section = section_range
289             return prod_info
290     
291     # Else, get the standard informations
292     if "default" in config.PRODUCTS[product_name]:
293         # returns the generic information (given version not found)
294         prod_info = config.PRODUCTS[product_name].default
295         prod_info.section = "default"
296         return prod_info
297     
298     # if noting was found, return None
299     return None
300     
301 def get_install_dir(config, base, version, prod_info):
302     '''Compute the installation directory of a given product 
303     
304     :param config Config: The global configuration
305     :param base str: This corresponds to the value given by user in its 
306                      application.pyconf for the specific product. If "yes", the
307                     user wants the product to be in base. If "no", he wants the
308                     product to be in the application workdir
309     :param version str: The version of the product
310     :param product_info Config: The configuration specific to 
311                                the product
312     
313     :return: The path of the product installation
314     :rtype: str
315     '''
316     install_dir = ""
317     in_base = False
318     if (("install_dir" in prod_info and prod_info.install_dir == "base") 
319                                                             or base == "yes"):
320         in_base = True
321     if (base == "no" or ("no_base" in config.APPLICATION 
322                          and config.APPLICATION.no_base == "yes")):
323         in_base = False
324     
325     if in_base:
326         install_dir = get_base_install_dir(config, prod_info, version)
327     else:
328         if "install_dir" not in prod_info or prod_info.install_dir == "base":
329             # Set it to the default value (in application directory)
330             install_dir = os.path.join(config.APPLICATION.workdir,
331                                                 "INSTALL",
332                                                 prod_info.name)
333         else:
334             install_dir = prod_info.install_dir
335
336     return install_dir
337
338 def get_base_install_dir(config, prod_info, version):
339     '''Compute the installation directory of a product in base 
340     
341     :param config Config: The global configuration
342     :param product_info Config: The configuration specific to 
343                                the product
344     :param version str: The version of the product    
345     :return: The path of the product installation
346     :rtype: str
347     '''    
348     base_path = src.get_base_path(config) 
349     prod_dir = os.path.join(base_path, prod_info.name + "-" + version)
350     if not os.path.exists(prod_dir):
351         return os.path.join(prod_dir, "config-1")
352     
353     exists, install_dir = check_config_exists(config, prod_dir, prod_info)
354     if exists:
355         return install_dir
356     
357     # Find the first config-<i> directory that is available in the product
358     # directory
359     found = False 
360     label = 1
361     while not found:
362         install_dir = os.path.join(prod_dir, "config-%i" % label)
363         if os.path.exists(install_dir):
364             label+=1
365         else:
366             found = True
367             
368     return install_dir
369
370 def check_config_exists(config, prod_dir, prod_info):
371     '''Verify that the installation directory of a product in a base exists
372        Check all the config-<i> directory and verify the sat-config.pyconf file
373        that is in it 
374     
375     :param config Config: The global configuration
376     :param prod_dir str: The product installation directory path 
377                          (without config-<i>)
378     :param product_info Config: The configuration specific to 
379                                the product
380     :return: True or false is the installation is found or not 
381              and if it is found, the path of the found installation
382     :rtype: (boolean, str)
383     '''   
384     # check if the directories or files of the directory corresponds to the 
385     # directory installation of the product
386     l_dir_and_files = os.listdir(prod_dir)
387     for dir_or_file in l_dir_and_files:
388         oExpr = re.compile(config_expression)
389         if not(oExpr.search(dir_or_file)):
390             # not config-<i>, not interesting
391             continue
392         # check if there is the file sat-config.pyconf file in the installation
393         # directory    
394         config_file = os.path.join(prod_dir, dir_or_file, src.CONFIG_FILENAME)
395         if not os.path.exists(config_file):
396             continue
397         
398         # If there is no dependency, it is the right path
399         if len(prod_info.depend)==0:
400             return True, os.path.join(prod_dir, dir_or_file)
401         
402         # check if there is the config described in the file corresponds the 
403         # dependencies of the product
404         config_corresponds = True    
405         compile_cfg = src.pyconf.Config(config_file)
406         for prod_dep in prod_info.depend:
407             # if the dependency is not in the config, 
408             # the config does not correspond
409             if prod_dep not in compile_cfg:
410                 config_corresponds = False
411                 break
412             else:
413                 prod_dep_info = get_product_config(config, prod_dep, False)
414                 # If the version of the dependency does not correspond, 
415                 # the config does not correspond
416                 if prod_dep_info.version != compile_cfg[prod_dep]:
417                     config_corresponds = False
418                     break
419         if config_corresponds:
420             return True, os.path.join(prod_dir, dir_or_file)
421     
422     return False, None
423             
424             
425     
426 def get_products_infos(lproducts, config):
427     '''Get the specific configuration of a list of products
428     
429     :param lproducts List: The list of product names
430     :param config Config: The global configuration
431     :return: the list of tuples 
432              (product name, specific configuration of the product)
433     :rtype: [(str, Config)]
434     '''
435     products_infos = []
436     # Loop on product names
437     for prod in lproducts:       
438         # Get the specific configuration of the product
439         prod_info = get_product_config(config, prod)
440         if prod_info is not None:
441             products_infos.append((prod, prod_info))
442         else:
443             msg = _("The %s product has no definition "
444                     "in the configuration.") % prod
445             raise src.SatException(msg)
446     return products_infos
447
448 def get_product_dependencies(config, product_info):
449     '''Get recursively the list of products that are 
450        in the product_info dependencies
451     
452     :param config Config: The global configuration
453     :param product_info Config: The configuration specific to 
454                                the product
455     :return: the list of products in dependence
456     :rtype: list
457     '''
458     if "depend" not in product_info or product_info.depend == []:
459         return []
460     res = []
461     for prod in product_info.depend:
462         if prod == product_info.name:
463             continue
464         if prod not in res:
465             res.append(prod)
466         prod_info = get_product_config(config, prod)
467         dep_prod = get_product_dependencies(config, prod_info)
468         for prod_in_dep in dep_prod:
469             if prod_in_dep not in res:
470                 res.append(prod_in_dep)
471     return res
472
473 def check_installation(product_info):
474     '''Verify if a product is well installed. Checks install directory presence
475        and some additional files if it is defined in the config 
476     
477     :param product_info Config: The configuration specific to 
478                                the product
479     :return: True if it is well installed
480     :rtype: boolean
481     '''
482     if not product_compiles(product_info):
483         return True
484     install_dir = product_info.install_dir
485     if not os.path.exists(install_dir):
486         return False
487     if ("present_files" in product_info and 
488         "install" in product_info.present_files):
489         for file_relative_path in product_info.present_files.install:
490             file_path = os.path.join(install_dir, file_relative_path)
491             if not os.path.exists(file_path):
492                 return False
493     return True
494
495 def product_is_sample(product_info):
496     '''Know if a product has the sample type
497     
498     :param product_info Config: The configuration specific to 
499                                the product
500     :return: True if the product has the sample type, else False
501     :rtype: boolean
502     '''
503     if 'type' in product_info:
504         ptype = product_info.type
505         return ptype.lower() == 'sample'
506     else:
507         return False
508
509 def product_is_salome(product_info):
510     '''Know if a product is of type salome
511     
512     :param product_info Config: The configuration specific to 
513                                the product
514     :return: True if the product is salome, else False
515     :rtype: boolean
516     '''
517     if 'type' in product_info:
518         ptype = product_info.type
519         return ptype.lower() == 'salome'
520     else:
521         return False
522
523 def product_is_fixed(product_info):
524     '''Know if a product is fixed
525     
526     :param product_info Config: The configuration specific to 
527                                the product
528     :return: True if the product is fixed, else False
529     :rtype: boolean
530     '''
531     get_src = product_info.get_source
532     return get_src.lower() == 'fixed'
533
534 def product_is_native(product_info):
535     '''Know if a product is native
536     
537     :param product_info Config: The configuration specific to 
538                                the product
539     :return: True if the product is native, else False
540     :rtype: boolean
541     '''
542     get_src = product_info.get_source
543     return get_src.lower() == 'native'
544
545 def product_is_dev(product_info):
546     '''Know if a product is in dev mode
547     
548     :param product_info Config: The configuration specific to 
549                                the product
550     :return: True if the product is in dev mode, else False
551     :rtype: boolean
552     '''
553     dev = product_info.dev
554     return dev.lower() == 'yes'
555
556 def product_is_debug(product_info):
557     '''Know if a product is in debug mode
558     
559     :param product_info Config: The configuration specific to 
560                                the product
561     :return: True if the product is in debug mode, else False
562     :rtype: boolean
563     '''
564     debug = product_info.debug
565     return debug.lower() == 'yes'
566
567 def product_is_autotools(product_info):
568     '''Know if a product is compiled using the autotools
569     
570     :param product_info Config: The configuration specific to 
571                                the product
572     :return: True if the product is autotools, else False
573     :rtype: boolean
574     '''
575     build_src = product_info.build_source
576     return build_src.lower() == 'autotools'
577
578 def product_is_cmake(product_info):
579     '''Know if a product is compiled using the cmake
580     
581     :param product_info Config: The configuration specific to 
582                                the product
583     :return: True if the product is cmake, else False
584     :rtype: boolean
585     '''
586     build_src = product_info.build_source
587     return build_src.lower() == 'cmake'
588
589 def product_is_vcs(product_info):
590     '''Know if a product is download using git, svn or cvs (not archive)
591     
592     :param product_info Config: The configuration specific to 
593                                the product
594     :return: True if the product is vcs, else False
595     :rtype: boolean
596     '''
597     return product_info.get_source in AVAILABLE_VCS
598
599 def product_is_SALOME(product_info):
600     '''Know if a product is a SALOME module
601     
602     :param product_info Config: The configuration specific to 
603                                the product
604     :return: True if the product is a SALOME module, else False
605     :rtype: boolean
606     '''
607     return ("properties" in product_info and
608             "is_SALOME_module" in product_info.properties and
609             product_info.properties.is_SALOME_module == "yes")
610
611 def product_is_smesh_plugin(product_info):
612     '''Know if a product is a SMESH plugin
613     
614     :param product_info Config: The configuration specific to 
615                                the product
616     :return: True if the product is a SMESH plugin, else False
617     :rtype: boolean
618     '''
619     return ("properties" in product_info and
620             "smesh_plugin" in product_info.properties and
621             product_info.properties.smesh_plugin == "yes")
622
623 def product_is_cpp(product_info):
624     '''Know if a product is cpp
625     
626     :param product_info Config: The configuration specific to 
627                                the product
628     :return: True if the product is a cpp, else False
629     :rtype: boolean
630     '''
631     return ("properties" in product_info and
632             "cpp" in product_info.properties and
633             product_info.properties.cpp == "yes")
634
635 def product_compiles(product_info):
636     '''Know if a product compiles or not (some products do not have a 
637        compilation procedure)
638     
639     :param product_info Config: The configuration specific to 
640                                the product
641     :return: True if the product compiles, else False
642     :rtype: boolean
643     '''
644     return not("properties" in product_info and
645             "compilation" in product_info.properties and
646             product_info.properties.compilation == "no")
647
648 def product_has_script(product_info):
649     '''Know if a product has a compilation script
650     
651     :param product_info Config: The configuration specific to 
652                                the product
653     :return: True if the product it has a compilation script, else False
654     :rtype: boolean
655     '''
656     if "build_source" not in product_info:
657         # Native case
658         return False
659     build_src = product_info.build_source
660     return build_src.lower() == 'script'
661
662 def product_has_env_script(product_info):
663     '''Know if a product has an environment script
664     
665     :param product_info Config: The configuration specific to 
666                                the product
667     :return: True if the product it has an environment script, else False
668     :rtype: boolean
669     '''
670     return "environ" in product_info and "env_script" in product_info.environ
671
672 def product_has_patches(product_info):
673     '''Know if a product has one or more patches
674     
675     :param product_info Config: The configuration specific to 
676                                the product
677     :return: True if the product has one or more patches
678     :rtype: boolean
679     '''
680     return "patches" in product_info and len(product_info.patches) > 0
681
682 def product_has_logo(product_info):
683     '''Know if a product has a logo (YACSGEN generate)
684     
685     :param product_info Config: The configuration specific to 
686                                the product
687     :return: The path of the logo if the product has a logo, else False
688     :rtype: Str
689     '''
690     if ("properties" in product_info and
691             "logo" in product_info.properties):
692         return product_info.properties.logo
693     else:
694         return False
695
696 def product_has_salome_gui(product_info):
697     '''Know if a product has a SALOME gui
698     
699     :param product_info Config: The configuration specific to 
700                                the product
701     :return: True if the product has a SALOME gui, else False
702     :rtype: Boolean
703     '''
704     return ("properties" in product_info and
705             "has_salome_gui" in product_info.properties and
706             product_info.properties.has_salome_gui == "yes")
707
708 def product_is_mpi(product_info):
709     '''Know if a product has openmpi in its dependencies
710     
711     :param product_info Config: The configuration specific to 
712                                the product
713     :return: True if the product has openmpi inits dependencies
714     :rtype: boolean
715     '''
716     return "openmpi" in product_info.depend
717
718 def product_is_generated(product_info):
719     '''Know if a product is generated (YACSGEN)
720     
721     :param product_info Config: The configuration specific to 
722                                the product
723     :return: True if the product is generated
724     :rtype: boolean
725     '''
726     return ("properties" in product_info and
727             "generate" in product_info.properties and
728             product_info.properties.generate == "yes")
729
730 def get_product_components(product_info):
731     '''Get the component list to generate with the product
732     
733     :param product_info Config: The configuration specific to 
734                                the product
735     :return: The list of names of the components
736     :rtype: List
737     
738     '''
739     if not product_is_generated(product_info):
740         return []
741     
742     compo_list = []
743     if "component_name" in product_info:
744         compo_list = product_info.component_name
745     
746         if isinstance(compo_list, str):
747             compo_list = [ compo_list ]
748
749     return compo_list