Salome HOME
FSAt package --binaries: substitute also the generated components paths
[tools/sat.git] / commands / source.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
19 import os
20 import shutil
21
22 import src
23 import prepare
24
25 # Define all possible option for patch command :  sat patch <options>
26 parser = src.options.Options()
27 parser.add_option('p', 'products', 'list2', 'products',
28     _('Optional: products from which to get the sources. This option can be'
29     ' passed several time to get the sources of several products.'))
30
31 def get_source_for_dev(config, product_info, source_dir, logger, pad):
32     '''The method called if the product is in development mode
33     
34     :param config Config: The global configuration
35     :param product_info Config: The configuration specific to 
36                                the product to be prepared
37     :param source_dir Path: The Path instance corresponding to the 
38                             directory where to put the sources
39     :param logger Logger: The logger instance to use for the display and logging
40     :param pad int: The gap to apply for the terminal display
41     :return: True if it succeed, else False
42     :rtype: boolean
43     '''
44        
45     # Call the function corresponding to get the sources with True checkout
46     retcode = get_product_sources(config, 
47                                  product_info, 
48                                  True, 
49                                  source_dir,
50                                  logger, 
51                                  pad, 
52                                  checkout=True)
53     logger.write("\n", 3, False)
54     # +2 because product name is followed by ': '
55     logger.write(" " * (pad+2), 3, False) 
56     
57     logger.write('dev: %s ... ' % 
58                  src.printcolors.printcInfo(product_info.source_dir), 3, False)
59     logger.flush()
60     
61     return retcode
62
63 def get_source_from_git(product_info,
64                         source_dir,
65                         logger,
66                         pad,
67                         is_dev=False,
68                         environ = None):
69     '''The method called if the product is to be get in git mode
70     
71     :param product_info Config: The configuration specific to 
72                                the product to be prepared
73     :param source_dir Path: The Path instance corresponding to the 
74                             directory where to put the sources
75     :param logger Logger: The logger instance to use for the display and logging
76     :param pad int: The gap to apply for the terminal display
77     :param is_dev boolean: True if the product is in development mode
78     :param environ src.environment.Environ: The environment to source when
79                                                 extracting.
80     :return: True if it succeed, else False
81     :rtype: boolean
82     '''
83     # The str to display
84     coflag = 'git'
85
86     # Get the repository address. (from repo_dev key if the product is 
87     # in dev mode.
88     if is_dev and 'repo_dev' in product_info.git_info:
89         coflag = src.printcolors.printcHighlight(coflag.upper())
90         repo_git = product_info.git_info.repo_dev    
91     else:
92         repo_git = product_info.git_info.repo    
93         
94     # Display informations
95     logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(repo_git)), 3, 
96                  False)
97     logger.write(' ' * (pad + 50 - len(repo_git)), 3, False)
98     logger.write(' tag:%s' % src.printcolors.printcInfo(
99                                                     product_info.git_info.tag), 
100                  3,
101                  False)
102     logger.write(' %s. ' % ('.' * (10 - len(product_info.git_info.tag))), 3, 
103                  False)
104     logger.flush()
105     logger.write('\n', 5, False)
106     # Call the system function that do the extraction in git mode
107     retcode = src.system.git_extract(repo_git,
108                                  product_info.git_info.tag,
109                                  source_dir, logger, environ)
110     return retcode
111
112 def get_source_from_archive(product_info, source_dir, logger):
113     '''The method called if the product is to be get in archive mode
114     
115     :param product_info Config: The configuration specific to 
116                                the product to be prepared
117     :param source_dir Path: The Path instance corresponding to the 
118                             directory where to put the sources
119     :param logger Logger: The logger instance to use for the display and logging
120     :return: True if it succeed, else False
121     :rtype: boolean
122     '''
123     # check archive exists
124     if not os.path.exists(product_info.archive_info.archive_name):
125         raise src.SatException(_("Archive not found: '%s'") % 
126                                product_info.archive_info.archive_name)
127
128     logger.write('arc:%s ... ' % 
129                  src.printcolors.printcInfo(product_info.archive_info.archive_name),
130                  3, 
131                  False)
132     logger.flush()
133     # Call the system function that do the extraction in archive mode
134     retcode, NameExtractedDirectory = src.system.archive_extract(
135                                     product_info.archive_info.archive_name,
136                                     source_dir.dir(), logger)
137     
138     # Rename the source directory if 
139     # it does not match with product_info.source_dir
140     if (NameExtractedDirectory.replace('/', '') != 
141             os.path.basename(product_info.source_dir)):
142         shutil.move(os.path.join(os.path.dirname(product_info.source_dir), 
143                                  NameExtractedDirectory), 
144                     product_info.source_dir)
145     
146     return retcode
147
148 def get_source_from_dir(product_info, source_dir, logger):
149     
150     if "dir_info" not in product_info:
151         msg = _("Error: you must put a dir_info section"
152                 " in the file %s.pyconf" % product_info.name)
153         logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
154         return False
155
156     if "dir" not in product_info.dir_info:
157         msg = _("Error: you must put a dir in the dir_info section"
158                 " in the file %s.pyconf" % product_info.name)
159         logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
160         return False
161
162     # check that source exists
163     if not os.path.exists(product_info.dir_info.dir):
164         msg = _("Error: the dir %s defined in the file"
165                 " %s.pyconf does not exists" % (product_info.dir_info.dir,
166                                                 product_info.name))
167         logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
168         return False
169     
170     logger.write('DIR: %s ... ' % src.printcolors.printcInfo(
171                                            product_info.dir_info.dir), 3)
172     logger.flush()
173
174     retcode = src.Path(product_info.dir_info.dir).copy(source_dir)
175     
176     return retcode
177     
178 def get_source_from_cvs(user,
179                         product_info,
180                         source_dir,
181                         checkout,
182                         logger,
183                         pad,
184                         environ = None):
185     '''The method called if the product is to be get in cvs mode
186     
187     :param user str: The user to use in for the cvs command
188     :param product_info Config: The configuration specific to 
189                                the product to be prepared
190     :param source_dir Path: The Path instance corresponding to the 
191                             directory where to put the sources
192     :param checkout boolean: If True, get the source in checkout mode
193     :param logger Logger: The logger instance to use for the display and logging
194     :param pad int: The gap to apply for the terminal display
195     :param environ src.environment.Environ: The environment to source when
196                                                 extracting.
197     :return: True if it succeed, else False
198     :rtype: boolean
199     '''
200     # Get the protocol to use in the command
201     if "protocol" in product_info.cvs_info:
202         protocol = product_info.cvs_info.protocol
203     else:
204         protocol = "pserver"
205     
206     # Construct the line to display
207     if "protocol" in product_info.cvs_info:
208         cvs_line = "%s:%s@%s:%s" % \
209             (protocol, user, product_info.cvs_info.server, 
210              product_info.cvs_info.product_base)
211     else:
212         cvs_line = "%s / %s" % (product_info.cvs_info.server, 
213                                 product_info.cvs_info.product_base)
214
215     coflag = 'cvs'
216     if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
217
218     logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(cvs_line)), 
219                  3, 
220                  False)
221     logger.write(' ' * (pad + 50 - len(cvs_line)), 3, False)
222     logger.write(' src:%s' % 
223                  src.printcolors.printcInfo(product_info.cvs_info.source), 
224                  3, 
225                  False)
226     logger.write(' ' * (pad + 1 - len(product_info.cvs_info.source)), 3, False)
227     logger.write(' tag:%s' % 
228                     src.printcolors.printcInfo(product_info.cvs_info.tag), 
229                  3, 
230                  False)
231     # at least one '.' is visible
232     logger.write(' %s. ' % ('.' * (10 - len(product_info.cvs_info.tag))), 
233                  3, 
234                  False) 
235     logger.flush()
236     logger.write('\n', 5, False)
237
238     # Call the system function that do the extraction in cvs mode
239     retcode = src.system.cvs_extract(protocol, user,
240                                  product_info.cvs_info.server,
241                                  product_info.cvs_info.product_base,
242                                  product_info.cvs_info.tag,
243                                  product_info.cvs_info.source,
244                                  source_dir, logger, checkout, environ)
245     return retcode
246
247 def get_source_from_svn(user,
248                         product_info,
249                         source_dir,
250                         checkout,
251                         logger,
252                         environ = None):
253     '''The method called if the product is to be get in svn mode
254     
255     :param user str: The user to use in for the svn command
256     :param product_info Config: The configuration specific to 
257                                the product to be prepared
258     :param source_dir Path: The Path instance corresponding to the 
259                             directory where to put the sources
260     :param checkout boolean: If True, get the source in checkout mode
261     :param logger Logger: The logger instance to use for the display and logging
262     :param environ src.environment.Environ: The environment to source when
263                                                 extracting.
264     :return: True if it succeed, else False
265     :rtype: boolean
266     '''
267     coflag = 'svn'
268     if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
269
270     logger.write('%s:%s ... ' % (coflag, 
271                                  src.printcolors.printcInfo(
272                                             product_info.svn_info.repo)), 
273                  3, 
274                  False)
275     logger.flush()
276     logger.write('\n', 5, False)
277     # Call the system function that do the extraction in svn mode
278     retcode = src.system.svn_extract(user, 
279                                      product_info.svn_info.repo, 
280                                      product_info.svn_info.tag,
281                                      source_dir, 
282                                      logger, 
283                                      checkout,
284                                      environ)
285     return retcode
286
287 def get_product_sources(config, 
288                        product_info, 
289                        is_dev, 
290                        source_dir,
291                        logger, 
292                        pad, 
293                        checkout=False):
294     '''Get the product sources.
295     
296     :param config Config: The global configuration
297     :param product_info Config: The configuration specific to 
298                                the product to be prepared
299     :param is_dev boolean: True if the product is in development mode
300     :param source_dir Path: The Path instance corresponding to the 
301                             directory where to put the sources
302     :param logger Logger: The logger instance to use for the display and logging
303     :param pad int: The gap to apply for the terminal display
304     :param checkout boolean: If True, get the source in checkout mode
305     :return: True if it succeed, else False
306     :rtype: boolean
307     '''
308     
309     # Get the application environment
310     logger.write(_("Set the application environment\n"), 5)
311     env_appli = src.environment.SalomeEnviron(config,
312                                       src.environment.Environ(dict(os.environ)))
313     env_appli.set_application_env(logger)
314     
315     # Call the right function to get sources regarding the product settings
316     if not checkout and is_dev:
317         return get_source_for_dev(config, 
318                                    product_info, 
319                                    source_dir, 
320                                    logger, 
321                                    pad)
322
323     if product_info.get_source == "git":
324         return get_source_from_git(product_info, source_dir, logger, pad, 
325                                     is_dev,env_appli)
326
327     if product_info.get_source == "archive":
328         return get_source_from_archive(product_info, source_dir, logger)
329
330     if product_info.get_source == "dir":
331         return get_source_from_dir(product_info, source_dir, logger)
332     
333     if product_info.get_source == "cvs":
334         cvs_user = config.USER.cvs_user
335         return get_source_from_cvs(cvs_user, 
336                                     product_info, 
337                                     source_dir, 
338                                     checkout, 
339                                     logger,
340                                     pad,
341                                     env_appli)
342
343     if product_info.get_source == "svn":
344         svn_user = config.USER.svn_user
345         return get_source_from_svn(svn_user, product_info, source_dir, 
346                                     checkout,
347                                     logger,
348                                     env_appli)
349
350     if product_info.get_source == "native":
351         # skip
352         logger.write('%s  ' % src.printcolors.printc(src.OK_STATUS),
353                      3,
354                      False)
355         msg = _('INFORMATION : Not doing anything because the product'
356                 ' is of type "native".\n')
357         logger.write(msg, 3)
358         return True        
359
360     if product_info.get_source == "fixed":
361         # skip
362         logger.write('%s  ' % src.printcolors.printc(src.OK_STATUS),
363                      3,
364                      False)
365         msg = _('INFORMATION : Not doing anything because the product'
366                 ' is of type "fixed".\n')
367         logger.write(msg, 3)
368         return True  
369
370     # if the get_source is not in [git, archive, cvs, svn, fixed, native]
371     logger.write(_("Unknown get source method \"%(get)s\" for product %(product)s") % \
372         { 'get': product_info.get_source, 'product': product_info.name }, 3, False)
373     logger.write(" ... ", 3, False)
374     logger.flush()
375     return False
376
377 def get_all_product_sources(config, products, logger):
378     '''Get all the product sources.
379     
380     :param config Config: The global configuration
381     :param products List: The list of tuples (product name, product informations)
382     :param logger Logger: The logger instance to be used for the logging
383     :return: the tuple (number of success, dictionary product_name/success_fail)
384     :rtype: (int,dict)
385     '''
386
387     # Initialize the variables that will count the fails and success
388     results = dict()
389     good_result = 0
390
391     # Get the maximum name length in order to format the terminal display
392     max_product_name_len = 1
393     if len(products) > 0:
394         max_product_name_len = max(map(lambda l: len(l), products[0])) + 4
395     
396     # The loop on all the products from which to get the sources
397     for product_name, product_info in products:
398         # get product name, product informations and the directory where to put
399         # the sources
400         if (not (src.product.product_is_fixed(product_info) or 
401                  src.product.product_is_native(product_info))):
402             source_dir = src.Path(product_info.source_dir)
403         else:
404             source_dir = src.Path('')
405
406         # display and log
407         logger.write('%s: ' % src.printcolors.printcLabel(product_name), 3)
408         logger.write(' ' * (max_product_name_len - len(product_name)), 3, False)
409         logger.write("\n", 4, False)
410         
411         # Remove the existing source directory if 
412         # the product is not in development mode
413         is_dev = src.product.product_is_dev(product_info)
414         if source_dir.exists():
415             logger.write('%s  ' % src.printcolors.printc(src.OK_STATUS),
416                          3,
417                          False)
418             msg = _("INFORMATION : Not doing anything because the source"
419                     " directory already exists.\n")
420             logger.write(msg, 3)
421             good_result = good_result + 1
422             # Do not get the sources and go to next product
423             continue
424
425         # Call to the function that get the sources for one product
426         retcode = get_product_sources(config, 
427                                      product_info, 
428                                      is_dev, 
429                                      source_dir,
430                                      logger, 
431                                      max_product_name_len, 
432                                      checkout=False)
433         
434         '''
435         if 'no_rpath' in product_info.keys():
436             if product_info.no_rpath:
437                 hack_no_rpath(config, product_info, logger)
438         '''
439         
440         # Check that the sources are correctly get using the files to be tested
441         # in product information
442         if retcode:
443             check_OK, wrong_path = check_sources(product_info, logger)
444             if not check_OK:
445                 # Print the missing file path
446                 msg = _("The required file %s does not exists. " % wrong_path)
447                 logger.write(src.printcolors.printcError("\nERROR: ") + msg, 3)
448                 retcode = False
449
450         # show results
451         results[product_name] = retcode
452         if retcode:
453             # The case where it succeed
454             res = src.OK_STATUS
455             good_result = good_result + 1
456         else:
457             # The case where it failed
458             res = src.KO_STATUS
459         
460         # print the result
461         if not(src.product.product_is_fixed(product_info) or 
462                src.product.product_is_native(product_info)):
463             logger.write('%s\n' % src.printcolors.printc(res), 3, False)
464
465     return good_result, results
466
467 def check_sources(product_info, logger):
468     '''Check that the sources are correctly get, using the files to be tested
469        in product information
470     
471     :param product_info Config: The configuration specific to 
472                                 the product to be prepared
473     :return: True if the files exists (or no files to test is provided).
474     :rtype: boolean
475     '''
476     # Get the files to test if there is any
477     if ("present_files" in product_info and 
478         "source" in product_info.present_files):
479         l_files_to_be_tested = product_info.present_files.source
480         for file_path in l_files_to_be_tested:
481             # The path to test is the source directory 
482             # of the product joined the file path provided
483             path_to_test = os.path.join(product_info.source_dir, file_path)
484             logger.write(_("\nTesting existence of file: \n"), 5)
485             logger.write(path_to_test, 5)
486             if not os.path.exists(path_to_test):
487                 return False, path_to_test
488             logger.write(src.printcolors.printcSuccess(" OK\n"), 5)
489     return True, ""
490
491 def description():
492     '''method that is called when salomeTools is called with --help option.
493     
494     :return: The text to display for the source command description.
495     :rtype: str
496     '''
497     return _("The source command gets the sources of the application products "
498              "from cvs, git or an archive.\n\nexample:"
499              "\nsat source SALOME-master --products KERNEL,GUI")
500   
501 def run(args, runner, logger):
502     '''method that is called when salomeTools is called with source parameter.
503     '''
504     # Parse the options
505     (options, args) = parser.parse_args(args)
506     
507     # check that the command has been called with an application
508     src.check_config_has_application( runner.cfg )
509
510     # Print some informations
511     logger.write(_('Getting sources of the application %s\n') % 
512                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
513     src.printcolors.print_value(logger, 'workdir', 
514                                 runner.cfg.APPLICATION.workdir, 2)
515     logger.write("\n", 2, False)
516        
517     # Get the products list with products informations regarding the options
518     products_infos = prepare.get_products_list(options, runner.cfg, logger)
519     
520     # Call to the function that gets all the sources
521     good_result, results = get_all_product_sources(runner.cfg, 
522                                                   products_infos,
523                                                   logger)
524
525     # Display the results (how much passed, how much failed, etc...)
526     status = src.OK_STATUS
527     details = []
528
529     logger.write("\n", 2, False)
530     if good_result == len(products_infos):
531         res_count = "%d / %d" % (good_result, good_result)
532     else:
533         status = src.KO_STATUS
534         res_count = "%d / %d" % (good_result, len(products_infos))
535
536         for product in results:
537             if results[product] == 0 or results[product] is None:
538                 details.append(product)
539
540     result = len(products_infos) - good_result
541
542     # write results
543     logger.write(_("Getting sources of the application:"), 1)
544     logger.write(" " + src.printcolors.printc(status), 1, False)
545     logger.write(" (%s)\n" % res_count, 1, False)
546
547     if len(details) > 0:
548         logger.write(_("Following sources haven't been get:\n"), 2)
549         logger.write(" ".join(details), 2)
550         logger.write("\n", 2, False)
551
552     return result