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