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