Salome HOME
Add make command first version
[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 os
23
24 import src
25
26 AVAILABLE_VCS = ['git', 'svn', 'cvs']
27
28 def get_product_config(config, product_name):
29     '''Get the specific configuration of a product from the global configuration
30     
31     :param config Config: The global configuration
32     :param product_name str: The name of the product
33     :return: the specific configuration of the product
34     :rtype: Config
35     '''
36     
37     # Get the version of the product from the application definition
38     version = config.APPLICATION.products[product_name]
39     # if no version, then take the default one defined in the application
40     if isinstance(version, bool): 
41         version = config.APPLICATION.tag      
42     
43     # Define debug and dev modes
44     # Get the tag if a dictionary is given in APPLICATION.products for the
45     # current product 
46     debug = 'no'
47     dev = 'no'
48     if isinstance(version, src.pyconf.Mapping):
49         dic_version = version
50         # Get the version/tag
51         if not 'tag' in dic_version:
52             version = config.APPLICATION.tag
53         else:
54             version = dic_version.tag
55         
56         # Get the debug if any
57         if 'debug' in dic_version:
58             debug = dic_version.debug
59         
60         # Get the dev if any
61         if 'dev' in dic_version:
62             dev = dic_version.dev
63     
64     vv = version
65     # substitute some character with _ in order to get the correct definition
66     # in config.PRODUCTS. This is done because the pyconf tool does not handle
67     # the . and - characters 
68     for c in ".-": vv = vv.replace(c, "_")
69     full_product_name = product_name + '_' + vv
70
71     prod_info = None
72     # If it exists, get the information of the product_version
73     if full_product_name in config.PRODUCTS:
74         # returns specific information for the given version
75         prod_info = config.PRODUCTS[full_product_name]    
76     # Get the standard informations
77     elif product_name in config.PRODUCTS:
78         # returns the generic information (given version not found)
79         prod_info = config.PRODUCTS[product_name]
80     
81     # merge opt_depend in depend
82     if prod_info is not None and 'opt_depend' in prod_info:
83         for depend in prod_info.opt_depend:
84             if depend in config.PRODUCTS:
85                 prod_info.depend.append(depend,'')
86     
87     # In case of a product get with a vcs, put the tag (equal to the version)
88     if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
89         
90         if prod_info.get_source == 'git':
91             prod_info.git_info.tag = version
92         
93         if prod_info.get_source == 'svn':
94             prod_info.svn_info.tag = version
95         
96         if prod_info.get_source == 'cvs':
97             prod_info.cvs_info.tag = version
98     
99     # In case of a fixed product, define the install_dir (equal to the version)
100     if prod_info is not None and prod_info.get_source=="fixed":
101         prod_info.install_dir = version
102     
103     # Check if the product is defined as native in the application
104     if prod_info is not None:
105         if version == "native":
106             prod_info.get_source = "native"
107         elif prod_info.get_source == "native":
108             msg = _("The product %(prod)s has version %(ver)s but is declared"
109                     " as native in its definition" %
110                 { 'prod': prod_info.name, 'ver': version})
111             raise src.SatException(msg)
112
113     # If there is no definition but the product is declared as native,
114     # construct a new definition containing only the get_source key
115     if prod_info is None and version == "native":
116         prod_info = src.pyconf.Config()
117         prod_info.name = product_name
118         prod_info.get_source = "native"
119     
120     # Set the debug, dev and version keys
121     if prod_info is not None:
122         prod_info.debug = debug
123         prod_info.dev = dev
124         prod_info.version = version
125      
126     # Set the install_dir key
127     if "install_dir" not in prod_info:
128         # Set it to the default value (in application directory)
129         prod_info.install_dir = os.path.join(config.APPLICATION.workdir,
130                                             "INSTALL",
131                                             prod_info.name)
132     else:
133         if prod_info.install_dir == "base":
134             # Get the product base of the application
135             base_path = src.get_base_path(config) 
136             prod_info.install_dir = os.path.join(base_path,
137                                             prod_info.name)
138        
139     return prod_info
140
141 def get_products_infos(lproducts, config):
142     '''Get the specific configuration of a list of products
143     
144     :param lproducts List: The list of product names
145     :param config Config: The global configuration
146     :return: the list of tuples 
147              (product name, specific configuration of the product)
148     :rtype: [(str, Config)]
149     '''
150     products_infos = []
151     # Loop on product names
152     for prod in lproducts:       
153         # Get the specific configuration of the product
154         prod_info = get_product_config(config, prod)
155         if prod_info is not None:
156             products_infos.append((prod, prod_info))
157         else:
158             msg = _("The %s product has no definition in the configuration.") % prod
159             raise src.SatException(msg)
160     return products_infos
161
162 def get_product_dependencies(config, product_info):
163     '''Get recursively the list of products that are 
164        in the product_info dependencies
165     
166     :param config Config: The global configuration
167     :param product_info Config: The configuration specific to 
168                                the product
169     :return: the list of products in dependence
170     :rtype: list
171     '''
172     if "depend" not in product_info or product_info.depend == []:
173         return []
174     res = []
175     for prod in product_info.depend:
176         if prod not in res:
177             res.append(prod)
178         prod_info = get_product_config(config, prod)
179         dep_prod = get_product_dependencies(config, prod_info)
180         for prod_in_dep in dep_prod:
181             if prod_in_dep not in res:
182                 res.append(prod_in_dep)
183     return res
184
185 def product_is_sample(product_info):
186     '''Know if a product has the sample type
187     
188     :param product_info Config: The configuration specific to 
189                                the product
190     :return: True if the product has the sample type, else False
191     :rtype: boolean
192     '''
193     if 'type' in product_info:
194         ptype = product_info.type
195         return ptype.lower() == 'sample'
196     else:
197         return False
198
199 def product_is_salome(product_info):
200     '''Know if a product is of type salome
201     
202     :param product_info Config: The configuration specific to 
203                                the product
204     :return: True if the product is salome, else False
205     :rtype: boolean
206     '''
207     if 'type' in product_info:
208         ptype = product_info.type
209         return ptype.lower() == 'salome'
210     else:
211         return False
212
213 def product_is_fixed(product_info):
214     '''Know if a product is fixed
215     
216     :param product_info Config: The configuration specific to 
217                                the product
218     :return: True if the product is fixed, else False
219     :rtype: boolean
220     '''
221     get_src = product_info.get_source
222     return get_src.lower() == 'fixed'
223
224 def product_is_native(product_info):
225     '''Know if a product is native
226     
227     :param product_info Config: The configuration specific to 
228                                the product
229     :return: True if the product is native, else False
230     :rtype: boolean
231     '''
232     get_src = product_info.get_source
233     return get_src.lower() == 'native'
234
235 def product_is_dev(product_info):
236     '''Know if a product is in dev mode
237     
238     :param product_info Config: The configuration specific to 
239                                the product
240     :return: True if the product is in dev mode, else False
241     :rtype: boolean
242     '''
243     dev = product_info.dev
244     return dev.lower() == 'yes'
245
246 def product_is_debug(product_info):
247     '''Know if a product is in debug mode
248     
249     :param product_info Config: The configuration specific to 
250                                the product
251     :return: True if the product is in debug mode, else False
252     :rtype: boolean
253     '''
254     debug = product_info.debug
255     return debug.lower() == 'yes'
256
257 def product_is_autotools(product_info):
258     '''Know if a product is compiled using the autotools
259     
260     :param product_info Config: The configuration specific to 
261                                the product
262     :return: True if the product is autotools, else False
263     :rtype: boolean
264     '''
265     build_src = product_info.build_source
266     return build_src.lower() == 'autotools'
267
268 def product_is_cmake(product_info):
269     '''Know if a product is compiled using the cmake
270     
271     :param product_info Config: The configuration specific to 
272                                the product
273     :return: True if the product is cmake, else False
274     :rtype: boolean
275     '''
276     build_src = product_info.build_source
277     return build_src.lower() == 'cmake'
278
279 def product_has_script(product_info):
280     '''Know if a product has a compilation script
281     
282     :param product_info Config: The configuration specific to 
283                                the product
284     :return: True if the product it has a compilation script, else False
285     :rtype: boolean
286     '''
287     build_src = product_info.build_source
288     return build_src.lower() == 'script'