+ if verbose():
+ print
+ print " --- registered objects tree in Naming Service ---"
+ clt.showNS()
+ pass
+
+ if not args['gui'] or not args['session_gui']:
+ if args['shutdown_servers']:
+ class __utils__(object):
+ def __init__(self, port):
+ self.port = port
+ import killSalomeWithPort
+ self.killSalomeWithPort = killSalomeWithPort
+ return
+ def __del__(self):
+ self.killSalomeWithPort.killMyPort(self.port)
+ return
+ pass
+ def func(s):
+ del s
+ import atexit
+ atexit.register(func, __utils__(args['port']))
+ pass
+ pass
+
+ # run python scripts, passed as command line arguments
+ toimport = []
+ if args.has_key('gui') and args.has_key('session_gui'):
+ if not args['gui'] or not args['session_gui']:
+ if args.has_key('study_hdf'):
+ toopen = args['study_hdf']
+ if toopen:
+ import salome
+ salome.salome_init(toopen)
+ if args.has_key('pyscript'):
+ toimport = args['pyscript']
+ from salomeContextUtils import formatScriptsAndArgs
+ command = formatScriptsAndArgs(toimport)
+ if command:
+ proc = subprocess.Popen(command, shell=True)
+ addToKillList(proc.pid, command, args['port'])
+ res = proc.wait()
+ if res: sys.exit(1) # if there's an error when executing script, we should explicitly exit
+
+ return clt
+
+def execScript(script_path):
+ print 'executing', script_path
+ sys.path.insert(0, os.path.realpath(os.path.dirname(script_path)))
+ execfile(script_path,globals())
+ del sys.path[0]
+
+# -----------------------------------------------------------------------------
+
+def registerEnv(args, modules_list, modules_root_dir):
+ """
+ Register args, modules_list, modules_root_dir in a file
+ for further use, when SALOME is launched embedded in an other application.
+ """
+ from salome_utils import getTmpDir
+ fileEnv = getTmpDir()
+ from salome_utils import getUserName
+ fileEnv += getUserName() + "_" + str(args['port']) \
+ + '_' + args['appname'].upper() + '_env'
+ fenv=open(fileEnv,'w')
+ pickle.dump((args, modules_list, modules_root_dir),fenv)
+ fenv.close()
+ os.environ["SALOME_LAUNCH_CONFIG"] = fileEnv
+
+# -----------------------------------------------------------------------------
+
+def no_main():
+ """Salome Launch, when embedded in other application"""
+ fileEnv = os.environ["SALOME_LAUNCH_CONFIG"]
+ fenv=open(fileEnv,'r')
+ args, modules_list, modules_root_dir = pickle.load(fenv)
+ fenv.close()
+ kill_salome(args)
+ from searchFreePort import searchFreePort
+ searchFreePort(args, 0)
+ clt = useSalome(args, modules_list, modules_root_dir)
+ return clt
+
+# -----------------------------------------------------------------------------
+
+def main(exeName=None):
+ """Salome launch as a main application"""
+
+ # define folder to store omniorb config (initially in virtual application folder)
+ try:
+ from salomeContextUtils import setOmniOrbUserPath
+ setOmniOrbUserPath()
+ except Exception, e:
+ print e
+ sys.exit(1)
+
+ from salome_utils import getHostName
+ args, modules_list, modules_root_dir = setenv.get_config(exeName=exeName)
+ print "runSalome running on %s" % getHostName()
+
+ kill_salome(args)
+ save_config = True
+ if args.has_key('save_config'):
+ save_config = args['save_config']
+ # --
+ test = True
+ if args['wake_up_session']:
+ test = False
+ pass
+ if test:
+ from searchFreePort import searchFreePort
+ searchFreePort(args, save_config, args.get('useport'))
+ pass
+ # --
+ #setenv.main()
+ setenv.set_env(args, modules_list, modules_root_dir)
+ clt = useSalome(args, modules_list, modules_root_dir)
+ return clt,args
+
+# -----------------------------------------------------------------------------
+
+def foreGround(clt, args):
+ # --
+ if "session_object" not in args:
+ return
+ session = args["session_object"]
+ # --
+ # Wait until gui is arrived
+ # tmax = nbtot * dt
+ # --
+ gui_detected = False
+ dt = 0.1
+ nbtot = 100
+ nb = 0
+ while 1:
+ try:
+ status = session.GetStatSession()
+ gui_detected = status.activeGUI
+ except:
+ pass
+ if gui_detected:
+ break
+ from time import sleep
+ sleep(dt)
+ nb += 1
+ if nb == nbtot:
+ break
+ pass
+ # --
+ if not gui_detected:
+ return
+ # --
+ from salome_utils import getPortNumber
+ port = getPortNumber()
+ # --
+ server = Server({})
+ if sys.platform == "win32":
+ server.CMD = [os.getenv("PYTHONBIN"), "-m", "killSalomeWithPort", "--spy", "%s"%(os.getpid()), "%s"%(port)]
+ else:
+ server.CMD = ["killSalomeWithPort.py", "--spy", "%s"%(os.getpid()), "%s"%(port)]
+ server.run()
+ # os.system("killSalomeWithPort.py --spy %s %s &"%(os.getpid(), port))
+ # --
+ dt = 1.0
+ try:
+ while 1:
+ try:
+ status = session.GetStatSession()
+ assert status.activeGUI
+ except:
+ break
+ from time import sleep
+ sleep(dt)
+ pass
+ pass
+ except KeyboardInterrupt:
+ from killSalomeWithPort import killMyPort
+ killMyPort(port)
+ pass
+ return
+#
+
+def runSalome():
+ import user
+ clt,args = main()
+ # --
+ test = args['gui'] and args['session_gui']
+ test = test or args['wake_up_session']
+ # --
+ # The next test covers the --pinter option or var PYTHONINSPECT setted
+ # --
+ test = test and not os.environ.get('PYTHONINSPECT')
+ # --
+ # The next test covers the python -i $KERNEL_ROOT_DIR/bin/salome/runSalome.py case
+ # --
+ try:
+ from ctypes import POINTER, c_int, cast, pythonapi
+ iflag_ptr = cast(pythonapi.Py_InteractiveFlag, POINTER(c_int))
+ test = test and not iflag_ptr.contents.value
+ except:
+ pass
+ # --
+ test = test and os.getenv("SALOME_TEST_MODE", "0") != "1"
+ test = test and args['foreground']
+ # --
+ if test:
+ foreGround(clt, args)
+ pass
+ pass
+#
+
+# -----------------------------------------------------------------------------
+
+if __name__ == "__main__":
+ runSalome()
+#