]> SALOME platform Git repositories - modules/kernel.git/blob - bin/setenv.py
Salome HOME
CCAR: remove debug prints from the standard trace and put these prints
[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     import os
157     from salome_utils import getTmpDir, generateFileName, makeTmpDir, getPortNumber
158
159     # create temporary directory for environment files needed by modules from the list
160     port = getPortNumber(False)
161     if port:
162         tmp_dir = getTmpDir()
163         env_dir = generateFileName(tmp_dir, prefix="env", with_port=True)
164         makeTmpDir(env_dir)
165         pass
166
167     python_version="python%d.%d" % sys.version_info[0:2]
168     modules_root_dir_list = []
169     if os.getenv('SALOME_BATCH') == None:
170       os.putenv('SALOME_BATCH','0')
171     if args["gui"] :
172         modules_list = modules_list[:] + ["GUI"] 
173     modules_list = modules_list[:] + ["KERNEL"] 
174     for module in modules_list :
175         if modules_root_dir.has_key(module):
176             module_root_dir = modules_root_dir[module]
177             modules_root_dir_list[:0] = [module_root_dir]
178             if sys.platform == "win32":
179               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
180                      "PATH")
181             else:
182               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
183                      "LD_LIBRARY_PATH")
184             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
185                      "PATH")
186             if os.path.exists(module_root_dir + "/examples") :
187                 add_path(os.path.join(module_root_dir,"examples"),
188                          "PYTHONPATH")
189                 pass
190             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
191                      "PYTHONPATH")
192             # add lib before site-packages to load script instead of dll if any (win32 platform)
193             add_path(os.path.join(module_root_dir,get_lib_dir(),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                      "PYTHONPATH")
199             add_path(os.path.join(module_root_dir,get_lib_dir(),
200                                   python_version,"site-packages",
201                                   salome_subdir,
202                                   "shared_modules"),
203                      "PYTHONPATH")
204                      
205             # set environment by modules from the list
206             if port:
207                 try:
208                     mod=__import__(module.lower()+"_setenv")
209                     mod.set_env(args)
210                     pass
211                 except:
212                     pass
213                 pass
214             pass
215         pass
216
217     if sys.platform == 'win32':
218         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
219     else:
220         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
221     
222     # set trace environment variable
223     
224     if not os.environ.has_key("SALOME_trace"):
225         os.environ["SALOME_trace"]="local"
226     if args['file']:
227         os.environ["SALOME_trace"]="file:"+args['file'][0]
228     if args['logger']:
229         os.environ["SALOME_trace"]="with_logger"
230
231     # set environment for SUPERV module
232     os.environ["ENABLE_MACRO_NODE"]="1"
233     # set resources variables if not yet set
234     # Done now by launchConfigureParser.py
235     #if os.getenv("GUI_ROOT_DIR"):
236         #if not os.getenv("SalomeAppConfig"): os.environ["SalomeAppConfig"] =  os.getenv("GUI_ROOT_DIR") + "/share/salome/resources/gui"
237
238     os.environ["CSF_SALOMEDS_ResourcesDefaults"] \
239     = os.path.join(modules_root_dir["KERNEL"],"share",
240                    salome_subdir,"resources","kernel")
241
242 # -----------------------------------------------------------------------------
243
244 def main(silent=False):
245     args, modules_list, modules_root_dir = get_config(silent=silent)
246     set_env(args, modules_list, modules_root_dir, silent=silent)
247     return args
248
249 # -----------------------------------------------------------------------------
250
251 if __name__ == "__main__":
252    import user
253    args = main()