]> SALOME platform Git repositories - tools/sat.git/blob - commands/clean.py
Salome HOME
cd8d5f88e7dcf8dd1a21fcb17f3bda97074c064a
[tools/sat.git] / commands / clean.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 re
20 import os
21
22 import src
23
24 # Compatibility python 2/3 for input function
25 # input stays input for python 3 and input = raw_input for python 2
26 try: 
27     input = raw_input
28 except NameError: 
29     pass
30
31
32 # Define all possible option for the clean command :  sat clean <options>
33 parser = src.options.Options()
34 parser.add_option('p', 'products', 'list2', 'products',
35     _('Optional: Products to clean. This option accepts a comma separated list.'))
36 parser.add_option('s', 'sources', 'boolean', 'sources',
37     _("Optional: Clean the product source directories."))
38 parser.add_option('b', 'build', 'boolean', 'build', 
39     _("Optional: Clean the product build directories."))
40 parser.add_option('i', 'install', 'boolean', 'install', 
41     _("Optional: Clean the product install directories."))
42 parser.add_option('g', 'generated', 'boolean', 'generated', 
43     _("Optional: Clean source, build and install directories for generated products."))
44 parser.add_option('a', 'all', 'boolean', 'all', 
45     _("Optional: Clean the product source, build and install directories."))
46 parser.add_option('', 'sources_without_dev', 'boolean', 'sources_without_dev', 
47     _("Optional: do not clean the products in development mode."))
48
49
50 def get_source_directories(products_infos, without_dev):
51     """\
52     Returns the list of directory source paths corresponding 
53     to the list of product information given as input.
54     If without_dev (bool) the dev products are ignored.
55     
56     :param products_infos list: The list of (name, config) corresponding to one
57                                 product.
58     :param without_dev boolean: If True, then ignore the dev products.
59     :return: the list of source paths.
60     :rtype: list
61     """
62     l_dir_source = []
63     for __, product_info in products_infos:
64         if product_has_dir(product_info, without_dev):
65             l_dir_source.append(src.Path(product_info.source_dir))
66     return l_dir_source
67
68 def get_build_directories(products_infos):
69     """\
70     Returns the list of directory build paths corresponding to the list of 
71     product information given as input.
72     
73     :param products_infos list: The list of (name, config) corresponding to one
74                                 product.
75     :return: the list of build paths.
76     :rtype: list
77     """
78     l_dir_build = []
79     for __, product_info in products_infos:
80         if product_has_dir(product_info):
81             if "build_dir" in product_info:
82                 l_dir_build.append(src.Path(product_info.build_dir))
83     return l_dir_build
84
85 def get_install_directories(products_infos):
86     """\
87     Returns the list of directory install paths corresponding to the list of 
88     product information given as input.
89     
90     :param products_infos list: The list of (name, config) corresponding to one product.
91     :return: the list of install paths.
92     :rtype: list
93     """
94     l_dir_install = []
95     for __, product_info in products_infos:
96         if product_has_dir(product_info):
97             l_dir_install.append(src.Path(product_info.install_dir))
98     return l_dir_install
99
100 def get_generated_directories(config, products_infos):
101     """\
102     Returns the list of directories (source, build, install) corresponding to the 
103     list of products information given as input.
104     Nothing is returned for non generated products (those with...)
105     
106     :param products_infos list: The list of (name, config) corresponding to one product.
107     :return: the list of install paths.
108     :rtype: list
109     """
110     l_dir_install = []
111     for __, product_info in products_infos:
112         if not src.product.product_is_generated(product_info):
113             continue
114         workdir = config.APPLICATION.workdir
115         compo = product_info.component_name
116         generate_dir = os.path.join(workdir, "GENERATED")
117         source_dir = os.path.join(generate_dir, compo + "_SRC")
118         build_dir = os.path.join(os.path.join(workdir, "BUILD"), compo)
119         install_dir = os.path.join(os.path.join(workdir, "INSTALL"), compo)
120         l_dir_install.append(src.Path(source_dir))
121         l_dir_install.append(src.Path(build_dir))
122         l_dir_install.append(src.Path(install_dir))
123         
124     return l_dir_install
125
126
127
128 def product_has_dir(product_info, without_dev=False):
129     """\
130     Returns a boolean at True if there is a source, build and install
131     directory corresponding to the product described by product_info.
132     
133     :param products_info Config: The config corresponding to the product.
134     :return: True if there is a source, build and install directory corresponding to the product described by product_info.
135     :rtype: boolean
136     """
137     if (src.product.product_is_native(product_info) or 
138                             src.product.product_is_fixed(product_info)):
139         return False
140     if without_dev:
141         if src.product.product_is_dev(product_info):
142             return False
143     return True
144     
145 def suppress_directories(l_paths, logger):
146     """Suppress the paths given in the list in l_paths.
147     
148     :param l_paths list: The list of Path to be suppressed
149     :param logger Logger: The logger instance to use for the display and logging
150     """    
151     for path in l_paths:
152         if not path.isdir():
153             msg = _("Warning: the path %s does not "
154                     "exists (or is not a directory)\n" % path.__str__())
155             logger.write(src.printcolors.printcWarning(msg), 1)
156         else:
157             logger.write(_("Removing %s ...") % path.__str__())
158             path.rm()
159             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
160
161 def description():
162     """method called when salomeTools is called with --help option.
163     
164     :return: The text to display for the clean command description.
165     :rtype: str
166     """
167     return _("""\
168 The clean command suppress the SOURCES, BUILD or INSTALL directories of the application products.
169 Use the options to define what directories you want to suppress and to set the list of products
170
171 example:
172 >> sat clean SALOME-xx --build --install --properties is_salome_module:yes
173 """)
174   
175 def run(args, runner, logger):
176     """\
177     method called when salomeTools is called with clean parameter.
178     """
179     
180     # Parse the options
181     (options, args) = parser.parse_args(args)
182
183     # check that the command has been called with an application
184     src.check_config_has_application( runner.cfg )
185
186
187     # Get the list of products to threat
188     products_infos = src.product.get_products_list(options, runner.cfg, logger)
189
190     # Construct the list of directories to suppress
191     l_dir_to_suppress = []
192     if options.all:
193         l_dir_to_suppress += (get_source_directories(products_infos, 
194                                             options.sources_without_dev) +
195                              get_build_directories(products_infos) + 
196                              get_install_directories(products_infos) + 
197                              get_generated_directories(runner.cfg, products_infos) )
198     else:
199         if options.install:
200             l_dir_to_suppress += get_install_directories(products_infos)
201         
202         if options.build:
203             l_dir_to_suppress += get_build_directories(products_infos)
204             
205         if options.sources or options.sources_without_dev:
206             l_dir_to_suppress += get_source_directories(products_infos, 
207                                                 options.sources_without_dev)
208         if options.generated:
209             l_dir_to_suppress += get_generated_directories(runner.cfg, products_infos)
210     
211     if len(l_dir_to_suppress) == 0:
212         logger.write(src.printcolors.printcWarning(_("Nothing to suppress\n")))
213         logger.write(_("""\
214 Please specify what you want to suppress:
215 try 'sat --help clean' and 'sat clean ... --products ... --sources --build --install
216 """))
217         return
218     
219     # Check with the user if he really wants to suppress the directories
220     if not runner.options.batch:
221         logger.write(_("Remove the following directories ?\n"), 1)
222         for directory in l_dir_to_suppress:
223             logger.write("  %s\n" % directory, 1)
224         rep = input(_("Are you sure you want to continue? [Yes/No] "))
225         if rep.upper() != _("YES"):
226             return 0
227     
228     # Suppress the list of paths
229     suppress_directories(l_dir_to_suppress, logger)
230     
231     return 0