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 # Define all possible option for the shell command : sat base <options>
24 parser = src.options.Options()
25 parser.add_option('s', 'set', 'string', 'base_path',
26 _('Optional: The path directory to use as base.'), None)
28 def set_base(config, path, site_file_path, logger):
29 """ Edit the site.pyconf file and change the base path
31 :param config Config: The global configuration.
32 :param path src.Path: The path to put in the site.pyconf file.
33 :param site_file_path Str: The path to the site.pyconf file.
34 :param logger Logger: The logger instance.
35 :return: 0 if all is OK, else 1
38 # Update the site.pyconf file
40 site_pyconf_cfg = src.pyconf.Config(site_file_path)
41 site_pyconf_cfg.SITE.base = str(path)
42 ff = open(site_file_path, 'w')
43 site_pyconf_cfg.__save__(ff, 1)
45 config.SITE.base = str(path)
46 except Exception as e:
48 msg = _("Unable to update the site.pyconf file: %s\n" % err)
52 display_base_path(config, logger)
57 def display_base_path(config, logger):
58 """ Display the base path
60 :param config Config: The global configuration.
61 :param logger Logger: The logger instance.
63 info = [(_("Base path"), config.SITE.base)]
64 src.print_info(logger, info)
67 '''method that is called when salomeTools is called with --help option.
69 :return: The text to display for the base command description.
72 return _("Display or set the base where to put installations of base "
73 "products\n\nexample\nsat base --set /tmp/BASE")
75 def run(args, runner, logger):
76 '''method that is called when salomeTools is called with base parameter.
80 (options, args) = parser.parse_args(args)
82 # Get the path to the site.pyconf file that contain the base path
83 site_file_path = os.path.join(runner.cfg.VARS.salometoolsway,
87 # If the option set is passed
90 path = src.Path(options.base_path)
92 # If it is a file, do nothing and return error
94 msg = _("Error: The given path is a file. Please provide a path to "
96 logger.write(src.printcolors.printcError(msg), 1)
99 # If it is an existing directory, set the base to it
100 if path.exists() and path.isdir():
101 # Set the base in the site.pyconf file
102 res = set_base(runner.cfg, path, site_file_path, logger)
105 # Try to create the given path
107 src.ensure_path_exists(str(path))
108 except Exception as e:
109 err = src.printcolors.printcError(str(e))
110 msg = _("Unable to create the directory %s: %s\n" % (str(path),
115 # Set the base in the site.pyconf file
116 res = set_base(runner.cfg, path, site_file_path, logger)
119 # Display the base that is in the site.pyconf file
120 display_base_path(runner.cfg, logger)
121 if not options.base_path:
122 logger.write(_("To change the value of the base path, use the --set"