Salome HOME
Add Builder class
[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: bash (default), batch or all."), [])
26 parser.add_option('p', 'products', 'list2', 'products',
27     _("Includes only the specified products."))
28 parser.add_option('p', 'prefix', 'string', 'prefix',
29     _("Specifies the prefix for the environment files."), "env")
30 parser.add_option('t', 'target', 'string', 'out_dir',
31     _("Specifies the directory path where to put the environment files."), None)
32
33 # list of available shells with extensions
34 C_SHELLS = { "bash": "sh", "batch": "bat" }
35 C_ALL_SHELL = [ "bash", "batch" ]
36
37
38 ##
39 # Writes all the environment files
40 def write_all_source_files(config, logger, out_dir=None, src_root=None,
41                            single_dir=None, silent=False, shells=["bash"], prefix="env", env_info=None):
42     if not out_dir:
43         out_dir = config.APPLICATION.workdir
44
45     if not os.path.exists(out_dir):
46         raise src.SatException(_("Target directory not found: %s") % out_dir)
47
48     if not silent:
49         logger.write(_("Creating environment files for %s\n") % src.printcolors.printcLabel(config.APPLICATION.name), 2)
50         src.printcolors.print_value(logger, _("Target"), src.printcolors.printcInfo(out_dir), 3)
51         logger.write("\n", 3, False)
52     
53     shells_list = []
54     all_shells = C_ALL_SHELL
55     if "all" in shells:
56         shells = all_shells
57     else:
58         shells = filter(lambda l: l in all_shells, shells)
59
60     for shell in shells:
61         if shell not in C_SHELLS:
62             logger.write(_("Unknown shell: %s\n") % shell, 2)
63         else:
64             shells_list.append(src.environment.Shell(shell, C_SHELLS[shell]))
65     
66     writer = src.environment.FileEnvWriter(config, logger, out_dir, src_root, single_dir, env_info)
67     writer.silent = silent
68     files = []
69     for_build = True
70     for_launch = False
71     for shell in shells_list:
72         files.append(writer.write_env_file("%s_launch.%s" % (prefix, shell.extension), for_launch, shell.name))
73         files.append(writer.write_env_file("%s_build.%s" % (prefix, shell.extension),  for_build,  shell.name))
74
75     return files
76
77 ##################################################
78
79 ##
80 # Describes the command
81 def description():
82     return _("""The environ command generates the "
83                 "environment files of your application.""")
84
85 ##
86 # Runs the command.
87 def run(args, runner, logger):
88     (options, args) = parser.parse_args(args)
89
90     # check that the command was called with an application
91     src.check_config_has_application( runner.cfg )
92    
93     if options.products is None:
94         environ_info = None
95     else:
96         environ_info = {}
97
98         # add products specified by user (only products included in the application)
99         environ_info['products'] = filter(lambda l: l in runner.cfg.APPLICATION.products.keys(), options.products)
100     
101     if options.shell == []:
102         shell = ["bash"]
103         if src.architecture.is_windows():
104             shell = ["batch"]
105     else:
106         shell = options.shell
107     
108     write_all_source_files(runner.cfg, logger, out_dir=options.out_dir, shells=shell,
109                            prefix=options.prefix, env_info=environ_info)
110     logger.write("\n", 3, False)