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 # 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.'))
31 def get_source_for_dev(config, product_info, source_dir, logger, pad):
32 '''The method called if the product is in development mode
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
45 # Call the function corresponding to get the sources with True checkout
46 retcode = get_product_sources(config,
53 logger.write("\n", 3, False)
54 # +2 because product name is followed by ': '
55 logger.write(" " * (pad+2), 3, False)
57 logger.write('dev: %s ... ' %
58 src.printcolors.printcInfo(product_info.source_dir), 3, False)
63 def get_source_from_git(product_info,
69 '''The method called if the product is to be get in git mode
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
80 :return: True if it succeed, else False
86 # Get the repository address. (from repo_dev key if the product is
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
92 repo_git = product_info.git_info.repo
94 # Display informations
95 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(repo_git)), 3,
97 logger.write(' ' * (pad + 50 - len(repo_git)), 3, False)
98 logger.write(' tag:%s' % src.printcolors.printcInfo(
99 product_info.git_info.tag),
102 logger.write(' %s. ' % ('.' * (10 - len(product_info.git_info.tag))), 3,
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)
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
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
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)
128 logger.write('arc:%s ... ' %
129 src.printcolors.printcInfo(product_info.archive_info.archive_name),
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)
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)
148 def get_source_from_dir(product_info, source_dir, logger):
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)
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)
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,
167 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
170 logger.write('DIR: %s ... ' % src.printcolors.printcInfo(
171 product_info.dir_info.dir), 3)
174 retcode = src.Path(product_info.dir_info.dir).copy(source_dir)
178 def get_source_from_cvs(user,
185 '''The method called if the product is to be get in cvs mode
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
197 :return: True if it succeed, else False
200 # Get the protocol to use in the command
201 if "protocol" in product_info.cvs_info:
202 protocol = product_info.cvs_info.protocol
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)
212 cvs_line = "%s / %s" % (product_info.cvs_info.server,
213 product_info.cvs_info.product_base)
216 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
218 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(cvs_line)),
221 logger.write(' ' * (pad + 50 - len(cvs_line)), 3, False)
222 logger.write(' src:%s' %
223 src.printcolors.printcInfo(product_info.cvs_info.source),
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),
231 # at least one '.' is visible
232 logger.write(' %s. ' % ('.' * (10 - len(product_info.cvs_info.tag))),
236 logger.write('\n', 5, False)
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)
247 def get_source_from_svn(user,
253 '''The method called if the product is to be get in svn mode
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
264 :return: True if it succeed, else False
268 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
270 logger.write('%s:%s ... ' % (coflag,
271 src.printcolors.printcInfo(
272 product_info.svn_info.repo)),
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,
287 def get_product_sources(config,
294 '''Get the product sources.
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
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)
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,
323 if product_info.get_source == "git":
324 return get_source_from_git(product_info, source_dir, logger, pad,
327 if product_info.get_source == "archive":
328 return get_source_from_archive(product_info, source_dir, logger)
330 if product_info.get_source == "dir":
331 return get_source_from_dir(product_info, source_dir, logger)
333 if product_info.get_source == "cvs":
334 cvs_user = config.USER.cvs_user
335 return get_source_from_cvs(cvs_user,
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,
350 if product_info.get_source == "native":
352 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
355 msg = _('INFORMATION : Not doing anything because the product'
356 ' is of type "native".\n')
360 if product_info.get_source == "fixed":
362 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
365 msg = _('INFORMATION : Not doing anything because the product'
366 ' is of type "fixed".\n')
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)
377 def get_all_product_sources(config, products, logger):
378 '''Get all the product sources.
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)
387 # Initialize the variables that will count the fails and success
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
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
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)
404 source_dir = src.Path('')
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)
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),
418 msg = _("INFORMATION : Not doing anything because the source"
419 " directory already exists.\n")
421 good_result = good_result + 1
422 # Do not get the sources and go to next product
425 # Call to the function that get the sources for one product
426 retcode = get_product_sources(config,
431 max_product_name_len,
435 if 'no_rpath' in product_info.keys():
436 if product_info.no_rpath:
437 hack_no_rpath(config, product_info, logger)
440 # Check that the sources are correctly get using the files to be tested
441 # in product information
443 check_OK, wrong_path = check_sources(product_info, logger)
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)
451 results[product_name] = retcode
453 # The case where it succeed
455 good_result = good_result + 1
457 # The case where it failed
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)
465 return good_result, results
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
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).
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)
492 '''method that is called when salomeTools is called with --help option.
494 :return: The text to display for the source command description.
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")
501 def run(args, runner, logger):
502 '''method that is called when salomeTools is called with source parameter.
505 (options, args) = parser.parse_args(args)
507 # check that the command has been called with an application
508 src.check_config_has_application( runner.cfg )
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)
517 # Get the products list with products informations regarding the options
518 products_infos = prepare.get_products_list(options, runner.cfg, logger)
520 # Call to the function that gets all the sources
521 good_result, results = get_all_product_sources(runner.cfg,
525 # Display the results (how much passed, how much failed, etc...)
526 status = src.OK_STATUS
529 logger.write("\n", 2, False)
530 if good_result == len(products_infos):
531 res_count = "%d / %d" % (good_result, good_result)
533 status = src.KO_STATUS
534 res_count = "%d / %d" % (good_result, len(products_infos))
536 for product in results:
537 if results[product] == 0 or results[product] is None:
538 details.append(product)
540 result = len(products_infos) - good_result
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)
548 logger.write(_("Following sources haven't been get:\n"), 2)
549 logger.write(" ".join(details), 2)
550 logger.write("\n", 2, False)