]> SALOME platform Git repositories - modules/kernel.git/blob - bin/setenv.py
Salome HOME
Merging from V3_2_6pre4
[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 not os.path.samefile(_dir, directory):
51                   newpath.append(_dir)
52             else:
53                 if os.path.abspath(_dir) != os.path.abspath(directory):
54                   newpath.append(_dir)
55             pass
56         import string
57         newpath[:0] = [ directory ]
58         newpath = string.join(newpath, splitsym)
59         os.environ[variable_name] = newpath
60         if variable_name == "PYTHONPATH":
61             sys.path[:0] = [directory]
62
63 # -----------------------------------------------------------------------------
64
65 __lib__dir__ = None
66 def get_lib_dir():
67     global __lib__dir__
68     if __lib__dir__: return __lib__dir__
69     import platform
70     if platform.architecture()[0] == "64bit":
71         if platform.machine() == "ia64":
72             __lib__dir__ = "lib"
73         else:
74             __lib__dir__ = "lib64"
75     else:
76         __lib__dir__ = "lib"
77     return get_lib_dir()
78
79 # -----------------------------------------------------------------------------
80
81 def get_config():
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()
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 += args["modules"]
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.upper()+"_ROOT_DIR"
121         if not os.environ.has_key(module_variable):
122             print "*******************************************************"
123             print "*"
124             print "* Environment variable",module_variable,"must be set"
125             print "* Module", module, "will be not available"
126             print "*"
127             print "********************************************************"
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 'superv' in args['standalone']:
146         args['standalone'].append("superv")
147         pass
148    
149     return args, modules_list, modules_root_dir
150
151 # -----------------------------------------------------------------------------
152
153 def set_env(args, modules_list, modules_root_dir):
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 args["gui"] :
159         modules_list = modules_list[:] + ["GUI"] 
160     modules_list = modules_list[:] + ["KERNEL"] 
161     for module in modules_list :
162         if modules_root_dir.has_key(module):
163             module_root_dir = modules_root_dir[module]
164             modules_root_dir_list[:0] = [module_root_dir]
165             if sys.platform == "win32":
166               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
167                      "PATH")
168             else:
169               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
170                      "LD_LIBRARY_PATH")
171             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
172                      "PATH")
173             if os.path.exists(module_root_dir + "/examples") :
174                 add_path(os.path.join(module_root_dir,"examples"),
175                          "PYTHONPATH")
176                 pass
177             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
178                      "PYTHONPATH")
179             # add lib before site-packages to load script instead of dll if any (win32 platform)
180             add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
181                      "PYTHONPATH")
182             add_path(os.path.join(module_root_dir,get_lib_dir(),
183                                   python_version,"site-packages",
184                                   salome_subdir),
185                      "PYTHONPATH")
186             add_path(os.path.join(module_root_dir,get_lib_dir(),
187                                   python_version,"site-packages",
188                                   salome_subdir,
189                                   "shared_modules"),
190                      "PYTHONPATH")
191             pass
192         pass
193
194     if sys.platform == 'win32':
195         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
196     else:
197         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
198     
199     # set trace environment variable
200     
201     if not os.environ.has_key("SALOME_trace"):
202         os.environ["SALOME_trace"]="local"
203     if args['file']:
204         os.environ["SALOME_trace"]="file:"+args['file'][0]
205     if args['logger']:
206         os.environ["SALOME_trace"]="with_logger"
207
208     # set environment for SMESH plugins
209
210     if "SMESH" in modules_list:
211         os.environ["SMESH_MeshersList"]="StdMeshers"
212         if not os.environ.has_key("SALOME_StdMeshersResources"):
213             os.environ["SALOME_StdMeshersResources"] \
214             = modules_root_dir["SMESH"]+"/share/"+salome_subdir+"/resources/smesh"
215             pass
216         if args.has_key("SMESH_plugins"):
217             for plugin in args["SMESH_plugins"]:
218                 plugin_root = ""
219                 if os.environ.has_key(plugin+"_ROOT_DIR"):
220                     plugin_root = os.environ[plugin+"_ROOT_DIR"]
221                 else:
222                     # workaround to avoid modifications of existing environment
223                     if os.environ.has_key(plugin.upper()+"_ROOT_DIR"):
224                         plugin_root = os.environ[plugin.upper()+"_ROOT_DIR"]
225                         pass
226                     pass
227                 if plugin_root != "":
228                     os.environ["SMESH_MeshersList"] \
229                     = os.environ["SMESH_MeshersList"]+":"+plugin
230                     if not os.environ.has_key("SALOME_"+plugin+"Resources"):
231                         os.environ["SALOME_"+plugin+"Resources"] \
232                         = plugin_root+"/share/"+salome_subdir+"/resources/"+plugin.lower()
233                     add_path(os.path.join(plugin_root,get_lib_dir(),python_version,
234                                           "site-packages",salome_subdir),
235                              "PYTHONPATH")
236                     add_path(os.path.join(plugin_root,get_lib_dir(),salome_subdir),
237                              "PYTHONPATH")
238
239
240                     if sys.platform == "win32":
241                       add_path(os.path.join(plugin_root,get_lib_dir(),salome_subdir),
242                                "PATH")
243                     else:
244                       add_path(os.path.join(plugin_root,get_lib_dir(),salome_subdir),
245                                "LD_LIBRARY_PATH")
246                     add_path(os.path.join(plugin_root,"bin",salome_subdir),
247                              "PYTHONPATH")
248                     add_path(os.path.join(plugin_root,"bin",salome_subdir),
249                              "PATH")
250             pass
251         pass
252
253     # set environment for SUPERV module
254     os.environ["ENABLE_MACRO_NODE"]="1"
255     # set resources variables if not yet set
256     # Done now by launchConfigureParser.py
257     #if os.getenv("GUI_ROOT_DIR"):
258         #if not os.getenv("SalomeAppConfig"): os.environ["SalomeAppConfig"] =  os.getenv("GUI_ROOT_DIR") + "/share/salome/resources/gui"
259
260     # set CSF_PluginDefaults variable only if it is not customized
261     # by the user
262     if not os.getenv("CSF_PluginDefaults"):
263         os.environ["CSF_PluginDefaults"] \
264         = os.path.join(modules_root_dir["KERNEL"],"share",
265                        salome_subdir,"resources","kernel")
266     os.environ["CSF_SALOMEDS_ResourcesDefaults"] \
267     = os.path.join(modules_root_dir["KERNEL"],"share",
268                    salome_subdir,"resources","kernel")
269
270     if "GEOM" in modules_list:
271         if verbose(): print "GEOM OCAF Resources"
272         os.environ["CSF_GEOMDS_ResourcesDefaults"] \
273         = os.path.join(modules_root_dir["GEOM"],"share",
274                        salome_subdir,"resources","geom")
275         if verbose(): print "GEOM Shape Healing Resources"
276         os.environ["CSF_ShHealingDefaults"] \
277         = os.path.join(modules_root_dir["GEOM"],"share",
278                        salome_subdir,"resources","geom")
279
280 # -----------------------------------------------------------------------------
281
282 def main():
283     args, modules_list, modules_root_dir = get_config()
284     set_env(args, modules_list, modules_root_dir)
285     return args
286
287 # -----------------------------------------------------------------------------
288
289 if __name__ == "__main__":
290    import user
291    args = main()