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 log command : sat log <options>
26 parser = src.options.Options()
27 parser.add_option('m', 'module', 'list2', 'modules',
28 _('modules to get the sources. This option can be'
29 ' passed several time to get the sources of several modules.'))
30 parser.add_option('', 'no_sample', 'boolean', 'no_sample',
31 _("do not get sources from sample modules."))
33 def apply_patch(config, module_info, logger):
34 '''The method called to apply patches on a module
36 :param config Config: The global configuration
37 :param module_info Config: The configuration specific to
38 the module to be prepared
39 :param logger Logger: The logger instance to use for the display and logging
40 :return: (True if it succeed, else False, message to display)
41 :rtype: (boolean, str)
44 if not "patches" in module_info or len(module_info.patches) == 0:
45 msg = _("No patch for the %s module") % module_info.name
50 if not os.path.exists(module_info.source_dir):
51 msg = _("No sources found for the %s module\n") % module_info.name
52 logger.write(src.printcolors.printcWarning(msg), 1)
55 # At this point, there one or more patches and the source directory exists
58 # Loop on all the patches of the module
59 for patch in module_info.patches:
62 # Check the existence and apply the patch
63 if os.path.isfile(patch):
64 #patch_exe = "patch" # old patch command (now replace by patch.py)
65 patch_exe = os.path.join(config.VARS.srcDir, "patching.py")
66 patch_cmd = "python %s -p1 -- < %s" % (patch_exe, patch)
68 logger.write((" >%s\n" % patch_cmd),5)
69 res_cmd = (subprocess.call(patch_cmd,
71 cwd=module_info.source_dir,
72 stdout=logger.logTxtFile,
73 stderr=subprocess.STDOUT) == 0)
77 src.printcolors.printcError(_("Not a valid patch: %s") % patch))
82 message = (_("Apply patch %s") %
83 src.printcolors.printcHighlight(patch))
85 message = src.printcolors.printcWarning(
86 _("Failed to apply patch %s") % patch)
88 if config.USER.output_level >= 3:
89 retcode.append(" %s" % message)
91 retcode.append("%s: %s" % (module_info.name, message))
94 retcode.extend(details)
96 res = not (False in res)
98 return res, "\n".join(retcode) + "\n"
101 '''method that is called when salomeTools is called with --help option.
103 :return: The text to display for the patch command description.
106 return _("The patch command apply the patches on the sources of "
107 "the application modules if there is any")
109 def run(args, runner, logger):
110 '''method that is called when salomeTools is called with patch parameter.
113 (options, args) = parser.parse_args(args)
115 # check that the command has been called with an application
116 src.check_config_has_application( runner.cfg )
118 # Print some informations
119 logger.write('Patching sources of the application %s\n' %
120 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
122 src.printcolors.print_value(logger, 'out_dir',
123 runner.cfg.APPLICATION.out_dir, 2)
124 logger.write("\n", 2, False)
126 # Get the modules list with modules informations reagrding the options
127 modules_infos = prepare.get_modules_list(options, runner.cfg, logger)
129 # Get the maximum name length in order to format the terminal display
130 max_module_name_len = 1
131 if len(modules_infos) > 0:
132 max_module_name_len = max(map(lambda l: len(l), modules_infos[0])) + 4
134 # The loop on all the modules on which to apply the patches
136 for module_name, module_info in modules_infos:
138 logger.write('%s: ' % src.printcolors.printcLabel(module_name), 3)
139 logger.write(' ' * (max_module_name_len - len(module_name)), 3, False)
140 logger.write("\n", 4, False)
141 return_code, patch_res = apply_patch(runner.cfg, module_info, logger)
142 logger.write(patch_res, 1, False)
146 # Display the results (how much passed, how much failed, etc...)
148 logger.write("\n", 2, False)
149 if good_result == len(modules_infos):
150 status = src.OK_STATUS
151 res_count = "%d / %d" % (good_result, good_result)
153 status = src.KO_STATUS
154 res_count = "%d / %d" % (good_result, len(modules_infos))
157 logger.write("Patching sources of the application:", 1)
158 logger.write(" " + src.printcolors.printc(status), 1, False)
159 logger.write(" (%s)\n" % res_count, 1, False)
161 return len(modules_infos) - good_result