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 # Define all possible option for log command : sat log <options>
25 parser = src.options.Options()
26 parser.add_option('m', 'module', 'list2', 'modules',
27 _('modules from which to get the sources. This option can be'
28 ' passed several time to get the sources of several modules.'))
29 parser.add_option('', 'no_sample', 'boolean', 'no_sample',
30 _("do not get sources from sample modules."))
31 parser.add_option('f', 'force', 'boolean', 'force',
32 _("force to get the sources of the modules in development mode."))
34 def prepare_for_dev(config, module_info, source_dir, force, logger, pad):
35 '''The method called if the module is in development mode
37 :param config Config: The global configuration
38 :param module_info Config: The configuration specific to
39 the module to be prepared
40 :param source_dir Path: The Path instance corresponding to the
41 directory where to put the sources
42 :param force boolean: True if the --force option was invoked
43 :param logger Logger: The logger instance to use for the display and logging
44 :param pad int: The gap to apply for the terminal display
45 :return: True if it succeed, else False
49 # if the module source directory does not exist,
50 # get it in checkout mode, else, do not do anything
51 # unless the force option is invoked
52 if not os.path.exists(module_info.source_dir) or force:
53 # Call the function corresponding to get the sources with True checkout
54 retcode = get_module_sources(config,
62 logger.write("\n", 3, False)
63 # +2 because module name is followed by ': '
64 logger.write(" " * (pad+2), 3, False)
66 logger.write('dev: %s ... ' %
67 src.printcolors.printcInfo(module_info.source_dir), 3, False)
72 def get_sources_from_git(module_info, source_dir, logger, pad, is_dev=False):
73 '''The method called if the module is to be get in git mode
75 :param module_info Config: The configuration specific to
76 the module to be prepared
77 :param source_dir Path: The Path instance corresponding to the
78 directory where to put the sources
79 :param logger Logger: The logger instance to use for the display and logging
80 :param pad int: The gap to apply for the terminal display
81 :param is_dev boolean: True if the module is in development mode
82 :return: True if it succeed, else False
88 # Get the repository address. (from repo_dev key if the module is
90 if is_dev and 'repo_dev' in module_info.git_info:
91 coflag = src.printcolors.printcHighlight(coflag.upper())
92 repo_git = module_info.git_info.repo_dev
94 repo_git = module_info.git_info.repo
96 # Display informations
97 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(repo_git)), 3,
99 logger.write(' ' * (pad + 50 - len(repo_git)), 3, False)
100 logger.write(' tag:%s' % src.printcolors.printcInfo(
101 module_info.git_info.tag),
104 logger.write(' %s. ' % ('.' * (10 - len(module_info.git_info.tag))), 3,
107 logger.write('\n', 5, False)
108 # Call the system function that do the extraction in git mode
109 retcode = src.system.git_extract(repo_git,
110 module_info.git_info.tag,
114 def get_sources_from_archive(module_info, source_dir, logger):
115 '''The method called if the module is to be get in archive mode
117 :param module_info Config: The configuration specific to
118 the module to be prepared
119 :param source_dir Path: The Path instance corresponding to the
120 directory where to put the sources
121 :param logger Logger: The logger instance to use for the display and logging
122 :return: True if it succeed, else False
125 # check archive exists
126 if not os.path.exists(module_info.archive_info.archive_name):
127 raise src.SatException(_("Archive not found: '%s'") %
128 module_info.archive_info.archive_name)
130 logger.write('arc:%s ... ' %
131 src.printcolors.printcInfo(module_info.archive_info.archive_name),
135 # Call the system function that do the extraction in archive mode
136 retcode, NameExtractedDirectory = src.system.archive_extract(
137 module_info.archive_info.archive_name,
138 source_dir.dir(), logger)
140 # Rename the source directory if
141 # it does not match with module_info.source_dir
142 if (NameExtractedDirectory.replace('/', '') !=
143 os.path.basename(module_info.source_dir)):
144 shutil.move(os.path.join(os.path.dirname(module_info.source_dir),
145 NameExtractedDirectory),
146 module_info.source_dir)
150 def get_sources_from_cvs(user, module_info, source_dir, checkout, logger, pad):
151 '''The method called if the module is to be get in cvs mode
153 :param user str: The user to use in for the cvs command
154 :param module_info Config: The configuration specific to
155 the module to be prepared
156 :param source_dir Path: The Path instance corresponding to the
157 directory where to put the sources
158 :param checkout boolean: If True, get the source in checkout mode
159 :param logger Logger: The logger instance to use for the display and logging
160 :param pad int: The gap to apply for the terminal display
161 :return: True if it succeed, else False
164 # Get the protocol to use in the command
165 if "protocol" in module_info.cvs_info:
166 protocol = module_info.cvs_info.protocol
170 # Construct the line to display
171 if "protocol" in module_info.cvs_info:
172 cvs_line = "%s:%s@%s:%s" % \
173 (protocol, user, module_info.cvs_info.server,
174 module_info.cvs_info.module_base)
176 cvs_line = "%s / %s" % (module_info.cvs_info.server,
177 module_info.cvs_info.module_base)
180 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
182 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(cvs_line)),
185 logger.write(' ' * (pad + 50 - len(cvs_line)), 3, False)
186 logger.write(' src:%s' %
187 src.printcolors.printcInfo(module_info.cvs_info.source),
190 logger.write(' ' * (pad + 1 - len(module_info.cvs_info.source)), 3, False)
191 logger.write(' tag:%s' %
192 src.printcolors.printcInfo(module_info.cvs_info.tag),
195 # at least one '.' is visible
196 logger.write(' %s. ' % ('.' * (10 - len(module_info.cvs_info.tag))),
200 logger.write('\n', 5, False)
202 # Call the system function that do the extraction in cvs mode
203 retcode = src.system.cvs_extract(protocol, user,
204 module_info.cvs_info.server,
205 module_info.cvs_info.module_base,
206 module_info.cvs_info.tag,
207 module_info.cvs_info.source,
208 source_dir, logger, checkout)
211 def get_sources_from_svn(user, module_info, source_dir, checkout, logger):
212 '''The method called if the module is to be get in svn mode
214 :param user str: The user to use in for the svn command
215 :param module_info Config: The configuration specific to
216 the module to be prepared
217 :param source_dir Path: The Path instance corresponding to the
218 directory where to put the sources
219 :param checkout boolean: If True, get the source in checkout mode
220 :param logger Logger: The logger instance to use for the display and logging
221 :return: True if it succeed, else False
225 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
227 logger.write('%s:%s ... ' % (coflag,
228 src.printcolors.printcInfo(
229 module_info.svn_info.repo)),
233 logger.write('\n', 5, False)
234 # Call the system function that do the extraction in svn mode
235 retcode = src.system.svn_extract(user,
236 module_info.svn_info.repo,
237 module_info.svn_info.tag,
243 def get_sources_from_dir(module_info, source_dir, logger):
244 '''The method called if the module is to be get in dir mode
246 :param module_info Config: The configuration specific to
247 the module to be prepared
248 :param source_dir Path: The Path instance corresponding to the
249 directory where to put the sources
250 :param logger Logger: The logger instance to use for the display and logging
251 :return: True if it succeed, else False
254 # Check if it is a symlink?
255 use_link = ('symlink' in module_info.dir_info and
256 module_info.dir_info.symlink)
258 if use_link: dirflag = 'lnk'
260 # check that source exists if it is not a symlink
261 if (not use_link) and (not os.path.exists(module_info.dir_info.dir)):
262 raise src.SatException(_("Source directory not found: '%s'") %
263 module_info.dir_info.dir)
265 logger.write('%s:%s ... ' %
266 (dirflag, src.printcolors.printcInfo(module_info.dir_info.dir)),
272 retcode = src.Path(source_dir).symlink(module_info.dir_info.dir)
274 retcode = src.Path(module_info.dir_info.dir).copy(source_dir)
278 def get_module_sources(config,
286 '''Get the module sources.
288 :param config Config: The global configuration
289 :param module_info Config: The configuration specific to
290 the module to be prepared
291 :param is_dev boolean: True if the module is in development mode
292 :param source_dir Path: The Path instance corresponding to the
293 directory where to put the sources
294 :param force boolean: True if the --force option was invoked
295 :param logger Logger: The logger instance to use for the display and logging
296 :param pad int: The gap to apply for the terminal display
297 :param checkout boolean: If True, get the source in checkout mode
298 :return: True if it succeed, else False
301 if not checkout and is_dev:
302 return prepare_for_dev(config, module_info, source_dir, force, logger, pad)
304 if module_info.get_method == "git":
305 return get_sources_from_git(module_info, source_dir, logger, pad,
308 if module_info.get_method == "archive":
309 return get_sources_from_archive(module_info, source_dir, logger)
311 if module_info.get_method == "cvs":
312 cvs_user = config.USER.cvs_user
313 return get_sources_from_cvs(cvs_user,
320 if module_info.get_method == "svn":
321 svn_user = config.USER.svn_user
322 return get_sources_from_svn(svn_user, module_info, source_dir,
326 if module_info.get_method == "dir":
327 return get_sources_from_dir(module_info, source_dir, logger)
329 if len(module_info.get_method) == 0:
331 logger.write('%s ...' % _("ignored"), 3, False)
334 # if the get_method is not in [git, archive, cvs, svn, dir]
335 logger.write(_("Unknown get_mehtod %(get)s for module %(module)s") % \
336 { 'get': module_info.get_method, 'module': module_info.name }, 3, False)
337 logger.write(" ... ", 3, False)
341 def get_all_module_sources(config, modules, force, logger):
342 '''Get all the module sources.
344 :param config Config: The global configuration
345 :param modules List: The list of tuples (module name, module informations)
346 :param force boolean: True if the --force option was invoked
347 :param logger Logger: The logger instance to be used for the logging
348 :return: the tuple (number of success, dictionary module_name/success_fail)
352 # Initialize the variables that will count the fails and success
356 # Get the maximum name length in order to format the terminal display
357 max_module_name_len = 1
359 max_module_name_len = max(map(lambda l: len(l), modules[0])) + 4
361 # The loop on all the modules from which to get the sources
362 for module in modules:
363 # get module name, module informations and the directory where to put
365 module_name = module[0]
366 module_info = module[1]
367 source_dir = src.Path(module_info.source_dir)
370 logger.write('%s: ' % src.printcolors.printcLabel(module_name), 3)
371 logger.write(' ' * (max_module_name_len - len(module_name)), 3, False)
372 logger.write("\n", 4, False)
374 # Remove the existing source directory if
375 # the module is not in development mode
376 is_dev = ("dev_modules" in config.APPLICATION and
377 module_name in config.APPLICATION.dev_modules)
378 if source_dir.exists() and not is_dev:
379 logger.write(" " + _('remove %s') % source_dir, 4)
380 logger.write("\n ", 4, False)
383 # Call to the function that get the sources for one module
384 retcode = get_module_sources(config,
394 if 'no_rpath' in module_info.keys():
395 if module_info.no_rpath:
396 hack_no_rpath(config, module_info, logger)
400 results[module_name] = retcode
402 # The case where the module was not prepared because it is
403 # in development mode
404 res =(src.printcolors.printc(src.OK_STATUS) +
405 src.printcolors.printcWarning(_(
406 ' source directory already exists')))
407 good_result = good_result + 1
409 # The case where it succeed
411 good_result = good_result + 1
413 # The case where it failed
417 logger.write('%s\n' % src.printcolors.printc(res), 3, False)
419 return good_result, results
422 '''method that is called when salomeTools is called with --help option.
424 :return: The text to display for the source command description.
427 return _("The source command gets the sources of the application modules "
428 "from cvs, git, an archive or a directory..")
430 def run(args, runner, logger):
431 '''method that is called when salomeTools is called with source parameter.
434 (options, args) = parser.parse_args(args)
436 # check that the command has been called with an application
437 src.check_config_has_application( runner.cfg )
439 # Print some informations
440 logger.write(_('Preparing sources of application %s\n') %
441 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
442 src.printcolors.print_value(logger, 'out_dir',
443 runner.cfg.APPLICATION.out_dir, 2)
444 logger.write("\n", 2, False)
446 # Get the force option if it was passed
447 force = options.force
449 msg = _("Warning: the --force option has effect only "
450 "on modules in development mode\n\n")
451 logger.write(src.printcolors.printcWarning(msg))
453 # Get the modules to be prepared, regarding the options
454 if options.modules is None:
455 # No options, get all modules sources
456 modules = runner.cfg.APPLICATION.modules
458 # if option --modules, check that all modules of the command line
459 # are present in the application.
460 modules = options.modules
462 if m not in runner.cfg.APPLICATION.modules:
463 raise src.SatException(_("Module %(module)s "
464 "not defined in appplication %(application)s") %
465 { 'module': m, 'application': runner.cfg.VARS.application} )
467 # Construct the list of tuple containing
468 # the modules name and their definition
469 modules_infos = src.module.get_modules_infos(modules, runner.cfg)
471 # if the --no_sample option is invoked, supress the sample modules from
473 if options.no_sample:
474 modules_infos = filter(lambda l: not src.module.module_is_sample(l[1]),
477 # Call to the function that gets all the sources
478 good_result, results = get_all_module_sources(runner.cfg,
483 # Display the results (how much passed, how much failed, etc...)
484 status = src.OK_STATUS
487 logger.write("\n", 2, False)
488 if good_result == len(modules):
489 res_count = "%d / %d" % (good_result, good_result)
491 status = src.KO_STATUS
492 res_count = "%d / %d" % (good_result, len(modules))
494 for module in results:
495 if results[module] == 0 or results[module] is None:
496 details.append(module)
498 result = len(modules) - good_result
501 logger.write(_("Getting sources of application:"), 1)
502 logger.write(" " + src.printcolors.printc(status), 1, False)
503 logger.write(" (%s)\n" % res_count, 1, False)
506 logger.write(_("Following sources haven't been get:\n"), 2)
507 logger.write(" ".join(details), 2)
508 logger.write("\n", 2, False)