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 init command : sat init <options>
24 parser = src.options.Options()
25 parser.add_option('b', 'base', 'string', 'base',
26 _('Optional: The path to the products base'))
27 parser.add_option('w', 'workdir', 'string', 'workdir',
28 _('Optional: The path to the working directory '
29 '(where to install the applications'))
30 parser.add_option('a', 'archive_dir', 'string', 'archive_dir',
31 _('Optional: The path to the local archive directory '
32 '(where to install local source archives'))
33 parser.add_option('v', 'VCS', 'string', 'VCS',
34 _('Optional: The address of the repository of SAT '
35 '(only informative)'))
36 parser.add_option('t', 'tag', 'string', 'tag',
37 _('Optional: The tag of SAT (only informative)'))
38 parser.add_option('l', 'log_dir', 'string', 'log_dir',
39 _('Optional: The directory where to put all the logs of SAT'))
41 def set_local_value(config, key, value, logger):
42 """ Edit the site.pyconf file and change a value.
44 :param config Config: The global configuration.
45 :param key Str: The key from which to change the value.
46 :param value Str: The path to change.
47 :param logger Logger: The logger instance.
48 :return: 0 if all is OK, else 1
51 local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
52 # Update the local.pyconf file
54 local_cfg = src.pyconf.Config(local_file_path)
55 local_cfg.LOCAL[key] = value
56 ff = open(local_file_path, 'w')
57 local_cfg.__save__(ff, 1)
60 config.LOCAL[key] = value
61 except Exception as e:
63 msg = _("Unable to update the local.pyconf file: %s\n" % err)
69 def display_local_values(config, logger):
70 """ Display the base path
72 :param config Config: The global configuration.
73 :param key Str: The key from which to change the value.
74 :param logger Logger: The logger instance.
76 info = [("base", config.LOCAL.base),
77 ("workdir", config.LOCAL.workdir),
78 ("log_dir", config.LOCAL.log_dir),
79 ("archive_dir", config.LOCAL.archive_dir),
80 ("VCS", config.LOCAL.VCS),
81 ("tag", config.LOCAL.tag)]
82 src.print_info(logger, info)
86 def check_path(path_to_check, logger):
87 """ Verify that the given path is not a file and can be created.
89 :param path_to_check Str: The path to check.
90 :param logger Logger: The logger instance.
92 if path_to_check == "default":
96 path = src.Path(path_to_check)
98 # If it is a file, do nothing and return error
100 msg = _("Error: The given path is a file. Please provide a path to "
102 logger.write(src.printcolors.printcError(msg), 1)
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),
118 '''method that is called when salomeTools is called with --help option.
120 :return: The text to display for the init command description.
123 return _("The init command Changes the local settings of SAT.")
125 def run(args, runner, logger):
126 '''method that is called when salomeTools is called with init parameter.
130 (options, args) = parser.parse_args(args)
132 # Print some informations
133 logger.write(_('Local Settings of SAT %s\n\n') %
134 src.printcolors.printcLabel(runner.cfg.VARS.salometoolsway), 1)
138 # Set the options corresponding to a directory
139 for opt in [("base" , options.base),
140 ("workdir", options.workdir),
141 ("log_dir", options.log_dir),
142 ("archive_dir", options.archive_dir)]:
145 res_check = check_path(value, logger)
148 res_set = set_local_value(runner.cfg, key, value, logger)
151 # Set the options corresponding to an informative value
152 for opt in [("VCS", options.VCS), ("tag", options.tag)]:
154 res_set = set_local_value(runner.cfg, key, value, logger)
157 display_local_values(runner.cfg, logger)