]> SALOME platform Git repositories - tools/sat.git/blob - src/product.py
Salome HOME
sat jobs : add an option that reads csv files and fills the boards with expected...
[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: 
141             arch_name = product_name + "-" + version + ".tar.gz"
142             arch_path = src.find_file_in_lpath(arch_name,
143                                                config.PATHS.ARCHIVEPATH)
144             if not arch_path:
145                 msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
146                             "\n" % {"arch_name" : arch_name,
147                                      "prod_name" : prod_info.name}) 
148                 raise src.SatException(msg)
149             prod_info.archive_info.archive_name = arch_path
150         else:
151             if (os.path.basename(prod_info.archive_info.archive_name) == 
152                                         prod_info.archive_info.archive_name):
153             
154                 arch_path = src.find_file_in_lpath(
155                                             prod_info.archive_info.archive_name,
156                                             config.PATHS.ARCHIVEPATH)
157                 if not arch_path:
158                     msg = _("Archive %(arch_name)s for %(prod_name)s not found:"
159                                 "\n" % {"arch_name" : arch_name,
160                                          "prod_name" : prod_info.name}) 
161                     raise src.SatException(msg)
162                 prod_info.archive_info.archive_name = arch_path
163     
164     # Set the install_dir key
165     if "install_dir" not in prod_info:
166         # Set it to the default value (in application directory)
167         prod_info.install_dir = os.path.join(config.APPLICATION.workdir,
168                                             "INSTALL",
169                                             prod_info.name)
170     else:
171         if prod_info.install_dir == "base":
172             # Get the product base of the application
173             base_path = src.get_base_path(config) 
174             prod_info.install_dir = os.path.join(base_path,
175                                             prod_info.name + "-" + version)
176     
177     # If the product compiles with a script, check the script existence
178     # and if it is executable
179     if product_has_script(prod_info):
180         # Check the compil_script key existence
181         if "compil_script" not in prod_info:
182             msg = _("No compilation script found for the product %s\n"
183                 "Please provide a \"compil_script\" key in its definition." 
184                 % (product_name))
185             raise src.SatException(msg)
186         
187         # Get the path of the script
188         script = prod_info.compil_script
189         script_name = os.path.basename(script)
190         if script == script_name:
191             # Only a name is given. Search in the default directory
192             script_path = src.find_file_in_lpath(script_name,
193                                                  config.PATHS.PRODUCTPATH,
194                                                  "compil_scripts")
195             prod_info.compil_script = script_path
196
197         # Check script existence
198         if not os.path.exists(prod_info.compil_script):
199             raise src.SatException(_("Compilation script not found: %s") % 
200                                    prod_info.compil_script)
201         
202         # Check that the script is executable
203         if not os.access(prod_info.compil_script, os.X_OK):
204             raise src.SatException(
205                     _("Compilation script cannot be executed: %s") % 
206                     prod_info.compil_script)
207     
208     # Get the full paths of all the patches
209     if "patches" in prod_info:
210         patches = []
211         for patch in prod_info.patches:
212             patch_path = patch
213             # If only a filename, then search for the patch in the PRODUCTPATH
214             if os.path.basename(patch_path) == patch_path:
215                 # Search in the PRODUCTPATH/patches
216                 patch_path = src.find_file_in_lpath(patch,
217                                                     config.PATHS.PRODUCTPATH,
218                                                     "patches")
219                 if not patch_path:
220                     msg = _("Patch %(patch_name)s for %(prod_name)s not found:"
221                             "\n" % {"patch_name" : patch,
222                                      "prod_name" : prod_info.name}) 
223                     raise src.SatException(msg)
224             patches.append(patch_path)
225         prod_info.patches = patches
226
227     # Get the full paths of the environment scripts
228     if "environ" in prod_info and "env_script" in prod_info.environ:
229         env_script_path = prod_info.environ.env_script
230         # If only a filename, then search for the environment script 
231         # in the PRODUCTPATH/env_scripts
232         if os.path.basename(env_script_path) == env_script_path:
233             # Search in the PRODUCTPATH/env_scripts
234             env_script_path = src.find_file_in_lpath(
235                                             prod_info.environ.env_script,
236                                             config.PATHS.PRODUCTPATH,
237                                             "env_scripts")
238             if not env_script_path:
239                 msg = _("Environment script %(env_name)s for %(prod_name)s not "
240                         "found.\n" % {"env_name" : env_script_path,
241                                        "prod_name" : prod_info.name}) 
242                 raise src.SatException(msg)
243
244         prod_info.environ.env_script = env_script_path
245                     
246     return prod_info
247
248 def get_products_infos(lproducts, config):
249     '''Get the specific configuration of a list of products
250     
251     :param lproducts List: The list of product names
252     :param config Config: The global configuration
253     :return: the list of tuples 
254              (product name, specific configuration of the product)
255     :rtype: [(str, Config)]
256     '''
257     products_infos = []
258     # Loop on product names
259     for prod in lproducts:       
260         # Get the specific configuration of the product
261         prod_info = get_product_config(config, prod)
262         if prod_info is not None:
263             products_infos.append((prod, prod_info))
264         else:
265             msg = _("The %s product has no definition "
266                     "in the configuration.") % prod
267             raise src.SatException(msg)
268     return products_infos
269
270 def get_product_dependencies(config, product_info):
271     '''Get recursively the list of products that are 
272        in the product_info dependencies
273     
274     :param config Config: The global configuration
275     :param product_info Config: The configuration specific to 
276                                the product
277     :return: the list of products in dependence
278     :rtype: list
279     '''
280     if "depend" not in product_info or product_info.depend == []:
281         return []
282     res = []
283     for prod in product_info.depend:
284         if prod not in res:
285             res.append(prod)
286         prod_info = get_product_config(config, prod)
287         dep_prod = get_product_dependencies(config, prod_info)
288         for prod_in_dep in dep_prod:
289             if prod_in_dep not in res:
290                 res.append(prod_in_dep)
291     return res
292
293 def check_installation(product_info):
294     '''Verify if a product is well installed. Checks install directory presence
295        and some additional files if it is defined in the config 
296     
297     :param product_info Config: The configuration specific to 
298                                the product
299     :return: True if it is well installed
300     :rtype: boolean
301     '''
302     install_dir = product_info.install_dir
303     if not os.path.exists(install_dir):
304         return False
305     if ("present_files" in product_info and 
306         "install" in product_info.present_files):
307         for file_relative_path in product_info.present_files.install:
308             file_path = os.path.join(install_dir, file_relative_path)
309             if not os.path.exists(file_path):
310                 return False
311     return True
312
313 def product_is_sample(product_info):
314     '''Know if a product has the sample type
315     
316     :param product_info Config: The configuration specific to 
317                                the product
318     :return: True if the product has the sample type, else False
319     :rtype: boolean
320     '''
321     if 'type' in product_info:
322         ptype = product_info.type
323         return ptype.lower() == 'sample'
324     else:
325         return False
326
327 def product_is_salome(product_info):
328     '''Know if a product is of type salome
329     
330     :param product_info Config: The configuration specific to 
331                                the product
332     :return: True if the product is salome, else False
333     :rtype: boolean
334     '''
335     if 'type' in product_info:
336         ptype = product_info.type
337         return ptype.lower() == 'salome'
338     else:
339         return False
340
341 def product_is_fixed(product_info):
342     '''Know if a product is fixed
343     
344     :param product_info Config: The configuration specific to 
345                                the product
346     :return: True if the product is fixed, else False
347     :rtype: boolean
348     '''
349     get_src = product_info.get_source
350     return get_src.lower() == 'fixed'
351
352 def product_is_native(product_info):
353     '''Know if a product is native
354     
355     :param product_info Config: The configuration specific to 
356                                the product
357     :return: True if the product is native, else False
358     :rtype: boolean
359     '''
360     get_src = product_info.get_source
361     return get_src.lower() == 'native'
362
363 def product_is_dev(product_info):
364     '''Know if a product is in dev mode
365     
366     :param product_info Config: The configuration specific to 
367                                the product
368     :return: True if the product is in dev mode, else False
369     :rtype: boolean
370     '''
371     dev = product_info.dev
372     return dev.lower() == 'yes'
373
374 def product_is_debug(product_info):
375     '''Know if a product is in debug mode
376     
377     :param product_info Config: The configuration specific to 
378                                the product
379     :return: True if the product is in debug mode, else False
380     :rtype: boolean
381     '''
382     debug = product_info.debug
383     return debug.lower() == 'yes'
384
385 def product_is_autotools(product_info):
386     '''Know if a product is compiled using the autotools
387     
388     :param product_info Config: The configuration specific to 
389                                the product
390     :return: True if the product is autotools, else False
391     :rtype: boolean
392     '''
393     build_src = product_info.build_source
394     return build_src.lower() == 'autotools'
395
396 def product_is_cmake(product_info):
397     '''Know if a product is compiled using the cmake
398     
399     :param product_info Config: The configuration specific to 
400                                the product
401     :return: True if the product is cmake, else False
402     :rtype: boolean
403     '''
404     build_src = product_info.build_source
405     return build_src.lower() == 'cmake'
406
407 def product_has_script(product_info):
408     '''Know if a product has a compilation script
409     
410     :param product_info Config: The configuration specific to 
411                                the product
412     :return: True if the product it has a compilation script, else False
413     :rtype: boolean
414     '''
415     if "build_source" not in product_info:
416         # Native case
417         return False
418     build_src = product_info.build_source
419     return build_src.lower() == 'script'