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
24 import src.debug as DBG
26 # Define all possible option for patch command : sat patch <options>
27 parser = src.options.Options()
28 parser.add_option('p', 'products', 'list2', 'products',
29 _('Optional: products from which to get the sources. This option can be'
30 ' passed several time to get the sources of several products.'))
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(product_info,
70 '''The method called if the product is to be get in git mode
72 :param product_info Config: The configuration specific to
73 the product to be prepared
74 :param source_dir Path: The Path instance corresponding to the
75 directory where to put the sources
76 :param logger Logger: The logger instance to use for the display and logging
77 :param pad int: The gap to apply for the terminal display
78 :param is_dev boolean: True if the product is in development mode
79 :param environ src.environment.Environ: The environment to source when
81 :return: True if it succeed, else False
87 # Get the repository address. (from repo_dev key if the product is
89 if is_dev and 'repo_dev' in product_info.git_info:
90 coflag = src.printcolors.printcHighlight(coflag.upper())
91 repo_git = product_info.git_info.repo_dev
93 repo_git = product_info.git_info.repo
95 # Display informations
96 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(repo_git)), 3,
98 logger.write(' ' * (pad + 50 - len(repo_git)), 3, False)
99 logger.write(' tag:%s' % src.printcolors.printcInfo(
100 product_info.git_info.tag),
103 logger.write(' %s. ' % ('.' * (10 - len(product_info.git_info.tag))), 3,
106 logger.write('\n', 5, False)
107 # Call the system function that do the extraction in git mode
108 retcode = src.system.git_extract(repo_git,
109 product_info.git_info.tag,
110 source_dir, logger, environ)
113 def get_source_from_archive(product_info, source_dir, logger):
114 '''The method called if the product is to be get in archive mode
116 :param product_info Config: The configuration specific to
117 the product to be prepared
118 :param source_dir Path: The Path instance corresponding to the
119 directory where to put the sources
120 :param logger Logger: The logger instance to use for the display and logging
121 :return: True if it succeed, else False
124 # check archive exists
125 if not os.path.exists(product_info.archive_info.archive_name):
126 raise src.SatException(_("Archive not found in config.PATHS.ARCHIVEPATH: '%s'") %
127 product_info.archive_info.archive_name)
129 logger.write('arc:%s ... ' %
130 src.printcolors.printcInfo(product_info.archive_info.archive_name),
134 # Call the system function that do the extraction in archive mode
135 retcode, NameExtractedDirectory = src.system.archive_extract(
136 product_info.archive_info.archive_name,
137 source_dir.dir(), logger)
139 # Rename the source directory if
140 # it does not match with product_info.source_dir
141 if (NameExtractedDirectory.replace('/', '') !=
142 os.path.basename(product_info.source_dir)):
143 shutil.move(os.path.join(os.path.dirname(product_info.source_dir),
144 NameExtractedDirectory),
145 product_info.source_dir)
149 def get_source_from_dir(product_info, source_dir, logger):
151 if "dir_info" not in product_info:
152 msg = _("Error: you must put a dir_info section"
153 " in the file %s.pyconf" % product_info.name)
154 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
157 if "dir" not in product_info.dir_info:
158 msg = _("Error: you must put a dir in the dir_info section"
159 " in the file %s.pyconf" % product_info.name)
160 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
163 # check that source exists
164 if not os.path.exists(product_info.dir_info.dir):
165 msg = _("Error: the dir %s defined in the file"
166 " %s.pyconf does not exists" % (product_info.dir_info.dir,
168 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
171 logger.write('DIR: %s ... ' % src.printcolors.printcInfo(
172 product_info.dir_info.dir), 3)
175 retcode = src.Path(product_info.dir_info.dir).copy(source_dir)
179 def get_source_from_cvs(user,
186 '''The method called if the product is to be get in cvs mode
188 :param user str: The user to use in for the cvs command
189 :param product_info Config: The configuration specific to
190 the product to be prepared
191 :param source_dir Path: The Path instance corresponding to the
192 directory where to put the sources
193 :param checkout boolean: If True, get the source in checkout mode
194 :param logger Logger: The logger instance to use for the display and logging
195 :param pad int: The gap to apply for the terminal display
196 :param environ src.environment.Environ: The environment to source when
198 :return: True if it succeed, else False
201 # Get the protocol to use in the command
202 if "protocol" in product_info.cvs_info:
203 protocol = product_info.cvs_info.protocol
207 # Construct the line to display
208 if "protocol" in product_info.cvs_info:
209 cvs_line = "%s:%s@%s:%s" % \
210 (protocol, user, product_info.cvs_info.server,
211 product_info.cvs_info.product_base)
213 cvs_line = "%s / %s" % (product_info.cvs_info.server,
214 product_info.cvs_info.product_base)
217 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
219 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(cvs_line)),
222 logger.write(' ' * (pad + 50 - len(cvs_line)), 3, False)
223 logger.write(' src:%s' %
224 src.printcolors.printcInfo(product_info.cvs_info.source),
227 logger.write(' ' * (pad + 1 - len(product_info.cvs_info.source)), 3, False)
228 logger.write(' tag:%s' %
229 src.printcolors.printcInfo(product_info.cvs_info.tag),
232 # at least one '.' is visible
233 logger.write(' %s. ' % ('.' * (10 - len(product_info.cvs_info.tag))),
237 logger.write('\n', 5, False)
239 # Call the system function that do the extraction in cvs mode
240 retcode = src.system.cvs_extract(protocol, user,
241 product_info.cvs_info.server,
242 product_info.cvs_info.product_base,
243 product_info.cvs_info.tag,
244 product_info.cvs_info.source,
245 source_dir, logger, checkout, environ)
248 def get_source_from_svn(user,
254 '''The method called if the product is to be get in svn mode
256 :param user str: The user to use in for the svn command
257 :param product_info Config: The configuration specific to
258 the product to be prepared
259 :param source_dir Path: The Path instance corresponding to the
260 directory where to put the sources
261 :param checkout boolean: If True, get the source in checkout mode
262 :param logger Logger: The logger instance to use for the display and logging
263 :param environ src.environment.Environ: The environment to source when
265 :return: True if it succeed, else False
269 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
271 logger.write('%s:%s ... ' % (coflag,
272 src.printcolors.printcInfo(
273 product_info.svn_info.repo)),
277 logger.write('\n', 5, False)
278 # Call the system function that do the extraction in svn mode
279 retcode = src.system.svn_extract(user,
280 product_info.svn_info.repo,
281 product_info.svn_info.tag,
288 def get_product_sources(config,
295 '''Get the product sources.
297 :param config Config: The global configuration
298 :param product_info Config: The configuration specific to
299 the product to be prepared
300 :param is_dev boolean: True if the product is in development mode
301 :param source_dir Path: The Path instance corresponding to the
302 directory where to put the sources
303 :param logger Logger: The logger instance to use for the display and logging
304 :param pad int: The gap to apply for the terminal display
305 :param checkout boolean: If True, get the source in checkout mode
306 :return: True if it succeed, else False
310 # Get the application environment
311 logger.write(_("Set the application environment\n"), 5)
312 env_appli = src.environment.SalomeEnviron(config,
313 src.environment.Environ(dict(os.environ)))
314 env_appli.set_application_env(logger)
316 # Call the right function to get sources regarding the product settings
317 if not checkout and is_dev:
318 return get_source_for_dev(config,
324 if product_info.get_source == "git":
325 return get_source_from_git(product_info, source_dir, logger, pad,
328 if product_info.get_source == "archive":
329 return get_source_from_archive(product_info, source_dir, logger)
331 if product_info.get_source == "dir":
332 return get_source_from_dir(product_info, source_dir, logger)
334 if product_info.get_source == "cvs":
335 cvs_user = config.USER.cvs_user
336 return get_source_from_cvs(cvs_user,
344 if product_info.get_source == "svn":
345 svn_user = config.USER.svn_user
346 return get_source_from_svn(svn_user, product_info, source_dir,
351 if product_info.get_source == "native":
353 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
356 msg = _('INFORMATION : Not doing anything because the product'
357 ' is of type "native".\n')
361 if product_info.get_source == "fixed":
363 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
366 msg = _('INFORMATION : Not doing anything because the product'
367 ' is of type "fixed".\n')
371 # if the get_source is not in [git, archive, cvs, svn, fixed, native]
372 logger.write(_("Unknown get source method \"%(get)s\" for product %(product)s") % \
373 { 'get': product_info.get_source, 'product': product_info.name }, 3, False)
374 logger.write(" ... ", 3, False)
378 def get_all_product_sources(config, products, logger):
379 '''Get all the product sources.
381 :param config Config: The global configuration
382 :param products List: The list of tuples (product name, product informations)
383 :param logger Logger: The logger instance to be used for the logging
384 :return: the tuple (number of success, dictionary product_name/success_fail)
388 # Initialize the variables that will count the fails and success
392 # Get the maximum name length in order to format the terminal display
393 max_product_name_len = 1
394 if len(products) > 0:
395 max_product_name_len = max(map(lambda l: len(l), products[0])) + 4
397 # The loop on all the products from which to get the sources
398 # DBG.write("source.get_all_product_sources config id", id(config), True)
399 for product_name, product_info in products:
400 # get product name, product informations and the directory where to put
402 if (not (src.product.product_is_fixed(product_info) or
403 src.product.product_is_native(product_info))):
404 source_dir = src.Path(product_info.source_dir)
406 source_dir = src.Path('')
409 logger.write('%s: ' % src.printcolors.printcLabel(product_name), 3)
410 logger.write(' ' * (max_product_name_len - len(product_name)), 3, False)
411 logger.write("\n", 4, False)
413 # Remove the existing source directory if
414 # the product is not in development mode
415 is_dev = src.product.product_is_dev(product_info)
416 if source_dir.exists():
417 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS), 3, False)
418 msg = _("INFO : Not doing anything because the source directory already exists:\n %s\n") % source_dir
420 good_result = good_result + 1
421 # Do not get the sources and go to next product
424 # Call to the function that get the sources for one product
425 retcode = get_product_sources(config,
430 max_product_name_len,
434 if 'no_rpath' in product_info.keys():
435 if product_info.no_rpath:
436 hack_no_rpath(config, product_info, logger)
439 # Check that the sources are correctly get using the files to be tested
440 # in product information
442 check_OK, wrong_path = check_sources(product_info, logger)
444 # Print the missing file path
445 msg = _("The required file %s does not exists. " % wrong_path)
446 logger.write(src.printcolors.printcError("\nERROR: ") + msg, 3)
450 results[product_name] = retcode
452 # The case where it succeed
454 good_result = good_result + 1
456 # The case where it failed
460 if not(src.product.product_is_fixed(product_info) or
461 src.product.product_is_native(product_info)):
462 logger.write('%s\n' % src.printcolors.printc(res), 3, False)
464 return good_result, results
466 def check_sources(product_info, logger):
467 '''Check that the sources are correctly get, using the files to be tested
468 in product information
470 :param product_info Config: The configuration specific to
471 the product to be prepared
472 :return: True if the files exists (or no files to test is provided).
475 # Get the files to test if there is any
476 if ("present_files" in product_info and
477 "source" in product_info.present_files):
478 l_files_to_be_tested = product_info.present_files.source
479 for file_path in l_files_to_be_tested:
480 # The path to test is the source directory
481 # of the product joined the file path provided
482 path_to_test = os.path.join(product_info.source_dir, file_path)
483 logger.write(_("\nTesting existence of file: \n"), 5)
484 logger.write(path_to_test, 5)
485 if not os.path.exists(path_to_test):
486 return False, path_to_test
487 logger.write(src.printcolors.printcSuccess(" OK\n"), 5)
491 '''method that is called when salomeTools is called with --help option.
493 :return: The text to display for the source command description.
496 return _("The source command gets the sources of the application products "
497 "from cvs, git or an archive.\n\nexample:"
498 "\nsat source SALOME-master --products KERNEL,GUI")
500 def run(args, runner, logger):
501 '''method that is called when salomeTools is called with source parameter.
503 DBG.write("source.run()", args)
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)