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
26 import src.debug as DBG
28 # Define all possible option for patch command : sat patch <options>
29 parser = src.options.Options()
30 parser.add_option('p', 'products', 'list2', 'products',
31 _('Optional: products from which to get the sources. This option accepts a comma separated list.'))
33 def get_source_for_dev(config, product_info, source_dir, logger, pad):
34 '''The method called if the product is in development mode
36 :param config Config: The global configuration
37 :param product_info Config: The configuration specific to
38 the product to be prepared
39 :param source_dir Path: The Path instance corresponding to the
40 directory where to put the sources
41 :param logger Logger: The logger instance to use for the display and logging
42 :param pad int: The gap to apply for the terminal display
43 :return: True if it succeed, else False
47 # Call the function corresponding to get the sources with True checkout
48 retcode = get_product_sources(config,
55 logger.write("\n", 3, False)
56 # +2 because product name is followed by ': '
57 logger.write(" " * (pad+2), 3, False)
59 logger.write('dev: %s ... ' %
60 src.printcolors.printcInfo(product_info.source_dir), 3, False)
65 def get_source_from_git(config,
72 '''The method called if the product is to be get in git mode
74 :param product_info Config: The configuration specific to
75 the product to be prepared
76 :param source_dir Path: The Path instance corresponding to the
77 directory where to put the sources
78 :param logger Logger: The logger instance to use for the display and logging
79 :param pad int: The gap to apply for the terminal display
80 :param is_dev boolean: True if the product is in development mode
81 :param environ src.environment.Environ: The environment to source when
83 :return: True if it succeed, else False
90 if ("APPLICATION" in config and
91 "properties" in config.APPLICATION and
92 "repo_dev" in config.APPLICATION.properties and
93 config.APPLICATION.properties.repo_dev == "yes") :
96 # Get the repository address.
97 # If the application has the repo_dev property
98 # Or if the product is in dev mode
99 # Then we use repo_dev if the key exists
100 if (is_dev or use_repo_dev) and 'repo_dev' in product_info.git_info:
101 coflag = src.printcolors.printcHighlight(coflag.upper())
102 repo_git = product_info.git_info.repo_dev
104 repo_git = product_info.git_info.repo
107 # Display informations
108 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(repo_git)), 3,
110 logger.write(' ' * (pad + 50 - len(repo_git)), 3, False)
111 logger.write(' tag:%s' % src.printcolors.printcInfo(
112 product_info.git_info.tag),
115 logger.write(' %s. ' % ('.' * (10 - len(product_info.git_info.tag))), 3,
118 logger.write('\n', 5, False)
122 # what do we do with git tree structure and history
123 if is_dev and "sub_dir" in product_info.git_info:
124 logger.error("dev mode for product is incompatible with 'sub_dir' option")
127 if not is_dev and "sub_dir" in product_info.git_info:
128 sub_dir = product_info.git_info.sub_dir
131 # Call the system function that do the extraction in git mode
132 retcode = src.system.git_extract(repo_git,
133 product_info.git_info.tag,
134 source_dir, logger, environ)
136 # Call the system function that do the extraction of a sub_dir in git mode
137 logger.write("sub_dir:%s " % sub_dir, 3)
138 retcode = src.system.git_extract_sub_dir(repo_git,
139 product_info.git_info.tag,
140 source_dir, sub_dir, logger, environ)
145 def get_source_from_archive(config, product_info, source_dir, logger):
146 '''The method called if the product is to be get in archive mode
148 :param config Config: The global configuration
149 :param product_info Config: The configuration specific to
150 the product to be prepared
151 :param source_dir Path: The Path instance corresponding to the
152 directory where to put the sources
153 :param logger Logger: The logger instance to use for the display and logging
154 :return: True if it succeed, else False
158 # check if pip should be used : pip mode id activated if the application and product have pip property
159 if (src.appli_test_property(config,"pip", "yes") and
160 src.product.product_test_property(product_info,"pip", "yes")):
161 pip_msg = "PIP : do nothing, product will be downloaded later at compile time "
162 logger.write(pip_msg, 3)
165 # check archive exists
166 if not os.path.exists(product_info.archive_info.archive_name):
167 # The archive is not found on local file system (ARCHIVEPATH)
169 logger.write("\n The archive is not found on local file system, we try ftp\n", 3)
170 ret=src.find_file_in_ftppath(product_info.archive_info.archive_name,
171 config.PATHS.ARCHIVEFTP, config.LOCAL.archive_dir, logger)
173 # archive was found on ftp and stored in ret
174 product_info.archive_info.archive_name=ret
176 raise src.SatException(_("Archive not found in ARCHIVEPATH, nor on ARCHIVEFTP: '%s'") %
177 product_info.archive_info.archive_name)
179 logger.write('arc:%s ... ' %
180 src.printcolors.printcInfo(product_info.archive_info.archive_name),
184 # Call the system function that do the extraction in archive mode
185 retcode, NameExtractedDirectory = src.system.archive_extract(
186 product_info.archive_info.archive_name,
187 source_dir.dir(), logger)
189 # Rename the source directory if
190 # it does not match with product_info.source_dir
191 if (NameExtractedDirectory.replace('/', '') !=
192 os.path.basename(product_info.source_dir)):
193 shutil.move(os.path.join(os.path.dirname(product_info.source_dir),
194 NameExtractedDirectory),
195 product_info.source_dir)
199 def get_source_from_dir(product_info, source_dir, logger):
201 if "dir_info" not in product_info:
202 msg = _("Error: you must put a dir_info section"
203 " in the file %s.pyconf" % product_info.name)
204 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
207 if "dir" not in product_info.dir_info:
208 msg = _("Error: you must put a dir in the dir_info section"
209 " in the file %s.pyconf" % product_info.name)
210 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
213 # check that source exists
214 if not os.path.exists(product_info.dir_info.dir):
215 msg = _("Error: the dir %s defined in the file"
216 " %s.pyconf does not exists" % (product_info.dir_info.dir,
218 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
221 logger.write('DIR: %s ... ' % src.printcolors.printcInfo(
222 product_info.dir_info.dir), 3)
225 retcode = src.Path(product_info.dir_info.dir).copy(source_dir)
229 def get_source_from_cvs(user,
236 '''The method called if the product is to be get in cvs mode
238 :param user str: The user to use in for the cvs command
239 :param product_info Config: The configuration specific to
240 the product to be prepared
241 :param source_dir Path: The Path instance corresponding to the
242 directory where to put the sources
243 :param checkout boolean: If True, get the source in checkout mode
244 :param logger Logger: The logger instance to use for the display and logging
245 :param pad int: The gap to apply for the terminal display
246 :param environ src.environment.Environ: The environment to source when
248 :return: True if it succeed, else False
251 # Get the protocol to use in the command
252 if "protocol" in product_info.cvs_info:
253 protocol = product_info.cvs_info.protocol
257 # Construct the line to display
258 if "protocol" in product_info.cvs_info:
259 cvs_line = "%s:%s@%s:%s" % \
260 (protocol, user, product_info.cvs_info.server,
261 product_info.cvs_info.product_base)
263 cvs_line = "%s / %s" % (product_info.cvs_info.server,
264 product_info.cvs_info.product_base)
267 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
269 logger.write('%s:%s' % (coflag, src.printcolors.printcInfo(cvs_line)),
272 logger.write(' ' * (pad + 50 - len(cvs_line)), 3, False)
273 logger.write(' src:%s' %
274 src.printcolors.printcInfo(product_info.cvs_info.source),
277 logger.write(' ' * (pad + 1 - len(product_info.cvs_info.source)), 3, False)
278 logger.write(' tag:%s' %
279 src.printcolors.printcInfo(product_info.cvs_info.tag),
282 # at least one '.' is visible
283 logger.write(' %s. ' % ('.' * (10 - len(product_info.cvs_info.tag))),
287 logger.write('\n', 5, False)
289 # Call the system function that do the extraction in cvs mode
290 retcode = src.system.cvs_extract(protocol, user,
291 product_info.cvs_info.server,
292 product_info.cvs_info.product_base,
293 product_info.cvs_info.tag,
294 product_info.cvs_info.source,
295 source_dir, logger, checkout, environ)
298 def get_source_from_svn(user,
304 '''The method called if the product is to be get in svn mode
306 :param user str: The user to use in for the svn command
307 :param product_info Config: The configuration specific to
308 the product to be prepared
309 :param source_dir Path: The Path instance corresponding to the
310 directory where to put the sources
311 :param checkout boolean: If True, get the source in checkout mode
312 :param logger Logger: The logger instance to use for the display and logging
313 :param environ src.environment.Environ: The environment to source when
315 :return: True if it succeed, else False
319 if checkout: coflag = src.printcolors.printcHighlight(coflag.upper())
321 logger.write('%s:%s ... ' % (coflag,
322 src.printcolors.printcInfo(
323 product_info.svn_info.repo)),
327 logger.write('\n', 5, False)
328 # Call the system function that do the extraction in svn mode
329 retcode = src.system.svn_extract(user,
330 product_info.svn_info.repo,
331 product_info.svn_info.tag,
338 def get_product_sources(config,
345 '''Get the product sources.
347 :param config Config: The global configuration
348 :param product_info Config: The configuration specific to
349 the product to be prepared
350 :param is_dev boolean: True if the product is in development mode
351 :param source_dir Path: The Path instance corresponding to the
352 directory where to put the sources
353 :param logger Logger: The logger instance to use for the display and logging
354 :param pad int: The gap to apply for the terminal display
355 :param checkout boolean: If True, get the source in checkout mode
356 :return: True if it succeed, else False
360 # Get the application environment
361 logger.write(_("Set the application environment\n"), 5)
362 env_appli = src.environment.SalomeEnviron(config,
363 src.environment.Environ(dict(os.environ)))
364 env_appli.set_application_env(logger)
366 # Call the right function to get sources regarding the product settings
367 if not checkout and is_dev:
368 return get_source_for_dev(config,
374 if product_info.get_source == "git":
375 return get_source_from_git(config, product_info, source_dir, logger, pad,
378 if product_info.get_source == "archive":
379 return get_source_from_archive(config, product_info, source_dir, logger)
381 if product_info.get_source == "dir":
382 return get_source_from_dir(product_info, source_dir, logger)
384 if product_info.get_source == "cvs":
385 cvs_user = config.USER.cvs_user
386 return get_source_from_cvs(cvs_user,
394 if product_info.get_source == "svn":
395 svn_user = config.USER.svn_user
396 return get_source_from_svn(svn_user, product_info, source_dir,
401 if product_info.get_source == "native":
402 # for native products we check the corresponding system packages are installed
403 logger.write("Native : Checking system packages are installed\n" , 3)
404 check_cmd=src.system.get_pkg_check_cmd() # (either rmp or apt)
405 run_pkg,build_pkg=src.product.check_system_dep(check_cmd, product_info)
408 logger.write(" - "+pkg + " : " + run_pkg[pkg], 1)
409 if "KO" in run_pkg[pkg]:
411 for pkg in build_pkg:
412 logger.write(" - "+pkg + " : " + build_pkg[pkg], 1)
413 if "KO" in build_pkg[pkg]:
416 logger.error("some system dependencies are missing, please install them with "+\
420 if product_info.get_source == "fixed":
422 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS),
425 msg = "FIXED : %s\n" % product_info.install_dir
427 if not os.path.isdir(product_info.install_dir):
428 logger.warning("The fixed path do not exixts!! Please check it : %s\n" % product_info.install_dir)
433 # if the get_source is not in [git, archive, cvs, svn, fixed, native]
434 logger.write(_("Unknown get source method \"%(get)s\" for product %(product)s") % \
435 { 'get': product_info.get_source, 'product': product_info.name }, 3, False)
436 logger.write(" ... ", 3, False)
440 def get_all_product_sources(config, products, logger):
441 '''Get all the product sources.
443 :param config Config: The global configuration
444 :param products List: The list of tuples (product name, product informations)
445 :param logger Logger: The logger instance to be used for the logging
446 :return: the tuple (number of success, dictionary product_name/success_fail)
450 # Initialize the variables that will count the fails and success
454 # Get the maximum name length in order to format the terminal display
455 max_product_name_len = 1
456 if len(products) > 0:
457 max_product_name_len = max(map(lambda l: len(l), products[0])) + 4
459 # The loop on all the products from which to get the sources
460 # DBG.write("source.get_all_product_sources config id", id(config), True)
461 for product_name, product_info in products:
462 # get product name, product informations and the directory where to put
464 if (not (src.product.product_is_fixed(product_info) or
465 src.product.product_is_native(product_info))):
466 source_dir = src.Path(product_info.source_dir)
468 source_dir = src.Path('')
471 logger.write('%s: ' % src.printcolors.printcLabel(product_name), 3)
472 logger.write(' ' * (max_product_name_len - len(product_name)), 3, False)
473 logger.write("\n", 4, False)
475 # Remove the existing source directory if
476 # the product is not in development mode
477 is_dev = src.product.product_is_dev(product_info)
478 if source_dir.exists():
479 logger.write('%s ' % src.printcolors.printc(src.OK_STATUS), 3, False)
480 msg = _("INFO : Not doing anything because the source directory already exists:\n %s\n") % source_dir
482 good_result = good_result + 1
483 # Do not get the sources and go to next product
486 # Call to the function that get the sources for one product
487 retcode = get_product_sources(config,
492 max_product_name_len,
496 if 'no_rpath' in product_info.keys():
497 if product_info.no_rpath:
498 hack_no_rpath(config, product_info, logger)
501 # Check that the sources are correctly get using the files to be tested
502 # in product information
504 check_OK, wrong_path = check_sources(product_info, logger)
506 # Print the missing file path
507 msg = _("The required file %s does not exists. " % wrong_path)
508 logger.write(src.printcolors.printcError("\nERROR: ") + msg, 3)
512 results[product_name] = retcode
514 # The case where it succeed
516 good_result = good_result + 1
518 # The case where it failed
522 if not(src.product.product_is_fixed(product_info) or
523 src.product.product_is_native(product_info)):
524 logger.write('%s\n' % src.printcolors.printc(res), 3, False)
526 return good_result, results
528 def check_sources(product_info, logger):
529 '''Check that the sources are correctly get, using the files to be tested
530 in product information
532 :param product_info Config: The configuration specific to
533 the product to be prepared
534 :return: True if the files exists (or no files to test is provided).
537 # Get the files to test if there is any
538 if ("present_files" in product_info and
539 "source" in product_info.present_files):
540 l_files_to_be_tested = product_info.present_files.source
541 for file_path in l_files_to_be_tested:
542 # The path to test is the source directory
543 # of the product joined the file path provided
544 path_to_test = os.path.join(product_info.source_dir, file_path)
545 logger.write(_("\nTesting existence of file: \n"), 5)
546 logger.write(path_to_test, 5)
547 if not os.path.exists(path_to_test):
548 return False, path_to_test
549 logger.write(src.printcolors.printcSuccess(" OK\n"), 5)
553 '''method that is called when salomeTools is called with --help option.
555 :return: The text to display for the source command description.
558 return _("The source command gets the sources of the application products "
559 "from cvs, git or an archive.\n\nexample:"
560 "\nsat source SALOME-master --products KERNEL,GUI")
562 def run(args, runner, logger):
563 '''method that is called when salomeTools is called with source parameter.
565 DBG.write("source.run()", args)
567 (options, args) = parser.parse_args(args)
569 # check that the command has been called with an application
570 src.check_config_has_application( runner.cfg )
572 # Print some informations
573 logger.write(_('Getting sources of the application %s\n') %
574 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
575 src.printcolors.print_value(logger, 'workdir',
576 runner.cfg.APPLICATION.workdir, 2)
577 logger.write("\n", 2, False)
579 # Get the products list with products informations regarding the options
580 products_infos = src.product.get_products_list(options, runner.cfg, logger)
582 # Call to the function that gets all the sources
583 good_result, results = get_all_product_sources(runner.cfg,
587 # Display the results (how much passed, how much failed, etc...)
588 status = src.OK_STATUS
591 logger.write("\n", 2, False)
592 if good_result == len(products_infos):
593 res_count = "%d / %d" % (good_result, good_result)
595 status = src.KO_STATUS
596 res_count = "%d / %d" % (good_result, len(products_infos))
598 for product in results:
599 if results[product] == 0 or results[product] is None:
600 details.append(product)
602 result = len(products_infos) - good_result
605 logger.write(_("Getting sources of the application:"), 1)
606 logger.write(" " + src.printcolors.printc(status), 1, False)
607 logger.write(" (%s)\n" % res_count, 1, False)
610 logger.write(_("Following sources haven't been get:\n"), 2)
611 logger.write(" ".join(details), 2)
612 logger.write("\n", 2, False)