2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 import sys, os, string, glob, time, pickle
27 from launchConfigureParser import verbose
29 # this file is extraction of set_env from runSalome.py
30 # for reusage in automated tests
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"
38 # -----------------------------------------------------------------------------
40 def add_path(directory, variable_name):
41 """Function helper to add environment variables"""
42 if sys.platform == "win32":
46 if variable_name not in os.environ:
47 os.environ[variable_name] = ""
49 if os.path.exists(directory):
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):
59 if os.path.abspath(_dir) != os.path.abspath(directory):
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)]
68 # -----------------------------------------------------------------------------
73 if __lib__dir__: return __lib__dir__
78 # -----------------------------------------------------------------------------
80 def get_config(silent=False, exeName=None, keepEnvironment=True):
82 Get list of modules, paths.
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
89 # read args from launch configure xml file and command line options
92 import launchConfigureParser
93 args = launchConfigureParser.get_env(exeName=exeName, keepEnvironment=keepEnvironment)
96 # Check variables <module>_ROOT_DIR
97 # and set list of used modules (without KERNEL)
100 if "modules" in args:
101 modules_list += [a for a in args["modules"] if a.strip()]
102 # KERNEL must be last in the list to locate it at the first place in PATH
104 modules_list[:0] = ["GUI"]
105 modules_list[:0] = ["KERNEL"]
106 modules_list.reverse()
108 modules_root_dir = {}
111 for module in modules_list :
112 module_variable=module+"_ROOT_DIR"
113 if module_variable not in os.environ:
115 print("*******************************************************")
117 print("* Environment variable",module_variable,"must be set")
118 print("* Module", module, "will be not available")
120 print("********************************************************")
122 to_remove_list.append(module)
125 module_root_dir = os.environ[module_variable]
126 modules_root_dir[module]=module_root_dir
128 for to_remove in to_remove_list:
129 modules_list.remove(to_remove)
131 while "KERNEL" in modules_list:
132 modules_list.remove("KERNEL")
135 while "GUI" in modules_list:
136 modules_list.remove("GUI")
139 return args, modules_list, modules_root_dir
141 # -----------------------------------------------------------------------------
143 def set_env(args, modules_list, modules_root_dir, silent=False, keepEnvironment=True):
144 """Add to the PATH-variables modules specific paths"""
147 from salome_utils import getLogDir, generateFileName, makeTmpDir, getPortNumber
149 if 'launcher' in args:
150 pos = args['launcher'].find(":")
152 machine = args['launcher'][0:pos]
153 port = args['launcher'][pos+1:]
154 os.environ["NSPORT"] = port
156 # create temporary directory for environment files needed by modules from the list
157 port = getPortNumber(False)
159 tmp_dir = getLogDir()
160 env_dir = generateFileName(tmp_dir, prefix="env", with_port=True)
164 python_version="python%d.%d" % sys.version_info[0:2]
165 modules_root_dir_list = []
166 if os.getenv('SALOME_BATCH') == None:
167 os.putenv('SALOME_BATCH','0')
169 modules_list = modules_list[:] + ["GUI"]
170 if not keepEnvironment:
171 modules_list = modules_list[:] + ["KERNEL"]
172 for module in modules_list :
173 if module in modules_root_dir:
174 module_root_dir = modules_root_dir[module]
175 if module_root_dir not in modules_root_dir_list:
176 modules_root_dir_list[:0] = [module_root_dir]
177 if sys.platform == "win32":
178 add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
180 elif sys.platform == "darwin":
181 add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
184 add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
186 add_path(os.path.join(module_root_dir,"bin",salome_subdir),
188 if os.path.exists(os.path.join(module_root_dir, "examples")):
189 add_path(os.path.join(module_root_dir,"examples"),
192 add_path(os.path.join(module_root_dir,"bin",salome_subdir),
194 # add lib before site-packages to load script instead of dll if any (win32 platform)
195 add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
197 add_path(os.path.join(module_root_dir,get_lib_dir(),
198 python_version,"site-packages",
202 if platform.machine() == "x86_64":
203 add_path(os.path.join(module_root_dir,"lib64",
204 python_version,"site-packages",
208 add_path(os.path.join(module_root_dir,get_lib_dir(),
209 python_version,"site-packages",
214 # set environment by modules from the list
217 mod=__import__(module.lower()+"_setenv")
226 if sys.platform == 'win32':
227 os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
229 os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
232 # set trace environment variable
234 if "SALOME_trace" not in os.environ:
235 os.environ["SALOME_trace"]="local"
237 os.environ["SALOME_trace"]="file:"+args['file'][0]
239 os.environ["SALOME_trace"]="with_logger"
241 # -----------------------------------------------------------------------------
243 def main(silent=False, exeName=None):
244 keep_env = not os.getenv('SALOME_PLEASE_SETUP_ENVIRONMENT_AS_BEFORE')
245 args, modules_list, modules_root_dir = get_config(silent=silent, exeName=exeName, keepEnvironment=keep_env)
246 set_env(args, modules_list, modules_root_dir, silent=silent, keepEnvironment=keep_env)
249 # -----------------------------------------------------------------------------
251 if __name__ == "__main__":