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