Salome HOME
continue
[tools/sat.git] / commands / prepare.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 src
20
21 # Define all possible option for log command :  sat log <options>
22 parser = src.options.Options()
23 parser.add_option('m', 'module', 'list2', 'modules',
24     _('modules to prepare. This option can be'
25     ' passed several time to prepare several modules.'))
26 parser.add_option('', 'no_sample', 'boolean', 'no_sample', 
27     _("do not prepare sample modules."))
28 parser.add_option('f', 'force', 'boolean', 'force', 
29     _("force to prepare the modules in development mode."))
30
31 def get_modules_list(options, cfg, logger):
32     '''method that gives the module list with their informations from 
33        configuration regarding the passed options.
34     
35     :param options Options: The Options instance that stores the commands 
36                             arguments
37     :param config Config: The global configuration
38     :param logger Logger: The logger instance to use for the display and logging
39     :return: The list of (module name, module_infomrmations).
40     :rtype: List
41     '''
42     # Get the modules to be prepared, regarding the options
43     if options.modules is None:
44         # No options, get all modules sources
45         modules = cfg.APPLICATION.modules
46     else:
47         # if option --modules, check that all modules of the command line
48         # are present in the application.
49         modules = options.modules
50         for m in modules:
51             if m not in cfg.APPLICATION.modules:
52                 raise src.SatException(_("Module %(module)s "
53                             "not defined in application %(application)s") %
54                 { 'module': m, 'application': cfg.VARS.application} )
55     
56     # Construct the list of tuple containing 
57     # the modules name and their definition
58     modules_infos = src.module.get_modules_infos(modules, cfg)
59
60     # if the --no_sample option is invoked, suppress the sample modules from 
61     # the list
62     if options.no_sample:
63         
64         lmodules_sample = [m for m in modules_infos if src.module.module_is_sample(m[1])]
65         
66         modules_infos = [m for m in modules_infos if m not in lmodules_sample]
67
68         if len(lmodules_sample) > 0:
69             logger.write(src.printcolors.printcWarning(_("Ignoring the following sample modules:\n")), 1)
70         for i, module in enumerate(lmodules_sample):
71             end_text = ', '
72             if i+1 == len(lmodules_sample):
73                 end_text = '\n'
74                 
75             logger.write(module[0] + end_text, 1)
76     
77     return modules_infos
78
79 def description():
80     '''method that is called when salomeTools is called with --help option.
81     
82     :return: The text to display for the prepare command description.
83     :rtype: str
84     '''
85     return _("The prepare command apply the patches on the sources of "
86              "the application modules if there is any")
87   
88 def run(args, runner, logger):
89     '''method that is called when salomeTools is called with prepare parameter.
90     '''
91     
92     # Parse the options
93     (options, args) = parser.parse_args(args)
94
95     # check that the command has been called with an application
96     src.check_config_has_application( runner.cfg )
97
98     modules_infos = get_modules_list(options, runner.cfg, logger)
99
100     # Construct the option to pass to the source command
101     args_source = runner.cfg.VARS.application + ' '
102     
103     if options.modules:
104         args_source += '--module ' + ','.join(options.modules)
105     
106     if options.no_sample:
107         args_source += ' --no_sample'
108         
109     if options.force:
110         args_source += ' --force'
111     
112     # Call the source command that gets the source
113     msg = src.printcolors.printcHeader(
114                                 _('Get the sources of the desired modules\n'))
115     logger.write(msg)
116     res_source = runner.source(args_source)
117     
118     # Construct the option to pass to the patch command
119     args_patch = args_source.replace(' --force', '')
120     
121     if ("dev_modules" in runner.cfg.APPLICATION and 
122                                 runner.cfg.APPLICATION.dev_modules is not []):
123         
124         dev_modules = runner.cfg.APPLICATION.dev_modules
125         ldev_modules = [m for m in modules_infos if m[0] in dev_modules]
126         
127         if len(ldev_modules) > 0:
128             msg = _("The patches are not applied on "
129                     "the module in development mode\n")
130             
131             logger.write()
132             
133             modules_infos = [m for m in modules_infos if m[0] not in ldev_modules]
134     
135     # Call the source command that gets the source
136     msg = src.printcolors.printcHeader(
137                     _('\nApply the patches to the sources of the modules\n'))
138     logger.write(msg)
139     res_patch = runner.patch(args_patch)
140     
141     return res_source + res_patch