3 # Copyright (C) 2010-2012 CEA/DEN
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.
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.
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
23 # Compatibility python 2/3 for input function
24 # input stays input for python 3 and input = raw_input for python 2
30 PROPERTY_EXPRESSION = "^.+:.+$"
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 can be'
36 ' passed several time to clean several products.'))
37 parser.add_option('', 'properties', 'string', 'properties',
38 _('Optional: Filter the products by their properties.\n\tSyntax: '
39 '--properties <property>:<value>'))
40 parser.add_option('s', 'sources', 'boolean', 'sources',
41 _("Optional: Clean the product source directories."))
42 parser.add_option('b', 'build', 'boolean', 'build',
43 _("Optional: Clean the product build directories."))
44 parser.add_option('i', 'install', 'boolean', 'install',
45 _("Optional: Clean the product install directories."))
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."))
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.
55 :param options Options: The Options instance that stores the commands
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).
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
67 # if option --products, check that all products of the command line
68 # are present in the application.
69 products = options.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} )
76 # Construct the list of tuple containing
77 # the products name and their definition
78 products_infos = src.product.get_products_infos(products, cfg)
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]
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.
96 :param products_infos list: The list of (name, config) corresponding to one
98 :param without_dev boolean: If True, then ignore the dev products.
99 :return: the list of source paths.
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))
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.
112 :param products_infos list: The list of (name, config) corresponding to one
114 :return: the list of build paths.
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))
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.
128 :param products_infos list: The list of (name, config) corresponding to one
130 :return: the list of install paths.
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))
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.
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.
148 if (src.product.product_is_native(product_info) or
149 src.product.product_is_fixed(product_info)):
152 if src.product.product_is_dev(product_info):
156 def suppress_directories(l_paths, logger):
157 '''Suppress the paths given in the list in l_paths.
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
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)
169 logger.write(_("Removing %s ...") % path.__str__())
171 logger.write('%s\n' % src.printcolors.printc(src.OK_STATUS), 3)
174 '''method that is called when salomeTools is called with --help option.
176 :return: The text to display for the clean command description.
179 return _("The clean command suppress the source, build, or install "
180 "directories of the application products.\nUse the options to"
181 " define what directories you want to suppress and to reduce "
182 "the list of products\n\nexample:\nsat clean SALOME-master "
183 "--build --install --properties is_salome_module:yes")
185 def run(args, runner, logger):
186 '''method that is called when salomeTools is called with clean parameter.
190 (options, args) = parser.parse_args(args)
192 # check that the command has been called with an application
193 src.check_config_has_application( runner.cfg )
195 # Verify the --properties option
196 if options.properties:
197 oExpr = re.compile(PROPERTY_EXPRESSION)
198 if not oExpr.search(options.properties):
199 msg = _('WARNING: the "--properties" options must have the '
200 'following syntax:\n--properties <property>:<value>')
201 logger.write(src.printcolors.printcWarning(msg), 1)
202 logger.write("\n", 1)
203 options.properties = None
206 # Get the list of products to threat
207 products_infos = get_products_list(options, runner.cfg, logger)
209 # Construct the list of directories to suppress
210 l_dir_to_suppress = []
212 l_dir_to_suppress += (get_source_directories(products_infos,
213 options.sources_without_dev) +
214 get_build_directories(products_infos) +
215 get_install_directories(products_infos))
218 l_dir_to_suppress += get_install_directories(products_infos)
221 l_dir_to_suppress += get_build_directories(products_infos)
223 if options.sources or options.sources_without_dev:
224 l_dir_to_suppress += get_source_directories(products_infos,
225 options.sources_without_dev)
227 if len(l_dir_to_suppress) == 0:
228 logger.write(src.printcolors.printcWarning(_("Nothing to suppress\n")))
229 sat_command = (runner.cfg.VARS.salometoolsway +
230 runner.cfg.VARS.sep +
232 logger.write(_("Please specify what you want to suppress: "
233 "tap \"%s\"\n" % sat_command))
236 # Check with the user if he really wants to suppress the directories
237 if not runner.options.batch:
238 logger.write(_("Remove the following directories ?\n"), 1)
239 for directory in l_dir_to_suppress:
240 logger.write(" %s\n" % directory, 1)
241 rep = input(_("Are you sure you want to continue? [Yes/No] "))
242 if rep.upper() != _("YES"):
245 # Suppress the list of paths
246 suppress_directories(l_dir_to_suppress, logger)