Salome HOME
Appli: do not add empty modules
[modules/kernel.git] / bin / setenv.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 import sys, os, string, glob, time, pickle
26 import orbmodule
27 from launchConfigureParser import verbose
28
29 # this file is extraction of set_env from runSalome.py
30 # for reusage in automated tests
31
32 # salome_subdir variable is used for composing paths like $KERNEL_ROOT_DIR/share/salome/resources, etc.
33 # before moving to SUIT-based gui, instead of salome_subdir there was args['appname'] used.
34 # but after - 'appname'  = "SalomeApp", so using it in making the subdirectory is an error.
35 salome_subdir = "salome"
36
37
38 # -----------------------------------------------------------------------------
39
40 def add_path(directory, variable_name):
41     """Function helper to add environment variables"""
42     if sys.platform == "win32":
43       splitsym = ";"
44     else:
45       splitsym = ":"
46     if not os.environ.has_key(variable_name):
47         os.environ[variable_name] = ""
48         pass
49     if os.path.exists(directory):
50         newpath=[]
51         for _dir in os.environ[variable_name].split(splitsym):
52             if os.path.exists(_dir):
53                 if sys.platform != "win32":
54                     if not os.path.samefile(_dir, directory):
55                         newpath.append(_dir)
56                 else:
57                     newpath.append(_dir)
58             else:
59                 if os.path.abspath(_dir) != os.path.abspath(directory):
60                   newpath.append(_dir)
61             pass
62         import string
63         newpath[:0] = [ directory ]
64         newpath = string.join(newpath, splitsym)
65         os.environ[variable_name] = newpath
66         if variable_name == "PYTHONPATH":
67             sys.path[:0] = [os.path.realpath(directory)]
68
69 # -----------------------------------------------------------------------------
70
71 __lib__dir__ = None
72 def get_lib_dir():
73     global __lib__dir__
74     if __lib__dir__: return __lib__dir__
75     import platform
76     __lib__dir__ = "lib"
77     return __lib__dir__
78
79 # -----------------------------------------------------------------------------
80
81 def get_config(silent=False, exeName=None):
82     """
83     Get list of modules, paths.
84
85     Read args from launch configure xml file and command line options.
86     Check variables <module>_ROOT_DIR and set list of used modules.
87     Return args, modules_list, modules_root_dir
88     """
89
90     # read args from launch configure xml file and command line options
91
92     #*** Test additional option
93     #*** import optparse
94     #*** help_str = "Test options addition."
95     #*** o_j = optparse.Option("-j", "--join", action="store_true", dest="join", help=help_str)
96
97     import launchConfigureParser
98     args = launchConfigureParser.get_env(exeName=exeName)
99
100     #*** Test additional option
101     #*** args = launchConfigureParser.get_env([o_j])
102     #*** if args.has_key("join"): print args["join"]
103
104     # Check variables <module>_ROOT_DIR
105     # and set list of used modules (without KERNEL)
106
107     modules_list = []
108     if args.has_key("modules"):
109         modules_list += [a for a in args["modules"] if a.strip()]
110     # KERNEL must be last in the list to locate it at the first place in PATH
111     if args["gui"] :
112         modules_list[:0] = ["GUI"]
113     modules_list[:0] = ["KERNEL"]
114     modules_list.reverse()
115
116     modules_root_dir = {}
117
118     to_remove_list=[]
119     for module in modules_list :
120         module_variable=module+"_ROOT_DIR"
121         if not os.environ.has_key(module_variable):
122             if not silent:
123                 print "*******************************************************"
124                 print "*"
125                 print "* Environment variable",module_variable,"must be set"
126                 print "* Module", module, "will be not available"
127                 print "*"
128                 print "********************************************************"
129                 pass
130             to_remove_list.append(module)
131             continue
132             pass
133         module_root_dir = os.environ[module_variable]
134         modules_root_dir[module]=module_root_dir
135
136     for to_remove in to_remove_list:
137         modules_list.remove(to_remove)
138
139     while "KERNEL" in modules_list:
140         modules_list.remove("KERNEL")
141         pass
142
143     while "GUI" in modules_list:
144         modules_list.remove("GUI")
145         pass
146
147     return args, modules_list, modules_root_dir
148
149 # -----------------------------------------------------------------------------
150
151 def set_env(args, modules_list, modules_root_dir, silent=False):
152     """Add to the PATH-variables modules specific paths"""
153
154     import os
155     from salome_utils import getLogDir, generateFileName, makeTmpDir, getPortNumber
156
157     # create temporary directory for environment files needed by modules from the list
158     port = getPortNumber(False)
159     if port:
160         tmp_dir = getLogDir()
161         env_dir = generateFileName(tmp_dir, prefix="env", with_port=True)
162         makeTmpDir(env_dir)
163         pass
164
165     python_version="python%d.%d" % sys.version_info[0:2]
166     modules_root_dir_list = []
167     if os.getenv('SALOME_BATCH') == None:
168       os.putenv('SALOME_BATCH','0')
169     if args["gui"] :
170         modules_list = modules_list[:] + ["GUI"]
171     modules_list = modules_list[:] + ["KERNEL"]
172     for module in modules_list :
173         if modules_root_dir.has_key(module):
174             module_root_dir = modules_root_dir[module]
175             if module_root_dir not in modules_root_dir_list:
176               modules_root_dir_list[:0] = [module_root_dir]
177             if sys.platform == "win32":
178               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
179                      "PATH")
180             elif sys.platform == "darwin":
181               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
182                      "DYLD_LIBRARY_PATH")
183             else:
184               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
185                      "LD_LIBRARY_PATH")
186             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
187                      "PATH")
188             if os.path.exists(module_root_dir + "/examples") :
189                 add_path(os.path.join(module_root_dir,"examples"),
190                          "PYTHONPATH")
191                 pass
192             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
193                      "PYTHONPATH")
194             # add lib before site-packages to load script instead of dll if any (win32 platform)
195             add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
196                      "PYTHONPATH")
197             add_path(os.path.join(module_root_dir,get_lib_dir(),
198                                   python_version,"site-packages",
199                                   salome_subdir),
200                      "PYTHONPATH")
201             import platform
202             if platform.machine() == "x86_64":
203                 add_path(os.path.join(module_root_dir,"lib64",
204                                       python_version,"site-packages",
205                                       salome_subdir),
206                          "PYTHONPATH")
207                 pass
208             add_path(os.path.join(module_root_dir,get_lib_dir(),
209                                   python_version,"site-packages",
210                                   salome_subdir,
211                                   "shared_modules"),
212                      "PYTHONPATH")
213
214             # set environment by modules from the list
215             if port:
216                 try:
217                     mod=__import__(module.lower()+"_setenv")
218                     mod.set_env(args)
219                     pass
220                 except:
221                     pass
222                 pass
223             pass
224         pass
225
226     if sys.platform == 'win32':
227         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
228     else:
229         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
230
231     # set trace environment variable
232
233     if not os.environ.has_key("SALOME_trace"):
234         os.environ["SALOME_trace"]="local"
235     if args['file']:
236         os.environ["SALOME_trace"]="file:"+args['file'][0]
237     if args['logger']:
238         os.environ["SALOME_trace"]="with_logger"
239
240     # set resources variables if not yet set
241
242     os.environ["CSF_SALOMEDS_ResourcesDefaults"] \
243     = os.path.join(modules_root_dir["KERNEL"],"share",
244                    salome_subdir,"resources","kernel")
245
246 # -----------------------------------------------------------------------------
247
248 def main(silent=False, exeName=None):
249     args, modules_list, modules_root_dir = get_config(silent=silent, exeName=exeName)
250     set_env(args, modules_list, modules_root_dir, silent=silent)
251     return args
252
253 # -----------------------------------------------------------------------------
254
255 if __name__ == "__main__":
256    import user
257    args = main()