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