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