Salome HOME
good raise in case of missing compilation script
[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     base = 'maybe'
49     if isinstance(version, src.pyconf.Mapping):
50         dic_version = version
51         # Get the version/tag
52         if not 'tag' in dic_version:
53             version = config.APPLICATION.tag
54         else:
55             version = dic_version.tag
56         
57         # Get the debug if any
58         if 'debug' in dic_version:
59             debug = dic_version.debug
60         
61         # Get the dev if any
62         if 'dev' in dic_version:
63             dev = dic_version.dev
64         
65         # Get the dev if any
66         if 'base' in dic_version:
67             base = dic_version.base
68     
69     vv = version
70     # substitute some character with _ in order to get the correct definition
71     # in config.PRODUCTS. This is done because the pyconf tool does not handle
72     # the . and - characters 
73     for c in ".-": vv = vv.replace(c, "_")
74     
75     prod_info = None
76     if product_name in config.PRODUCTS:
77         # If it exists, get the information of the product_version
78         if "version_" + vv in config.PRODUCTS[product_name]:
79             # returns specific information for the given version
80             prod_info = config.PRODUCTS[product_name]["version_" + vv]    
81         # Get the standard informations
82         elif "default" in config.PRODUCTS[product_name]:
83             # returns the generic information (given version not found)
84             prod_info = config.PRODUCTS[product_name].default
85         
86         # merge opt_depend in depend
87         if prod_info is not None and 'opt_depend' in prod_info:
88             for depend in prod_info.opt_depend:
89                 if depend in config.PRODUCTS:
90                     prod_info.depend.append(depend,'')
91         
92         # In case of a product get with a vcs, 
93         # put the tag (equal to the version)
94         if prod_info is not None and prod_info.get_source in AVAILABLE_VCS:
95             
96             if prod_info.get_source == 'git':
97                 prod_info.git_info.tag = version
98             
99             if prod_info.get_source == 'svn':
100                 prod_info.svn_info.tag = version
101             
102             if prod_info.get_source == 'cvs':
103                 prod_info.cvs_info.tag = version
104         
105         # In case of a fixed product, 
106         # define the install_dir (equal to the version)
107         if prod_info is not None and prod_info.get_source=="fixed":
108             prod_info.install_dir = version
109         
110         # Check if the product is defined as native in the application
111         if prod_info is not None:
112             if version == "native":
113                 prod_info.get_source = "native"
114             elif prod_info.get_source == "native":
115                 msg = _("The product %(prod)s has version %(ver)s but is "
116                         "declared as native in its definition" %
117                         { 'prod': prod_info.name, 'ver': version})
118                 raise src.SatException(msg)
119
120     # If there is no definition but the product is declared as native,
121     # construct a new definition containing only the get_source key
122     if prod_info is None and version == "native":
123         prod_info = src.pyconf.Config()
124         prod_info.name = product_name
125         prod_info.get_source = "native"
126     
127     # If prod_info is still None, it means that there is no product definition
128     # in the config. The user has to provide it.
129     if prod_info is None:
130         msg = _("No definition found for the product %s\n"
131             "Please create a %s.pyconf file." % (product_name, product_name))
132         raise src.SatException(msg)
133     
134     # Set the debug, dev and version keys
135     prod_info.debug = debug
136     prod_info.dev = dev
137     prod_info.version = version
138     
139     # Set the archive_info if the product is get in archive mode
140     if prod_info.get_source == "archive":
141         if not "archive_info" in prod_info:
142             prod_info.addMapping("archive_info",
143                                  src.pyconf.Mapping(prod_info),
144                                  "")
145         if "archive_name" not in prod_info.archive_info: 
146             arch_name = product_name + "-" + version + ".tar.gz"
147             arch_path = src.find_file_in_lpath(arch_name,
148                                                config.PATHS.ARCHIVEPATH)
149             if not arch_path:
150                 msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
151                             "\n" % {"arch_name" : arch_name,
152                                      "prod_name" : prod_info.name}) 
153                 raise src.SatException(msg)
154             prod_info.archive_info.archive_name = arch_path
155         else:
156             if (os.path.basename(prod_info.archive_info.archive_name) == 
157                                         prod_info.archive_info.archive_name):
158             
159                 arch_path = src.find_file_in_lpath(
160                                             prod_info.archive_info.archive_name,
161                                             config.PATHS.ARCHIVEPATH)
162                 if not arch_path:
163                     msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
164                                 "\n" % {"arch_name" : arch_name,
165                                          "prod_name" : prod_info.name}) 
166                     raise src.SatException(msg)
167                 prod_info.archive_info.archive_name = arch_path
168     
169     # Set the install_dir key
170     if "no_base" in config.APPLICATION and config.APPLICATION.no_base == "yes":
171         # Set it to the default value (in application directory)
172         prod_info.install_dir = os.path.join(config.APPLICATION.workdir,
173                                             "INSTALL",
174                                             prod_info.name)
175     else:
176         if base == "yes":
177             prod_info.install_dir = "base"
178         if "install_dir" not in prod_info:
179             # Set it to the default value (in application directory)
180             prod_info.install_dir = os.path.join(config.APPLICATION.workdir,
181                                                 "INSTALL",
182                                                 prod_info.name)
183         else:
184             if prod_info.install_dir == "base":
185                 # Get the product base of the application
186                 base_path = src.get_base_path(config) 
187                 prod_info.install_dir = os.path.join(base_path,
188                                                 prod_info.name + "-" + version)
189     
190     # If the product compiles with a script, check the script existence
191     # and if it is executable
192     if product_has_script(prod_info):
193         # Check the compil_script key existence
194         if "compil_script" not in prod_info:
195             msg = _("No compilation script found for the product %s\n"
196                 "Please provide a \"compil_script\" key in its definition." 
197                 % (product_name))
198             raise src.SatException(msg)
199         
200         # Get the path of the script
201         script = prod_info.compil_script
202         script_name = os.path.basename(script)
203         if script == script_name:
204             # Only a name is given. Search in the default directory
205             script_path = src.find_file_in_lpath(script_name,
206                                                  config.PATHS.PRODUCTPATH,
207                                                  "compil_scripts")
208             if not script_path:
209                 raise src.SatException(_("Compilation script not found: %s") % 
210                                    script_name)
211             prod_info.compil_script = script_path
212        
213         # Check that the script is executable
214         if not os.access(prod_info.compil_script, os.X_OK):
215             raise src.SatException(
216                     _("Compilation script cannot be executed: %s") % 
217                     prod_info.compil_script)
218     
219     # Get the full paths of all the patches
220     if "patches" in prod_info:
221         patches = []
222         for patch in prod_info.patches:
223             patch_path = patch
224             # If only a filename, then search for the patch in the PRODUCTPATH
225             if os.path.basename(patch_path) == patch_path:
226                 # Search in the PRODUCTPATH/patches
227                 patch_path = src.find_file_in_lpath(patch,
228                                                     config.PATHS.PRODUCTPATH,
229                                                     "patches")
230                 if not patch_path:
231                     msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
232                             "\n" % {"patch_name" : patch,
233                                      "prod_name" : prod_info.name}) 
234                     raise src.SatException(msg)
235             patches.append(patch_path)
236         prod_info.patches = patches
237
238     # Get the full paths of the environment scripts
239     if "environ" in prod_info and "env_script" in prod_info.environ:
240         env_script_path = prod_info.environ.env_script
241         # If only a filename, then search for the environment script 
242         # in the PRODUCTPATH/env_scripts
243         if os.path.basename(env_script_path) == env_script_path:
244             # Search in the PRODUCTPATH/env_scripts
245             env_script_path = src.find_file_in_lpath(
246                                             prod_info.environ.env_script,
247                                             config.PATHS.PRODUCTPATH,
248                                             "env_scripts")
249             if not env_script_path:
250                 msg = _("Environment script %(env_name)s for %(prod_name)s not "
251                         "found.\n" % {"env_name" : env_script_path,
252                                        "prod_name" : prod_info.name}) 
253                 raise src.SatException(msg)
254
255         prod_info.environ.env_script = env_script_path
256                     
257     return prod_info
258
259 def get_products_infos(lproducts, config):
260     '''Get the specific configuration of a list of products
261     
262     :param lproducts List: The list of product names
263     :param config Config: The global configuration
264     :return: the list of tuples 
265              (product name, specific configuration of the product)
266     :rtype: [(str, Config)]
267     '''
268     products_infos = []
269     # Loop on product names
270     for prod in lproducts:       
271         # Get the specific configuration of the product
272         prod_info = get_product_config(config, prod)
273         if prod_info is not None:
274             products_infos.append((prod, prod_info))
275         else:
276             msg = _("The %s product has no definition "
277                     "in the configuration.") % prod
278             raise src.SatException(msg)
279     return products_infos
280
281 def get_product_dependencies(config, product_info):
282     '''Get recursively the list of products that are 
283        in the product_info dependencies
284     
285     :param config Config: The global configuration
286     :param product_info Config: The configuration specific to 
287                                the product
288     :return: the list of products in dependence
289     :rtype: list
290     '''
291     if "depend" not in product_info or product_info.depend == []:
292         return []
293     res = []
294     for prod in product_info.depend:
295         if prod not in res:
296             res.append(prod)
297         prod_info = get_product_config(config, prod)
298         dep_prod = get_product_dependencies(config, prod_info)
299         for prod_in_dep in dep_prod:
300             if prod_in_dep not in res:
301                 res.append(prod_in_dep)
302     return res
303
304 def check_installation(product_info):
305     '''Verify if a product is well installed. Checks install directory presence
306        and some additional files if it is defined in the config 
307     
308     :param product_info Config: The configuration specific to 
309                                the product
310     :return: True if it is well installed
311     :rtype: boolean
312     '''
313     install_dir = product_info.install_dir
314     if not os.path.exists(install_dir):
315         return False
316     if ("present_files" in product_info and 
317         "install" in product_info.present_files):
318         for file_relative_path in product_info.present_files.install:
319             file_path = os.path.join(install_dir, file_relative_path)
320             if not os.path.exists(file_path):
321                 return False
322     return True
323
324 def product_is_sample(product_info):
325     '''Know if a product has the sample type
326     
327     :param product_info Config: The configuration specific to 
328                                the product
329     :return: True if the product has the sample type, else False
330     :rtype: boolean
331     '''
332     if 'type' in product_info:
333         ptype = product_info.type
334         return ptype.lower() == 'sample'
335     else:
336         return False
337
338 def product_is_salome(product_info):
339     '''Know if a product is of type salome
340     
341     :param product_info Config: The configuration specific to 
342                                the product
343     :return: True if the product is salome, else False
344     :rtype: boolean
345     '''
346     if 'type' in product_info:
347         ptype = product_info.type
348         return ptype.lower() == 'salome'
349     else:
350         return False
351
352 def product_is_fixed(product_info):
353     '''Know if a product is fixed
354     
355     :param product_info Config: The configuration specific to 
356                                the product
357     :return: True if the product is fixed, else False
358     :rtype: boolean
359     '''
360     get_src = product_info.get_source
361     return get_src.lower() == 'fixed'
362
363 def product_is_native(product_info):
364     '''Know if a product is native
365     
366     :param product_info Config: The configuration specific to 
367                                the product
368     :return: True if the product is native, else False
369     :rtype: boolean
370     '''
371     get_src = product_info.get_source
372     return get_src.lower() == 'native'
373
374 def product_is_dev(product_info):
375     '''Know if a product is in dev mode
376     
377     :param product_info Config: The configuration specific to 
378                                the product
379     :return: True if the product is in dev mode, else False
380     :rtype: boolean
381     '''
382     dev = product_info.dev
383     return dev.lower() == 'yes'
384
385 def product_is_debug(product_info):
386     '''Know if a product is in debug mode
387     
388     :param product_info Config: The configuration specific to 
389                                the product
390     :return: True if the product is in debug mode, else False
391     :rtype: boolean
392     '''
393     debug = product_info.debug
394     return debug.lower() == 'yes'
395
396 def product_is_autotools(product_info):
397     '''Know if a product is compiled using the autotools
398     
399     :param product_info Config: The configuration specific to 
400                                the product
401     :return: True if the product is autotools, else False
402     :rtype: boolean
403     '''
404     build_src = product_info.build_source
405     return build_src.lower() == 'autotools'
406
407 def product_is_cmake(product_info):
408     '''Know if a product is compiled using the cmake
409     
410     :param product_info Config: The configuration specific to 
411                                the product
412     :return: True if the product is cmake, else False
413     :rtype: boolean
414     '''
415     build_src = product_info.build_source
416     return build_src.lower() == 'cmake'
417
418 def product_has_script(product_info):
419     '''Know if a product has a compilation script
420     
421     :param product_info Config: The configuration specific to 
422                                the product
423     :return: True if the product it has a compilation script, else False
424     :rtype: boolean
425     '''
426     if "build_source" not in product_info:
427         # Native case
428         return False
429     build_src = product_info.build_source
430     return build_src.lower() == 'script'