Salome HOME
style: black format
[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 os
20 import src
21
22 # Compatibility python 2/3 for input function
23 # input stays input for python 3 and input = raw_input for python 2
24 try:
25     input = raw_input
26 except NameError:
27     pass
28
29
30 # Define all possible option for the clean command :  sat clean <options>
31 parser = src.options.Options()
32 parser.add_option(
33     "p",
34     "products",
35     "list2",
36     "products",
37     _("Optional: Products to clean. This option accepts a comma separated list."),
38 )
39 parser.add_option(
40     "s",
41     "sources",
42     "boolean",
43     "sources",
44     _("Optional: Clean the product source directories."),
45 )
46 parser.add_option(
47     "b",
48     "build",
49     "boolean",
50     "build",
51     _("Optional: Clean the product build directories."),
52 )
53 parser.add_option(
54     "i",
55     "install",
56     "boolean",
57     "install",
58     _("Optional: Clean the product install directories."),
59 )
60 parser.add_option(
61     "g",
62     "generated",
63     "boolean",
64     "generated",
65     _("Optional: Clean source, build and install directories for generated products."),
66 )
67 parser.add_option(
68     "",
69     "package",
70     "boolean",
71     "package",
72     _("Optional: Clean packages produced by sat package command."),
73 )
74 parser.add_option(
75     "a",
76     "all",
77     "boolean",
78     "all",
79     _("Optional: Clean the product source, build and install directories."),
80 )
81 parser.add_option(
82     "",
83     "sources_without_dev",
84     "boolean",
85     "sources_without_dev",
86     _("Optional: do not clean the products in development mode."),
87 )
88
89
90 def get_source_directories(config, products_infos, without_dev):
91     """\
92     Returns the list of directory source paths corresponding 
93     to the list of product information given as input.
94     If without_dev (bool) the dev products are ignored.
95     
96     :param products_infos list: The list of (name, config) corresponding to one
97                                 product.
98     :param without_dev boolean: If True, then ignore the dev products.
99     :return: the list of source paths.
100     :rtype: list
101     """
102     l_dir_source = []
103     for __, product_info in products_infos:
104         if product_has_dir(product_info, without_dev):
105             # we do not clean source dirs of pip products when pip is activated
106             if not (
107                 src.appli_test_property(config, "pip", "yes")
108                 and src.product.product_test_property(product_info, "pip", "yes")
109             ):
110                 l_dir_source.append(src.Path(product_info.source_dir))
111     return l_dir_source
112
113
114 def get_build_directories(products_infos):
115     """\
116     Returns the list of directory build paths corresponding to the list of 
117     product information given as input.
118     
119     :param products_infos list: The list of (name, config) corresponding to one
120                                 product.
121     :return: the list of build paths.
122     :rtype: list
123     """
124     l_dir_build = []
125     for __, product_info in products_infos:
126         if product_has_dir(product_info):
127             if "build_dir" in product_info:
128                 l_dir_build.append(src.Path(product_info.build_dir))
129     return l_dir_build
130
131
132 def get_install_directories(config, products_infos):
133     """\
134     Returns the list of directory install paths corresponding to the list of 
135     product information given as input.
136     
137     :param products_infos list: The list of (name, config) corresponding to one product.
138     :return: the list of install paths.
139     :rtype: list
140     """
141     l_dir_install = []
142     for __, product_info in products_infos:
143         if product_has_dir(product_info):
144             # we do not clean pip products installed in python install dir
145             if not (
146                 src.appli_test_property(config, "pip", "yes")
147                 and src.product.product_test_property(product_info, "pip", "yes")
148                 and src.appli_test_property(config, "pip_install_dir", "python")
149             ):
150                 l_dir_install.append(src.Path(product_info.install_dir))
151     return l_dir_install
152
153
154 def get_package_directory(config):
155     """\
156     Returns the package directory name corresponding to the sat package command
157     
158     :param config Config: The global configuration
159     :return: a list containing the PACKAGE full path.
160     :rtype: list
161     """
162     return [src.Path(os.path.join(config.APPLICATION.workdir, "PACKAGE"))]
163
164
165 def get_generated_directories(config, products_infos):
166     """\
167     Returns the list of directories (source, build, install) corresponding to the 
168     list of products information given as input.
169     Nothing is returned for non generated products (those with...)
170     
171     :param products_infos list: The list of (name, config) corresponding to one product.
172     :return: the list of install paths.
173     :rtype: list
174     """
175     l_dir_install = []
176     for __, product_info in products_infos:
177         if not src.product.product_is_generated(product_info):
178             continue
179         workdir = config.APPLICATION.workdir
180         compo = product_info.component_name
181         generate_dir = os.path.join(workdir, "GENERATED")
182         source_dir = os.path.join(generate_dir, compo + "_SRC")
183         build_dir = os.path.join(os.path.join(workdir, "BUILD"), compo)
184         install_dir = os.path.join(workdir, config.INTERNAL.config.install_dir, compo)
185         l_dir_install.append(src.Path(source_dir))
186         l_dir_install.append(src.Path(build_dir))
187         l_dir_install.append(src.Path(install_dir))
188
189     return l_dir_install
190
191
192 def product_has_dir(product_info, without_dev=False):
193     """\
194     Returns a boolean at True if there is a source, build and install
195     directory corresponding to the product described by product_info.
196     
197     :param products_info Config: The config corresponding to the product.
198     :return: True if there is a source, build and install directory corresponding to the product described by product_info.
199     :rtype: boolean
200     """
201     if src.product.product_is_native(product_info) or src.product.product_is_fixed(
202         product_info
203     ):
204         return False
205     if without_dev:
206         if src.product.product_is_dev(product_info):
207             return False
208     return True
209
210
211 def suppress_directories(l_paths, logger):
212     """Suppress the paths given in the list in l_paths.
213
214     :param l_paths list: The list of Path to be suppressed
215     :param logger Logger: The logger instance to use for the display and logging
216     """
217     for path in l_paths:
218         if not path.isdir():
219             msg = _(
220                 "Warning: the path %s does not "
221                 "exists (or is not a directory)\n" % path.__str__()
222             )
223             logger.write(src.printcolors.printcWarning(msg), 1)
224         else:
225             logger.write(_("Removing %s ...") % path.__str__())
226             path.rm()
227             logger.write("%s\n" % src.printcolors.printc(src.OK_STATUS), 3)
228
229
230 def description():
231     """method called when salomeTools is called with --help option.
232
233     :return: The text to display for the clean command description.
234     :rtype: str
235     """
236     return _(
237         """\
238 The clean command suppress the SOURCES, BUILD or INSTALL directories of the application products.
239 Use the options to define what directories you want to suppress and to set the list of products
240
241 example:
242 >> sat clean SALOME-xx --build --install --properties is_SALOME_module:yes
243 """
244     )
245
246
247 def run(args, runner, logger):
248     """\
249     method called when salomeTools is called with clean parameter.
250     """
251
252     # Parse the options
253     (options, args) = parser.parse_args(args)
254
255     # check that the command has been called with an application
256     src.check_config_has_application(runner.cfg)
257
258     # Get the list of products to threat
259     products_infos = src.product.get_products_list(options, runner.cfg, logger)
260
261     # Construct the list of directories to suppress
262     l_dir_to_suppress = []
263     if options.all:
264         l_dir_to_suppress += (
265             get_source_directories(
266                 runner.cfg, products_infos, options.sources_without_dev
267             )
268             + get_build_directories(products_infos)
269             + get_install_directories(runner.cfg, products_infos)
270             + get_generated_directories(runner.cfg, products_infos)
271             + get_package_directory(runner.cfg)
272         )
273     else:
274         if options.install:
275             l_dir_to_suppress += get_install_directories(runner.cfg, products_infos)
276
277         if options.build:
278             l_dir_to_suppress += get_build_directories(products_infos)
279
280         if options.sources or options.sources_without_dev:
281             l_dir_to_suppress += get_source_directories(
282                 runner.cfg, products_infos, options.sources_without_dev
283             )
284         if options.generated:
285             l_dir_to_suppress += get_generated_directories(runner.cfg, products_infos)
286
287         if options.package:
288             l_dir_to_suppress += get_package_directory(runner.cfg)
289
290     if len(l_dir_to_suppress) == 0:
291         logger.write(src.printcolors.printcWarning(_("Nothing to suppress\n")))
292         logger.write(
293             _(
294                 """\
295 Please specify what you want to suppress:
296 try 'sat --help clean' and 'sat clean ... --products ... --sources --build --install
297 """
298             )
299         )
300         return
301
302     # Check with the user if he really wants to suppress the directories
303     if not runner.options.batch:
304         logger.write(_("Remove the following directories ?\n"), 1)
305         for directory in l_dir_to_suppress:
306             logger.write("  %s\n" % directory, 1)
307         rep = input(_("Are you sure you want to continue? [Yes/No] "))
308         if rep.upper() != _("YES"):
309             return 0
310
311     # Suppress the list of paths
312     suppress_directories(l_dir_to_suppress, logger)
313
314     return 0