]> SALOME platform Git repositories - modules/kernel.git/blob - bin/setenv.py
Salome HOME
Updare for path with space
[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             pass
40         import string
41         newpath[:0] = [ directory ]
42         newpath = string.join(newpath, splitsym)
43         os.environ[variable_name] = newpath
44         if variable_name == "PYTHONPATH":
45             sys.path[:0] = [directory]
46
47 # -----------------------------------------------------------------------------
48
49 def get_config():
50     """
51     Get list of modules, paths.
52     
53     Read args from launch configure xml file and command line options.
54     Check variables <module>_ROOT_DIR and set list of used modules.
55     Return args, modules_list, modules_root_dir    
56     """
57     
58     # read args from launch configure xml file and command line options
59     
60     import launchConfigureParser
61     args = launchConfigureParser.args
62     
63     # Check variables <module>_ROOT_DIR
64     # and set list of used modules (without KERNEL)
65
66     modules_list = []
67     if args.has_key("modules"):
68         modules_list += args["modules"]
69     # KERNEL must be last in the list to locate it at the first place in PATH
70     if args["gui"] :
71         modules_list[:0] = ["GUI"]
72     modules_list[:0] = ["KERNEL"]
73     modules_list.reverse()
74
75     modules_root_dir = {}
76
77     to_remove_list=[]
78     for module in modules_list :
79         module_variable=module.upper()+"_ROOT_DIR"
80         if not os.environ.has_key(module_variable):
81             print "*******************************************************"
82             print "*"
83             print "* Environment variable",module_variable,"must be set"
84             print "* Module", module, "will be not available"
85             print "*"
86             print "********************************************************"
87             to_remove_list.append(module)
88             continue
89             pass
90         module_root_dir = os.environ[module_variable]
91         modules_root_dir[module]=module_root_dir
92
93     for to_remove in to_remove_list:
94         modules_list.remove(to_remove)
95
96     while "KERNEL" in modules_list:
97         modules_list.remove("KERNEL")
98         pass
99
100     while "GUI" in modules_list:
101         modules_list.remove("GUI")
102         pass
103
104     if "SUPERV" in modules_list and not 'superv' in args['standalone']:
105         args['standalone'].append("superv")
106         pass
107    
108     return args, modules_list, modules_root_dir
109
110 # -----------------------------------------------------------------------------
111
112 def set_env(args, modules_list, modules_root_dir):
113     """Add to the PATH-variables modules specific paths"""
114     
115     python_version="python%d.%d" % sys.version_info[0:2]
116     modules_root_dir_list = []
117     if args["gui"] :
118         modules_list = modules_list[:] + ["GUI"] 
119     modules_list = modules_list[:] + ["KERNEL"] 
120     for module in modules_list :
121         if modules_root_dir.has_key(module):
122             module_root_dir = modules_root_dir[module]
123             modules_root_dir_list[:0] = [module_root_dir]
124             if sys.platform == "win32":
125               add_path(os.path.join(module_root_dir,"lib",salome_subdir),
126                      "PATH")
127             else:
128               add_path(os.path.join(module_root_dir,"lib",salome_subdir),
129                      "LD_LIBRARY_PATH")
130             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
131                      "PATH")
132             if os.path.exists(module_root_dir + "/examples") :
133                 add_path(os.path.join(module_root_dir,"examples"),
134                          "PYTHONPATH")
135                 pass
136             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
137                      "PYTHONPATH")
138             # add lib before site-packages to load script instead of dll if any (win32 platform)
139             add_path(os.path.join(module_root_dir,"lib",salome_subdir),
140                      "PYTHONPATH")
141             add_path(os.path.join(module_root_dir,"lib",
142                                   python_version,"site-packages",
143                                   salome_subdir),
144                      "PYTHONPATH")
145             add_path(os.path.join(module_root_dir,"lib",
146                                   python_version,"site-packages",
147                                   salome_subdir,
148                                   "shared_modules"),
149                      "PYTHONPATH")
150             pass
151         pass
152
153     if sys.platform == 'win32':
154         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
155     else:
156         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
157     
158     # set trace environment variable
159     
160     if not os.environ.has_key("SALOME_trace"):
161         os.environ["SALOME_trace"]="local"
162     if args['file']:
163         os.environ["SALOME_trace"]="file:"+args['file'][0]
164     if args['logger']:
165         os.environ["SALOME_trace"]="with_logger"
166
167     # set environment for SMESH plugins
168
169     if "SMESH" in modules_list:
170         os.environ["SMESH_MeshersList"]="StdMeshers"
171         if not os.environ.has_key("SALOME_StdMeshersResources"):
172             os.environ["SALOME_StdMeshersResources"] \
173             = modules_root_dir["SMESH"]+"/share/"+args["appname"]+"/resources"
174             pass
175         if args.has_key("SMESH_plugins"):
176             for plugin in args["SMESH_plugins"]:
177                 plugin_root = ""
178                 if os.environ.has_key(plugin+"_ROOT_DIR"):
179                     plugin_root = os.environ[plugin+"_ROOT_DIR"]
180                 else:
181                     # workaround to avoid modifications of existing environment
182                     if os.environ.has_key(plugin.upper()+"_ROOT_DIR"):
183                         plugin_root = os.environ[plugin.upper()+"_ROOT_DIR"]
184                         pass
185                     pass
186                 if plugin_root != "":
187                     os.environ["SMESH_MeshersList"] \
188                     = os.environ["SMESH_MeshersList"]+":"+plugin
189                     if not os.environ.has_key("SALOME_"+plugin+"Resources"):
190                         os.environ["SALOME_"+plugin+"Resources"] \
191                         = plugin_root+"/share/"+args["appname"]+"/resources"
192                     add_path(os.path.join(plugin_root,"lib",python_version,
193                                           "site-packages",salome_subdir),
194                              "PYTHONPATH")
195                     add_path(os.path.join(plugin_root,"lib",salome_subdir),
196                              "PYTHONPATH")
197                     add_path(os.path.join(plugin_root,"lib",salome_subdir),
198                              "LD_LIBRARY_PATH")
199                     add_path(os.path.join(plugin_root,"bin",salome_subdir),
200                              "PYTHONPATH")
201                     add_path(os.path.join(plugin_root,"bin",salome_subdir),
202                              "PATH")
203             pass
204         pass
205
206     # set environment for SUPERV module
207     os.environ["ENABLE_MACRO_NODE"]="1"
208     # set resources variables if not yet set
209     # Done now by launchConfigureParser.py
210     #if os.getenv("GUI_ROOT_DIR"):
211         #if not os.getenv("SUITRoot"): os.environ["SUITRoot"] =  os.getenv("GUI_ROOT_DIR") + "/share/salome"
212         #if not os.getenv("SalomeAppConfig"): os.environ["SalomeAppConfig"] =  os.getenv("GUI_ROOT_DIR") + "/share/salome/resources"
213
214     # set CSF_PluginDefaults variable only if it is not customized
215     # by the user
216     if not os.getenv("CSF_PluginDefaults"):
217         os.environ["CSF_PluginDefaults"] \
218         = os.path.join(modules_root_dir["KERNEL"],"share",
219                        salome_subdir,"resources")
220     os.environ["CSF_SALOMEDS_ResourcesDefaults"] \
221     = os.path.join(modules_root_dir["KERNEL"],"share",
222                    salome_subdir,"resources")
223
224     if "GEOM" in modules_list:
225         print "GEOM OCAF Resources"
226         os.environ["CSF_GEOMDS_ResourcesDefaults"] \
227         = os.path.join(modules_root_dir["GEOM"],"share",
228                        salome_subdir,"resources")
229         print "GEOM Shape Healing Resources"
230         os.environ["CSF_ShHealingDefaults"] \
231         = os.path.join(modules_root_dir["GEOM"],"share",
232                        salome_subdir,"resources")
233
234 # -----------------------------------------------------------------------------
235
236 def main():
237     args, modules_list, modules_root_dir = get_config()
238     set_env(args, modules_list, modules_root_dir)
239     return args
240
241 # -----------------------------------------------------------------------------
242
243 if __name__ == "__main__":
244    import user
245    args = main()