Salome HOME
Improve the way to get the product definitions. Fix a bug of the log command
[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 classes and methods 
19    relative to the product notion of salomeTools
20 '''
21
22 import src
23
24 AVAILABLE_VCS = ['git', 'svn', 'cvs']
25
26 def get_product_config(config, product_name):
27     '''Get the specific configuration of a product from the global configuration
28     
29     :param config Config: The global configuration
30     :param product_name str: The name of the product
31     :return: the specific configuration of the product
32     :rtype: Config
33     '''
34     
35     # Get the version of the product from the application definition
36     version = config.APPLICATION.products[product_name]
37     # if no version, then take the default one defined in the application
38     if isinstance(version, bool): 
39         version = config.APPLICATION.tag      
40     
41     # Define debug and dev modes
42     # Get the tag if a dictionary is given in APPLICATION.products for the
43     # current product 
44     debug = 'no'
45     dev = 'no'
46     if isinstance(version, src.pyconf.Mapping):
47         dic_version = version
48         # Get the version/tag
49         if not 'tag' in dic_version:
50             version = config.APPLICATION.tag
51         else:
52             version = dic_version.tag
53         
54         # Get the debug if any
55         if 'debug' in dic_version:
56             debug = dic_version.debug
57         
58         # Get the dev if any
59         if 'dev' in dic_version:
60             dev = dic_version.dev
61     
62     vv = version
63     # substitute some character with _ in order to get the correct definition
64     # in config.PRODUCTS. This is done because the pyconf tool does not handle
65     # the . and - characters 
66     for c in ".-": vv = vv.replace(c, "_")
67     full_product_name = product_name + '_' + vv
68
69     prod_info = None
70     # If it exists, get the information of the product_version
71     if full_product_name in config.PRODUCTS:
72         # returns specific information for the given version
73         prod_info = config.PRODUCTS[full_product_name]    
74     # Get the standard informations
75     elif product_name in config.PRODUCTS:
76         # returns the generic information (given version not found)
77         prod_info = config.PRODUCTS[product_name]
78     
79     # merge opt_depend in depend
80     if prod_info is not None and 'opt_depend' in prod_info:
81         for depend in prod_info.opt_depend:
82             if depend in config.PRODUCTS:
83                 prod_info.depend.append(depend,'')
84     
85     # In case of a product get with a vcs, put the tag (equal to the version)
86     if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
87         
88         if prod_info.get_source == 'git':
89             prod_info.git_info.tag = version
90         
91         if prod_info.get_source == 'svn':
92             prod_info.svn_info.tag = version
93         
94         if prod_info.get_source == 'cvs':
95             prod_info.cvs_info.tag = version
96     
97     # In case of a fixed product, define the install_dir (equal to the version)
98     if prod_info is not None and prod_info.get_source=="fixed":
99         prod_info.install_dir = version
100     
101     # Check if the product is defined as native in the application
102     if prod_info is not None:
103         if version == "native":
104             prod_info.get_source = "native"
105         elif prod_info.get_source == "native":
106             msg = _("The product %(prod)s has version %(ver)s but is declared"
107                     " as native in its definition" %
108                 { 'prod': prod_info.name, 'ver': version})
109             raise src.SatException(msg)
110
111     # If there is no definition but the product is declared as native,
112     # construct a new defifnition containing only the get_source key
113     if prod_info is None and version == "native":
114         prod_info = src.pyconf.Config()
115         prod_info.name = product_name
116         prod_info.get_source = "native"
117     
118     # Set the debug and dev keys
119     if prod_info is not None:
120         prod_info.debug = debug
121         prod_info.dev = dev
122         
123     return prod_info
124
125 def get_products_infos(lproducts, config):
126     '''Get the specific configuration of a list of products
127     
128     :param lproducts List: The list of product names
129     :param config Config: The global configuration
130     :return: the list of tuples 
131              (product name, specific configuration of the product)
132     :rtype: [(str, Config)]
133     '''
134     products_infos = []
135     # Loop on product names
136     for prod in lproducts:       
137         # Get the specific configuration of the product
138         prod_info = get_product_config(config, prod)
139         if prod_info is not None:
140             products_infos.append((prod, prod_info))
141         else:
142             msg = _("The %s product has no definition in the configuration.") % prod
143             raise src.SatException(msg)
144     return products_infos
145
146
147 def product_is_sample(product_info):
148     '''Know if a product has the sample type
149     
150     :param product_info Config: The configuration specific to 
151                                the product
152     :return: True if the product has the sample type, else False
153     :rtype: boolean
154     '''
155     ptype = product_info.type
156     return ptype.lower() == 'sample'
157
158 def product_is_fixed(product_info):
159     '''Know if a product is fixed
160     
161     :param product_info Config: The configuration specific to 
162                                the product
163     :return: True if the product is fixed, else False
164     :rtype: boolean
165     '''
166     get_src = product_info.get_source
167     return get_src.lower() == 'fixed'
168
169 def product_is_native(product_info):
170     '''Know if a product is native
171     
172     :param product_info Config: The configuration specific to 
173                                the product
174     :return: True if the product is native, else False
175     :rtype: boolean
176     '''
177     get_src = product_info.get_source
178     return get_src.lower() == 'native'
179
180 def product_is_dev(product_info):
181     '''Know if a product is in dev mode
182     
183     :param product_info Config: The configuration specific to 
184                                the product
185     :return: True if the product is in dev mode, else False
186     :rtype: boolean
187     '''
188     dev = product_info.dev
189     return dev.lower() == 'yes'
190
191 def product_is_debug(product_info):
192     '''Know if a product is in debug mode
193     
194     :param product_info Config: The configuration specific to 
195                                the product
196     :return: True if the product is in debug mode, else False
197     :rtype: boolean
198     '''
199     debug = product_info.debug
200     return debug.lower() == 'yes'