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', 'product', 'list2', 'products',
28 _('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, source_dir, logger, pad, is_dev=False):
64 '''The method called if the product is to be get in git mode
66 :param product_info Config: The configuration specific to
67 the product to be prepared
68 :param source_dir Path: The Path instance corresponding to the
69 directory where to put the sources
70 :param logger Logger: The logger instance to use for the display and logging
71 :param pad int: The gap to apply for the terminal display
72 :param is_dev boolean: True if the product is in development mode
73 :return: True if it succeed, else False
79 # Get the repository address. (from repo_dev key if the product is
81 if is_dev and 'repo_dev' in product_info.git_info:
82 coflag = src.printcolors.printcHighlight(coflag.upper())
83 repo_git = product_info.git_info.repo_dev
85 repo_git = product_info.git_info.repo
87 # Display informations
88 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(repo_git)), 3,
90 logger.write(' ' * (pad + 50 - len(repo_git)), 3, False)
91 logger.write(' tag:%s' % src.printcolors.printcInfo(
92 product_info.git_info.tag),
95 logger.write(' %s. ' % ('.' * (10 - len(product_info.git_info.tag))), 3,
98 logger.write('\n', 5, False)
99 # Call the system function that do the extraction in git mode
100 retcode = src.system.git_extract(repo_git,
101 product_info.git_info.tag,
105 def get_source_from_archive(product_info, source_dir, logger):
106 '''The method called if the product is to be get in archive mode
108 :param product_info Config: The configuration specific to
109 the product to be prepared
110 :param source_dir Path: The Path instance corresponding to the
111 directory where to put the sources
112 :param logger Logger: The logger instance to use for the display and logging
113 :return: True if it succeed, else False
116 # check archive exists
117 if not os.path.exists(product_info.archive_info.archive_name):
118 raise src.SatException(_("Archive not found: '%s'") %
119 product_info.archive_info.archive_name)
121 logger.write('arc:%s ... ' %
122 src.printcolors.printcInfo(product_info.archive_info.archive_name),
126 # Call the system function that do the extraction in archive mode
127 retcode, NameExtractedDirectory = src.system.archive_extract(
128 product_info.archive_info.archive_name,
129 source_dir.dir(), logger)
131 # Rename the source directory if
132 # it does not match with product_info.source_dir
133 if (NameExtractedDirectory.replace('/', '') !=
134 os.path.basename(product_info.source_dir)):
135 shutil.move(os.path.join(os.path.dirname(product_info.source_dir),
136 NameExtractedDirectory),
137 product_info.source_dir)
141 def get_source_from_cvs(user, product_info, source_dir, checkout, logger, pad):
142 '''The method called if the product is to be get in cvs mode
144 :param user str: The user to use in for the cvs command
145 :param product_info Config: The configuration specific to
146 the product to be prepared
147 :param source_dir Path: The Path instance corresponding to the
148 directory where to put the sources
149 :param checkout boolean: If True, get the source in checkout mode
150 :param logger Logger: The logger instance to use for the display and logging
151 :param pad int: The gap to apply for the terminal display
152 :return: True if it succeed, else False
155 # Get the protocol to use in the command
156 if "protocol" in product_info.cvs_info:
157 protocol = product_info.cvs_info.protocol
161 # Construct the line to display
162 if "protocol" in product_info.cvs_info:
163 cvs_line = "%s:%s@%s:%s" % \
164 (protocol, user, product_info.cvs_info.server,
165 product_info.cvs_info.product_base)
167 cvs_line = "%s / %s" % (product_info.cvs_info.server,
168 product_info.cvs_info.product_base)
171 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
173 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(cvs_line)),
176 logger.write(' ' * (pad + 50 - len(cvs_line)), 3, False)
177 logger.write(' src:%s' %
178 src.printcolors.printcInfo(product_info.cvs_info.source),
181 logger.write(' ' * (pad + 1 - len(product_info.cvs_info.source)), 3, False)
182 logger.write(' tag:%s' %
183 src.printcolors.printcInfo(product_info.cvs_info.tag),
186 # at least one '.' is visible
187 logger.write(' %s. ' % ('.' * (10 - len(product_info.cvs_info.tag))),
191 logger.write('\n', 5, False)
193 # Call the system function that do the extraction in cvs mode
194 retcode = src.system.cvs_extract(protocol, user,
195 product_info.cvs_info.server,
196 product_info.cvs_info.product_base,
197 product_info.cvs_info.tag,
198 product_info.cvs_info.source,
199 source_dir, logger, checkout)
202 def get_source_from_svn(user, product_info, source_dir, checkout, logger):
203 '''The method called if the product is to be get in svn mode
205 :param user str: The user to use in for the svn command
206 :param product_info Config: The configuration specific to
207 the product to be prepared
208 :param source_dir Path: The Path instance corresponding to the
209 directory where to put the sources
210 :param checkout boolean: If True, get the source in checkout mode
211 :param logger Logger: The logger instance to use for the display and logging
212 :return: True if it succeed, else False
216 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
218 logger.write('%s:%s ... ' % (coflag,
219 src.printcolors.printcInfo(
220 product_info.svn_info.repo)),
224 logger.write('\n', 5, False)
225 # Call the system function that do the extraction in svn mode
226 retcode = src.system.svn_extract(user,
227 product_info.svn_info.repo,
228 product_info.svn_info.tag,
234 def get_product_sources(config,
241 '''Get the product sources.
243 :param config Config: The global configuration
244 :param product_info Config: The configuration specific to
245 the product to be prepared
246 :param is_dev boolean: True if the product is in development mode
247 :param source_dir Path: The Path instance corresponding to the
248 directory where to put the sources
249 :param logger Logger: The logger instance to use for the display and logging
250 :param pad int: The gap to apply for the terminal display
251 :param checkout boolean: If True, get the source in checkout mode
252 :return: True if it succeed, else False
255 if not checkout and is_dev:
256 return get_source_for_dev(config,
262 if product_info.get_source == "git":
263 return get_source_from_git(product_info, source_dir, logger, pad,
266 if product_info.get_source == "archive":
267 return get_source_from_archive(product_info, source_dir, logger)
269 if product_info.get_source == "cvs":
270 cvs_user = config.USER.cvs_user
271 return get_source_from_cvs(cvs_user,
278 if product_info.get_source == "svn":
279 svn_user = config.USER.svn_user
280 return get_source_from_svn(svn_user, product_info, source_dir,
284 if product_info.get_source == "native":
286 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
289 msg = _('INFORMATION : Not doing anything because the product'
290 ' is of type "native".\n')
294 if product_info.get_source == "fixed":
296 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
299 msg = _('INFORMATION : Not doing anything because the product'
300 ' is of type "fixed".\n')
304 # if the get_source is not in [git, archive, cvs, svn, fixed, native]
305 logger.write(_("Unknown get source method \"%(get)s\" for product %(product)s") % \
306 { 'get': product_info.get_source, 'product': product_info.name }, 3, False)
307 logger.write(" ... ", 3, False)
311 def get_all_product_sources(config, products, logger):
312 '''Get all the product sources.
314 :param config Config: The global configuration
315 :param products List: The list of tuples (product name, product informations)
316 :param logger Logger: The logger instance to be used for the logging
317 :return: the tuple (number of success, dictionary product_name/success_fail)
321 # Initialize the variables that will count the fails and success
325 # Get the maximum name length in order to format the terminal display
326 max_product_name_len = 1
327 if len(products) > 0:
328 max_product_name_len = max(map(lambda l: len(l), products[0])) + 4
330 # The loop on all the products from which to get the sources
331 for product_name, product_info in products:
332 # get product name, product informations and the directory where to put
334 if (not (src.product.product_is_fixed(product_info) or
335 src.product.product_is_native(product_info))):
336 source_dir = src.Path(product_info.source_dir)
338 source_dir = src.Path('')
341 logger.write('%s: ' % src.printcolors.printcLabel(product_name), 3)
342 logger.write(' ' * (max_product_name_len - len(product_name)), 3, False)
343 logger.write("\n", 4, False)
345 # Remove the existing source directory if
346 # the product is not in development mode
347 is_dev = src.product.product_is_dev(product_info)
348 if source_dir.exists():
349 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
352 msg = _("INFORMATION : Not doing anything because the source"
353 " directory already exists.\n")
355 good_result = good_result + 1
356 # Do not get the sources and go to next product
359 # Call to the function that get the sources for one product
360 retcode = get_product_sources(config,
365 max_product_name_len,
369 if 'no_rpath' in product_info.keys():
370 if product_info.no_rpath:
371 hack_no_rpath(config, product_info, logger)
374 # Check that the sources are correctly get using the files to be tested
375 # in product information
377 check_OK, wrong_path = check_sources(product_info, logger)
379 # Print the missing file path
380 msg = _("The required file %s does not exists. " % wrong_path)
381 logger.write(src.printcolors.printcError("\nERROR: ") + msg, 3)
385 results[product_name] = retcode
387 # The case where it succeed
391 good_result = good_result + 1
393 # The case where it failed
397 if not(src.product.product_is_fixed(product_info) or
398 src.product.product_is_native(product_info)):
399 logger.write('%s\n' % src.printcolors.printc(res), 3, False)
401 return good_result, results
403 def check_sources(product_info, logger):
404 '''Check that the sources are correctly get, using the files to be tested
405 in product information
407 :param product_info Config: The configuration specific to
408 the product to be prepared
409 :return: True if the files exists (or no files to test is provided).
412 # Get the files to test if there is any
413 if ("present_files" in product_info and
414 "source" in product_info.present_files):
415 l_files_to_be_tested = product_info.present_files.source
416 for file_path in l_files_to_be_tested:
417 # The path to test is the source directory
418 # of the product joined the file path provided
419 path_to_test = os.path.join(product_info.source_dir, file_path)
420 logger.write(_("\nTesting existence of file: \n"), 5)
421 logger.write(path_to_test, 5)
422 if not os.path.exists(path_to_test):
423 return False, path_to_test
424 logger.write(src.printcolors.printcSuccess(" OK\n"), 5)
428 '''method that is called when salomeTools is called with --help option.
430 :return: The text to display for the source command description.
433 return _("The source command gets the sources of the application products "
434 "from cvs, git, an archive or a directory..")
436 def run(args, runner, logger):
437 '''method that is called when salomeTools is called with source parameter.
440 (options, args) = parser.parse_args(args)
442 # check that the command has been called with an application
443 src.check_config_has_application( runner.cfg )
445 # Print some informations
446 logger.write(_('Getting sources of the application %s\n') %
447 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
448 src.printcolors.print_value(logger, 'workdir',
449 runner.cfg.APPLICATION.workdir, 2)
450 logger.write("\n", 2, False)
452 # Get the products list with products informations regarding the options
453 products_infos = prepare.get_products_list(options, runner.cfg, logger)
455 # Call to the function that gets all the sources
456 good_result, results = get_all_product_sources(runner.cfg,
460 # Display the results (how much passed, how much failed, etc...)
461 status = src.OK_STATUS
464 logger.write("\n", 2, False)
465 if good_result == len(products_infos):
466 res_count = "%d / %d" % (good_result, good_result)
468 status = src.KO_STATUS
469 res_count = "%d / %d" % (good_result, len(products_infos))
471 for product in results:
472 if results[product] == 0 or results[product] is None:
473 details.append(product)
475 result = len(products_infos) - good_result
478 logger.write(_("Getting sources of the application:"), 1)
479 logger.write(" " + src.printcolors.printc(status), 1, False)
480 logger.write(" (%s)\n" % res_count, 1, False)
483 logger.write(_("Following sources haven't been get:\n"), 2)
484 logger.write(" ".join(details), 2)
485 logger.write("\n", 2, False)