Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/kernel.git] / bin / runSalome.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2012  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.
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 ## @package runSalome
26 # \brief Module that provides services to launch SALOME
27 #
28
29 import sys, os, string, glob, time, pickle, re
30 import orbmodule
31 import setenv
32 from launchConfigureParser import verbose
33 from server import process_id, Server
34
35 if sys.platform == "win32":
36     SEP = ";"
37 else:
38     SEP = ":"
39
40 # -----------------------------------------------------------------------------
41
42 from killSalome import killAllPorts
43
44 def killLocalPort():
45     """
46     kill servers from a previous SALOME exection, if needed,
47     on the CORBA port given in args of runSalome
48     """
49     
50     from killSalomeWithPort import killMyPort
51     my_port=str(args['port'])
52     try:
53         killMyPort(my_port)
54     except:
55         print "problem in killLocalPort()"
56         pass
57     pass
58     
59 def givenPortKill(port):
60     """
61     kill servers from a previous SALOME exection, if needed,
62     on the same CORBA port
63     """
64     
65     from killSalomeWithPort import killMyPort
66     my_port=port
67     try:
68         killMyPort(my_port)
69     except:
70         print "problem in LocalPortKill(), killMyPort("<<port<<")"
71         pass
72     pass
73
74 def kill_salome(args):
75     """
76     Kill servers from previous SALOME executions, if needed;
77     depending on args 'killall' or 'portkill', kill all executions,
78     or only execution on the same CORBA port
79     """
80
81     if args['killall']:
82         killAllPorts()
83     elif args['portkill']:
84         givenPortKill(str(args['port']))
85
86 # -----------------------------------------------------------------------------
87 #
88 # Class definitions to launch CORBA Servers
89 #
90
91 class InterpServer(Server):
92     def __init__(self,args):
93         self.args=args
94         if sys.platform != "win32":
95           env_ld_library_path=['env', 'LD_LIBRARY_PATH=' + os.getenv("LD_LIBRARY_PATH")]
96           self.CMD=['xterm', '-e']+ env_ld_library_path + ['python']
97         else:
98           self.CMD=['cmd', '/c', 'start cmd.exe', '/K', 'python']
99        
100     def run(self):
101         global process_id
102         command = self.CMD
103         print "INTERPSERVER::command = ", command
104         if sys.platform == "win32":
105           import win32pm
106           pid = win32pm.spawnpid( string.join(command, " "),'-nc' )
107         else:
108           pid = os.spawnvp(os.P_NOWAIT, command[0], command)
109         process_id[pid]=self.CMD
110         self.PID = pid
111
112 # ---
113
114 def get_cata_path(list_modules,modules_root_dir):
115     """Build a list of catalog paths (cata_path) to initialize the ModuleCatalog server
116     """
117     modules_cata={}
118     cata_path=[]
119
120     for module in list_modules:
121         if modules_root_dir.has_key(module):
122             module_root_dir=modules_root_dir[module]
123             module_cata=module+"Catalog.xml"
124             cata_file=os.path.join(module_root_dir, "share",setenv.salome_subdir, "resources",module.lower(), module_cata)
125
126             if os.path.exists(cata_file):
127                 cata_path.append(cata_file)
128                 modules_cata[module]=cata_file
129             else:
130                 cata_file=os.path.join(module_root_dir, "share",setenv.salome_subdir, "resources", module_cata)
131                 if os.path.exists(cata_file):
132                     cata_path.append(cata_file)
133                     modules_cata[module]=cata_file
134
135     for path in os.getenv("SALOME_CATALOGS_PATH","").split(SEP):
136         if os.path.exists(path):
137             for cata_file in glob.glob(os.path.join(path,"*Catalog.xml")):
138                 module_name= os.path.basename(cata_file)[:-11]
139                 if not modules_cata.has_key(module_name):
140                     cata_path.append(cata_file)
141                     modules_cata[module_name]=cata_file
142
143     return cata_path
144
145
146
147 class CatalogServer(Server):
148     def __init__(self,args):
149         self.args=args
150         self.initArgs()
151         self.SCMD1=['SALOME_ModuleCatalog_Server','-common']
152         self.SCMD2=[]
153         home_dir=os.getenv('HOME')
154         if home_dir is not None:
155             self.SCMD2=['-personal',os.path.join(home_dir,'Salome/resources/CatalogModulePersonnel.xml')] 
156
157     def setpath(self,modules_list,modules_root_dir):
158         list_modules = modules_list[:]
159         list_modules.reverse()
160         if self.args["gui"] :
161             list_modules = ["KERNEL", "GUI"] + list_modules
162         else :
163             list_modules = ["KERNEL"] + list_modules
164
165         cata_path=get_cata_path(list_modules,modules_root_dir)
166
167         self.CMD=self.SCMD1 + ['"' + string.join(cata_path,'"::"') + '"'] + self.SCMD2
168
169 # ---
170
171 class SalomeDSServer(Server):
172     def __init__(self,args):
173         self.args=args
174         self.initArgs()
175         self.CMD=['SALOMEDS_Server']
176
177 # ---
178
179 class ConnectionManagerServer(Server):
180     def __init__(self,args):
181         self.args=args
182         self.initArgs()
183         self.CMD=['SALOME_ConnectionManagerServer']
184
185 # ---
186
187 class RegistryServer(Server):
188     def __init__(self,args):
189         self.args=args
190         self.initArgs()
191         self.CMD=['SALOME_Registry_Server', '--salome_session','theSession']
192
193 # ---
194
195 class ContainerCPPServer(Server):
196     def __init__(self,args):
197         self.args=args
198         self.initArgs()
199         self.CMD=['SALOME_Container','FactoryServer']
200
201 # ---
202
203 class ContainerPYServer(Server):
204     def __init__(self,args):
205         self.args=args
206         self.initArgs()
207         if sys.platform == "win32":
208           self.CMD=[os.environ["PYTHONBIN"], '\"'+os.environ["KERNEL_ROOT_DIR"] + '/bin/salome/SALOME_ContainerPy.py'+'\"','FactoryServerPy']
209         else:
210           self.CMD=['SALOME_ContainerPy.py','FactoryServerPy']
211
212 # ---
213
214 class LoggerServer(Server):
215     def __init__(self,args):
216         self.args=args
217         self.initArgs()
218         from salome_utils import generateFileName
219         if sys.platform == "win32": dirpath = os.environ["HOME"]
220         else:                       dirpath = "/tmp"
221         logfile = generateFileName( dirpath,
222                                     prefix="logger",
223                                     extension="log",
224                                     with_username=True,
225                                     with_hostname=True,
226                                     with_port=True)
227         print "==========================================================="
228         print "Logger server: put log to the file:"
229         print logfile
230         print "==========================================================="
231         self.CMD=['SALOME_Logger_Server', logfile]
232         pass
233     pass # end of LoggerServer class
234
235 # ---
236
237 class SessionServer(Server):
238     def __init__(self,args,modules_list,modules_root_dir):
239         self.args = args.copy()
240         # Bug 11512 (Problems with runSalome --xterm on Mandrake and Debian Sarge)
241         #self.args['xterm']=0
242         #
243         self.initArgs()
244         self.SCMD1=['SALOME_Session_Server']
245         self.SCMD2=[]
246         if 'registry' in self.args['embedded']:
247             self.SCMD1+=['--with','Registry',
248                          '(','--salome_session','theSession',')']
249         if 'moduleCatalog' in self.args['embedded']:
250             self.SCMD1+=['--with','ModuleCatalog','(','-common']
251             home_dir=os.getenv('HOME')
252             if home_dir is not None:
253                 self.SCMD2+=['-personal',os.path.join(home_dir,'Salome/resources/CatalogModulePersonnel.xml')] 
254             self.SCMD2+=[')']
255         if 'study' in self.args['embedded']:
256             self.SCMD2+=['--with','SALOMEDS','(',')']
257         if 'cppContainer' in self.args['embedded']:
258             self.SCMD2+=['--with','Container','(','FactoryServer',')']
259         if 'SalomeAppEngine' in self.args['embedded']:
260             self.SCMD2+=['--with','SalomeAppEngine','(',')']
261             
262         if 'cppContainer' in self.args['standalone'] or 'cppContainer' in self.args['embedded']:
263             self.SCMD2+=['CPP']
264         if 'pyContainer' in self.args['standalone'] or 'pyContainer' in self.args['embedded']:
265             self.SCMD2+=['PY']
266         if self.args['gui']:
267             session_gui = True
268             if self.args.has_key('session_gui'):
269                 session_gui = self.args['session_gui']
270             if session_gui:
271                 self.SCMD2+=['GUI']
272                 if self.args['splash']:
273                     self.SCMD2+=['SPLASH']
274                     pass
275                 if self.args['study_hdf'] is not None:
276                     self.SCMD2+=['--study-hdf=%s'%self.args['study_hdf']]
277                     pass
278                 pass
279             pass
280         if self.args['noexcepthandler']:
281             self.SCMD2+=['noexcepthandler']
282         if self.args.has_key('user_config'):
283             self.SCMD2+=['--resources=%s'%self.args['user_config']]
284         if self.args.has_key('modules'):
285             list_modules = []
286             #keep only modules with GUI
287             for m in modules_list:
288               if m not in modules_root_dir:
289                 list_modules.insert(0,m)
290               else:
291                 fr1 = os.path.join(modules_root_dir[m],"share","salome","resources",m.lower(),"SalomeApp.xml")
292                 fr2 = os.path.join(modules_root_dir[m],"share","salome","resources","SalomeApp.xml")
293                 if os.path.exists(fr1) or os.path.exists(fr2):
294                   list_modules.insert(0,m)
295             list_modules.reverse()
296             self.SCMD2+=['--modules (%s)' % ":".join(list_modules)]
297
298         if self.args.has_key('pyscript') and len(self.args['pyscript']) > 0:
299             self.SCMD2+=['--pyscript=%s'%(",".join(self.args['pyscript']))]
300
301     def setpath(self,modules_list,modules_root_dir):
302         list_modules = modules_list[:]
303         list_modules.reverse()
304         if self.args["gui"] :
305             list_modules = ["KERNEL", "GUI"] + list_modules
306         else :
307             list_modules = ["KERNEL"] + list_modules
308
309         cata_path=get_cata_path(list_modules,modules_root_dir)
310
311         if (self.args["gui"]) & ('moduleCatalog' in self.args['embedded']):
312             #Use '::' instead ":" because drive path with "D:\" is invalid on windows platform
313             self.CMD=self.SCMD1 + ['"' + string.join(cata_path,'"::"') + '"'] + self.SCMD2
314         else:
315             self.CMD=self.SCMD1 + self.SCMD2
316         if self.args.has_key('test'):
317             self.CMD+=['-test'] + self.args['test']
318         elif self.args.has_key('play'):
319             self.CMD+=['-play'] + self.args['play']
320
321         if self.args["gdb_session"] or self.args["ddd_session"]:
322             f = open(".gdbinit4salome", "w")
323             f.write("set args ")
324             args = " ".join(self.CMD[1:])
325             args = args.replace("(", "\(")
326             args = args.replace(")", "\)")
327             f.write(args)
328             f.write("\n")
329             f.close()
330             if self.args["ddd_session"]:
331                 self.CMD = ["ddd", "--command=.gdbinit4salome", self.CMD[0]]
332             elif self.args["gdb_session"]:
333                 self.CMD = ["xterm", "-e", "gdb", "--command=.gdbinit4salome", self.CMD[0]]
334                 pass
335             pass
336         
337         if self.args["valgrind_session"]:
338             l = ["valgrind"]
339             val = os.getenv("VALGRIND_OPTIONS")
340             if val:
341                 l += val.split()
342                 pass
343             self.CMD = l + self.CMD
344             pass
345         
346 # ---
347
348 class LauncherServer(Server):
349     def __init__(self,args):
350         self.args=args
351         self.initArgs()
352         self.SCMD1=['SALOME_LauncherServer']
353         self.SCMD2=[]
354         if args["gui"] :
355             if 'registry' in self.args['embedded']:
356                 self.SCMD1+=['--with','Registry',
357                              '(','--salome_session','theSession',')']
358             if 'moduleCatalog' in self.args['embedded']:
359                 self.SCMD1+=['--with','ModuleCatalog','(','-common']
360                 self.SCMD2+=['-personal',
361                              '${HOME}/Salome/resources/CatalogModulePersonnel.xml',')']
362             if 'study' in self.args['embedded']:
363                 self.SCMD2+=['--with','SALOMEDS','(',')']
364             if 'cppContainer' in self.args['embedded']:
365                 self.SCMD2+=['--with','Container','(','FactoryServer',')']
366
367     def setpath(self,modules_list,modules_root_dir):
368         list_modules = modules_list[:]
369         list_modules.reverse()
370         if self.args["gui"] :
371             list_modules = ["KERNEL", "GUI"] + list_modules
372         else :
373             list_modules = ["KERNEL"] + list_modules
374
375         cata_path=get_cata_path(list_modules,modules_root_dir)
376
377         if (self.args["gui"]) & ('moduleCatalog' in self.args['embedded']):
378             #Use '::' instead ":" because drive path with "D:\" is invalid on windows platform
379             self.CMD=self.SCMD1 + ['"' + string.join(cata_path,'"::"') + '"'] + self.SCMD2
380         else:
381             self.CMD=self.SCMD1 + self.SCMD2
382
383 class NotifyServer(Server):
384     def __init__(self,args,modules_root_dir):
385         self.args=args
386         self.initArgs()
387         self.modules_root_dir=modules_root_dir
388         myLogName = os.environ["LOGNAME"]
389         self.CMD=['notifd','-c',
390                   self.modules_root_dir["KERNEL"] +'/share/salome/resources/kernel/channel.cfg',
391                   '-DFactoryIORFileName=/tmp/'+myLogName+'_rdifact.ior',
392                   '-DChannelIORFileName=/tmp/'+myLogName+'_rdichan.ior',
393                   '-DReportLogFile=/tmp/'+myLogName+'_notifd.report',
394                   '-DDebugLogFile=/tmp/'+myLogName+'_notifd.debug',
395                   ]
396
397 #
398 # -----------------------------------------------------------------------------
399
400 def startGUI(clt):
401     """Salome Session Graphic User Interface activation"""
402     import Engines
403     import SALOME
404     import SALOMEDS
405     import SALOME_ModuleCatalog
406     import SALOME_Session_idl
407     session=clt.waitNS("/Kernel/Session",SALOME.Session)
408     session.GetInterface()
409   
410 # -----------------------------------------------------------------------------
411
412 def startSalome(args, modules_list, modules_root_dir):
413     """Launch all SALOME servers requested by args"""
414     init_time = os.times()
415
416     if verbose(): print "startSalome ", args
417     
418     #
419     # Set server launch command
420     #
421     if args.has_key('server_launch_mode'):
422         Server.set_server_launch_mode(args['server_launch_mode'])
423     
424     #
425     # Wake up session option
426     #
427     if args['wake_up_session']:
428         if "OMNIORB_CONFIG" not in os.environ:
429             from salome_utils import generateFileName
430             home  = os.getenv("HOME")
431             appli = os.getenv("APPLI")
432             kwargs={}
433             if appli is not None: 
434                 home = os.path.join(os.path.realpath(home), appli,"USERS")
435                 kwargs["with_username"] = True
436                 pass
437             last_running_config = generateFileName(home, prefix="omniORB",
438                                                    suffix="last",
439                                                    extension="cfg",
440                                                    hidden=True,
441                                                    **kwargs)
442             os.environ['OMNIORB_CONFIG'] = last_running_config
443             pass
444         pass
445     
446     #
447     # Initialisation ORB and Naming Service
448     #
449    
450     clt=orbmodule.client(args)
451
452     #
453     # Wake up session option
454     #
455     if args['wake_up_session']:
456         import Engines
457         import SALOME
458         import SALOMEDS
459         import SALOME_ModuleCatalog
460         import SALOME_Session_idl
461         session = clt.waitNS("/Kernel/Session",SALOME.Session)
462         status = session.GetStatSession()
463         if status.activeGUI:
464             from salome_utils import getPortNumber
465             port = getPortNumber()
466             msg  = "Warning :"
467             msg += "\n"
468             msg += "Session GUI for port number %s is already active."%(port)
469             msg += "\n"
470             msg += "If you which to wake up another session,"
471             msg += "\n"
472             msg += "please use variable OMNIORB_CONFIG"
473             msg += "\n"
474             msg += "to get the correct session object in naming service."
475             sys.stdout.write(msg+"\n")
476             sys.stdout.flush()
477             return clt
478         session.GetInterface()
479         args["session_object"] = session
480         return clt
481     
482     # Save Naming service port name into
483     # the file args["ns_port_log_file"]
484     if args.has_key('ns_port_log_file'):
485       home = os.environ['HOME']
486       appli= os.environ.get("APPLI")
487       if appli is not None:
488         home = os.path.join(os.path.realpath(home), appli, "USERS")
489       file_name = os.path.join(home, args["ns_port_log_file"])
490       f = open(file_name, "w")
491       f.write(os.environ['NSPORT'])
492       f.close()
493
494     # Launch Logger Server (optional)
495     # and wait until it is registered in naming service
496     #
497
498     if args['logger']:
499         myServer=LoggerServer(args)
500         myServer.run()
501         clt.waitLogger("Logger")
502
503     # Notify Server launch
504     #
505
506     if sys.platform != "win32":
507       if verbose(): print "Notify Server to launch"
508     
509       myServer=NotifyServer(args,modules_root_dir)
510       myServer.run()
511
512     # Launch  Session Server (to show splash ASAP)
513     #
514
515     if args["gui"]:
516         mySessionServ = SessionServer(args,args['modules'],modules_root_dir)
517         mySessionServ.setpath(modules_list,modules_root_dir)
518         mySessionServ.run()
519
520     #
521     # Launch Registry Server,
522     # and wait until it is registered in naming service
523     #
524
525     if ('registry' not in args['embedded']) | (args["gui"] == 0) :
526         myServer=RegistryServer(args)
527         myServer.run()
528         if sys.platform == "win32":
529           clt.waitNS("/Registry")
530         else:
531           clt.waitNSPID("/Registry",myServer.PID)
532
533     #
534     # Launch Catalog Server,
535     # and wait until it is registered in naming service
536     #
537
538     if ('moduleCatalog' not in args['embedded']) | (args["gui"] == 0):
539         cataServer=CatalogServer(args)
540         cataServer.setpath(modules_list,modules_root_dir)
541         cataServer.run()
542         import SALOME_ModuleCatalog
543         if sys.platform == "win32":
544           clt.waitNS("/Kernel/ModulCatalog",SALOME_ModuleCatalog.ModuleCatalog)
545         else:
546           clt.waitNSPID("/Kernel/ModulCatalog",cataServer.PID,SALOME_ModuleCatalog.ModuleCatalog)
547
548     #
549     # Launch SalomeDS Server,
550     # and wait until it is registered in naming service
551     #
552
553     #print "ARGS = ",args
554     if ('study' not in args['embedded']) | (args["gui"] == 0):
555         print "RunStudy"
556         myServer=SalomeDSServer(args)
557         myServer.run()
558         if sys.platform == "win32":
559           clt.waitNS("/myStudyManager")
560         else:
561           clt.waitNSPID("/myStudyManager",myServer.PID)
562
563     #
564     # Launch LauncherServer
565     #
566     
567     myCmServer = LauncherServer(args)
568     myCmServer.setpath(modules_list,modules_root_dir)
569     myCmServer.run()
570
571     #
572     # Launch ConnectionManagerServer
573     #
574
575     myConnectionServer = ConnectionManagerServer(args)
576     myConnectionServer.run()
577
578
579     from Utils_Identity import getShortHostName
580     
581     if os.getenv("HOSTNAME") == None:
582         if os.getenv("HOST") == None:
583             os.environ["HOSTNAME"]=getShortHostName()
584         else:
585             os.environ["HOSTNAME"]=os.getenv("HOST")
586
587     theComputer = getShortHostName()
588     
589     #
590     # Launch local C++ Container (FactoryServer),
591     # and wait until it is registered in naming service
592     #
593
594     if ('cppContainer' in args['standalone']) | (args["gui"] == 0) : 
595         myServer=ContainerCPPServer(args)
596         myServer.run()
597         if sys.platform == "win32":
598           clt.waitNS("/Containers/" + theComputer + "/FactoryServer")
599         else:
600           clt.waitNSPID("/Containers/" + theComputer + "/FactoryServer",myServer.PID)
601
602     #
603     # Launch local Python Container (FactoryServerPy),
604     # and wait until it is registered in naming service
605     #
606
607     if 'pyContainer' in args['standalone']:
608         myServer=ContainerPYServer(args)
609         myServer.run()
610         if sys.platform == "win32":
611           clt.waitNS("/Containers/" + theComputer + "/FactoryServerPy")
612         else:
613           clt.waitNSPID("/Containers/" + theComputer + "/FactoryServerPy",myServer.PID)
614
615     #
616     # Wait until Session Server is registered in naming service
617     #
618     
619     if args["gui"]:
620 ##----------------        
621         import Engines
622         import SALOME
623         import SALOMEDS
624         import SALOME_ModuleCatalog
625         import SALOME_Session_idl
626         if sys.platform == "win32":
627           session=clt.waitNS("/Kernel/Session",SALOME.Session)
628         else:
629           session=clt.waitNSPID("/Kernel/Session",mySessionServ.PID,SALOME.Session)
630         args["session_object"] = session
631     end_time = os.times()
632     if verbose(): print
633     print "Start SALOME, elapsed time : %5.1f seconds"% (end_time[4]
634                                                          - init_time[4])
635
636     # ASV start GUI without Loader
637     #if args['gui']:
638     #    session.GetInterface()
639
640     #
641     # additionnal external python interpreters
642     #
643     nbaddi=0
644     
645     try:
646         if 'interp' in args:
647             nbaddi = args['interp']
648     except:
649         import traceback
650         traceback.print_exc()
651         print "-------------------------------------------------------------"
652         print "-- to get an external python interpreter:runSalome --interp=1"
653         print "-------------------------------------------------------------"
654         
655     if verbose(): print "additional external python interpreters: ", nbaddi
656     if nbaddi:
657         for i in range(nbaddi):
658             print "i=",i
659             anInterp=InterpServer(args)
660             anInterp.run()
661
662     # set PYTHONINSPECT variable (python interpreter in interactive mode)
663     if args['pinter']:
664         os.environ["PYTHONINSPECT"]="1"
665         try:
666             import readline
667         except ImportError:
668             pass
669         
670     return clt
671
672 # -----------------------------------------------------------------------------
673
674 def useSalome(args, modules_list, modules_root_dir):
675     """
676     Launch all SALOME servers requested by args,
677     save list of process, give info to user,
678     show registered objects in Naming Service.
679     """
680     global process_id
681     
682     clt=None
683     try:
684         clt = startSalome(args, modules_list, modules_root_dir)
685     except:
686         import traceback
687         traceback.print_exc()
688         print
689         print
690         print "--- Error during Salome launch ---"
691         
692     #print process_id
693
694     from addToKillList import addToKillList
695     from killSalomeWithPort import getPiDict
696
697     filedict = getPiDict(args['port'])
698     for pid, cmd in process_id.items():
699         addToKillList(pid, cmd, args['port'])
700         pass
701
702     if verbose(): print """
703     Saving of the dictionary of Salome processes in %s
704     To kill SALOME processes from a console (kill all sessions from all ports):
705       python killSalome.py 
706     To kill SALOME from the present interpreter, if it is not closed :
707       killLocalPort()      --> kill this session
708                                (use CORBA port from args of runSalome)
709       givenPortKill(port)  --> kill a specific session with given CORBA port 
710       killAllPorts()       --> kill all sessions
711     
712     runSalome, with --killall option, starts with killing
713     the processes resulting from the previous execution.
714     """%filedict
715     
716     #
717     #  Print Naming Service directory list
718     #
719     
720     if clt != None:
721         if verbose():
722             print
723             print " --- registered objects tree in Naming Service ---"
724             clt.showNS()
725             pass
726         
727         if not args['gui'] or not args['session_gui']:
728             if args['shutdown_servers']:
729                 class __utils__(object):
730                     def __init__(self, port):
731                         self.port = port
732                         import killSalomeWithPort
733                         self.killSalomeWithPort = killSalomeWithPort
734                         return
735                     def __del__(self):
736                         self.killSalomeWithPort.killMyPort(self.port)
737                         return
738                     pass
739                 args['shutdown_servers'] = __utils__(args['port'])
740                 pass
741             pass
742         
743         # run python scripts, passed via --execute option
744         toimport = []
745         if args.has_key('pyscript'):
746             if args.has_key('gui') and args.has_key('session_gui'):
747                 if not args['gui'] or not args['session_gui']:
748                     toimport = args['pyscript']
749
750         for srcname in toimport :
751             if srcname == 'killall':
752                 clt.showNS()
753                 killAllPorts()
754                 sys.exit(0)
755             else:
756                 if os.path.isabs(srcname):
757                     if os.path.exists(srcname):
758                         execScript(srcname)
759                     elif os.path.exists(srcname+".py"):
760                         execScript(srcname+".py")
761                     else:
762                         print "Can't execute file %s" % srcname
763                     pass
764                 else:
765                     found = False
766                     for path in [os.getcwd()] + sys.path:
767                         if os.path.exists(os.path.join(path,srcname)):
768                             execScript(os.path.join(path,srcname))
769                             found = True
770                             break
771                         elif os.path.exists(os.path.join(path,srcname+".py")):
772                             execScript(os.path.join(path,srcname+".py"))
773                             found = True
774                             break
775                         pass
776                     if not found:
777                         print "Can't execute file %s" % srcname
778                         pass
779                     pass
780                 pass
781             pass
782         pass
783     return clt
784     
785 def execScript(script_path):
786     print 'executing', script_path
787     sys.path.insert(0, os.path.dirname(script_path))
788     execfile(script_path,globals())
789     del sys.path[0]
790
791 # -----------------------------------------------------------------------------
792
793 def registerEnv(args, modules_list, modules_root_dir):
794     """
795     Register args, modules_list, modules_root_dir in a file
796     for further use, when SALOME is launched embedded in an other application.
797     """
798     if sys.platform == "win32":
799       fileEnv = os.getenv('TEMP')
800     else:
801       fileEnv = '/tmp/'
802
803     fileEnv += os.getenv('USER') + "_" + str(args['port']) \
804             + '_' + args['appname'].upper() + '_env'
805     fenv=open(fileEnv,'w')
806     pickle.dump((args, modules_list, modules_root_dir),fenv)
807     fenv.close()
808     os.environ["SALOME_LAUNCH_CONFIG"] = fileEnv
809
810 # -----------------------------------------------------------------------------
811
812 def searchFreePort(args, save_config=1):
813     print "Searching for a free port for naming service:",
814     #
815     if sys.platform == "win32":
816         tmp_file = os.getenv('TEMP');
817     else:
818         tmp_file = '/tmp'
819     tmp_file = os.path.join(tmp_file, '.netstat_%s'%os.getpid())
820     #
821     ###status = os.system("netstat -ltn | grep -E :%s > /dev/null 2>&1"%(NSPORT))
822     os.system( "netstat -a -n > %s" % tmp_file );
823     f = open( tmp_file, 'r' );
824     ports = f.readlines();
825     f.close();
826     os.remove( tmp_file );
827     #
828     def portIsUsed(port, data):
829         regObj = re.compile( ".*tcp.*:([0-9]+).*:.*listen", re.IGNORECASE );
830         for item in data:
831             try:
832                 p = int(regObj.match(item).group(1))
833                 if p == port: return True
834                 pass
835             except:
836                 pass
837             pass
838         return False
839     #
840     NSPORT=2810
841     limit=NSPORT+100
842     #
843     while 1:
844         if not portIsUsed(NSPORT, ports):
845             print "%s - OK"%(NSPORT)
846             #
847             from salome_utils import generateFileName, getHostName
848             hostname = getHostName()
849             #
850             home  = os.getenv("HOME")
851             appli = os.getenv("APPLI")
852             kwargs={}
853             if appli is not None: 
854               home = os.path.join(os.path.realpath(home), appli,"USERS")
855               kwargs["with_username"]=True
856             #
857             omniorb_config = generateFileName(home, prefix="omniORB",
858                                               extension="cfg",
859                                               hidden=True,
860                                               with_hostname=True,
861                                               with_port=NSPORT,
862                                               **kwargs)
863             orbdata = []
864             initref = "NameService=corbaname::%s:%s"%(hostname, NSPORT)
865             from omniORB import CORBA
866             if CORBA.ORB_ID == "omniORB4":
867                 orbdata.append("InitRef = %s"%(initref))
868                 orbdata.append("giopMaxMsgSize = 2097152000  # 2 GBytes")
869                 orbdata.append("traceLevel = 0 # critical errors only")
870             else:
871                 orbdata.append("ORBInitRef %s"%(initref))
872                 orbdata.append("ORBgiopMaxMsgSize = 2097152000  # 2 GBytes")
873                 orbdata.append("ORBtraceLevel = 0 # critical errors only")
874                 pass
875             orbdata.append("")
876             f = open(omniorb_config, "w")
877             f.write("\n".join(orbdata))
878             f.close()
879             #
880             os.environ['OMNIORB_CONFIG'] = omniorb_config
881             os.environ['NSPORT'] = "%s"%(NSPORT)
882             os.environ['NSHOST'] = "%s"%(hostname)
883             args['port'] = os.environ['NSPORT']
884             #
885             if save_config:
886                 last_running_config = generateFileName(home, prefix="omniORB",
887                                                        suffix="last",
888                                                        extension="cfg",
889                                                        hidden=True,
890                                                        **kwargs)
891                 try:
892                     if sys.platform == "win32":
893                         import shutil       
894                         shutil.copyfile(omniorb_config, last_running_config)
895                     else:
896                         try:
897                             os.remove(last_running_config)
898                         except OSError:
899                             pass
900                         os.symlink(omniorb_config, last_running_config)
901                         pass
902                     pass
903                 except:
904                     pass
905             break
906         print "%s"%(NSPORT),
907         if NSPORT == limit:
908             msg  = "\n"
909             msg += "Can't find a free port to launch omniNames\n"
910             msg += "Try to kill the running servers and then launch SALOME again.\n"
911             raise RuntimeError, msg
912         NSPORT=NSPORT+1
913         pass
914     return
915     
916 # -----------------------------------------------------------------------------
917
918 def no_main():
919     """Salome Launch, when embedded in other application"""
920     fileEnv = os.environ["SALOME_LAUNCH_CONFIG"]
921     fenv=open(fileEnv,'r')
922     args, modules_list, modules_root_dir = pickle.load(fenv)
923     fenv.close()
924     kill_salome(args)
925     searchFreePort(args, 0)
926     clt = useSalome(args, modules_list, modules_root_dir)
927     return clt
928
929 # -----------------------------------------------------------------------------
930
931 def main():
932     """Salome launch as a main application"""
933     from salome_utils import getHostName
934     print "runSalome running on %s" % getHostName()
935     args, modules_list, modules_root_dir = setenv.get_config()
936     kill_salome(args)
937     save_config = True
938     if args.has_key('save_config'):
939         save_config = args['save_config']
940     # --
941     test = True
942     if args['wake_up_session']:
943         test = False
944         pass
945     if test:
946         searchFreePort(args, save_config)
947         pass
948     # --
949     #setenv.main()
950     setenv.set_env(args, modules_list, modules_root_dir)
951     clt = useSalome(args, modules_list, modules_root_dir)
952     return clt,args
953
954 # -----------------------------------------------------------------------------
955
956 def foreGround(clt, args):
957     # --
958     if "session_object" not in args:
959         return
960     session = args["session_object"]
961     # --
962     # Wait until gui is arrived
963     # tmax = nbtot * dt
964     # --
965     gui_detected = False
966     dt = 0.1
967     nbtot = 100
968     nb = 0
969     while 1:
970         try:
971             status = session.GetStatSession()
972             gui_detected = status.activeGUI
973         except:
974             pass
975         if gui_detected:
976             break
977         from time import sleep
978         sleep(dt)
979         nb += 1
980         if nb == nbtot:
981             break
982         pass
983     # --
984     if not gui_detected:
985         return
986     # --
987     from salome_utils import getPortNumber
988     port = getPortNumber()
989     # --
990     server = Server({})
991     if sys.platform == "win32":
992       server.CMD = [os.getenv("PYTHONBIN"), "-m", "killSalomeWithPort", "--spy", "%s"%(os.getpid()), "%s"%(port)]
993     else:
994       server.CMD = ["killSalomeWithPort.py", "--spy", "%s"%(os.getpid()), "%s"%(port)]
995     server.run()   
996     # os.system("killSalomeWithPort.py --spy %s %s &"%(os.getpid(), port))
997     # --
998     dt = 1.0
999     try:
1000         while 1:
1001             try:
1002                 status = session.GetStatSession()
1003                 assert status.activeGUI
1004             except:
1005                 break
1006             from time import sleep
1007             sleep(dt)
1008             pass
1009         pass
1010     except KeyboardInterrupt:
1011         from killSalomeWithPort import killMyPort
1012         killMyPort(port)
1013         pass
1014     return
1015
1016 # -----------------------------------------------------------------------------
1017
1018 if __name__ == "__main__":
1019     import user
1020     clt,args = main()
1021     # --
1022     test = args['gui'] and args['session_gui']
1023     test = test or args['wake_up_session']
1024     # --
1025     # The next test covers the --pinter option or var PYTHONINSPECT setted
1026     # --
1027     test = test and not os.environ.get('PYTHONINSPECT')
1028     # --
1029     # The next test covers the python -i $KERNEL_ROOT_DIR/bin/salome/runSalome.py case
1030     # --
1031     try:
1032         from ctypes import POINTER, c_int, cast, pythonapi
1033         iflag_ptr = cast(pythonapi.Py_InteractiveFlag, POINTER(c_int))
1034         test = test and not iflag_ptr.contents.value
1035     except:
1036         pass
1037     # --
1038     test = test and os.getenv("SALOME_TEST_MODE", "0") != "1"
1039     test = test and args['foreground']
1040     # --
1041     if test:
1042         foreGround(clt, args)
1043         pass
1044     # --
1045     pass