2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2022 CEA/DEN, EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 # \brief Module that provides services to launch SALOME
26 import sys, os, string, glob, time, pickle, re
29 from server import process_id, Server
32 from salomeContextUtils import ScriptAndArgsObjectEncoder
33 import runSalomeNoServer
34 import runSalomeCommon
37 logger = logging.getLogger()
39 # -----------------------------------------------------------------------------
41 from killSalome import killAllPorts
43 def kill_salome(args):
45 Kill servers from previous SALOME executions, if needed;
46 depending on args 'killall' or 'portkill', kill all executions,
47 or only execution on the same CORBA port
53 # -----------------------------------------------------------------------------
56 """Salome Session Graphic User Interface activation"""
60 import SALOME_ModuleCatalog
61 import SALOME_Session_idl
62 session=clt.waitNS("/Kernel/Session",SALOME.Session)
63 session.GetInterface()
65 # -----------------------------------------------------------------------------
67 def startSalome(args, modules_list, modules_root_dir):
68 """Launch all SALOME servers requested by args"""
69 init_time = os.times()
71 logger.debug("startSalome : {} ".format(args))
73 # Launch Session Server (to show splash ASAP)
76 if args["gui"] and not args['launcher_only']:
77 mySessionServ = runSalomeNoServer.NoSessionServer(args,args['modules'],modules_root_dir)
78 mySessionServ.setpath(modules_list,modules_root_dir)
84 # Wait until Session Server is registered in naming service
86 logger.debug("Start SALOME, elapsed time : %5.1f seconds"% (end_time[4] - init_time[4]))
89 # additional external python interpreters
95 nbaddi = args['interp']
99 print("-------------------------------------------------------------")
100 print("-- to get an external python interpreter:runSalome --interp=1")
101 print("-------------------------------------------------------------")
103 logger.debug("additional external python interpreters: {}".format(nbaddi))
105 for i in range(nbaddi):
107 anInterp=InterpServer(args)
110 # set PYTHONINSPECT variable (python interpreter in interactive mode)
112 os.environ["PYTHONINSPECT"]="1"
120 # -----------------------------------------------------------------------------
122 def useSalome(args, modules_list, modules_root_dir):
124 Launch all SALOME servers requested by args,
125 save list of process, give info to user,
126 show registered objects in Naming Service.
131 startSalome(args, modules_list, modules_root_dir)
134 traceback.print_exc()
137 print("--- Error during Salome launch ---")
141 from addToKillList import addToKillList
142 from killSalomeWithPort import getPiDict
144 filedict = getPiDict(args['port'])
145 for pid, cmd in list(process_id.items()):
146 addToKillList(pid, cmd, args['port'])
150 Saving of the dictionary of Salome processes in %s
151 To kill SALOME processes from a console (kill all sessions from all ports):
153 To kill SALOME from the present interpreter, if it is not closed :
154 killLocalPort() --> kill this session
155 (use CORBA port from args of runSalome)
156 givenPortKill(port) --> kill a specific session with given CORBA port
157 killAllPorts() --> kill all sessions
159 runSalome, with --killall option, starts with killing
160 the processes resulting from the previous execution.
165 def execScript(script_path):
166 print('executing', script_path)
167 sys.path.insert(0, os.path.realpath(os.path.dirname(script_path)))
168 exec(compile(open(script_path).read(), script_path, 'exec'),globals())
171 # -----------------------------------------------------------------------------
173 def addToPidict(args):
175 from addToKillList import addToKillList
176 for pid, cmd in list(process_id.items()):
177 addToKillList(pid, cmd, args['port'])
179 # -----------------------------------------------------------------------------
181 def main(exeName=None):
182 """Salome launch as a main application"""
183 keep_env = not os.getenv('SALOME_PLEASE_SETUP_ENVIRONMENT_AS_BEFORE')
184 args, modules_list, modules_root_dir = setenv.get_config(exeName=exeName, keepEnvironment=keep_env)
185 runSalomeCommon.setVerbose(args["verbosity"])
188 setenv.set_env(args, modules_list, modules_root_dir, keepEnvironment=keep_env)
189 useSalome(args, modules_list, modules_root_dir)
192 # -----------------------------------------------------------------------------
194 def foreGround(args):
196 if "session_object" not in args:
198 session = args["session_object"]
200 # Wait until gui is arrived
210 status = session.GetStatSession()
211 gui_detected = status.activeGUI
212 session_pid = session.getPID()
217 from time import sleep
227 from salome_utils import getPortNumber
228 port = getPortNumber()
231 if sys.platform == "win32":
232 server.CMD = [os.getenv("PYTHONBIN"), "-m", "killSalomeWithPort", "--spy", "%s"%(session_pid or os.getpid()), "%s"%(port)]
234 server.CMD = ["killSalomeWithPort.py", "--spy", "%s"%(session_pid or os.getpid()), "%s"%(port)]
236 # os.system("killSalomeWithPort.py --spy %s %s &"%(os.getpid(), port))
242 status = session.GetStatSession()
243 assert status.activeGUI
246 from time import sleep
250 except KeyboardInterrupt:
251 from killSalomeWithPort import killMyPort
260 test = args['gui'] and args['session_gui']
261 test = test or args['wake_up_session']
263 # The next test covers the --pinter option or if var PYTHONINSPECT is set
265 test = test and not os.environ.get('PYTHONINSPECT')
267 # The next test covers the python -i $KERNEL_ROOT_DIR/bin/salome/runSalome.py case
270 from ctypes import POINTER, c_int, cast, pythonapi
271 iflag_ptr = cast(pythonapi.Py_InteractiveFlag, POINTER(c_int))
272 test = test and not iflag_ptr.contents.value
276 # test = test and os.getenv("SALOME_TEST_MODE", "0") != "1"
277 test = test and args['foreground']
280 from time import sleep
287 # -----------------------------------------------------------------------------
289 if __name__ == "__main__":