Salome HOME
Pour résoudre un conflit entre les branches master et Windows.
[tools/sat.git] / commands / patch.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2012  CEA/DEN
4 #
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.
9 #
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.
14 #
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
18
19 import os
20 import subprocess
21
22 import src
23 import prepare
24
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.'))
30
31 def apply_patch(config, product_info, max_product_name_len, logger):
32     '''The method called to apply patches on a product
33
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)
40     '''
41
42     # if the product is native, do not apply patch
43     if src.product.product_is_native(product_info):
44         # display and log
45         logger.write('%s: ' % src.printcolors.printcLabel(product_info.name), 4)
46         logger.write(' ' * (max_product_name_len - len(product_info.name)), 4, False)
47         logger.write("\n", 4, False)
48         msg = _("The %s product is native. Do not apply "
49                 "any patch.") % product_info.name
50         logger.write(msg, 4)
51         logger.write("\n", 4)
52         return True, ""       
53
54     if not "patches" in product_info or len(product_info.patches) == 0:
55         # display and log
56         logger.write('%s: ' % src.printcolors.printcLabel(product_info.name), 4)
57         logger.write(' ' * (max_product_name_len - len(product_info.name)), 4, False)
58         logger.write("\n", 4, False)
59         msg = _("No patch for the %s product") % product_info.name
60         logger.write(msg, 4)
61         logger.write("\n", 4)
62         return True, ""
63     else:
64         # display and log
65         logger.write('%s: ' % src.printcolors.printcLabel(product_info.name), 3)
66         logger.write(' ' * (max_product_name_len - len(product_info.name)), 3, False)
67         logger.write("\n", 4, False)
68
69     if not os.path.exists(product_info.source_dir):
70         msg = _("No sources found for the %s product\n") % product_info.name
71         logger.write(src.printcolors.printcWarning(msg), 1)
72         return False, ""
73
74     # At this point, there one or more patches and the source directory exists
75     retcode = []
76     res = []
77     # Loop on all the patches of the product
78     for patch in product_info.patches:
79         details = []
80         
81         # Check the existence and apply the patch
82         if os.path.isfile(patch):
83             #patch_exe = "patch" # old patch command (now replace by patching.py)
84             patch_exe = os.path.join(config.VARS.srcDir, "patching.py")
85             patch_cmd = "python %s -p1 -- < %s" % (patch_exe, patch)
86
87             # Write the command in the terminal if verbose level is at 5
88             logger.write(("    >%s\n" % patch_cmd),5)
89             
90             # Write the command in the log file (can be seen using 'sat log')
91             logger.logTxtFile.write("\n    >%s\n" % patch_cmd)
92             logger.logTxtFile.flush()
93             
94             # Call the command
95             res_cmd = (subprocess.call(patch_cmd, 
96                                    shell=True, 
97                                    cwd=product_info.source_dir,
98                                    stdout=logger.logTxtFile, 
99                                    stderr=subprocess.STDOUT) == 0)        
100         else:
101             res_cmd = False
102             details.append("  " + 
103                 src.printcolors.printcError(_("Not a valid patch: %s") % patch))
104
105         res.append(res_cmd)
106         
107         if res_cmd:
108             message = (_("Apply patch %s") % 
109                        src.printcolors.printcHighlight(patch))
110         else:
111             message = src.printcolors.printcWarning(
112                                         _("Failed to apply patch %s") % patch)
113
114         if config.USER.output_verbose_level >= 3:
115             retcode.append("  %s" % message)
116         else:
117             retcode.append("%s: %s" % (product_info.name, message))
118         
119         if len(details) > 0:
120             retcode.extend(details)
121
122     res = not (False in res)
123     
124     return res, "\n".join(retcode) + "\n"
125
126 def description():
127     '''method that is called when salomeTools is called with --help option.
128     
129     :return: The text to display for the patch command description.
130     :rtype: str
131     '''
132     return _("The patch command apply the patches on the sources of "
133              "the application products if there is any.\n\nexample:\nsat "
134              "patch SALOME-master --products qt,boost")
135   
136 def run(args, runner, logger):
137     '''method that is called when salomeTools is called with patch parameter.
138     '''
139     # Parse the options
140     (options, args) = parser.parse_args(args)
141     
142     # check that the command has been called with an application
143     src.check_config_has_application( runner.cfg )
144
145     # Print some informations
146     logger.write('Patching sources of the application %s\n' % 
147                 src.printcolors.printcLabel(runner.cfg.VARS.application), 1)
148
149     src.printcolors.print_value(logger, 'workdir', 
150                                 runner.cfg.APPLICATION.workdir, 2)
151     logger.write("\n", 2, False)
152
153     # Get the products list with products informations regarding the options
154     products_infos = prepare.get_products_list(options, runner.cfg, logger)
155     
156     # Get the maximum name length in order to format the terminal display
157     max_product_name_len = 1
158     if len(products_infos) > 0:
159         max_product_name_len = max(map(lambda l: len(l), products_infos[0])) + 4
160     
161     # The loop on all the products on which to apply the patches
162     good_result = 0
163     for __, product_info in products_infos:
164         # Apply the patch
165         return_code, patch_res = apply_patch(runner.cfg,
166                                              product_info,
167                                              max_product_name_len,
168                                              logger)
169         logger.write(patch_res, 1, False)
170         if return_code:
171             good_result += 1
172     
173     # Display the results (how much passed, how much failed, etc...)
174
175     logger.write("\n", 2, False)
176     if good_result == len(products_infos):
177         status = src.OK_STATUS
178         res_count = "%d / %d" % (good_result, good_result)
179     else:
180         status = src.KO_STATUS
181         res_count = "%d / %d" % (good_result, len(products_infos))
182     
183     # write results
184     logger.write("Patching sources of the application:", 1)
185     logger.write(" " + src.printcolors.printc(status), 1, False)
186     logger.write(" (%s)\n" % res_count, 1, False)
187     
188     return len(products_infos) - good_result