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('', 'add_project ', 'string', 'add_project',
34 _('Optional: The path of the project to add'))
35 parser.add_option('', 'reset_projects', 'boolean', 'reset_projects',
36 _('Optional: Reset the list of projects'))
37 parser.add_option('v', 'VCS', 'string', 'VCS',
38 _('Optional: The address of the repository of SAT '
39 '(only informative)'))
40 parser.add_option('t', 'tag', 'string', 'tag',
41 _('Optional: The tag of SAT (only informative)'))
42 parser.add_option('l', 'log_dir', 'string', 'log_dir',
43 _('Optional: The directory where to put all the logs of SAT'))
45 def set_local_value(config, key, value, logger):
46 """ Edit the site.pyconf file and change a value.
48 :param config Config: The global configuration.
49 :param key Str: The key from which to change the value.
50 :param value Str: The path to change.
51 :param logger Logger: The logger instance.
52 :return: 0 if all is OK, else 1
55 local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
56 # Update the local.pyconf file
58 local_cfg = src.pyconf.Config(local_file_path)
59 local_cfg.LOCAL[key] = value
60 ff = open(local_file_path, 'w')
61 local_cfg.__save__(ff, 1)
64 config.LOCAL[key] = value
65 except Exception as e:
67 msg = _("Unable to update the local.pyconf file: %s\n" % err)
73 def add_local_project(config, project_file, logger):
74 """ Add a project in local configuration (file data/local.pyconf).
76 :param config Config: The global configuration.
77 :param new_project Str: The project pyconf file to add in local config.
78 :param logger Logger: The logger instance.
79 :return: 0 if all is OK, else 1
82 if not os.path.isfile(project_file):
83 logger.write("Unable to add a project in local configuration, project file %s does not exist\n" % project_file, 1)
86 # check that the project file exists
87 local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
89 # Update the local.pyconf file
91 local_cfg = src.pyconf.Config(local_file_path)
92 local_cfg.PROJECTS.project_file_paths.append(project_file, "")
93 ff = open(local_file_path, 'w')
94 local_cfg.__save__(ff, 1)
96 config.PROJECTS.project_file_paths.append(project_file, "")
98 except Exception as e:
100 msg = _("Unable to update the local.pyconf file: %s\n" % err)
107 def reset_local_projects(config, logger):
108 """ Reinitialise the list of projects in local configuration (file data/local.pyconf).
110 :param config Config: The global configuration.
111 :param logger Logger: The logger instance.
112 :return: 0 if all is OK, else 1
116 local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
117 # Update the local.pyconf file
119 local_cfg = src.pyconf.Config(local_file_path)
120 local_cfg.PROJECTS.project_file_paths=src.pyconf.Sequence(local_cfg.PROJECTS)
121 ff = open(local_file_path, 'w')
122 local_cfg.__save__(ff, 1)
124 config.PROJECTS.project_file_paths=src.pyconf.Sequence(config.PROJECTS)
126 except Exception as e:
128 msg = _("Unable to update the local.pyconf file: %s\n" % err)
135 def display_local_values(config, logger):
136 """ Display the base path
138 :param config Config: The global configuration.
139 :param key Str: The key from which to change the value.
140 :param logger Logger: The logger instance.
142 info = [("base", config.LOCAL.base),
143 ("workdir", config.LOCAL.workdir),
144 ("log_dir", config.LOCAL.log_dir),
145 ("archive_dir", config.LOCAL.archive_dir),
146 ("VCS", config.LOCAL.VCS),
147 ("tag", config.LOCAL.tag),
148 ("projects", config.PROJECTS.project_file_paths)]
149 src.print_info(logger, info)
153 def check_path(path_to_check, logger):
154 """ Verify that the given path is not a file and can be created.
156 :param path_to_check Str: The path to check.
157 :param logger Logger: The logger instance.
159 if path_to_check == "default":
163 path = src.Path(path_to_check)
165 # If it is a file, do nothing and return error
167 msg = _("Error: The given path is a file. Please provide a path to "
169 logger.write(src.printcolors.printcError(msg), 1)
172 # Try to create the given path
174 src.ensure_path_exists(str(path))
175 except Exception as e:
176 err = src.printcolors.printcError(str(e))
177 msg = _("Unable to create the directory %s: %s\n" % (str(path),
185 '''method that is called when salomeTools is called with --help option.
187 :return: The text to display for the init command description.
190 return _("The init command Changes the local settings of SAT.")
192 def run(args, runner, logger):
193 '''method that is called when salomeTools is called with init parameter.
197 (options, args) = parser.parse_args(args)
199 # Print some informations
200 logger.write(_('Local Settings of SAT %s\n\n') %
201 src.printcolors.printcLabel(runner.cfg.VARS.salometoolsway), 1)
206 # Set the options corresponding to a directory
207 for opt in [("base" , options.base),
208 ("workdir", options.workdir),
209 ("log_dir", options.log_dir),
210 ("archive_dir", options.archive_dir)]:
213 res_check = check_path(value, logger)
216 res_set = set_local_value(runner.cfg, key, value, logger)
219 # set the options corresponding to projects file names
220 if options.add_project:
221 res_add=add_local_project(runner.cfg, options.add_project, logger)
224 if options.reset_projects:
225 res_rem=reset_local_projects(runner.cfg, logger)
228 # Set the options corresponding to an informative value
229 for opt in [("VCS", options.VCS), ("tag", options.tag)]:
232 res_set = set_local_value(runner.cfg, key, value, logger)
235 display_local_values(runner.cfg, logger)