Salome HOME
Default archive name for products that are in archive mode
[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     # If prod_info is still None, it means that there is no product definition
121     # in the config. The user has to provide it.
122     if prod_info is None:
123         msg = _("No definition found for the product %s\n"
124             "Please create a %s.pyconf file." % (product_name, product_name))
125         raise src.SatException(msg)
126     
127     # Set the debug, dev and version keys
128     prod_info.debug = debug
129     prod_info.dev = dev
130     prod_info.version = version
131     
132     # Set the archive_info if the product is get in archive mode
133     if prod_info.get_source == "archive":
134         if not "archive_info" in prod_info:
135             prod_info.addMapping("archive_info",
136                                  src.pyconf.Mapping(prod_info),
137                                  "")
138         if "archive_name" not in prod_info.archive_info:
139             arch_name = os.path.join(config.SITE.prepare.archive_dir,
140                                      product_name + "-" + version + ".tar.gz")
141             prod_info.archive_info.archive_name = arch_name
142     
143     # Set the install_dir key
144     if "install_dir" not in prod_info:
145         # Set it to the default value (in application directory)
146         prod_info.install_dir = os.path.join(config.APPLICATION.workdir,
147                                             "INSTALL",
148                                             prod_info.name)
149     else:
150         if prod_info.install_dir == "base":
151             # Get the product base of the application
152             base_path = src.get_base_path(config) 
153             prod_info.install_dir = os.path.join(base_path,
154                                             prod_info.name + "-" + version)
155     
156     # If the product compiles with a script, check the script existence
157     # and if it is executable
158     if product_has_script(prod_info):
159         # Check the compil_script key existence
160         if "compil_script" not in prod_info:
161             msg = _("No compilation script found for the product %s\n"
162                 "Please provide a \"compil_script\" key in its definition." 
163                 % (product_name))
164             raise src.SatException(msg)
165         
166         # Get the path of the script
167         script = prod_info.compil_script
168         script_name = os.path.basename(script)
169         if script == script_name:
170             # Only a name is given. Search in the default directory
171             script_path = os.path.join(
172                     config.INTERNAL.compile.default_script_dir, script_name)
173             prod_info.compil_script = script_path
174
175         # Check script existence
176         if not os.path.exists(prod_info.compil_script):
177             raise src.SatException(_("Compilation script not found: %s") % 
178                                    prod_info.compil_script)
179         
180         # Check that the script is executable
181         if not os.access(prod_info.compil_script, os.X_OK):
182             raise src.SatException(
183                     _("Compilation script cannot be executed: %s") % 
184                     prod_info.compil_script)
185         
186     return prod_info
187
188 def get_products_infos(lproducts, config):
189     '''Get the specific configuration of a list of products
190     
191     :param lproducts List: The list of product names
192     :param config Config: The global configuration
193     :return: the list of tuples 
194              (product name, specific configuration of the product)
195     :rtype: [(str, Config)]
196     '''
197     products_infos = []
198     # Loop on product names
199     for prod in lproducts:       
200         # Get the specific configuration of the product
201         prod_info = get_product_config(config, prod)
202         if prod_info is not None:
203             products_infos.append((prod, prod_info))
204         else:
205             msg = _("The %s product has no definition "
206                     "in the configuration.") % prod
207             raise src.SatException(msg)
208     return products_infos
209
210 def get_product_dependencies(config, product_info):
211     '''Get recursively the list of products that are 
212        in the product_info dependencies
213     
214     :param config Config: The global configuration
215     :param product_info Config: The configuration specific to 
216                                the product
217     :return: the list of products in dependence
218     :rtype: list
219     '''
220     if "depend" not in product_info or product_info.depend == []:
221         return []
222     res = []
223     for prod in product_info.depend:
224         if prod not in res:
225             res.append(prod)
226         prod_info = get_product_config(config, prod)
227         dep_prod = get_product_dependencies(config, prod_info)
228         for prod_in_dep in dep_prod:
229             if prod_in_dep not in res:
230                 res.append(prod_in_dep)
231     return res
232
233 def check_installation(product_info):
234     '''Verify if a product is well installed. Checks install directory presence
235        and some additional files if it is defined in the config 
236     
237     :param product_info Config: The configuration specific to 
238                                the product
239     :return: True if it is well installed
240     :rtype: boolean
241     '''
242     install_dir = product_info.install_dir
243     if not os.path.exists(install_dir):
244         return False
245     if ("present_files" in product_info and 
246         "install" in product_info.present_files):
247         for file_relative_path in product_info.present_files.install:
248             file_path = os.path.join(install_dir, file_relative_path)
249             if not os.path.exists(file_path):
250                 return False
251     return True
252
253 def product_is_sample(product_info):
254     '''Know if a product has the sample type
255     
256     :param product_info Config: The configuration specific to 
257                                the product
258     :return: True if the product has the sample type, else False
259     :rtype: boolean
260     '''
261     if 'type' in product_info:
262         ptype = product_info.type
263         return ptype.lower() == 'sample'
264     else:
265         return False
266
267 def product_is_salome(product_info):
268     '''Know if a product is of type salome
269     
270     :param product_info Config: The configuration specific to 
271                                the product
272     :return: True if the product is salome, else False
273     :rtype: boolean
274     '''
275     if 'type' in product_info:
276         ptype = product_info.type
277         return ptype.lower() == 'salome'
278     else:
279         return False
280
281 def product_is_fixed(product_info):
282     '''Know if a product is fixed
283     
284     :param product_info Config: The configuration specific to 
285                                the product
286     :return: True if the product is fixed, else False
287     :rtype: boolean
288     '''
289     get_src = product_info.get_source
290     return get_src.lower() == 'fixed'
291
292 def product_is_native(product_info):
293     '''Know if a product is native
294     
295     :param product_info Config: The configuration specific to 
296                                the product
297     :return: True if the product is native, else False
298     :rtype: boolean
299     '''
300     get_src = product_info.get_source
301     return get_src.lower() == 'native'
302
303 def product_is_dev(product_info):
304     '''Know if a product is in dev mode
305     
306     :param product_info Config: The configuration specific to 
307                                the product
308     :return: True if the product is in dev mode, else False
309     :rtype: boolean
310     '''
311     dev = product_info.dev
312     return dev.lower() == 'yes'
313
314 def product_is_debug(product_info):
315     '''Know if a product is in debug mode
316     
317     :param product_info Config: The configuration specific to 
318                                the product
319     :return: True if the product is in debug mode, else False
320     :rtype: boolean
321     '''
322     debug = product_info.debug
323     return debug.lower() == 'yes'
324
325 def product_is_autotools(product_info):
326     '''Know if a product is compiled using the autotools
327     
328     :param product_info Config: The configuration specific to 
329                                the product
330     :return: True if the product is autotools, else False
331     :rtype: boolean
332     '''
333     build_src = product_info.build_source
334     return build_src.lower() == 'autotools'
335
336 def product_is_cmake(product_info):
337     '''Know if a product is compiled using the cmake
338     
339     :param product_info Config: The configuration specific to 
340                                the product
341     :return: True if the product is cmake, else False
342     :rtype: boolean
343     '''
344     build_src = product_info.build_source
345     return build_src.lower() == 'cmake'
346
347 def product_has_script(product_info):
348     '''Know if a product has a compilation script
349     
350     :param product_info Config: The configuration specific to 
351                                the product
352     :return: True if the product it has a compilation script, else False
353     :rtype: boolean
354     '''
355     if "build_source" not in product_info:
356         # Native case
357         return False
358     build_src = product_info.build_source
359     return build_src.lower() == 'script'