Salome HOME
suppresion d'un log
[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('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'))
40
41 def set_local_value(config, key, value, logger):
42     """ Edit the site.pyconf file and change a value.
43
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
49     :rtype: int
50     """
51     local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
52     # Update the local.pyconf file
53     try:
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)
58         ff.close()
59         if key != "log_dir":
60             config.LOCAL[key] = value
61     except Exception as e:
62         err = str(e)
63         msg = _("Unable to update the local.pyconf file: %s\n" % err)
64         logger.write(msg, 1)
65         return 1
66     
67     return 0
68     
69 def display_local_values(config, logger):
70     """ Display the base path
71
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.
75     """
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)
83
84     return 0
85
86 def check_path(path_to_check, logger):
87     """ Verify that the given path is not a file and can be created.
88     
89     :param path_to_check Str: The path to check.
90     :param logger Logger: The logger instance.
91     """
92     if path_to_check == "default":
93         return 0
94     
95     # Get the path
96     path = src.Path(path_to_check)
97     
98     # If it is a file, do nothing and return error
99     if path.isfile():
100         msg = _("Error: The given path is a file. Please provide a path to "
101                 "a directory")
102         logger.write(src.printcolors.printcError(msg), 1)
103         return 1
104       
105     # Try to create the given path
106     try:
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),
111                                                              err))
112         logger.write(msg, 1)
113         return 1
114     
115     return 0
116
117 def description():
118     '''method that is called when salomeTools is called with --help option.
119     
120     :return: The text to display for the init command description.
121     :rtype: str
122     '''
123     return _("Changes the local settings of SAT.")
124   
125 def run(args, runner, logger):
126     '''method that is called when salomeTools is called with init parameter.
127     '''
128     
129     # Parse the options
130     (options, args) = parser.parse_args(args)
131     
132     # Print some informations
133     logger.write(_('Local Settings of SAT %s\n\n') % 
134                 src.printcolors.printcLabel(runner.cfg.VARS.salometoolsway), 1)
135
136     res = 0
137     
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)]:
143         key, value = opt
144         if value:
145             res_check = check_path(value, logger)
146             res += res_check
147             if res_check == 0:
148                 res_set = set_local_value(runner.cfg, key, value, logger)
149                 res += res_set
150
151     # Set the options corresponding to an informative value            
152     for opt in [("VCS", options.VCS), ("tag", options.tag)]:
153         key, value = opt
154         res_set = set_local_value(runner.cfg, key, value, logger)
155         res += res_set
156     
157     display_local_values(runner.cfg, logger)
158     
159     return res