Salome HOME
ajout option d'impression des graphes de dépendance
[tools/sat.git] / commands / init.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2012  CEA/DEN
4 #
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.
9 #
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.
14 #
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
18
19 import os
20
21 import src
22
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'))
44
45 def set_local_value(config, key, value, logger):
46     """ Edit the site.pyconf file and change a value.
47
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
53     :rtype: int
54     """
55     local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
56     # Update the local.pyconf file
57     try:
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)
62         ff.close()
63         if key != "log_dir":
64             config.LOCAL[key] = value
65     except Exception as e:
66         err = str(e)
67         msg = _("Unable to update the local.pyconf file: %s\n" % err)
68         logger.write(msg, 1)
69         return 1
70     
71     return 0
72     
73 def add_local_project(config, project_file, logger):
74     """ Add a project in local configuration (file data/local.pyconf).
75
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
80     :rtype: int
81     """
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)
84         return 1
85
86     # check that the project file exists
87     local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
88
89     # Update the local.pyconf file
90     try:
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)
95         ff.close()
96         config.PROJECTS.project_file_paths.append(project_file, "")
97
98     except Exception as e:
99         err = str(e)
100         msg = _("Unable to update the local.pyconf file: %s\n" % err)
101         logger.write(msg, 1)
102         return 1
103
104     return 0
105
106
107 def reset_local_projects(config, logger):
108     """ Reinitialise the list of projects in local configuration (file data/local.pyconf).
109
110     :param config Config: The global configuration.    
111     :param logger Logger: The logger instance.
112     :return: 0 if all is OK, else 1
113     :rtype: int
114     """
115
116     local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
117     # Update the local.pyconf file
118     try:
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)
123         ff.close()
124         config.PROJECTS.project_file_paths=src.pyconf.Sequence(config.PROJECTS)
125
126     except Exception as e:
127         err = str(e)
128         msg = _("Unable to update the local.pyconf file: %s\n" % err)
129         logger.write(msg, 1)
130         return 1
131
132     return 0
133
134
135 def display_local_values(config, logger):
136     """ Display the base path
137
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.
141     """
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)
150
151     return 0
152
153 def check_path(path_to_check, logger):
154     """ Verify that the given path is not a file and can be created.
155     
156     :param path_to_check Str: The path to check.
157     :param logger Logger: The logger instance.
158     """
159     if path_to_check == "default":
160         return 0
161     
162     # Get the path
163     path = src.Path(path_to_check)
164     
165     # If it is a file, do nothing and return error
166     if path.isfile():
167         msg = _("Error: The given path is a file. Please provide a path to "
168                 "a directory")
169         logger.write(src.printcolors.printcError(msg), 1)
170         return 1
171       
172     # Try to create the given path
173     try:
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),
178                                                              err))
179         logger.write(msg, 1)
180         return 1
181     
182     return 0
183
184 def description():
185     '''method that is called when salomeTools is called with --help option.
186     
187     :return: The text to display for the init command description.
188     :rtype: str
189     '''
190     return _("The init command Changes the local settings of SAT.")
191   
192 def run(args, runner, logger):
193     '''method that is called when salomeTools is called with init parameter.
194     '''
195     
196     # Parse the options
197     (options, args) = parser.parse_args(args)
198     
199     # Print some informations
200     logger.write(_('Local Settings of SAT %s\n\n') % 
201                 src.printcolors.printcLabel(runner.cfg.VARS.salometoolsway), 1)
202
203     res = 0
204     
205
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)]:
211         key, value = opt
212         if value:
213             res_check = check_path(value, logger)
214             res += res_check
215             if res_check == 0:
216                 res_set = set_local_value(runner.cfg, key, value, logger)
217                 res += res_set
218
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)
222         res += res_add
223
224     if options.reset_projects:
225         res_rem=reset_local_projects(runner.cfg, logger)
226         res += res_rem
227
228     # Set the options corresponding to an informative value            
229     for opt in [("VCS", options.VCS), ("tag", options.tag)]:
230         key, value = opt
231         if value:
232             res_set = set_local_value(runner.cfg, key, value, logger)
233             res += res_set
234     
235     display_local_values(runner.cfg, logger)
236     
237     return res