3 # Copyright (C) 2010-2012 CEA/DEN
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.
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.
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
25 import src.debug as DBG
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.'))
32 def get_source_for_dev(config, product_info, source_dir, logger, pad):
33 '''The method called if the product is in development mode
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
46 # Call the function corresponding to get the sources with True checkout
47 retcode = get_product_sources(config,
54 logger.write("\n", 3, False)
55 # +2 because product name is followed by ': '
56 logger.write(" " * (pad+2), 3, False)
58 logger.write('dev: %s ... ' %
59 src.printcolors.printcInfo(product_info.source_dir), 3, False)
64 def get_source_from_git(config,
71 '''The method called if the product is to be get in git mode
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
82 :return: True if it succeed, else 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") :
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
103 repo_git = product_info.git_info.repo
106 # Display informations
107 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(repo_git)), 3,
109 logger.write(' ' * (pad + 50 - len(repo_git)), 3, False)
110 logger.write(' tag:%s' % src.printcolors.printcInfo(
111 product_info.git_info.tag),
114 logger.write(' %s. ' % ('.' * (10 - len(product_info.git_info.tag))), 3,
117 logger.write('\n', 5, False)
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")
126 if not is_dev and "sub_dir" in product_info.git_info:
127 sub_dir = product_info.git_info.sub_dir
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)
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)
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
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
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)
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)
164 # archive was found on ftp and stored in ret
165 product_info.archive_info.archive_name=ret
167 raise src.SatException(_("Archive not found in ARCHIVEPATH, nor on ARCHIVEFTP: '%s'") %
168 product_info.archive_info.archive_name)
170 logger.write('arc:%s ... ' %
171 src.printcolors.printcInfo(product_info.archive_info.archive_name),
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)
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)
190 def get_source_from_dir(product_info, source_dir, logger):
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)
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)
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,
209 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
212 logger.write('DIR: %s ... ' % src.printcolors.printcInfo(
213 product_info.dir_info.dir), 3)
216 retcode = src.Path(product_info.dir_info.dir).copy(source_dir)
220 def get_source_from_cvs(user,
227 '''The method called if the product is to be get in cvs mode
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
239 :return: True if it succeed, else False
242 # Get the protocol to use in the command
243 if "protocol" in product_info.cvs_info:
244 protocol = product_info.cvs_info.protocol
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)
254 cvs_line = "%s / %s" % (product_info.cvs_info.server,
255 product_info.cvs_info.product_base)
258 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
260 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(cvs_line)),
263 logger.write(' ' * (pad + 50 - len(cvs_line)), 3, False)
264 logger.write(' src:%s' %
265 src.printcolors.printcInfo(product_info.cvs_info.source),
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),
273 # at least one '.' is visible
274 logger.write(' %s. ' % ('.' * (10 - len(product_info.cvs_info.tag))),
278 logger.write('\n', 5, False)
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)
289 def get_source_from_svn(user,
295 '''The method called if the product is to be get in svn mode
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
306 :return: True if it succeed, else False
310 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
312 logger.write('%s:%s ... ' % (coflag,
313 src.printcolors.printcInfo(
314 product_info.svn_info.repo)),
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,
329 def get_product_sources(config,
336 '''Get the product sources.
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
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)
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,
365 if product_info.get_source == "git":
366 return get_source_from_git(config, product_info, source_dir, logger, pad,
369 if product_info.get_source == "archive":
370 return get_source_from_archive(config, product_info, source_dir, logger)
372 if product_info.get_source == "dir":
373 return get_source_from_dir(product_info, source_dir, logger)
375 if product_info.get_source == "cvs":
376 cvs_user = config.USER.cvs_user
377 return get_source_from_cvs(cvs_user,
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,
392 if product_info.get_source == "native":
394 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
397 msg = _('INFORMATION : Not doing anything because the product'
398 ' is of type "native".\n')
402 if product_info.get_source == "fixed":
404 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
407 msg = "FIXED : %s\n" % product_info.install_dir
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)
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)
422 def get_all_product_sources(config, products, logger):
423 '''Get all the product sources.
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)
432 # Initialize the variables that will count the fails and success
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
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
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)
450 source_dir = src.Path('')
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)
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
464 good_result = good_result + 1
465 # Do not get the sources and go to next product
468 # Call to the function that get the sources for one product
469 retcode = get_product_sources(config,
474 max_product_name_len,
478 if 'no_rpath' in product_info.keys():
479 if product_info.no_rpath:
480 hack_no_rpath(config, product_info, logger)
483 # Check that the sources are correctly get using the files to be tested
484 # in product information
486 check_OK, wrong_path = check_sources(product_info, logger)
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)
494 results[product_name] = retcode
496 # The case where it succeed
498 good_result = good_result + 1
500 # The case where it failed
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)
508 return good_result, results
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
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).
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)
535 '''method that is called when salomeTools is called with --help option.
537 :return: The text to display for the source command description.
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")
544 def run(args, runner, logger):
545 '''method that is called when salomeTools is called with source parameter.
547 DBG.write("source.run()", args)
549 (options, args) = parser.parse_args(args)
551 # check that the command has been called with an application
552 src.check_config_has_application( runner.cfg )
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)
561 # Get the products list with products informations regarding the options
562 products_infos = src.product.get_products_list(options, runner.cfg, logger)
564 # Call to the function that gets all the sources
565 good_result, results = get_all_product_sources(runner.cfg,
569 # Display the results (how much passed, how much failed, etc...)
570 status = src.OK_STATUS
573 logger.write("\n", 2, False)
574 if good_result == len(products_infos):
575 res_count = "%d / %d" % (good_result, good_result)
577 status = src.KO_STATUS
578 res_count = "%d / %d" % (good_result, len(products_infos))
580 for product in results:
581 if results[product] == 0 or results[product] is None:
582 details.append(product)
584 result = len(products_infos) - good_result
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)
592 logger.write(_("Following sources haven't been get:\n"), 2)
593 logger.write(" ".join(details), 2)
594 logger.write("\n", 2, False)