Salome HOME
CCAR: change computer name in container log file name
[modules/kernel.git] / bin / setenv.py
1 #!/usr/bin/env python
2
3 # Copyright (C) 2005  OPEN CASCADE, CEA, EDF R&D, LEG
4 #           PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
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 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20
21
22 import sys, os, string, glob, time, pickle
23 import orbmodule
24 from launchConfigureParser import verbose
25
26 # this file is extraction of set_env from runSalome.py
27 # for reusage in automated tests
28
29 # salome_subdir variable is used for composing paths like $KERNEL_ROOT_DIR/share/salome/resources, etc.
30 # before moving to SUIT-based gui, instead of salome_subdir there was args['appname'] used.
31 # but after - 'appname'  = "SalomeApp", so using it in making the subdirectory is an error.
32 salome_subdir = "salome"
33
34
35 # -----------------------------------------------------------------------------
36
37 def add_path(directory, variable_name):
38     """Function helper to add environment variables"""
39     if sys.platform == "win32":
40       splitsym = ";"
41     else:
42       splitsym = ":"
43     if not os.environ.has_key(variable_name):
44         os.environ[variable_name] = ""
45         pass
46     if os.path.exists(directory):
47         newpath=[]
48         for _dir in os.environ[variable_name].split(splitsym):
49             if os.path.exists(_dir):
50                 if sys.platform != "win32":
51                     if not os.path.samefile(_dir, directory):
52                         newpath.append(_dir)
53                 else:
54                     newpath.append(_dir)
55             else:
56                 if os.path.abspath(_dir) != os.path.abspath(directory):
57                   newpath.append(_dir)
58             pass
59         import string
60         newpath[:0] = [ directory ]
61         newpath = string.join(newpath, splitsym)
62         os.environ[variable_name] = newpath
63         if variable_name == "PYTHONPATH":
64             sys.path[:0] = [directory]
65
66 # -----------------------------------------------------------------------------
67
68 __lib__dir__ = None
69 def get_lib_dir():
70     global __lib__dir__
71     if __lib__dir__: return __lib__dir__
72     import platform
73     if platform.architecture()[0] == "64bit":
74         if platform.machine() == "ia64":
75             __lib__dir__ = "lib"
76         else:
77             __lib__dir__ = "lib64"
78     else:
79         __lib__dir__ = "lib"
80     return __lib__dir__
81
82 # -----------------------------------------------------------------------------
83
84 def get_config():
85     """
86     Get list of modules, paths.
87     
88     Read args from launch configure xml file and command line options.
89     Check variables <module>_ROOT_DIR and set list of used modules.
90     Return args, modules_list, modules_root_dir    
91     """
92     
93     # read args from launch configure xml file and command line options
94
95     #*** Test additional option
96     #*** import optparse
97     #*** help_str = "Test options addition."
98     #*** o_j = optparse.Option("-j", "--join", action="store_true", dest="join", help=help_str)
99
100     import launchConfigureParser
101     args = launchConfigureParser.get_env()
102
103     #*** Test additional option
104     #*** args = launchConfigureParser.get_env([o_j])
105     #*** if args.has_key("join"): print args["join"]
106
107     # Check variables <module>_ROOT_DIR
108     # and set list of used modules (without KERNEL)
109
110     modules_list = []
111     if args.has_key("modules"):
112         modules_list += args["modules"]
113     # KERNEL must be last in the list to locate it at the first place in PATH
114     if args["gui"] :
115         modules_list[:0] = ["GUI"]
116     modules_list[:0] = ["KERNEL"]
117     modules_list.reverse()
118
119     modules_root_dir = {}
120
121     to_remove_list=[]
122     for module in modules_list :
123         module_variable=module+"_ROOT_DIR"
124         if not os.environ.has_key(module_variable):
125             print "*******************************************************"
126             print "*"
127             print "* Environment variable",module_variable,"must be set"
128             print "* Module", module, "will be not available"
129             print "*"
130             print "********************************************************"
131             to_remove_list.append(module)
132             continue
133             pass
134         module_root_dir = os.environ[module_variable]
135         modules_root_dir[module]=module_root_dir
136
137     for to_remove in to_remove_list:
138         modules_list.remove(to_remove)
139
140     while "KERNEL" in modules_list:
141         modules_list.remove("KERNEL")
142         pass
143
144     while "GUI" in modules_list:
145         modules_list.remove("GUI")
146         pass
147
148     if "SUPERV" in modules_list and not 'supervContainer' in args['standalone']:
149         args['standalone'].append("supervContainer")
150         pass
151    
152     return args, modules_list, modules_root_dir
153
154 # -----------------------------------------------------------------------------
155
156 def set_env(args, modules_list, modules_root_dir):
157     """Add to the PATH-variables modules specific paths"""
158     
159     python_version="python%d.%d" % sys.version_info[0:2]
160     modules_root_dir_list = []
161     if os.getenv('SALOME_BATCH') == None:
162       os.putenv('SALOME_BATCH','0')
163     if args["gui"] :
164         modules_list = modules_list[:] + ["GUI"] 
165     modules_list = modules_list[:] + ["KERNEL"] 
166     for module in modules_list :
167         if modules_root_dir.has_key(module):
168             module_root_dir = modules_root_dir[module]
169             modules_root_dir_list[:0] = [module_root_dir]
170             if sys.platform == "win32":
171               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
172                      "PATH")
173             else:
174               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
175                      "LD_LIBRARY_PATH")
176             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
177                      "PATH")
178             if os.path.exists(module_root_dir + "/examples") :
179                 add_path(os.path.join(module_root_dir,"examples"),
180                          "PYTHONPATH")
181                 pass
182             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
183                      "PYTHONPATH")
184             # add lib before site-packages to load script instead of dll if any (win32 platform)
185             add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
186                      "PYTHONPATH")
187             add_path(os.path.join(module_root_dir,get_lib_dir(),
188                                   python_version,"site-packages",
189                                   salome_subdir),
190                      "PYTHONPATH")
191             add_path(os.path.join(module_root_dir,get_lib_dir(),
192                                   python_version,"site-packages",
193                                   salome_subdir,
194                                   "shared_modules"),
195                      "PYTHONPATH")
196             pass
197         pass
198
199     if sys.platform == 'win32':
200         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
201     else:
202         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
203     
204     # set trace environment variable
205     
206     if not os.environ.has_key("SALOME_trace"):
207         os.environ["SALOME_trace"]="local"
208     if args['file']:
209         os.environ["SALOME_trace"]="file:"+args['file'][0]
210     if args['logger']:
211         os.environ["SALOME_trace"]="with_logger"
212
213     # set environment for SMESH plugins
214
215     if "SMESH" in modules_list:
216         os.environ["SMESH_MeshersList"]="StdMeshers"
217         if not os.environ.has_key("SALOME_StdMeshersResources"):
218             os.environ["SALOME_StdMeshersResources"] \
219             = modules_root_dir["SMESH"]+"/share/"+salome_subdir+"/resources/smesh"
220             pass
221         if args.has_key("SMESH_plugins"):
222             for plugin in args["SMESH_plugins"]:
223                 plugin_root = ""
224                 if os.environ.has_key(plugin+"_ROOT_DIR"):
225                     plugin_root = os.environ[plugin+"_ROOT_DIR"]
226                 else:
227                     # workaround to avoid modifications of existing environment
228                     if os.environ.has_key(plugin.upper()+"_ROOT_DIR"):
229                         plugin_root = os.environ[plugin.upper()+"_ROOT_DIR"]
230                         pass
231                     pass
232                 if plugin_root != "":
233                     os.environ["SMESH_MeshersList"] \
234                     = os.environ["SMESH_MeshersList"]+":"+plugin
235                     if not os.environ.has_key("SALOME_"+plugin+"Resources"):
236                         os.environ["SALOME_"+plugin+"Resources"] \
237                         = plugin_root+"/share/"+salome_subdir+"/resources/"+plugin.lower()
238                     add_path(os.path.join(plugin_root,get_lib_dir(),python_version,
239                                           "site-packages",salome_subdir),
240                              "PYTHONPATH")
241                     add_path(os.path.join(plugin_root,get_lib_dir(),salome_subdir),
242                              "PYTHONPATH")
243
244
245                     if sys.platform == "win32":
246                       add_path(os.path.join(plugin_root,get_lib_dir(),salome_subdir),
247                           "PATH")
248                     else:
249                       add_path(os.path.join(plugin_root,get_lib_dir(),salome_subdir),
250                                "LD_LIBRARY_PATH")
251                     add_path(os.path.join(plugin_root,"bin",salome_subdir),
252                              "PYTHONPATH")
253                     add_path(os.path.join(plugin_root,"bin",salome_subdir),
254                              "PATH")
255             pass
256         pass
257
258     # set environment for SUPERV module
259     os.environ["ENABLE_MACRO_NODE"]="1"
260     # set resources variables if not yet set
261     # Done now by launchConfigureParser.py
262     #if os.getenv("GUI_ROOT_DIR"):
263         #if not os.getenv("SalomeAppConfig"): os.environ["SalomeAppConfig"] =  os.getenv("GUI_ROOT_DIR") + "/share/salome/resources/gui"
264
265     os.environ["CSF_SALOMEDS_ResourcesDefaults"] \
266     = os.path.join(modules_root_dir["KERNEL"],"share",
267                    salome_subdir,"resources","kernel")
268
269     if "GEOM" in modules_list:
270         if verbose(): print "GEOM OCAF Resources" 
271         
272         # set CSF_PluginDefaults variable only if it is not customized
273         # by the user
274
275         if not os.getenv("CSF_PluginDefaults"):
276             os.environ["CSF_PluginDefaults"] \
277             = os.path.join(modules_root_dir["GEOM"],"share",
278                            salome_subdir,"resources","geom")
279         os.environ["CSF_GEOMDS_ResourcesDefaults"] \
280         = os.path.join(modules_root_dir["GEOM"],"share",
281                        salome_subdir,"resources","geom")
282         if verbose(): print "GEOM Shape Healing Resources"
283         os.environ["CSF_ShHealingDefaults"] \
284         = os.path.join(modules_root_dir["GEOM"],"share",
285                        salome_subdir,"resources","geom")
286
287 # -----------------------------------------------------------------------------
288
289 def main():
290     args, modules_list, modules_root_dir = get_config()
291     set_env(args, modules_list, modules_root_dir)
292     return args
293
294 # -----------------------------------------------------------------------------
295
296 if __name__ == "__main__":
297    import user
298    args = main()