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', 'products', 'list2', 'products',
28 _('Optional: products to get the sources. This option can be'
29 ' passed several time to get the sources of several products.'))
31 def apply_patch(config, product_info, max_product_name_len, logger):
32 '''The method called to apply patches on a product
34 :param config Config: The global configuration
35 :param product_info Config: The configuration specific to
36 the product to be patched
37 :param logger Logger: The logger instance to use for the display and logging
38 :return: (True if it succeed, else False, message to display)
39 :rtype: (boolean, str)
42 if not "patches" in product_info or len(product_info.patches) == 0:
44 logger.write('%s: ' % src.printcolors.printcLabel(product_info.name), 4)
45 logger.write(' ' * (max_product_name_len - len(product_info.name)), 4, False)
46 logger.write("\n", 4, False)
47 msg = _("No patch for the %s product") % product_info.name
53 logger.write('%s: ' % src.printcolors.printcLabel(product_info.name), 3)
54 logger.write(' ' * (max_product_name_len - len(product_info.name)), 3, False)
55 logger.write("\n", 4, False)
57 if not os.path.exists(product_info.source_dir):
58 msg = _("No sources found for the %s product\n") % product_info.name
59 logger.write(src.printcolors.printcWarning(msg), 1)
62 # At this point, there one or more patches and the source directory exists
65 # Loop on all the patches of the product
66 for patch in product_info.patches:
69 # Check the existence and apply the patch
70 if os.path.isfile(patch):
71 #patch_exe = "patch" # old patch command (now replace by patching.py)
72 patch_exe = os.path.join(config.VARS.srcDir, "patching.py")
73 patch_cmd = "python %s -p1 -- < %s" % (patch_exe, patch)
75 # Write the command in the terminal if verbose level is at 5
76 logger.write((" >%s\n" % patch_cmd),5)
78 # Write the command in the log file (can be seen using 'sat log')
79 logger.logTxtFile.write("\n >%s\n" % patch_cmd)
80 logger.logTxtFile.flush()
83 res_cmd = (subprocess.call(patch_cmd,
85 cwd=product_info.source_dir,
86 stdout=logger.logTxtFile,
87 stderr=subprocess.STDOUT) == 0)
91 src.printcolors.printcError(_("Not a valid patch: %s") % patch))
96 message = (_("Apply patch %s") %
97 src.printcolors.printcHighlight(patch))
99 message = src.printcolors.printcWarning(
100 _("Failed to apply patch %s") % patch)
102 if config.USER.output_verbose_level >= 3:
103 retcode.append(" %s" % message)
105 retcode.append("%s: %s" % (product_info.name, message))
108 retcode.extend(details)
110 res = not (False in res)
112 return res, "\n".join(retcode) + "\n"
115 '''method that is called when salomeTools is called with --help option.
117 :return: The text to display for the patch command description.
120 return _("The patch command apply the patches on the sources of "
121 "the application products if there is any.\n\nexample:\nsat "
122 "patch SALOME-master --products qt,boost")
124 def run(args, runner, logger):
125 '''method that is called when salomeTools is called with patch parameter.
128 (options, args) = parser.parse_args(args)
130 # check that the command has been called with an application
131 src.check_config_has_application( runner.cfg )
133 # Print some informations
134 logger.write('Patching sources of the application %s\n' %
135 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
137 src.printcolors.print_value(logger, 'workdir',
138 runner.cfg.APPLICATION.workdir, 2)
139 logger.write("\n", 2, False)
141 # Get the products list with products informations regarding the options
142 products_infos = prepare.get_products_list(options, runner.cfg, logger)
144 # Get the maximum name length in order to format the terminal display
145 max_product_name_len = 1
146 if len(products_infos) > 0:
147 max_product_name_len = max(map(lambda l: len(l), products_infos[0])) + 4
149 # The loop on all the products on which to apply the patches
151 for __, product_info in products_infos:
153 return_code, patch_res = apply_patch(runner.cfg,
155 max_product_name_len,
157 logger.write(patch_res, 1, False)
161 # Display the results (how much passed, how much failed, etc...)
163 logger.write("\n", 2, False)
164 if good_result == len(products_infos):
165 status = src.OK_STATUS
166 res_count = "%d / %d" % (good_result, good_result)
168 status = src.KO_STATUS
169 res_count = "%d / %d" % (good_result, len(products_infos))
172 logger.write("Patching sources of the application:", 1)
173 logger.write(" " + src.printcolors.printc(status), 1, False)
174 logger.write(" (%s)\n" % res_count, 1, False)
176 return len(products_infos) - good_result