Salome HOME
Add the --properties option to the command clean
[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
21 import src
22
23 # Compatibility python 2/3 for input function
24 # input stays input for python 3 and input = raw_input for python 2
25 try: 
26     input = raw_input
27 except NameError: 
28     pass
29
30 PROPERTY_EXPRESSION = "^.+:.+$"
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     _('Products to clean. This option can be'
36     ' passed several time to clean several products.'))
37 parser.add_option('', 'properties', 'string', 'properties',
38     _('Filter the products by their properties.\n\tSyntax: '
39       '--properties <property>:<value>'))
40 parser.add_option('s', 'sources', 'boolean', 'sources', 
41     _("Clean the product source directories."))
42 parser.add_option('b', 'build', 'boolean', 'build', 
43     _("Clean the product build directories."))
44 parser.add_option('i', 'install', 'boolean', 'install', 
45     _("Clean the product install directories."))
46 parser.add_option('a', 'all', 'boolean', 'all', 
47     _("Clean the product source, build and install directories."))
48 parser.add_option('', 'sources_without_dev', 'boolean', 'sources_without_dev', 
49     _("do not clean the products in development mode."))
50
51 def get_products_list(options, cfg, logger):
52     '''method that gives the product list with their informations from 
53        configuration regarding the passed options.
54     
55     :param options Options: The Options instance that stores the commands 
56                             arguments
57     :param config Config: The global configuration
58     :param logger Logger: The logger instance to use for the display and logging
59     :return: The list of (product name, product_informations).
60     :rtype: List
61     '''
62     # Get the products to be prepared, regarding the options
63     if options.products is None:
64         # No options, get all products sources
65         products = cfg.APPLICATION.products
66     else:
67         # if option --products, check that all products of the command line
68         # are present in the application.
69         products = options.products
70         for p in products:
71             if p not in cfg.APPLICATION.products:
72                 raise src.SatException(_("Product %(product)s "
73                             "not defined in application %(application)s") %
74                         { 'product': p, 'application': cfg.VARS.application} )
75     
76     # Construct the list of tuple containing 
77     # the products name and their definition
78     products_infos = src.product.get_products_infos(products, cfg)
79     
80     # if the property option was passed, filter the list
81     if options.properties:
82         [prop, value] = options.properties.split(":")
83         products_infos = [(p_name, p_info) for 
84                           (p_name, p_info) in products_infos 
85                           if "properties" in p_info and 
86                           prop in p_info.properties and 
87                           p_info.properties[prop] == value]
88         
89     return products_infos
90
91 def get_source_directories(products_infos, without_dev):
92     '''Returns the list of directory source paths corresponding to the list of 
93        product information given as input. If without_dev (bool), then
94        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             l_dir_source.append(src.Path(product_info.source_dir))
106     return l_dir_source
107
108 def get_build_directories(products_infos):
109     '''Returns the list of directory build paths corresponding to the list of 
110        product information given as input.
111     
112     :param products_infos list: The list of (name, config) corresponding to one
113                                 product.
114     :return: the list of build paths.
115     :rtype: list
116     '''
117     l_dir_build = []
118     for __, product_info in products_infos:
119         if product_has_dir(product_info):
120             if "build_dir" in product_info:
121                 l_dir_build.append(src.Path(product_info.build_dir))
122     return l_dir_build
123
124 def get_install_directories(products_infos):
125     '''Returns the list of directory install paths corresponding to the list of 
126        product information given as input.
127     
128     :param products_infos list: The list of (name, config) corresponding to one
129                                 product.
130     :return: the list of install paths.
131     :rtype: list
132     '''
133     l_dir_install = []
134     for __, product_info in products_infos:
135         if product_has_dir(product_info):
136             l_dir_install.append(src.Path(product_info.install_dir))
137     return l_dir_install
138
139 def product_has_dir(product_info, without_dev=False):
140     '''Returns a boolean at True if there is a source, build and install
141        directory corresponding to the product described by product_info.
142     
143     :param products_info Config: The config corresponding to the product.
144     :return: True if there is a source, build and install
145              directory corresponding to the product described by product_info.
146     :rtype: boolean
147     '''
148     if (src.product.product_is_native(product_info) or 
149                             src.product.product_is_fixed(product_info)):
150         return False
151     if without_dev:
152         if src.product.product_is_dev(product_info):
153             return False
154     return True
155     
156 def suppress_directories(l_paths, logger):
157     '''Suppress the paths given in the list in l_paths.
158     
159     :param l_paths list: The list of Path to be suppressed
160     :param logger Logger: The logger instance to use for the display and 
161                           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 that is 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 _("The clean command suppress the source, build, or install "
180              "directories of the application products.")
181   
182 def run(args, runner, logger):
183     '''method that is called when salomeTools is called with clean parameter.
184     '''
185     
186     # Parse the options
187     (options, args) = parser.parse_args(args)
188
189     # check that the command has been called with an application
190     src.check_config_has_application( runner.cfg )
191
192     # Verify the --properties option
193     if options.properties:
194         oExpr = re.compile(PROPERTY_EXPRESSION)
195         if not oExpr.search(options.properties):
196             msg = _('WARNING: the "--properties" options must have the '
197                     'following syntax:\n--properties <property>:<value>')
198             logger.write(src.printcolors.printcWarning(msg), 1)
199             logger.write("\n", 1)
200             options.properties = None
201             
202
203     # Get the list of products to threat
204     products_infos = get_products_list(options, runner.cfg, logger)
205
206     # Construct the list of directories to suppress
207     l_dir_to_suppress = []
208     if options.all:
209         l_dir_to_suppress += (get_source_directories(products_infos, 
210                                             options.sources_without_dev) +
211                              get_build_directories(products_infos) + 
212                              get_install_directories(products_infos))
213     else:
214         if options.install:
215             l_dir_to_suppress += get_install_directories(products_infos)
216         
217         if options.build:
218             l_dir_to_suppress += get_build_directories(products_infos)
219             
220         if options.sources or options.sources_without_dev:
221             l_dir_to_suppress += get_source_directories(products_infos, 
222                                                 options.sources_without_dev)
223     
224     if len(l_dir_to_suppress) == 0:
225         logger.write(src.printcolors.printcWarning(_("Nothing to suppress\n")))
226         sat_command = (runner.cfg.VARS.salometoolsway +
227                        runner.cfg.VARS.sep +
228                        "sat -h clean") 
229         logger.write(_("Please specify what you want to suppress: "
230                        "tap \"%s\"\n" % sat_command))
231         return
232     
233     # Check with the user if he really wants to suppress the directories
234     if not runner.options.batch:
235         logger.write(_("Remove the following directories ?\n"), 1)
236         for directory in l_dir_to_suppress:
237             logger.write("  %s\n" % directory, 1)
238         rep = input(_("Are you sure you want to continue? [Yes/No] "))
239         if rep.upper() != _("YES"):
240             return 0
241     
242     # Suppress the list of paths
243     suppress_directories(l_dir_to_suppress, logger)
244     
245     return 0