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