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