Salome HOME
To see which hdf5 we're using in configure log
[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     return args, modules_list, modules_root_dir
146
147 # -----------------------------------------------------------------------------
148
149 def set_env(args, modules_list, modules_root_dir, silent=False):
150     """Add to the PATH-variables modules specific paths"""
151     
152     import os
153     from salome_utils import getTmpDir, generateFileName, makeTmpDir, getPortNumber
154
155     # create temporary directory for environment files needed by modules from the list
156     port = getPortNumber(False)
157     if port:
158         tmp_dir = getTmpDir()
159         env_dir = generateFileName(tmp_dir, prefix="env", with_port=True)
160         makeTmpDir(env_dir)
161         pass
162
163     python_version="python%d.%d" % sys.version_info[0:2]
164     modules_root_dir_list = []
165     if os.getenv('SALOME_BATCH') == None:
166       os.putenv('SALOME_BATCH','0')
167     if args["gui"] :
168         modules_list = modules_list[:] + ["GUI"] 
169     modules_list = modules_list[:] + ["KERNEL"] 
170     for module in modules_list :
171         if modules_root_dir.has_key(module):
172             module_root_dir = modules_root_dir[module]
173             modules_root_dir_list[:0] = [module_root_dir]
174             if sys.platform == "win32":
175               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
176                      "PATH")
177             else:
178               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
179                      "LD_LIBRARY_PATH")
180             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
181                      "PATH")
182             if os.path.exists(module_root_dir + "/examples") :
183                 add_path(os.path.join(module_root_dir,"examples"),
184                          "PYTHONPATH")
185                 pass
186             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
187                      "PYTHONPATH")
188             # add lib before site-packages to load script instead of dll if any (win32 platform)
189             add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
190                      "PYTHONPATH")
191             add_path(os.path.join(module_root_dir,get_lib_dir(),
192                                   python_version,"site-packages",
193                                   salome_subdir),
194                      "PYTHONPATH")
195             add_path(os.path.join(module_root_dir,get_lib_dir(),
196                                   python_version,"site-packages",
197                                   salome_subdir,
198                                   "shared_modules"),
199                      "PYTHONPATH")
200                      
201             # set environment by modules from the list
202             if port:
203                 try:
204                     mod=__import__(module.lower()+"_setenv")
205                     mod.set_env(args)
206                     pass
207                 except:
208                     pass
209                 pass
210             pass
211         pass
212
213     if sys.platform == 'win32':
214         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
215     else:
216         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
217     
218     # set trace environment variable
219     
220     if not os.environ.has_key("SALOME_trace"):
221         os.environ["SALOME_trace"]="local"
222     if args['file']:
223         os.environ["SALOME_trace"]="file:"+args['file'][0]
224     if args['logger']:
225         os.environ["SALOME_trace"]="with_logger"
226
227     # set resources variables if not yet set
228
229     os.environ["CSF_SALOMEDS_ResourcesDefaults"] \
230     = os.path.join(modules_root_dir["KERNEL"],"share",
231                    salome_subdir,"resources","kernel")
232
233 # -----------------------------------------------------------------------------
234
235 def main(silent=False):
236     args, modules_list, modules_root_dir = get_config(silent=silent)
237     set_env(args, modules_list, modules_root_dir, silent=silent)
238     return args
239
240 # -----------------------------------------------------------------------------
241
242 if __name__ == "__main__":
243    import user
244    args = main()