]> SALOME platform Git repositories - tools/sat.git/blob - commands/clean.py
Salome HOME
23ee598df499b2aeb1fa8986ff66bdbdd14e2a0c
[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', 'sources', 'boolean', 'sources', 
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             if "build_dir" in product_info:
76                 l_dir_build.append(src.Path(product_info.build_dir))
77     return l_dir_build
78
79 def get_install_directories(products_infos):
80     '''Returns the list of directory install paths corresponding to the list of 
81        product information given as input.
82     
83     :param products_infos list: The list of (name, config) corresponding to one
84                                 product.
85     :return: the list of install paths.
86     :rtype: list
87     '''
88     l_dir_install = []
89     for __, product_info in products_infos:
90         if product_has_dir(product_info):
91             l_dir_install.append(src.Path(product_info.install_dir))
92     return l_dir_install
93
94 def product_has_dir(product_info, without_dev=False):
95     '''Returns a boolean at True if there is a source, build and install
96        directory corresponding to the product described by product_info.
97     
98     :param products_info Config: The config corresponding to the product.
99     :return: True if there is a source, build and install
100              directory corresponding to the product described by product_info.
101     :rtype: boolean
102     '''
103     if (src.product.product_is_native(product_info) or 
104                             src.product.product_is_fixed(product_info)):
105         return False
106     if without_dev:
107         if src.product.product_is_dev(product_info):
108             return False
109     return True
110     
111 def suppress_directories(l_paths, logger):
112     '''Suppress the paths given in the list in l_paths.
113     
114     :param l_paths list: The list of Path to be suppressed
115     :param logger Logger: The logger instance to use for the display and 
116                           logging
117     '''    
118     for path in l_paths:
119         if not path.isdir():
120             msg = _("Warning: the path %s does not "
121                     "exists (or is not a directory)\n" % path.__str__())
122             logger.write(src.printcolors.printcWarning(msg), 1)
123         else:
124             logger.write(_("Removing %s ...") % path.__str__())
125             path.rm()
126             logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
127
128 def description():
129     '''method that is called when salomeTools is called with --help option.
130     
131     :return: The text to display for the clean command description.
132     :rtype: str
133     '''
134     return _("The clean command suppress the source, build, or install "
135              "directories of the application products.")
136   
137 def run(args, runner, logger):
138     '''method that is called when salomeTools is called with clean parameter.
139     '''
140     
141     # Parse the options
142     (options, args) = parser.parse_args(args)
143
144     # check that the command has been called with an application
145     src.check_config_has_application( runner.cfg )
146
147     # Get the list of products to threat
148     products_infos = prepare.get_products_list(options, runner.cfg, logger)
149
150     # Construct the list of directories to suppress
151     l_dir_to_suppress = []
152     if options.all:
153         l_dir_to_suppress += (get_source_directories(products_infos, 
154                                             options.sources_without_dev) +
155                              get_build_directories(products_infos) + 
156                              get_install_directories(products_infos))
157     else:
158         if options.install:
159             l_dir_to_suppress += get_install_directories(products_infos)
160         
161         if options.build:
162             l_dir_to_suppress += get_build_directories(products_infos)
163             
164         if options.sources or options.sources_without_dev:
165             l_dir_to_suppress += get_source_directories(products_infos, 
166                                                 options.sources_without_dev)
167     
168     if len(l_dir_to_suppress) == 0:
169         logger.write(src.printcolors.printcWarning(_("Nothing to suppress\n")))
170         sat_command = (runner.cfg.VARS.salometoolsway +
171                        runner.cfg.VARS.sep +
172                        "sat -h clean") 
173         logger.write(_("Please specify what you want to suppress: "
174                        "tap \"%s\"\n" % sat_command))
175         return
176     
177     # Check with the user if he really wants to suppress the directories
178     if not runner.options.batch:
179         logger.write(_("Remove the following directories ?\n"), 1)
180         for directory in l_dir_to_suppress:
181             logger.write("  %s\n" % directory, 1)
182         rep = input(_("Are you sure you want to continue? [Yes/No] "))
183         if rep.upper() != _("YES"):
184             return 0
185     
186     # Suppress the list of paths
187     suppress_directories(l_dir_to_suppress, logger)
188     
189     return 0