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()
36 "Optional: products from which to get the sources. This option accepts a comma separated list."
41 def update_source_for_dev(config, product_info, source_dir, logger, pad):
42 """The method called if the product is in development mode
44 :param config Config: The global configuration
45 :param product_info Config: The configuration specific to
46 the product to be prepared
47 :param source_dir Path: The Path instance corresponding to the
48 directory where to put the sources
49 :param logger Logger: The logger instance to use for the display and logging
50 :param pad int: The gap to apply for the terminal display
51 :return: True if it succeed, else False
55 # Call the function corresponding to get the sources with True checkout
56 retcode = update_product_sources(
57 config, product_info, True, source_dir, logger, pad, checkout=True
59 logger.write("\n", 3, False)
60 # +2 because product name is followed by ': '
61 logger.write(" " * (pad + 2), 3, False)
64 "dev: %s ... " % src.printcolors.printcInfo(product_info.source_dir), 3, False
71 def update_source_from_git(
72 config, product_info, source_dir, logger, pad, is_dev=False, environ=None
74 """The method called if the product is to be get in git mode
76 :param product_info Config: The configuration specific to
77 the product to be prepared
78 :param source_dir Path: The Path instance corresponding to the
79 directory where to put the sources
80 :param logger Logger: The logger instance to use for the display and logging
81 :param pad int: The gap to apply for the terminal display
82 :param is_dev boolean: True if the product is in development mode
83 :param environ src.environment.Environ: The environment to source when
85 :return: True if it succeed, else False
93 "APPLICATION" in config
94 and "properties" in config.APPLICATION
95 and "repo_dev" in config.APPLICATION.properties
96 and config.APPLICATION.properties.repo_dev == "yes"
100 # Get the repository address.
101 # If the application has the repo_dev property
102 # Or if the product is in dev mode
103 # Then we use repo_dev if the key exists
104 if (is_dev or use_repo_dev) and "repo_dev" in product_info.git_info:
105 coflag = src.printcolors.printcHighlight(coflag.upper())
106 repo_git = product_info.git_info.repo_dev
108 repo_git = product_info.git_info.repo
110 # Display informations
111 logger.write("%s:%s" % (coflag, src.printcolors.printcInfo(repo_git)), 3, False)
112 logger.write(" " * (pad + 50 - len(repo_git)), 3, False)
114 " tag:%s" % src.printcolors.printcInfo(product_info.git_info.tag), 3, False
116 logger.write(" %s. " % ("." * (10 - len(product_info.git_info.tag))), 3, False)
118 logger.write("\n", 5, False)
121 if "git_options" in product_info.git_info:
122 git_options = product_info.git_info.git_options
124 # Call the system function that do the checkout in git mode
125 retcode = src.system.git_checkout(
126 product_info.git_info.tag,
135 Error, %(repo)s could not checkout %(branch)s. Please check you don't
136 have uncommited work and branch or tag %(branch)s exists."""
137 % {"repo": repo_git, "branch": product_info.git_info.tag}
141 # Call the system function that do the checkout in git mode
142 retcode = src.system.git_pull(
144 product_info.git_info.tag,
153 Error, %(repo)s could not pull branch %(branch)s. Please check that your branch
154 has not diverged from repo."""
155 % {"repo": repo_git, "branch": product_info.git_info.tag}
161 def get_source_from_archive(config, product_info, source_dir, logger):
162 """The method called if the product is to be get in archive mode
164 :param config Config: The global configuration
165 :param product_info Config: The configuration specific to
166 the product to be prepared
167 :param source_dir Path: The Path instance corresponding to the
168 directory where to put the sources
169 :param logger Logger: The logger instance to use for the display and logging
170 :return: True if it succeed, else False
174 # check if pip should be used : pip mode id activated if the application
175 # and product have pip property
176 if src.appli_test_property(
178 ) and src.product.product_test_property(product_info, "pip", "yes"):
179 pip_msg = "PIP : do nothing, product will be downloaded later at compile time "
180 logger.write(pip_msg, 3)
183 # check archive exists
184 if not os.path.exists(product_info.archive_info.archive_name):
185 # The archive is not found on local file system (ARCHIVEPATH)
188 "\n The archive is not found on local file system, we try ftp\n", 3
190 ret = src.find_file_in_ftppath(
191 product_info.archive_info.archive_name,
192 config.PATHS.ARCHIVEFTP,
193 config.LOCAL.archive_dir,
197 # archive was found on ftp and stored in ret
198 product_info.archive_info.archive_name = ret
200 raise src.SatException(
201 _("Archive not found in ARCHIVEPATH, nor on ARCHIVEFTP: '%s'")
202 % product_info.archive_info.archive_name
207 % src.printcolors.printcInfo(product_info.archive_info.archive_name),
212 # Call the system function that do the extraction in archive mode
213 retcode, NameExtractedDirectory = src.system.archive_extract(
214 product_info.archive_info.archive_name, source_dir.dir(), logger
217 # Rename the source directory if
218 # it does not match with product_info.source_dir
219 if NameExtractedDirectory.replace("/", "") != os.path.basename(
220 product_info.source_dir
224 os.path.dirname(product_info.source_dir), NameExtractedDirectory
226 product_info.source_dir,
232 def get_source_from_dir(product_info, source_dir, logger):
233 if "dir_info" not in product_info:
235 "Error: you must put a dir_info section"
236 " in the file %s.pyconf" % product_info.name
238 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
241 if "dir" not in product_info.dir_info:
243 "Error: you must put a dir in the dir_info section"
244 " in the file %s.pyconf" % product_info.name
246 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
249 # check that source exists
250 if not os.path.exists(product_info.dir_info.dir):
252 "Error: the dir %s defined in the file"
253 " %s.pyconf does not exists"
254 % (product_info.dir_info.dir, product_info.name)
256 logger.write("\n%s\n" % src.printcolors.printcError(msg), 1)
260 "DIR: %s ... " % src.printcolors.printcInfo(product_info.dir_info.dir), 3
264 retcode = src.Path(product_info.dir_info.dir).copy(source_dir)
269 def get_source_from_cvs(
270 user, product_info, source_dir, checkout, logger, pad, environ=None
272 """The method called if the product is to be get in cvs mode
274 :param user str: The user to use in for the cvs command
275 :param product_info Config: The configuration specific to
276 the product to be prepared
277 :param source_dir Path: The Path instance corresponding to the
278 directory where to put the sources
279 :param checkout boolean: If True, get the source in checkout mode
280 :param logger Logger: The logger instance to use for the display and logging
281 :param pad int: The gap to apply for the terminal display
282 :param environ src.environment.Environ: The environment to source when
284 :return: True if it succeed, else False
287 # Get the protocol to use in the command
288 if "protocol" in product_info.cvs_info:
289 protocol = product_info.cvs_info.protocol
293 # Construct the line to display
294 if "protocol" in product_info.cvs_info:
295 cvs_line = "%s:%s@%s:%s" % (
298 product_info.cvs_info.server,
299 product_info.cvs_info.product_base,
302 cvs_line = "%s / %s" % (
303 product_info.cvs_info.server,
304 product_info.cvs_info.product_base,
309 coflag = src.printcolors.printcHighlight(coflag.upper())
311 logger.write("%s:%s" % (coflag, src.printcolors.printcInfo(cvs_line)), 3, False)
312 logger.write(" " * (pad + 50 - len(cvs_line)), 3, False)
314 " src:%s" % src.printcolors.printcInfo(product_info.cvs_info.source), 3, False
316 logger.write(" " * (pad + 1 - len(product_info.cvs_info.source)), 3, False)
318 " tag:%s" % src.printcolors.printcInfo(product_info.cvs_info.tag), 3, False
320 # at least one '.' is visible
321 logger.write(" %s. " % ("." * (10 - len(product_info.cvs_info.tag))), 3, False)
323 logger.write("\n", 5, False)
325 # Call the system function that do the extraction in cvs mode
326 retcode = src.system.cvs_extract(
329 product_info.cvs_info.server,
330 product_info.cvs_info.product_base,
331 product_info.cvs_info.tag,
332 product_info.cvs_info.source,
341 def get_source_from_svn(user, product_info, source_dir, checkout, logger, environ=None):
342 """The method called if the product is to be get in svn mode
344 :param user str: The user to use in for the svn command
345 :param product_info Config: The configuration specific to
346 the product to be prepared
347 :param source_dir Path: The Path instance corresponding to the
348 directory where to put the sources
349 :param checkout boolean: If True, get the source in checkout mode
350 :param logger Logger: The logger instance to use for the display and logging
351 :param environ src.environment.Environ: The environment to source when
353 :return: True if it succeed, else False
358 coflag = src.printcolors.printcHighlight(coflag.upper())
361 "%s:%s ... " % (coflag, src.printcolors.printcInfo(product_info.svn_info.repo)),
366 logger.write("\n", 5, False)
367 # Call the system function that do the extraction in svn mode
368 retcode = src.system.svn_extract(
370 product_info.svn_info.repo,
371 product_info.svn_info.tag,
380 def update_product_sources(
381 config, product_info, is_dev, source_dir, logger, pad, checkout=False
383 """Get the product sources.
385 :param config Config: The global configuration
386 :param product_info Config: The configuration specific to
387 the product to be prepared
388 :param is_dev boolean: True if the product is in development mode
389 :param source_dir Path: The Path instance corresponding to the
390 directory where to put the sources
391 :param logger Logger: The logger instance to use for the display and logging
392 :param pad int: The gap to apply for the terminal display
393 :param checkout boolean: If True, get the source in checkout mode
394 :return: True if it succeed, else False
398 # Get the application environment
399 logger.write(_("Set the application environment\n"), 5)
400 env_appli = src.environment.SalomeEnviron(
401 config, src.environment.Environ(dict(os.environ))
403 env_appli.set_application_env(logger)
405 # Call the right function to get sources regarding the product settings
406 if not checkout and is_dev:
407 return get_source_for_dev(config, product_info, source_dir, logger, pad)
409 if product_info.get_source == "git":
410 return update_source_from_git(
411 config, product_info, source_dir, logger, pad, is_dev, env_appli
414 if product_info.get_source == "archive":
415 return get_source_from_archive(config, product_info, source_dir, logger)
417 if product_info.get_source == "dir":
418 return get_source_from_dir(product_info, source_dir, logger)
420 if product_info.get_source == "cvs":
421 cvs_user = config.USER.cvs_user
422 return get_source_from_cvs(
423 cvs_user, product_info, source_dir, checkout, logger, pad, env_appli
426 if product_info.get_source == "svn":
427 svn_user = config.USER.svn_user
428 return get_source_from_svn(
429 svn_user, product_info, source_dir, checkout, logger, env_appli
432 if product_info.get_source == "native":
433 # for native products we check the corresponding system packages are installed
434 logger.write("Native : Checking system packages are installed\n", 3)
435 check_cmd = src.system.get_pkg_check_cmd(
436 config.VARS.dist_name
437 ) # (either rmp or apt)
438 run_pkg, build_pkg = src.product.check_system_dep(
439 config.VARS.dist, check_cmd, product_info
443 logger.write(" - " + pkg + " : " + run_pkg[pkg] + "\n", 1)
444 if "KO" in run_pkg[pkg]:
446 for pkg in build_pkg:
447 logger.write(" - " + pkg + " : " + build_pkg[pkg] + "\n", 1)
448 if "KO" in build_pkg[pkg]:
452 "some system dependencies are missing, please install them with "
457 if product_info.get_source == "fixed":
459 logger.write("%s " % src.printcolors.printc(src.OK_STATUS), 3, False)
460 msg = "FIXED : %s\n" % product_info.install_dir
462 if not os.path.isdir(product_info.install_dir):
464 "The fixed path do not exixts!! Please check it : %s\n"
465 % product_info.install_dir
471 # if the get_source is not in [git, archive, cvs, svn, fixed, native]
473 _('Unknown get source method "%(get)s" for product %(product)s')
474 % {"get": product_info.get_source, "product": product_info.name},
478 logger.write(" ... ", 3, False)
483 def update_all_product_sources(config, products, logger):
484 """Update/get all the product sources.
486 :param config Config: The global configuration
487 :param products List: The list of tuples (product name, product informations)
488 :param logger Logger: The logger instance to be used for the logging
489 :return: the tuple (number of success, dictionary product_name/success_fail)
493 # Initialize the variables that will count the fails and success
497 # Get the maximum name length in order to format the terminal display
498 max_product_name_len = 1
499 if len(products) > 0:
500 max_product_name_len = max(map(lambda l: len(l), products[0])) + 4
502 # The loop on all the products from which to get the sources
503 # DBG.write("source.get_all_product_sources config id", id(config), True)
504 for product_name, product_info in products:
505 # get product name, product informations and the directory where to put
508 src.product.product_is_fixed(product_info)
509 or src.product.product_is_native(product_info)
511 source_dir = src.Path(product_info.source_dir)
513 source_dir = src.Path("")
516 logger.write("%s: " % src.printcolors.printcLabel(product_name), 3)
517 logger.write(" " * (max_product_name_len - len(product_name)), 3, False)
518 logger.write("\n", 4, False)
520 # TODO: check that there is no pb on not .git tracked products
521 # if source_dir.exists():
522 # logger.write("%s " % src.printcolors.printc(src.OK_STATUS), 3, False)
525 # "INFO : Not doing anything because the source directory already exists:\n %s\n"
529 # logger.write(msg, 3)
530 # good_result = good_result + 1
531 # # Do not get the sources and go to next product
534 is_dev = src.product.product_is_dev(product_info)
535 # Call to the function that get the sources for one product
536 retcode = update_product_sources(
542 max_product_name_len,
546 # Check that the sources are correctly get using the files to be tested
547 # in product information
549 check_OK, wrong_path = check_sources(product_info, logger)
551 # Print the missing file path
552 msg = _("The required file %s does not exists. " % wrong_path)
553 logger.write(src.printcolors.printcError("\nERROR: ") + msg, 3)
557 results[product_name] = retcode
559 # The case where it succeed
561 good_result = good_result + 1
563 # The case where it failed
568 src.product.product_is_fixed(product_info)
569 or src.product.product_is_native(product_info)
571 logger.write("%s\n" % src.printcolors.printc(res), 3, False)
573 return good_result, results
576 def check_sources(product_info, logger):
577 """Check that the sources are correctly get, using the files to be tested
578 in product information
580 :param product_info Config: The configuration specific to
581 the product to be prepared
582 :return: True if the files exists (or no files to test is provided).
585 # Get the files to test if there is any
586 if "present_files" in product_info and "source" in product_info.present_files:
587 l_files_to_be_tested = product_info.present_files.source
588 for file_path in l_files_to_be_tested:
589 # The path to test is the source directory
590 # of the product joined the file path provided
591 path_to_test = os.path.join(product_info.source_dir, file_path)
592 logger.write(_("\nTesting existence of file: \n"), 5)
593 logger.write(path_to_test, 5)
594 if not os.path.exists(path_to_test):
595 return False, path_to_test
596 logger.write(src.printcolors.printcSuccess(" OK\n"), 5)
601 """method that is called when salomeTools is called with --help option.
603 :return: The text to display for the source command description.
607 "The source command gets the sources of the application products "
608 "from cvs, git or an archive.\n\nexample:"
609 "\nsat source SALOME-master --products KERNEL,GUI"
613 def run(args, runner, logger):
614 """method that is called when salomeTools is called with source parameter."""
615 DBG.write("source.run()", args)
617 (options, args) = parser.parse_args(args)
619 # check that the command has been called with an application
620 src.check_config_has_application(runner.cfg)
622 # Print some informations
624 _("Getting sources of the application %s\n")
625 % src.printcolors.printcLabel(runner.cfg.VARS.application),
628 src.printcolors.print_value(logger, "workdir", runner.cfg.APPLICATION.workdir, 2)
629 logger.write("\n", 2, False)
631 # Get the products list with products informations regarding the options
632 products_infos = src.product.get_products_list(options, runner.cfg, logger)
634 # Call to the function that gets all the sources
635 good_result, results = update_all_product_sources(runner.cfg, products_infos, logger)
637 # Display the results (how much passed, how much failed, etc...)
638 status = src.OK_STATUS
641 logger.write("\n", 2, False)
642 if good_result == len(products_infos):
643 res_count = "%d / %d" % (good_result, good_result)
645 status = src.KO_STATUS
646 res_count = "%d / %d" % (good_result, len(products_infos))
648 for product in results:
649 if results[product] == 0 or results[product] is None:
650 details.append(product)
652 result = len(products_infos) - good_result
655 logger.write(_("Getting sources of the application:"), 1)
656 logger.write(" " + src.printcolors.printc(status), 1, False)
657 logger.write(" (%s)\n" % res_count, 1, False)
660 logger.write(_("Following sources haven't been get:\n"), 2)
661 logger.write(" ".join(details), 2)
662 logger.write("\n", 2, False)