Salome HOME
bug fix in clean command
[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 src
20
21 import prepare
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 # Define all possible option for the clean command :  sat clean <options>
31 parser = src.options.Options()
32 parser.add_option('p', 'products', 'list2', 'products',
33     _('products to clean. This option can be'
34     ' passed several time to clean several products.'))
35 parser.add_option('s', 'source', 'boolean', 'source', 
36     _("Clean the product source directories."))
37 parser.add_option('b', 'build', 'boolean', 'build', 
38     _("Clean the product build directories."))
39 parser.add_option('i', 'install', 'boolean', 'install', 
40     _("Clean the product install directories."))
41 parser.add_option('a', 'all', 'boolean', 'all', 
42     _("Clean the product source, build and install directories."))
43 parser.add_option('', 'sources_without_dev', 'boolean', 'sources_without_dev', 
44     _("do not clean the products in development mode."))
45
46 def get_source_directories(products_infos, without_dev):
47     '''Returns the list of directory source paths corresponding to the list of 
48        product information given as input. If without_dev (bool), then
49        the dev products are ignored.
50     
51     :param products_infos list: The list of (name, config) corresponding to one
52                                 product.
53     :param without_dev boolean: If True, then ignore the dev products.
54     :return: the list of source paths.
55     :rtype: list
56     '''
57     l_dir_source = []
58     for __, product_info in products_infos:
59         if product_has_dir(product_info, without_dev):
60             l_dir_source.append(src.Path(product_info.source_dir))
61     return l_dir_source
62
63 def get_build_directories(products_infos):
64     '''Returns the list of directory build paths corresponding to the list of 
65        product information given as input.
66     
67     :param products_infos list: The list of (name, config) corresponding to one
68                                 product.
69     :return: the list of build paths.
70     :rtype: list
71     '''
72     l_dir_build = []
73     for __, product_info in products_infos:
74         if product_has_dir(product_info):
75             l_dir_build.append(src.Path(product_info.build_dir))
76     return l_dir_build
77
78 def get_install_directories(products_infos):
79     '''Returns the list of directory install paths corresponding to the list of 
80        product information given as input.
81     
82     :param products_infos list: The list of (name, config) corresponding to one
83                                 product.
84     :return: the list of install paths.
85     :rtype: list
86     '''
87     l_dir_install = []
88     for __, product_info in products_infos:
89         if product_has_dir(product_info):
90             l_dir_install.append(src.Path(product_info.install_dir))
91     return l_dir_install
92
93 def product_has_dir(product_info, without_dev=False):
94     '''Returns a boolean at True if there is a source, build and install
95        directory corresponding to the product described by product_info.
96     
97     :param products_info Config: The config corresponding to the product.
98     :return: True if there is a source, build and install
99              directory corresponding to the product described by product_info.
100     :rtype: boolean
101     '''
102     if (src.product.product_is_native(product_info) or 
103                             src.product.product_is_fixed(product_info)):
104         return False
105     if without_dev:
106         if src.product.product_is_dev(product_info):
107             return False
108     return True
109     
110 def suppress_directories(l_paths, logger):
111     '''Suppress the paths given in the list in l_paths.
112     
113     :param l_paths list: The list of Path to be suppressed
114     :param logger Logger: The logger instance to use for the display and 
115                           logging
116     '''    
117     for path in l_paths:
118         if not path.isdir():
119             msg = _("Warning: the path %s does not "
120                     "exists (or is not a directory)\n" % path.__str__())
121             logger.write(src.printcolors.printcWarning(msg), 1)
122         else:
123             logger.write(_("Removing %s ...") % path.__str__())
124             path.rm()
125             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
126
127 def description():
128     '''method that is called when salomeTools is called with --help option.
129     
130     :return: The text to display for the clean command description.
131     :rtype: str
132     '''
133     return _("The clean command suppress the source, build, or install "
134              "directories of the application products.")
135   
136 def run(args, runner, logger):
137     '''method that is called when salomeTools is called with clean parameter.
138     '''
139     
140     # Parse the options
141     (options, args) = parser.parse_args(args)
142
143     # check that the command has been called with an application
144     src.check_config_has_application( runner.cfg )
145
146     # Get the list of products to threat
147     products_infos = prepare.get_products_list(options, runner.cfg, logger)
148
149     # Construct the list of directories to suppress
150     l_dir_to_suppress = []
151     if options.all:
152         l_dir_to_suppress += (get_source_directories(products_infos, 
153                                             options.sources_without_dev) +
154                              get_build_directories(products_infos) + 
155                              get_install_directories(products_infos))
156     else:
157         if options.install:
158             l_dir_to_suppress += get_install_directories(products_infos)
159         
160         if options.build:
161             l_dir_to_suppress += get_build_directories(products_infos)
162             
163         if options.source or options.sources_without_dev:
164             l_dir_to_suppress += get_source_directories(products_infos, 
165                                                 options.sources_without_dev)
166     
167     if len(l_dir_to_suppress) == 0:
168         logger.write(src.printcolors.printcWarning(_("Nothing to suppress\n")))
169         return
170     
171     # Check with the user if he really wants to suppress the directories
172     if not runner.options.batch:
173         logger.write(_("Remove the following directories ?\n"), 1)
174         for directory in l_dir_to_suppress:
175             logger.write("  %s\n" % directory, 1)
176         rep = input(_("Are you sure you want to continue? [Yes/No] "))
177         if rep.upper() != _("YES"):
178             return
179     
180     # Suppress the list of paths
181     suppress_directories(l_dir_to_suppress, logger)
182     
183     return 0