Salome HOME
sat #18867 : pour les url des bases git : substitution des references par leur valeur...
[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     _("Optional: Generates the environment files for the given format: "
26       "bash (default), bat (for windows), cfg (salome context file) or all."), [])
27 parser.add_option('p', 'products', 'list2', 'products',
28     _("Optional: Includes only the specified products."))
29 parser.add_option('', 'prefix', 'string', 'prefix',
30     _("Optional: Specifies the prefix for the environment files."), "env")
31 parser.add_option('t', 'target', 'string', 'out_dir',
32     _("Optional: Specifies the directory path where to put the environment "
33       "files."),
34     None)
35
36 # list of available shells with extensions
37 C_SHELLS = { "bash": "sh", "bat": "bat", "cfg" : "cfg", "tcl" : ""}
38 C_ALL_SHELL = [ "bash", "bat", "cfg", "tcl" ]
39
40
41 ##
42 # Writes all the environment files
43 def write_all_source_files(config,
44                            logger,
45                            out_dir=None,
46                            src_root=None,
47                            silent=False,
48                            shells=["bash"],
49                            prefix="env",
50                            env_info=None):
51     '''Generates the environment files.
52     
53     :param config Config: The global configuration
54     :param logger Logger: The logger instance to use for the display 
55                           and logging
56     :param out_dir str: The path to the directory where the files will be put
57     :param src_root str: The path to the directory where the sources are
58     :param silent boolean: If True, do not print anything in the terminal
59     :param shells list: The list of shells to generate
60     :param prefix str: The prefix to add to the file names.
61     :param env_info str: The list of products to add in the files.
62     :return: The list of the generated files.
63     :rtype: List
64     '''
65         
66     if not out_dir:
67         out_dir = config.APPLICATION.workdir
68
69     if not os.path.exists(out_dir):
70         raise src.SatException(_("Target directory not found: %s") % out_dir)
71
72     if not silent:
73         logger.write(_("Creating environment files for %s\n") % 
74                      src.printcolors.printcLabel(config.APPLICATION.name), 2)
75         src.printcolors.print_value(logger,
76                                     _("Target"),
77                                     src.printcolors.printcInfo(out_dir), 3)
78         logger.write("\n", 3, False)
79     
80     shells_list = []
81     all_shells = C_ALL_SHELL
82     if "all" in shells:
83         shells = all_shells
84     else:
85         shells = filter(lambda l: l in all_shells, shells)
86
87     for shell in shells:
88         if shell not in C_SHELLS:
89             logger.write(_("Unknown shell: %s\n") % shell, 2)
90         else:
91             shells_list.append(src.environment.Shell(shell, C_SHELLS[shell]))
92     
93     writer = src.environment.FileEnvWriter(config,
94                                            logger,
95                                            out_dir,
96                                            src_root,
97                                            env_info)
98     writer.silent = silent
99     files = []
100     for_build = True
101     for_launch = False
102     for shell in shells_list:
103         if shell.name=="tcl":
104             files.append(writer.write_tcl_files(for_launch,
105                                                 shell.name))
106         else:
107             files.append(writer.write_env_file("%s_launch.%s" %
108                                                (prefix, shell.extension),
109                                                for_launch,
110                                                shell.name))
111             files.append(writer.write_env_file("%s_build.%s" %
112                                                (prefix, shell.extension),
113                                                for_build,
114                                                shell.name))
115
116     for f in files:
117         if f:
118             logger.write("    "+f+"\n", 3)
119     return files
120
121 ##################################################
122
123 ##
124 # Describes the command
125 def description():
126     return _("The environ command generates the environment files of your "
127              "application.\n\nexample:\nsat environ SALOME-master")
128
129 ##
130 # Runs the command.
131 def run(args, runner, logger):
132     (options, args) = parser.parse_args(args)
133
134     # check that the command was called with an application
135     src.check_config_has_application( runner.cfg )
136     
137     if options.products is None:
138         environ_info = None
139     else:
140         # add products specified by user (only products 
141         # included in the application)
142         environ_info = filter(lambda l:
143                               l in runner.cfg.APPLICATION.products.keys(),
144                               options.products)
145     
146     if options.shell == []:
147         shell = ["bash"]
148         if src.architecture.is_windows():
149             shell = ["bat"]
150     else:
151         shell = options.shell
152     
153     out_dir = options.out_dir
154     if out_dir:
155         out_dir = os.path.abspath(out_dir)
156     
157     write_all_source_files(runner.cfg, logger, out_dir=out_dir, shells=shell,
158                            prefix=options.prefix, env_info=environ_info)
159     logger.write("\n", 3, False)