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