]> SALOME platform Git repositories - tools/sat.git/blob - commands/base.py
Salome HOME
Add the base command
[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     _('The path directory to use as base.'), None)
27
28 def set_base(config, path, site_file_path, logger):
29     # Update the site.pyconf file
30     try:
31         site_pyconf_cfg = src.pyconf.Config(site_file_path)
32         site_pyconf_cfg.SITE.base = str(path)
33         ff = open(site_file_path, 'w')
34         site_pyconf_cfg.__save__(ff, 1)
35         ff.close()
36         config.SITE.base = str(path)
37     except Exception as e:
38         err = str(e)
39         msg = _("Unable to update the site.pyconf file: %s\n" % err)
40         logger.write(msg, 1)
41         return 1
42     
43     display_base_path(config, logger)
44     
45     return 0
46
47
48 def display_base_path(config, logger):
49     info = [(_("Base path"), config.SITE.base)]
50     src.print_info(logger, info)
51
52 def description():
53     '''method that is called when salomeTools is called with --help option.
54     
55     :return: The text to display for the base command description.
56     :rtype: str
57     '''
58     return _("Display or set the base where to put installations of base "
59              "products")
60   
61 def run(args, runner, logger):
62     '''method that is called when salomeTools is called with base parameter.
63     '''
64     
65     # Parse the options
66     (options, args) = parser.parse_args(args)
67     
68     # Get the path to the site.pyconf file that contain the base path
69     site_file_path = os.path.join(runner.cfg.VARS.salometoolsway,
70                                   "data",
71                                   "site.pyconf")
72     
73     # If the option set is passed
74     if options.base_path:
75         # Get the path
76         path = src.Path(options.base_path)
77         
78         # If it is a file, do nothing and return error
79         if path.isfile():
80             msg = _("Error: The given path is a file. Please provide a path to "
81                     "a directory")
82             logger.write(src.printcolors.printcError(msg), 1)
83             return 1
84         
85         # If it is an existing directory, set the base to it
86         if path.exists() and path.isdir():
87             # Set the base in the site.pyconf file
88             res = set_base(runner.cfg, path, site_file_path, logger)
89             return res
90         
91         # Try to create the given path
92         try:
93             src.ensure_path_exists(str(path))
94         except Exception as e:
95             err = src.printcolors.printcError(str(e))
96             msg = _("Unable to create the directory %s: %s\n" % (str(path),
97                                                                  err))
98             logger.write(msg, 1)
99             return 1
100         
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     # Display the base that is in the site.pyconf file
106     display_base_path(runner.cfg, logger)
107     if not options.base_path:
108         logger.write(_("To change the value of the base path, use the --set"
109                        " option.\n"), 2)
110     
111     return