Salome HOME
Add the run command
[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     '''Generates the environment files.
51     
52     :param config Config: The global configuration
53     :param logger Logger: The logger instance to use for the display 
54                           and logging
55     :param out_dir str: The path to the directory where the files will be put
56     :param src_root str: The path to the directory where the sources are
57     :param silent boolean: If True, do not print anything in the terminal
58     :param shells list: The list of shells to generate
59     :param prefix str: The prefix to add to the file names.
60     :param env_info str: The list of products to add in the files.
61     :return: The list of the generated files.
62     :rtype: List
63     '''
64         
65     if not out_dir:
66         out_dir = config.APPLICATION.workdir
67
68     if not os.path.exists(out_dir):
69         raise src.SatException(_("Target directory not found: %s") % out_dir)
70
71     if not silent:
72         logger.write(_("Creating environment files for %s\n") % 
73                      src.printcolors.printcLabel(config.APPLICATION.name), 2)
74         src.printcolors.print_value(logger,
75                                     _("Target"),
76                                     src.printcolors.printcInfo(out_dir), 3)
77         logger.write("\n", 3, False)
78     
79     shells_list = []
80     all_shells = C_ALL_SHELL
81     if "all" in shells:
82         shells = all_shells
83     else:
84         shells = filter(lambda l: l in all_shells, shells)
85
86     for shell in shells:
87         if shell not in C_SHELLS:
88             logger.write(_("Unknown shell: %s\n") % shell, 2)
89         else:
90             shells_list.append(src.environment.Shell(shell, C_SHELLS[shell]))
91     
92     writer = src.environment.FileEnvWriter(config,
93                                            logger,
94                                            out_dir,
95                                            src_root,
96                                            env_info)
97     writer.silent = silent
98     files = []
99     for_build = True
100     for_launch = False
101     for shell in shells_list:
102         files.append(writer.write_env_file("%s_launch.%s" %
103                                            (prefix, shell.extension),
104                                            for_launch,
105                                            shell.name))
106         files.append(writer.write_env_file("%s_build.%s" %
107                                            (prefix, shell.extension),
108                                            for_build,
109                                            shell.name))
110
111     return files
112
113 ##################################################
114
115 ##
116 # Describes the command
117 def description():
118     return _("The environ command generates the "
119                 "environment files of your application.")
120
121 ##
122 # Runs the command.
123 def run(args, runner, logger):
124     (options, args) = parser.parse_args(args)
125
126     # check that the command was called with an application
127     src.check_config_has_application( runner.cfg )
128     
129     if options.products is None:
130         environ_info = None
131     else:
132         # add products specified by user (only products 
133         # included in the application)
134         environ_info = filter(lambda l:
135                               l in runner.cfg.APPLICATION.products.keys(),
136                               options.products)
137     
138     if options.shell == []:
139         shell = ["bash"]
140         if src.architecture.is_windows():
141             shell = ["bat"]
142     else:
143         shell = options.shell
144     
145     out_dir = options.out_dir
146     if out_dir:
147         out_dir = os.path.abspath(out_dir)
148     
149     write_all_source_files(runner.cfg, logger, out_dir=out_dir, shells=shell,
150                            prefix=options.prefix, env_info=environ_info)
151     logger.write("\n", 3, False)