]> SALOME platform Git repositories - tools/sat.git/blob - commands/environ.py
Salome HOME
Add configure command first version
[tools/sat.git] / commands / environ.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  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 parser = src.options.Options()
24 parser.add_option('', 'shell', 'list2', 'shell',
25     _("Generates the environment files for the given format: "
26       "bash (default), bat (for windows) or all."), [])
27 parser.add_option('p', 'products', 'list2', 'products',
28     _("Includes only the specified products."))
29 parser.add_option('', 'prefix', 'string', 'prefix',
30     _("Specifies the prefix for the environment files."), "env")
31 parser.add_option('t', 'target', 'string', 'out_dir',
32     _("Specifies the directory path where to put the environment files."),
33     None)
34
35 # list of available shells with extensions
36 C_SHELLS = { "bash": "sh", "bat": "bat" }
37 C_ALL_SHELL = [ "bash", "bat" ]
38
39
40 ##
41 # Writes all the environment files
42 def write_all_source_files(config,
43                            logger,
44                            out_dir=None,
45                            src_root=None,
46                            silent=False,
47                            shells=["bash"],
48                            prefix="env",
49                            env_info=None):
50     
51     if not out_dir:
52         out_dir = config.APPLICATION.workdir
53
54     if not os.path.exists(out_dir):
55         raise src.SatException(_("Target directory not found: %s") % out_dir)
56
57     if not silent:
58         logger.write(_("Creating environment files for %s\n") % 
59                      src.printcolors.printcLabel(config.APPLICATION.name), 2)
60         src.printcolors.print_value(logger,
61                                     _("Target"),
62                                     src.printcolors.printcInfo(out_dir), 3)
63         logger.write("\n", 3, False)
64     
65     shells_list = []
66     all_shells = C_ALL_SHELL
67     if "all" in shells:
68         shells = all_shells
69     else:
70         shells = filter(lambda l: l in all_shells, shells)
71
72     for shell in shells:
73         if shell not in C_SHELLS:
74             logger.write(_("Unknown shell: %s\n") % shell, 2)
75         else:
76             shells_list.append(src.environment.Shell(shell, C_SHELLS[shell]))
77     
78     writer = src.environment.FileEnvWriter(config,
79                                            logger,
80                                            out_dir,
81                                            src_root,
82                                            env_info)
83     writer.silent = silent
84     files = []
85     for_build = True
86     for_launch = False
87     for shell in shells_list:
88         files.append(writer.write_env_file("%s_launch.%s" %
89                                            (prefix, shell.extension),
90                                            for_launch,
91                                            shell.name))
92         files.append(writer.write_env_file("%s_build.%s" %
93                                            (prefix, shell.extension),
94                                            for_build,
95                                            shell.name))
96
97     return files
98
99 ##################################################
100
101 ##
102 # Describes the command
103 def description():
104     return _("The environ command generates the "
105                 "environment files of your application.")
106
107 ##
108 # Runs the command.
109 def run(args, runner, logger):
110     (options, args) = parser.parse_args(args)
111
112     # check that the command was called with an application
113     src.check_config_has_application( runner.cfg )
114     
115     if options.products is None:
116         environ_info = None
117     else:
118         # add products specified by user (only products 
119         # included in the application)
120         environ_info = filter(lambda l:
121                               l in runner.cfg.APPLICATION.products.keys(),
122                               options.products)
123     
124     if options.shell == []:
125         shell = ["bash"]
126         if src.architecture.is_windows():
127             shell = ["bat"]
128     else:
129         shell = options.shell
130     
131     out_dir = options.out_dir
132     if out_dir:
133         out_dir = os.path.abspath(out_dir)
134     
135     write_all_source_files(runner.cfg, logger, out_dir=out_dir, shells=shell,
136                            prefix=options.prefix, env_info=environ_info)
137     logger.write("\n", 3, False)