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