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 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."))
32 def apply_patch(config, module_info, logger):
33 '''The method called to apply patches on a module
35 :param config Config: The global configuration
36 :param module_info Config: The configuration specific to
37 the module to be prepared
38 :param logger Logger: The logger instance to use for the display and logging
39 :return: (True if it succeed, else False, message to display)
40 :rtype: (boolean, str)
43 if not "patches" in module_info or len(module_info.patches) == 0:
44 msg = _("No patch for the %s module") % module_info.name
49 if not os.path.exists(module_info.source_dir):
50 msg = _("No sources found for the %s module\n") % module_info.name
51 logger.write(src.printcolors.printcWarning(msg), 1)
54 # At this point, there one or more patches and the source directory exists
57 # Loop on all the patches of the module
58 for patch in module_info.patches:
61 # Check the existence and apply the patch
62 if os.path.isfile(patch):
63 #patch_exe = "patch" # old patch command (now replace by patch.py)
64 patch_exe = os.path.join(config.VARS.srcDir, "patching.py")
65 patch_cmd = "python %s -p1 -- < %s" % (patch_exe, patch)
67 logger.write((" >%s\n" % patch_cmd),5)
68 res_cmd = (subprocess.call(patch_cmd,
70 cwd=module_info.source_dir,
71 stdout=logger.logTxtFile,
72 stderr=subprocess.STDOUT) == 0)
76 src.printcolors.printcError(_("Not a valid patch: %s") % patch))
81 message = (_("Apply patch %s") %
82 src.printcolors.printcHighlight(patch))
84 message = src.printcolors.printcWarning(
85 _("Failed to apply patch %s") % patch)
87 if config.USER.output_level >= 3:
88 retcode.append(" %s" % message)
90 retcode.append("%s: %s" % (module_info.name, message))
93 retcode.extend(details)
95 res = not (False in res)
97 return res, "\n".join(retcode) + "\n"
100 '''method that is called when salomeTools is called with --help option.
102 :return: The text to display for the patch command description.
105 return _("The patch command apply the patches on the sources of "
106 "the application modules if there is any")
108 def run(args, runner, logger):
109 '''method that is called when salomeTools is called with patch parameter.
112 (options, args) = parser.parse_args(args)
114 # check that the command has been called with an application
115 src.check_config_has_application( runner.cfg )
117 # Print some informations
118 logger.write('Patching sources of the application %s\n' %
119 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
121 src.printcolors.print_value(logger, 'out_dir',
122 runner.cfg.APPLICATION.out_dir, 2)
123 logger.write("\n", 2, False)
125 # Get the modules to be prepared, regarding the options
126 if options.modules is None:
127 # No options, get all modules sources
128 modules = runner.cfg.APPLICATION.modules
130 # if option --modules, check that all modules of the command line
131 # are present in the application.
132 modules = options.modules
134 if m not in runner.cfg.APPLICATION.modules:
135 raise src.SatException(_("Module %(module)s "
136 "not defined in application %(application)s") %
137 { 'module': m, 'application': runner.cfg.VARS.application} )
139 # Construct the list of tuple containing
140 # the modules name and their definition
141 modules_infos = src.module.get_modules_infos(modules, runner.cfg)
143 # if the --no_sample option is invoked, suppress the sample modules from
145 if options.no_sample:
146 modules_infos = filter(lambda l: not src.module.module_is_sample(l[1]),
149 # Get the maximum name length in order to format the terminal display
150 max_module_name_len = 1
151 if len(modules_infos) > 0:
152 max_module_name_len = max(map(lambda l: len(l), modules_infos[0])) + 4
154 # The loop on all the modules on which to apply the patches
156 for module_name, module_info in modules_infos:
158 logger.write('%s: ' % src.printcolors.printcLabel(module_name), 3)
159 logger.write(' ' * (max_module_name_len - len(module_name)), 3, False)
160 logger.write("\n", 4, False)
161 return_code, patch_res = apply_patch(runner.cfg, module_info, logger)
162 logger.write(patch_res, 1, False)
166 # Display the results (how much passed, how much failed, etc...)
168 logger.write("\n", 2, False)
169 if good_result == len(modules_infos):
170 status = src.OK_STATUS
171 res_count = "%d / %d" % (good_result, good_result)
173 status = src.KO_STATUS
174 res_count = "%d / %d" % (good_result, len(modules))
177 logger.write("Patching sources of the application:", 1)
178 logger.write(" " + src.printcolors.printc(status), 1, False)
179 logger.write(" (%s)\n" % res_count, 1, False)
181 return len(modules_infos) - good_result