]> SALOME platform Git repositories - modules/kernel.git/blob - bin/setenv.py
Salome HOME
#19007 [CEA][Windows] SALOME non regression tests on Windows: fix KERNEL_SALOME_COMMA...
[modules/kernel.git] / bin / setenv.py
1 #!/usr/bin/env python3
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2020  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 variable_name not in os.environ:
43         os.environ[variable_name] = ""
44         pass
45     if os.path.exists(directory):
46         newpath=[]
47         for _dir in os.environ[variable_name].split(os.pathsep):
48             if os.path.exists(_dir):
49                 if not os.path.samefile(_dir, directory):
50                     newpath.append(_dir)
51             else:
52                 if os.path.abspath(_dir) != os.path.abspath(directory):
53                   newpath.append(_dir)
54             pass
55         newpath[:0] = [ directory ]
56         newpath = os.pathsep.join(newpath)
57         os.environ[variable_name] = newpath
58         if variable_name == "PYTHONPATH":
59             sys.path[:0] = [os.path.realpath(directory)]
60
61 # -----------------------------------------------------------------------------
62
63 __lib__dir__ = None
64 def get_lib_dir():
65     global __lib__dir__
66     if __lib__dir__: return __lib__dir__
67     import platform
68     __lib__dir__ = "lib"
69     return __lib__dir__
70
71 # -----------------------------------------------------------------------------
72
73 def get_config(silent=False, exeName=None, keepEnvironment=True):
74     """
75     Get list of modules, paths.
76
77     Read args from launch configure xml file and command line options.
78     Check variables <module>_ROOT_DIR and set list of used modules.
79     Return args, modules_list, modules_root_dir
80     """
81
82     # read args from launch configure xml file and command line options
83
84
85     import launchConfigureParser
86     args = launchConfigureParser.get_env(exeName=exeName, keepEnvironment=keepEnvironment)
87
88
89     # Check variables <module>_ROOT_DIR
90     # and set list of used modules (without KERNEL)
91
92     modules_list = []
93     if "modules" in args:
94         modules_list += [a for a in args["modules"] if a.strip()]
95     # KERNEL must be last in the list to locate it at the first place in PATH
96     if args["gui"] :
97         modules_list[:0] = ["GUI"]
98     modules_list[:0] = ["KERNEL"]
99     modules_list.reverse()
100
101     modules_root_dir = {}
102
103     to_remove_list=[]
104     for module in modules_list :
105         module_variable=module+"_ROOT_DIR"
106         if module_variable not in os.environ:
107             if not silent:
108                 print("*******************************************************")
109                 print("*")
110                 print("* Environment variable",module_variable,"must be set")
111                 print("* Module", module, "will be not available")
112                 print("*")
113                 print("********************************************************")
114                 pass
115             to_remove_list.append(module)
116             continue
117             pass
118         module_root_dir = os.environ[module_variable]
119         modules_root_dir[module]=module_root_dir
120
121     for to_remove in to_remove_list:
122         modules_list.remove(to_remove)
123
124     while "KERNEL" in modules_list:
125         modules_list.remove("KERNEL")
126         pass
127
128     while "GUI" in modules_list:
129         modules_list.remove("GUI")
130         pass
131
132     return args, modules_list, modules_root_dir
133
134 # -----------------------------------------------------------------------------
135
136 def set_env(args, modules_list, modules_root_dir, silent=False, keepEnvironment=True):
137     """Add to the PATH-variables modules specific paths"""
138
139     import os
140     from salome_utils import getLogDir, generateFileName, makeTmpDir, getPortNumber
141
142     if 'launcher' in args:
143       pos = args['launcher'].find(":")
144       if pos != -1:
145         machine = args['launcher'][0:pos]
146         port = args['launcher'][pos+1:]
147         os.environ["NSPORT"] = port
148
149     # create temporary directory for environment files needed by modules from the list
150     port = getPortNumber(False)
151     if port:
152         tmp_dir = getLogDir()
153         env_dir = generateFileName(tmp_dir, prefix="env", with_port=True)
154         makeTmpDir(env_dir)
155         pass
156
157     python_version="python%d.%d" % sys.version_info[0:2]
158     modules_root_dir_list = []
159     if os.getenv('SALOME_BATCH') == None:
160       os.putenv('SALOME_BATCH','0')
161     if args["gui"] :
162         modules_list = modules_list[:] + ["GUI"]
163     if not keepEnvironment:
164         modules_list = modules_list[:] + ["KERNEL"]
165         for module in modules_list :
166             if module in modules_root_dir:
167                 module_root_dir = modules_root_dir[module]
168                 if module_root_dir not in modules_root_dir_list:
169                   modules_root_dir_list[:0] = [module_root_dir]
170                 if sys.platform == "win32":
171                   add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
172                          "PATH")
173                 elif sys.platform == "darwin":
174                   add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
175                          "DYLD_LIBRARY_PATH")
176                 else:
177                   add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
178                          "LD_LIBRARY_PATH")
179                 add_path(os.path.join(module_root_dir,"bin",salome_subdir),
180                          "PATH")
181                 if os.path.exists(os.path.join(module_root_dir, "examples")):
182                     add_path(os.path.join(module_root_dir,"examples"),
183                              "PYTHONPATH")
184                     pass
185                 add_path(os.path.join(module_root_dir,"bin",salome_subdir),
186                          "PYTHONPATH")
187                 # add lib before site-packages to load script instead of dll if any (win32 platform)
188                 add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
189                          "PYTHONPATH")
190                 add_path(os.path.join(module_root_dir,get_lib_dir(),
191                                       python_version,"site-packages",
192                                       salome_subdir),
193                          "PYTHONPATH")
194                 import platform
195                 if platform.machine() == "x86_64":
196                     add_path(os.path.join(module_root_dir,"lib64",
197                                           python_version,"site-packages",
198                                           salome_subdir),
199                              "PYTHONPATH")
200                     pass
201                 add_path(os.path.join(module_root_dir,get_lib_dir(),
202                                       python_version,"site-packages",
203                                       salome_subdir,
204                                       "shared_modules"),
205                          "PYTHONPATH")
206
207                 # set environment by modules from the list
208                 if port:
209                     try:
210                         mod=__import__(module.lower()+"_setenv")
211                         mod.set_env(args)
212                         pass
213                     except:
214                         pass
215                     pass
216                 pass
217             pass
218
219         if sys.platform == 'win32':
220             os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
221         else:
222             os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
223
224         pass
225     # set trace environment variable
226
227     if "SALOME_trace" not in os.environ:
228         os.environ["SALOME_trace"]="local"
229     if args['file']:
230         os.environ["SALOME_trace"]="file:"+args['file'][0]
231     if args['logger']:
232         os.environ["SALOME_trace"]="with_logger"
233
234 # -----------------------------------------------------------------------------
235
236 def main(silent=False, exeName=None):
237     keep_env = not os.getenv('SALOME_PLEASE_SETUP_ENVIRONMENT_AS_BEFORE')
238     args, modules_list, modules_root_dir = get_config(silent=silent, exeName=exeName, keepEnvironment=keep_env)
239     set_env(args, modules_list, modules_root_dir, silent=silent, keepEnvironment=keep_env)
240     return args
241
242 # -----------------------------------------------------------------------------
243
244 if __name__ == "__main__":
245    import user
246    args = main()