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(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)
110 # what do we do with git tree structure and history
111 if is_dev and "sub_dir" in product_info.git_info:
112 logger.error("dev mode for product is incompatible with 'sub_dir' option")
115 if not is_dev and "sub_dir" in product_info.git_info:
116 sub_dir = product_info.git_info.sub_dir
119 # Call the system function that do the extraction in git mode
120 retcode = src.system.git_extract(repo_git,
121 product_info.git_info.tag,
122 source_dir, logger, environ)
124 # Call the system function that do the extraction of a sub_dir in git mode
125 logger.write("sub_dir:%s " % sub_dir, 3)
126 retcode = src.system.git_extract_sub_dir(repo_git,
127 product_info.git_info.tag,
128 source_dir, sub_dir, logger, environ)
133 def get_source_from_archive(product_info, source_dir, logger):
134 '''The method called if the product is to be get in archive mode
136 :param product_info Config: The configuration specific to
137 the product to be prepared
138 :param source_dir Path: The Path instance corresponding to the
139 directory where to put the sources
140 :param logger Logger: The logger instance to use for the display and logging
141 :return: True if it succeed, else False
144 # check archive exists
145 if not os.path.exists(product_info.archive_info.archive_name):
146 raise src.SatException(_("Archive not found in config.PATHS.ARCHIVEPATH: '%s'") %
147 product_info.archive_info.archive_name)
149 logger.write('arc:%s ... ' %
150 src.printcolors.printcInfo(product_info.archive_info.archive_name),
154 # Call the system function that do the extraction in archive mode
155 retcode, NameExtractedDirectory = src.system.archive_extract(
156 product_info.archive_info.archive_name,
157 source_dir.dir(), logger)
159 # Rename the source directory if
160 # it does not match with product_info.source_dir
161 if (NameExtractedDirectory.replace('/', '') !=
162 os.path.basename(product_info.source_dir)):
163 shutil.move(os.path.join(os.path.dirname(product_info.source_dir),
164 NameExtractedDirectory),
165 product_info.source_dir)
169 def get_source_from_dir(product_info, source_dir, logger):
171 if "dir_info" not in product_info:
172 msg = _("Error: you must put a dir_info section"
173 " in the file %s.pyconf" % product_info.name)
174 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
177 if "dir" not in product_info.dir_info:
178 msg = _("Error: you must put a dir in the dir_info section"
179 " in the file %s.pyconf" % product_info.name)
180 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
183 # check that source exists
184 if not os.path.exists(product_info.dir_info.dir):
185 msg = _("Error: the dir %s defined in the file"
186 " %s.pyconf does not exists" % (product_info.dir_info.dir,
188 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
191 logger.write('DIR: %s ... ' % src.printcolors.printcInfo(
192 product_info.dir_info.dir), 3)
195 retcode = src.Path(product_info.dir_info.dir).copy(source_dir)
199 def get_source_from_cvs(user,
206 '''The method called if the product is to be get in cvs mode
208 :param user str: The user to use in for the cvs command
209 :param product_info Config: The configuration specific to
210 the product to be prepared
211 :param source_dir Path: The Path instance corresponding to the
212 directory where to put the sources
213 :param checkout boolean: If True, get the source in checkout mode
214 :param logger Logger: The logger instance to use for the display and logging
215 :param pad int: The gap to apply for the terminal display
216 :param environ src.environment.Environ: The environment to source when
218 :return: True if it succeed, else False
221 # Get the protocol to use in the command
222 if "protocol" in product_info.cvs_info:
223 protocol = product_info.cvs_info.protocol
227 # Construct the line to display
228 if "protocol" in product_info.cvs_info:
229 cvs_line = "%s:%s@%s:%s" % \
230 (protocol, user, product_info.cvs_info.server,
231 product_info.cvs_info.product_base)
233 cvs_line = "%s / %s" % (product_info.cvs_info.server,
234 product_info.cvs_info.product_base)
237 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
239 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(cvs_line)),
242 logger.write(' ' * (pad + 50 - len(cvs_line)), 3, False)
243 logger.write(' src:%s' %
244 src.printcolors.printcInfo(product_info.cvs_info.source),
247 logger.write(' ' * (pad + 1 - len(product_info.cvs_info.source)), 3, False)
248 logger.write(' tag:%s' %
249 src.printcolors.printcInfo(product_info.cvs_info.tag),
252 # at least one '.' is visible
253 logger.write(' %s. ' % ('.' * (10 - len(product_info.cvs_info.tag))),
257 logger.write('\n', 5, False)
259 # Call the system function that do the extraction in cvs mode
260 retcode = src.system.cvs_extract(protocol, user,
261 product_info.cvs_info.server,
262 product_info.cvs_info.product_base,
263 product_info.cvs_info.tag,
264 product_info.cvs_info.source,
265 source_dir, logger, checkout, environ)
268 def get_source_from_svn(user,
274 '''The method called if the product is to be get in svn mode
276 :param user str: The user to use in for the svn command
277 :param product_info Config: The configuration specific to
278 the product to be prepared
279 :param source_dir Path: The Path instance corresponding to the
280 directory where to put the sources
281 :param checkout boolean: If True, get the source in checkout mode
282 :param logger Logger: The logger instance to use for the display and logging
283 :param environ src.environment.Environ: The environment to source when
285 :return: True if it succeed, else False
289 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
291 logger.write('%s:%s ... ' % (coflag,
292 src.printcolors.printcInfo(
293 product_info.svn_info.repo)),
297 logger.write('\n', 5, False)
298 # Call the system function that do the extraction in svn mode
299 retcode = src.system.svn_extract(user,
300 product_info.svn_info.repo,
301 product_info.svn_info.tag,
308 def get_product_sources(config,
315 '''Get the product sources.
317 :param config Config: The global configuration
318 :param product_info Config: The configuration specific to
319 the product to be prepared
320 :param is_dev boolean: True if the product is in development mode
321 :param source_dir Path: The Path instance corresponding to the
322 directory where to put the sources
323 :param logger Logger: The logger instance to use for the display and logging
324 :param pad int: The gap to apply for the terminal display
325 :param checkout boolean: If True, get the source in checkout mode
326 :return: True if it succeed, else False
330 # Get the application environment
331 logger.write(_("Set the application environment\n"), 5)
332 env_appli = src.environment.SalomeEnviron(config,
333 src.environment.Environ(dict(os.environ)))
334 env_appli.set_application_env(logger)
336 # Call the right function to get sources regarding the product settings
337 if not checkout and is_dev:
338 return get_source_for_dev(config,
344 if product_info.get_source == "git":
345 return get_source_from_git(product_info, source_dir, logger, pad,
348 if product_info.get_source == "archive":
349 return get_source_from_archive(product_info, source_dir, logger)
351 if product_info.get_source == "dir":
352 return get_source_from_dir(product_info, source_dir, logger)
354 if product_info.get_source == "cvs":
355 cvs_user = config.USER.cvs_user
356 return get_source_from_cvs(cvs_user,
364 if product_info.get_source == "svn":
365 svn_user = config.USER.svn_user
366 return get_source_from_svn(svn_user, product_info, source_dir,
371 if product_info.get_source == "native":
373 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
376 msg = _('INFORMATION : Not doing anything because the product'
377 ' is of type "native".\n')
381 if product_info.get_source == "fixed":
383 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
386 msg = _('INFORMATION : Not doing anything because the product'
387 ' is of type "fixed".\n')
391 # if the get_source is not in [git, archive, cvs, svn, fixed, native]
392 logger.write(_("Unknown get source method \"%(get)s\" for product %(product)s") % \
393 { 'get': product_info.get_source, 'product': product_info.name }, 3, False)
394 logger.write(" ... ", 3, False)
398 def get_all_product_sources(config, products, logger):
399 '''Get all the product sources.
401 :param config Config: The global configuration
402 :param products List: The list of tuples (product name, product informations)
403 :param logger Logger: The logger instance to be used for the logging
404 :return: the tuple (number of success, dictionary product_name/success_fail)
408 # Initialize the variables that will count the fails and success
412 # Get the maximum name length in order to format the terminal display
413 max_product_name_len = 1
414 if len(products) > 0:
415 max_product_name_len = max(map(lambda l: len(l), products[0])) + 4
417 # The loop on all the products from which to get the sources
418 # DBG.write("source.get_all_product_sources config id", id(config), True)
419 for product_name, product_info in products:
420 # get product name, product informations and the directory where to put
422 if (not (src.product.product_is_fixed(product_info) or
423 src.product.product_is_native(product_info))):
424 source_dir = src.Path(product_info.source_dir)
426 source_dir = src.Path('')
429 logger.write('%s: ' % src.printcolors.printcLabel(product_name), 3)
430 logger.write(' ' * (max_product_name_len - len(product_name)), 3, False)
431 logger.write("\n", 4, False)
433 # Remove the existing source directory if
434 # the product is not in development mode
435 is_dev = src.product.product_is_dev(product_info)
436 if source_dir.exists():
437 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS), 3, False)
438 msg = _("INFO : Not doing anything because the source directory already exists:\n %s\n") % source_dir
440 good_result = good_result + 1
441 # Do not get the sources and go to next product
444 # Call to the function that get the sources for one product
445 retcode = get_product_sources(config,
450 max_product_name_len,
454 if 'no_rpath' in product_info.keys():
455 if product_info.no_rpath:
456 hack_no_rpath(config, product_info, logger)
459 # Check that the sources are correctly get using the files to be tested
460 # in product information
462 check_OK, wrong_path = check_sources(product_info, logger)
464 # Print the missing file path
465 msg = _("The required file %s does not exists. " % wrong_path)
466 logger.write(src.printcolors.printcError("\nERROR: ") + msg, 3)
470 results[product_name] = retcode
472 # The case where it succeed
474 good_result = good_result + 1
476 # The case where it failed
480 if not(src.product.product_is_fixed(product_info) or
481 src.product.product_is_native(product_info)):
482 logger.write('%s\n' % src.printcolors.printc(res), 3, False)
484 return good_result, results
486 def check_sources(product_info, logger):
487 '''Check that the sources are correctly get, using the files to be tested
488 in product information
490 :param product_info Config: The configuration specific to
491 the product to be prepared
492 :return: True if the files exists (or no files to test is provided).
495 # Get the files to test if there is any
496 if ("present_files" in product_info and
497 "source" in product_info.present_files):
498 l_files_to_be_tested = product_info.present_files.source
499 for file_path in l_files_to_be_tested:
500 # The path to test is the source directory
501 # of the product joined the file path provided
502 path_to_test = os.path.join(product_info.source_dir, file_path)
503 logger.write(_("\nTesting existence of file: \n"), 5)
504 logger.write(path_to_test, 5)
505 if not os.path.exists(path_to_test):
506 return False, path_to_test
507 logger.write(src.printcolors.printcSuccess(" OK\n"), 5)
511 '''method that is called when salomeTools is called with --help option.
513 :return: The text to display for the source command description.
516 return _("The source command gets the sources of the application products "
517 "from cvs, git or an archive.\n\nexample:"
518 "\nsat source SALOME-master --products KERNEL,GUI")
520 def run(args, runner, logger):
521 '''method that is called when salomeTools is called with source parameter.
523 DBG.write("source.run()", args)
525 (options, args) = parser.parse_args(args)
527 # check that the command has been called with an application
528 src.check_config_has_application( runner.cfg )
530 # Print some informations
531 logger.write(_('Getting sources of the application %s\n') %
532 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
533 src.printcolors.print_value(logger, 'workdir',
534 runner.cfg.APPLICATION.workdir, 2)
535 logger.write("\n", 2, False)
537 # Get the products list with products informations regarding the options
538 products_infos = src.product.get_products_list(options, runner.cfg, logger)
540 # Call to the function that gets all the sources
541 good_result, results = get_all_product_sources(runner.cfg,
545 # Display the results (how much passed, how much failed, etc...)
546 status = src.OK_STATUS
549 logger.write("\n", 2, False)
550 if good_result == len(products_infos):
551 res_count = "%d / %d" % (good_result, good_result)
553 status = src.KO_STATUS
554 res_count = "%d / %d" % (good_result, len(products_infos))
556 for product in results:
557 if results[product] == 0 or results[product] is None:
558 details.append(product)
560 result = len(products_infos) - good_result
563 logger.write(_("Getting sources of the application:"), 1)
564 logger.write(" " + src.printcolors.printc(status), 1, False)
565 logger.write(" (%s)\n" % res_count, 1, False)
568 logger.write(_("Following sources haven't been get:\n"), 2)
569 logger.write(" ".join(details), 2)
570 logger.write("\n", 2, False)