]> SALOME platform Git repositories - modules/kernel.git/blob - bin/setenv.py
Salome HOME
Use isinstance() instead of type() and remove useless list(....keys())
[modules/kernel.git] / bin / setenv.py
1 #!/usr/bin/env python3
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 import sys, os, string, glob, time, pickle
26 import orbmodule
27 from launchConfigureParser import verbose
28
29 # this file is extraction of set_env from runSalome.py
30 # for reusage in automated tests
31
32 # salome_subdir variable is used for composing paths like $KERNEL_ROOT_DIR/share/salome/resources, etc.
33 # before moving to SUIT-based gui, instead of salome_subdir there was args['appname'] used.
34 # but after - 'appname'  = "SalomeApp", so using it in making the subdirectory is an error.
35 salome_subdir = "salome"
36
37
38 # -----------------------------------------------------------------------------
39
40 def add_path(directory, variable_name):
41     """Function helper to add environment variables"""
42     if sys.platform == "win32":
43       splitsym = ";"
44     else:
45       splitsym = ":"
46     if variable_name not in os.environ:
47         os.environ[variable_name] = ""
48         pass
49     if os.path.exists(directory):
50         newpath=[]
51         for _dir in os.environ[variable_name].split(splitsym):
52             if os.path.exists(_dir):
53                 if sys.platform != "win32":
54                     if not os.path.samefile(_dir, directory):
55                         newpath.append(_dir)
56                 else:
57                     newpath.append(_dir)
58             else:
59                 if os.path.abspath(_dir) != os.path.abspath(directory):
60                   newpath.append(_dir)
61             pass
62         newpath[:0] = [ directory ]
63         newpath = splitsym.join(newpath)
64         os.environ[variable_name] = newpath
65         if variable_name == "PYTHONPATH":
66             sys.path[:0] = [os.path.realpath(directory)]
67
68 # -----------------------------------------------------------------------------
69
70 __lib__dir__ = None
71 def get_lib_dir():
72     global __lib__dir__
73     if __lib__dir__: return __lib__dir__
74     import platform
75     __lib__dir__ = "lib"
76     return __lib__dir__
77
78 # -----------------------------------------------------------------------------
79
80 def get_config(silent=False, exeName=None):
81     """
82     Get list of modules, paths.
83
84     Read args from launch configure xml file and command line options.
85     Check variables <module>_ROOT_DIR and set list of used modules.
86     Return args, modules_list, modules_root_dir
87     """
88
89     # read args from launch configure xml file and command line options
90
91     import launchConfigureParser
92     args = launchConfigureParser.get_env(exeName=exeName)
93
94     # Check variables <module>_ROOT_DIR
95     # and set list of used modules (without KERNEL)
96
97     modules_list = []
98     if "modules" in args:
99         modules_list += args["modules"]
100     # KERNEL must be last in the list to locate it at the first place in PATH
101     if args["gui"] :
102         modules_list[:0] = ["GUI"]
103     modules_list[:0] = ["KERNEL"]
104     modules_list.reverse()
105
106     modules_root_dir = {}
107
108     to_remove_list=[]
109     for module in modules_list :
110         module_variable=module+"_ROOT_DIR"
111         if module_variable not in os.environ:
112             if not silent:
113                 print("*******************************************************")
114                 print("*")
115                 print("* Environment variable",module_variable,"must be set")
116                 print("* Module", module, "will be not available")
117                 print("*")
118                 print("********************************************************")
119                 pass
120             to_remove_list.append(module)
121             continue
122             pass
123         module_root_dir = os.environ[module_variable]
124         modules_root_dir[module]=module_root_dir
125
126     for to_remove in to_remove_list:
127         modules_list.remove(to_remove)
128
129     while "KERNEL" in modules_list:
130         modules_list.remove("KERNEL")
131         pass
132
133     while "GUI" in modules_list:
134         modules_list.remove("GUI")
135         pass
136
137     return args, modules_list, modules_root_dir
138
139 # -----------------------------------------------------------------------------
140
141 def set_env(args, modules_list, modules_root_dir, silent=False):
142     """Add to the PATH-variables modules specific paths"""
143
144     import os
145     from salome_utils import getLogDir, generateFileName, makeTmpDir, getPortNumber
146
147     # create temporary directory for environment files needed by modules from the list
148     port = getPortNumber(False)
149     if port:
150         tmp_dir = getLogDir()
151         env_dir = generateFileName(tmp_dir, prefix="env", with_port=True)
152         makeTmpDir(env_dir)
153         pass
154
155     python_version="python%d.%d" % sys.version_info[0:2]
156     modules_root_dir_list = []
157     if os.getenv('SALOME_BATCH') == None:
158       os.putenv('SALOME_BATCH','0')
159     if args["gui"] :
160         modules_list = modules_list[:] + ["GUI"]
161     modules_list = modules_list[:] + ["KERNEL"]
162     for module in modules_list :
163         if module in modules_root_dir:
164             module_root_dir = modules_root_dir[module]
165             if module_root_dir not in modules_root_dir_list:
166               modules_root_dir_list[:0] = [module_root_dir]
167             if sys.platform == "win32":
168               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
169                      "PATH")
170             elif sys.platform == "darwin":
171               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
172                      "DYLD_LIBRARY_PATH")
173             else:
174               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
175                      "LD_LIBRARY_PATH")
176             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
177                      "PATH")
178             if os.path.exists(os.path.join(module_root_dir, "examples")):
179                 add_path(os.path.join(module_root_dir, "examples"),
180                          "PYTHONPATH")
181                 pass
182             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
183                      "PYTHONPATH")
184             # add lib before site-packages to load script instead of dll if any (win32 platform)
185             add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
186                      "PYTHONPATH")
187             add_path(os.path.join(module_root_dir,get_lib_dir(),
188                                   python_version,"site-packages",
189                                   salome_subdir),
190                      "PYTHONPATH")
191             import platform
192             if platform.machine() == "x86_64":
193                 add_path(os.path.join(module_root_dir,"lib64",
194                                       python_version,"site-packages",
195                                       salome_subdir),
196                          "PYTHONPATH")
197                 pass
198             add_path(os.path.join(module_root_dir,get_lib_dir(),
199                                   python_version,"site-packages",
200                                   salome_subdir,
201                                   "shared_modules"),
202                      "PYTHONPATH")
203
204             # set environment by modules from the list
205             if port:
206                 try:
207                     mod=__import__(module.lower()+"_setenv")
208                     mod.set_env(args)
209                     pass
210                 except:
211                     pass
212                 pass
213             pass
214         pass
215
216     if sys.platform == 'win32':
217         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
218     else:
219         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
220
221     # set trace environment variable
222
223     if "SALOME_trace" not in os.environ:
224         os.environ["SALOME_trace"]="local"
225     if args['file']:
226         os.environ["SALOME_trace"] = "file:" + args['file'][0]
227     if args['logger']:
228         os.environ["SALOME_trace"]="with_logger"
229
230     # set resources variables if not yet set
231
232     os.environ["CSF_SALOMEDS_ResourcesDefaults"] \
233     = os.path.join(modules_root_dir["KERNEL"],"share",
234                    salome_subdir,"resources","kernel")
235
236 # -----------------------------------------------------------------------------
237
238 def main(silent=False, exeName=None):
239     args, modules_list, modules_root_dir = get_config(silent=silent, exeName=exeName)
240     set_env(args, modules_list, modules_root_dir, silent=silent)
241     return args
242
243 # -----------------------------------------------------------------------------
244
245 if __name__ == "__main__":
246    import user
247    args = main()