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