Salome HOME
Bug IPAL19893 4.x: debug information is in terminal.
[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     __lib__dir__ = "lib"
74     return __lib__dir__
75
76 # -----------------------------------------------------------------------------
77
78 def get_config(silent=False):
79     """
80     Get list of modules, paths.
81     
82     Read args from launch configure xml file and command line options.
83     Check variables <module>_ROOT_DIR and set list of used modules.
84     Return args, modules_list, modules_root_dir    
85     """
86     
87     # read args from launch configure xml file and command line options
88
89     #*** Test additional option
90     #*** import optparse
91     #*** help_str = "Test options addition."
92     #*** o_j = optparse.Option("-j", "--join", action="store_true", dest="join", help=help_str)
93
94     import launchConfigureParser
95     args = launchConfigureParser.get_env()
96
97     #*** Test additional option
98     #*** args = launchConfigureParser.get_env([o_j])
99     #*** if args.has_key("join"): print args["join"]
100
101     # Check variables <module>_ROOT_DIR
102     # and set list of used modules (without KERNEL)
103
104     modules_list = []
105     if args.has_key("modules"):
106         modules_list += args["modules"]
107     # KERNEL must be last in the list to locate it at the first place in PATH
108     if args["gui"] :
109         modules_list[:0] = ["GUI"]
110     modules_list[:0] = ["KERNEL"]
111     modules_list.reverse()
112
113     modules_root_dir = {}
114
115     to_remove_list=[]
116     for module in modules_list :
117         module_variable=module+"_ROOT_DIR"
118         if not os.environ.has_key(module_variable):
119             if not silent:
120                 print "*******************************************************"
121                 print "*"
122                 print "* Environment variable",module_variable,"must be set"
123                 print "* Module", module, "will be not available"
124                 print "*"
125                 print "********************************************************"
126                 pass
127             to_remove_list.append(module)
128             continue
129             pass
130         module_root_dir = os.environ[module_variable]
131         modules_root_dir[module]=module_root_dir
132
133     for to_remove in to_remove_list:
134         modules_list.remove(to_remove)
135
136     while "KERNEL" in modules_list:
137         modules_list.remove("KERNEL")
138         pass
139
140     while "GUI" in modules_list:
141         modules_list.remove("GUI")
142         pass
143
144     if "SUPERV" in modules_list and not 'supervContainer' in args['standalone']:
145         args['standalone'].append("supervContainer")
146         pass
147    
148     return args, modules_list, modules_root_dir
149
150 # -----------------------------------------------------------------------------
151
152 def set_env(args, modules_list, modules_root_dir, silent=False):
153     """Add to the PATH-variables modules specific paths"""
154     
155     python_version="python%d.%d" % sys.version_info[0:2]
156     modules_root_dir_list = []
157     if os.getenv('SALOME_BATCH') == None:
158       os.putenv('SALOME_BATCH','0')
159     if args["gui"] :
160         modules_list = modules_list[:] + ["GUI"] 
161     modules_list = modules_list[:] + ["KERNEL"] 
162     for module in modules_list :
163         if modules_root_dir.has_key(module):
164             module_root_dir = modules_root_dir[module]
165             modules_root_dir_list[:0] = [module_root_dir]
166             if sys.platform == "win32":
167               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
168                      "PATH")
169             else:
170               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
171                      "LD_LIBRARY_PATH")
172             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
173                      "PATH")
174             if os.path.exists(module_root_dir + "/examples") :
175                 add_path(os.path.join(module_root_dir,"examples"),
176                          "PYTHONPATH")
177                 pass
178             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
179                      "PYTHONPATH")
180             # add lib before site-packages to load script instead of dll if any (win32 platform)
181             add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
182                      "PYTHONPATH")
183             add_path(os.path.join(module_root_dir,get_lib_dir(),
184                                   python_version,"site-packages",
185                                   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                                   "shared_modules"),
191                      "PYTHONPATH")
192             
193             # set environment for SMESH plugins
194             if module == "SMESH" :
195                 os.environ["SMESH_MeshersList"]="StdMeshers"
196                 if not os.environ.has_key("SALOME_StdMeshersResources"):
197                     os.environ["SALOME_StdMeshersResources"] \
198                     = modules_root_dir["SMESH"]+"/share/"+salome_subdir+"/resources/smesh"
199                     pass
200                 if args.has_key("SMESH_plugins"):
201                     for plugin in args["SMESH_plugins"]:
202                         plugin_root = ""
203                         if os.environ.has_key(plugin+"_ROOT_DIR"):
204                             plugin_root = os.environ[plugin+"_ROOT_DIR"]
205                         else:
206                             # workaround to avoid modifications of existing environment
207                             if os.environ.has_key(plugin.upper()+"_ROOT_DIR"):
208                                 plugin_root = os.environ[plugin.upper()+"_ROOT_DIR"]
209                                 pass
210                             pass
211                         if plugin_root != "":
212                             os.environ["SMESH_MeshersList"] \
213                             = os.environ["SMESH_MeshersList"]+":"+plugin
214                             if not os.environ.has_key("SALOME_"+plugin+"Resources"):
215                                 os.environ["SALOME_"+plugin+"Resources"] \
216                                 = plugin_root+"/share/"+salome_subdir+"/resources/"+plugin.lower()
217                                 add_path(os.path.join(plugin_root,get_lib_dir(),python_version, "site-packages",salome_subdir), "PYTHONPATH")
218                                 add_path(os.path.join(plugin_root,get_lib_dir(),salome_subdir), "PYTHONPATH")
219                                 
220                                 if sys.platform == "win32":
221                                     add_path(os.path.join(plugin_root,get_lib_dir(),salome_subdir), "PATH")
222                                 else:
223                                     add_path(os.path.join(plugin_root,get_lib_dir(),salome_subdir), "LD_LIBRARY_PATH")
224                                     add_path(os.path.join(plugin_root,"bin",salome_subdir), "PYTHONPATH")
225                                     add_path(os.path.join(plugin_root,"bin",salome_subdir), "PATH")
226                                     pass
227                                 pass
228                             pass
229                         pass
230             
231     if sys.platform == 'win32':
232         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
233     else:
234         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
235     
236     # set trace environment variable
237     
238     if not os.environ.has_key("SALOME_trace"):
239         os.environ["SALOME_trace"]="local"
240     if args['file']:
241         os.environ["SALOME_trace"]="file:"+args['file'][0]
242     if args['logger']:
243         os.environ["SALOME_trace"]="with_logger"
244
245     # set environment for SUPERV module
246     os.environ["ENABLE_MACRO_NODE"]="1"
247     # set resources variables if not yet set
248     # Done now by launchConfigureParser.py
249     #if os.getenv("GUI_ROOT_DIR"):
250         #if not os.getenv("SalomeAppConfig"): os.environ["SalomeAppConfig"] =  os.getenv("GUI_ROOT_DIR") + "/share/salome/resources/gui"
251
252     os.environ["CSF_SALOMEDS_ResourcesDefaults"] \
253     = os.path.join(modules_root_dir["KERNEL"],"share",
254                    salome_subdir,"resources","kernel")
255
256     if "GUI" in modules_list:
257         if sys.platform == 'win32':
258             graphicShr = "OCCViewer.dll"        
259         else:
260             graphicShr = "libOCCViewer.so"      
261         os.environ["CSF_GraphicShr"] \
262         = os.path.join(modules_root_dir["GUI"],"lib","salome",graphicShr)
263
264     if "GEOM" in modules_list:
265         if verbose() and not silent: print "GEOM OCAF Resources" 
266         
267         # set CSF_PluginDefaults variable only if it is not customized
268         # by the user
269
270         if not os.getenv("CSF_PluginDefaults"):
271             os.environ["CSF_PluginDefaults"] \
272             = os.path.join(modules_root_dir["GEOM"],"share",
273                            salome_subdir,"resources","geom")
274         os.environ["CSF_GEOMDS_ResourcesDefaults"] \
275         = os.path.join(modules_root_dir["GEOM"],"share",
276                        salome_subdir,"resources","geom")
277         if verbose() and not silent: print "GEOM Shape Healing Resources"
278         os.environ["CSF_ShHealingDefaults"] \
279         = os.path.join(modules_root_dir["GEOM"],"share",
280                        salome_subdir,"resources","geom")
281
282 # -----------------------------------------------------------------------------
283
284 def main(silent=False):
285     args, modules_list, modules_root_dir = get_config(silent=silent)
286     set_env(args, modules_list, modules_root_dir, silent=silent)
287     return args
288
289 # -----------------------------------------------------------------------------
290
291 if __name__ == "__main__":
292    import user
293    args = main()