Salome HOME
New command sait init. USER.workdir is replaced by LOCAL.workdir. Idem for USER.base...
[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('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'))
37
38 def set_local_value(config, key, value, logger):
39     """ Edit the site.pyconf file and change a value.
40
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
46     :rtype: int
47     """
48     local_file_path = os.path.join(config.VARS.datadir, "local.pyconf")
49     # Update the local.pyconf file
50     try:
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)
55         ff.close()
56         if key != "log_dir":
57             config.LOCAL[key] = value
58     except Exception as e:
59         err = str(e)
60         msg = _("Unable to update the local.pyconf file: %s\n" % err)
61         logger.write(msg, 1)
62         return 1
63     
64     return 0
65     
66 def display_local_values(config, logger):
67     """ Display the base path
68
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.
72     """
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)
79
80     return 0
81
82 def check_path(path_to_check, logger):
83     """ Verify that the given path is not a file and can be created.
84     
85     :param path_to_check Str: The path to check.
86     :param logger Logger: The logger instance.
87     """
88     if path_to_check == "unknown":
89         return 0
90     
91     # Get the path
92     path = src.Path(path_to_check)
93     
94     # If it is a file, do nothing and return error
95     if path.isfile():
96         msg = _("Error: The given path is a file. Please provide a path to "
97                 "a directory")
98         logger.write(src.printcolors.printcError(msg), 1)
99         return 1
100       
101     # Try to create the given path
102     try:
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),
107                                                              err))
108         logger.write(msg, 1)
109         return 1
110     
111     return 0
112
113 def description():
114     '''method that is called when salomeTools is called with --help option.
115     
116     :return: The text to display for the init command description.
117     :rtype: str
118     '''
119     return _("Changes the local settings of SAT.")
120   
121 def run(args, runner, logger):
122     '''method that is called when salomeTools is called with init parameter.
123     '''
124     
125     # Parse the options
126     (options, args) = parser.parse_args(args)
127     
128     # Print some informations
129     logger.write(_('Local Settings of SAT %s\n\n') % 
130                 src.printcolors.printcLabel(runner.cfg.VARS.salometoolsway), 1)
131
132     res = 0
133     
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)]:
138         key, value = opt
139         if value:
140             res_check = check_path(value, logger)
141             res += res_check
142             if res_check == 0:
143                 res_set = set_local_value(runner.cfg, key, value, logger)
144                 res += res_set
145
146     # Set the options corresponding to an informative value            
147     for opt in [("VCS", options.VCS), ("tag", options.tag)]:
148         key, value = opt
149         res_set = set_local_value(runner.cfg, key, value, logger)
150         res += res_set
151     
152     display_local_values(runner.cfg, logger)
153     
154     return res