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('v', 'VCS', 'string', 'VCS',
31 _('Optional: The address of the repository of SAT '
32 '(only informative)'))
33 parser.add_option('t', 'tag', 'string', 'tag',
34 _('Optional: The tag of SAT (only informative)'))
35 parser.add_option('l', 'log_dir', 'string', 'log_dir',
36 _('Optional: The directory where to put all the logs of SAT'))
38 def set_local_value(config, key, value, logger):
39 """ Edit the site.pyconf file and change a value.
41 :param config Config: The global configuration.
42 :param key Str: The key from which to change the value.
43 :param value Str: The path to change.
44 :param logger Logger: The logger instance.
45 :return: 0 if all is OK, else 1
48 local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
49 # Update the local.pyconf file
51 local_cfg = src.pyconf.Config(local_file_path)
52 local_cfg.LOCAL[key] = value
53 ff = open(local_file_path, 'w')
54 local_cfg.__save__(ff, 1)
57 config.LOCAL[key] = value
58 except Exception as e:
60 msg = _("Unable to update the local.pyconf file: %s\n" % err)
66 def display_local_values(config, logger):
67 """ Display the base path
69 :param config Config: The global configuration.
70 :param key Str: The key from which to change the value.
71 :param logger Logger: The logger instance.
73 info = [("base", config.LOCAL.base),
74 ("workdir", config.LOCAL.workdir),
75 ("log_dir", config.LOCAL.log_dir),
76 ("VCS", config.LOCAL.VCS),
77 ("tag", config.LOCAL.tag)]
78 src.print_info(logger, info)
82 def check_path(path_to_check, logger):
83 """ Verify that the given path is not a file and can be created.
85 :param path_to_check Str: The path to check.
86 :param logger Logger: The logger instance.
88 if path_to_check == "unknown":
92 path = src.Path(path_to_check)
94 # If it is a file, do nothing and return error
96 msg = _("Error: The given path is a file. Please provide a path to "
98 logger.write(src.printcolors.printcError(msg), 1)
101 # Try to create the given path
103 src.ensure_path_exists(str(path))
104 except Exception as e:
105 err = src.printcolors.printcError(str(e))
106 msg = _("Unable to create the directory %s: %s\n" % (str(path),
114 '''method that is called when salomeTools is called with --help option.
116 :return: The text to display for the init command description.
119 return _("Changes the local settings of SAT.")
121 def run(args, runner, logger):
122 '''method that is called when salomeTools is called with init parameter.
126 (options, args) = parser.parse_args(args)
128 # Print some informations
129 logger.write(_('Local Settings of SAT %s\n\n') %
130 src.printcolors.printcLabel(runner.cfg.VARS.salometoolsway), 1)
134 # Set the options corresponding to a directory
135 for opt in [("base" , options.base),
136 ("workdir", options.workdir),
137 ("log_dir", options.log_dir)]:
140 res_check = check_path(value, logger)
143 res_set = set_local_value(runner.cfg, key, value, logger)
146 # Set the options corresponding to an informative value
147 for opt in [("VCS", options.VCS), ("tag", options.tag)]:
149 res_set = set_local_value(runner.cfg, key, value, logger)
152 display_local_values(runner.cfg, logger)