Salome HOME
Add an option to the log command in order to access the products compilation logs
[tools/sat.git] / commands / base.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 shell command :  sat base <options>
24 parser = src.options.Options()
25 parser.add_option('s', 'set', 'string', 'base_path',
26     _('Optional: The path directory to use as base.'), None)
27
28 def set_base(config, path, site_file_path, logger):
29     """ Edit the site.pyconf file and change the base path
30
31     :param config Config: The global configuration.    
32     :param path src.Path: The path to put in the site.pyconf file.
33     :param site_file_path Str: The path to the site.pyconf file.
34     :param logger Logger: The logger instance.
35     :return: 0 if all is OK, else 1
36     :rtype: int
37     """
38     # Update the site.pyconf file
39     try:
40         site_pyconf_cfg = src.pyconf.Config(site_file_path)
41         site_pyconf_cfg.SITE.base = str(path)
42         ff = open(site_file_path, 'w')
43         site_pyconf_cfg.__save__(ff, 1)
44         ff.close()
45         config.SITE.base = str(path)
46     except Exception as e:
47         err = str(e)
48         msg = _("Unable to update the site.pyconf file: %s\n" % err)
49         logger.write(msg, 1)
50         return 1
51     
52     display_base_path(config, logger)
53     
54     return 0
55
56
57 def display_base_path(config, logger):
58     """ Display the base path
59
60     :param config Config: The global configuration.    
61     :param logger Logger: The logger instance.
62     """
63     info = [(_("Base path"), config.SITE.base)]
64     src.print_info(logger, info)
65
66 def description():
67     '''method that is called when salomeTools is called with --help option.
68     
69     :return: The text to display for the base command description.
70     :rtype: str
71     '''
72     return _("Display or set the base where to put installations of base "
73              "products\n\nexample\nsat base --set /tmp/BASE")
74   
75 def run(args, runner, logger):
76     '''method that is called when salomeTools is called with base parameter.
77     '''
78     
79     # Parse the options
80     (options, args) = parser.parse_args(args)
81     
82     # Get the path to the site.pyconf file that contain the base path
83     site_file_path = os.path.join(runner.cfg.VARS.salometoolsway,
84                                   "data",
85                                   "site.pyconf")
86     
87     # If the option set is passed
88     if options.base_path:
89         # Get the path
90         path = src.Path(options.base_path)
91         
92         # If it is a file, do nothing and return error
93         if path.isfile():
94             msg = _("Error: The given path is a file. Please provide a path to "
95                     "a directory")
96             logger.write(src.printcolors.printcError(msg), 1)
97             return 1
98         
99         # If it is an existing directory, set the base to it
100         if path.exists() and path.isdir():
101             # Set the base in the site.pyconf file
102             res = set_base(runner.cfg, path, site_file_path, logger)
103             return res
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         # Set the base in the site.pyconf file
116         res = set_base(runner.cfg, path, site_file_path, logger)
117         return res
118     
119     # Display the base that is in the site.pyconf file
120     display_base_path(runner.cfg, logger)
121     if not options.base_path:
122         logger.write(_("To change the value of the base path, use the --set"
123                        " option.\n"), 2)
124     
125     return